Joint UI function between dispatchers

I have a function related to the user interface, such as a warning popup, displaying an activity indicator in the middle of the screen, or displaying animation on the screen using a special message.
I want to use then in several viewControllers (VC1, and VC2 in this case), so I do not repeat myself.
Initially, I have the following code that works with both VCs inherited from BaseVC that takes care of these functions.
VC1 is a UIViewController with a built-in TablelView, VC2 is a UIViewController with a built-in collection element.

class VC1: BaseVC {
    func viewDidAppear(animated: Bool) {
        activityIndicatorBegin()
    }

    func btnPressed() {
        activityIndicatorEnd()
    }
}

class VC2: BaseVC {
    func viewDidAppear(animated: Bool) {
        activityIndicatorBegin()
    }

    func btnPressed() {
        activityIndicatorEnd()
    }
}

class BaseVC: UIViewController {

    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
    var isCustomViewOnScreen = false

    func activityIndicatorBegin() {
        if activityIndicator.isAnimating() == false {
            activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,20,20))
            activityIndicator.center = view.center
            activityIndicator.hidesWhenStopped = true
            activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
            activityIndicator.color = UIColor.blackColor()
            view.addSubview(activityIndicator)
            activityIndicator.startAnimating()
        }
    }

    func activityIndicatorEnd() {
        if activityIndicator.isAnimating() == true {
            activityIndicator.stopAnimating()
            activityIndicator.removeFromSuperview()
        }
    }

    func animateACustomViewOnScreen() {
        if isCustomViewOnScreen == false {
            // Some animation code 
        }
    }

    func removeCustomView() {
        if isCustomViewOnScreen == true {
            // Some removal code
        }
    }
}

VC1 UITableViewController VC2 UICollectionViewController.
, BaseVC, UIViewController. , VC ?

+4
2

Protocol Extensions. BaseVC , , , , :

(BaseVC ActivityIndicatorDisplaying .)

protocol ActivityIndicatorDisplaying {

  var activityIndicator: UIActivityIndicatorView { get set }
  var showsCustomView: Bool { get }

  func showActivityIndicator()
  func dismissActivityIndicator()
}

extension ActivityIndicatorDisplaying where Self: UIViewController {

  func showActivityIndicator() {
    if activityIndicator.isAnimating() { return }

    activityIndicator.center = CGPointMake(view.bounds.width / 2, view.bounds.height / 2)
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = .WhiteLarge
    activityIndicator.color = UIColor.blackColor()
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
  }

  func dismissActivityIndicator() {
    activityIndicator.stopAnimating()
    activityIndicator.removeFromSuperview()
  }

  func animateACustomViewOnScreen() {
    if !showsCustomView {
      // Some animation code
    }
  }

  func removeCustomView() {
    if showsCustomView {
      // Some removal code
    }
  }
}

class VC1: UITableViewController, ActivityIndicatorDisplaying {

  var activityIndicator = UIActivityIndicatorView()
  var showsCustomView: Bool = false

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    showActivityIndicator()
  }

  func btnPressed() {
    dismissActivityIndicator()
  }
}

class VC2: UICollectionViewController, ActivityIndicatorDisplaying {

  var activityIndicator = UIActivityIndicatorView()
  var showsCustomView: Bool = true

  ...
}
+3

UIActivityViewController? .

class VC1: UIViewController {
    var customActivityIndicatorView: CustomActivityIndicatorView? = nil

    func viewDidAppear(animated: Bool) {
        if let view = view {
            customActivityIndicatorView = CustomActivityIndicatorView(parentView: view)
        }
    }

    func btnPressed() {
        customActivityIndicatorView?.end()
    }
}

class VC2: UITableViewController {
    var customActivityIndicatorView: CustomActivityIndicatorView? = nil

    func viewDidAppear(animated: Bool) {
        if let view = view {
            customActivityIndicatorView = CustomActivityIndicatorView(parentView: view)
        }
    }

    func btnPressed() {
        customActivityIndicatorView?.end()
    }
}

class CustomActivityIndicatorView: UIActivityIndicatorView {
    var isCustomViewOnScreen = false

    convenience init?(parentView: UIView) {
        if isAnimating == false {
            self.init(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
            center = parentView.center
            hidesWhenStopped = true
            activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
            color = UIColor.black
            parentView.addSubview(self)
            startAnimating()
        } else {
            return nil
        }
    }

    func end() {
        if isAnimating == true {
            stopAnimating()
            removeFromSuperview()
        }
    }

    func animateACustomViewOnScreen() {
        if isCustomViewOnScreen == false {
            // Some animation code
        }
    }

    func removeCustomView() {
        if isCustomViewOnScreen == true {
            // Some removal code
        }
    }
}
0

Source: https://habr.com/ru/post/1656295/


All Articles