Created
October 26, 2017 21:58
-
-
Save mecid/3b258665119240bf383c28f7a1da234c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ActivityPresentable.swift | |
// SnowMine | |
// | |
// Created by Majid Jabrayilov on 10/24/17. | |
// Copyright © 2017 SnowDog. All rights reserved. | |
// | |
import UIKit | |
enum ActivityStatus { | |
case visible(text: String?) | |
case hidden | |
} | |
protocol ActivityPresentable { | |
func setActivityStatus(_ status: ActivityStatus) | |
} | |
extension ActivityPresentable where Self: UIViewController { | |
func setActivityStatus(_ status: ActivityStatus) { | |
defaultActivityStatus(status) | |
} | |
func defaultActivityStatus(_ status: ActivityStatus) { | |
guard isViewLoaded else { return } | |
switch status { | |
case .hidden: | |
findActivityIndicatorView()?.stopAnimating() | |
case .visible(let text): | |
if let activityIndicatorView = findActivityIndicatorView() { | |
activityIndicatorView.isHidden = false | |
guard let text = text else { return } | |
activityIndicatorView.text = text | |
} else { | |
let activityIndicatorView = ActivityIndicatorView() | |
view.addSubview(activityIndicatorView) | |
setupConstraints(for: activityIndicatorView) | |
activityIndicatorView.startAnimating() | |
guard let text = text else { return } | |
activityIndicatorView.text = text | |
} | |
} | |
} | |
private func setupConstraints(for activityIndicatorView: ActivityIndicatorView) { | |
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false | |
activityIndicatorView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true | |
activityIndicatorView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor).isActive = true | |
activityIndicatorView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
activityIndicatorView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
} | |
private func findActivityIndicatorView() -> ActivityIndicatorView? { | |
return view.subviews.flatMap { $0 as? ActivityIndicatorView }.first | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment