Created
January 2, 2015 20:32
-
-
Save moudy/1f2e925820c4fdf7cb7f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class CommentsTableViewBackgroundView: UIView { | |
dynamic var isEmpty = true | |
dynamic var isLoading = false | |
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) | |
lazy var label: UILabel = { | |
let label = UILabel() | |
label.text = "No comments yet. Be the first one!" | |
label.numberOfLines = 0 | |
label.textColor = UIColor.eoDarkGrayText() | |
label.textAlignment = .Center | |
return label | |
}() | |
convenience override init() { | |
self.init(frame: CGRectZero) | |
setupViews() | |
setupLayout() | |
setupBindings() | |
} | |
func setupViews() { | |
addSubview(activityIndicator) | |
addSubview(label) | |
} | |
func setupLayout() { | |
activityIndicator.alignCenterYWithView(self, predicate: "*1.3") | |
activityIndicator.alignCenterXWithView(self, predicate: "0") | |
label.alignCenterWithView(activityIndicator) | |
label.alignLeading("20", trailing: "-20", toView: self) | |
} | |
func setupBindings() { | |
let isLoadingSignal = RACObserve(self, "isLoading") | |
let isEmptySignal = RACObserve(self, "isEmpty") | |
let showEmptyLabelSignal = isEmptySignal | |
.combineLatestWith(isLoadingSignal) | |
.map { (tuple) -> AnyObject! in | |
let tuple = tuple as RACTuple | |
return (tuple.first as Bool) && !(tuple.second as Bool) | |
} | |
RAC(activityIndicator, "hidden") <~ isLoadingSignal.not() | |
RAC(label, "hidden") <~ showEmptyLabelSignal.not() | |
isLoadingSignal.subscribeNext { [weak self] (object) -> () in | |
if let a = self?.activityIndicator { | |
(object as Bool) ? a.startAnimating() : a.stopAnimating() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment