From bcf6ea5ba6aadbfec1dd369e7dbc9bbc41a03ae1 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 23 Sep 2015 12:00:38 +0800 Subject: [PATCH] refactor ActivityIndicatorTitleView --- Yep.xcodeproj/project.pbxproj | 12 +++ .../ConversationsViewController.swift | 59 ++------------ .../ActivityIndicatorTitleView.swift | 77 +++++++++++++++++++ 3 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 Yep/Views/TitleViews/ActivityIndicatorTitleView.swift diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 40df7f6c..73f98a59 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -193,6 +193,7 @@ 50A9D44E1ACD382B000B2599 /* NSDateTimeAgo.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 50A9D44C1ACD382B000B2599 /* NSDateTimeAgo.bundle */; }; 50A9D4511ACD383E000B2599 /* NSDate+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A9D4501ACD383E000B2599 /* NSDate+Extension.swift */; }; 50A9D4571ACD393A000B2599 /* YepWaverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A9D4541ACD393A000B2599 /* YepWaverView.swift */; }; + 50AFBF431BB257D2007D19DA /* ActivityIndicatorTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50AFBF421BB257D2007D19DA /* ActivityIndicatorTitleView.swift */; settings = {ASSET_TAGS = (); }; }; 50B38FB71B69CD960064C132 /* Array+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B38FB61B69CD960064C132 /* Array+Yep.swift */; }; 50B38FCC1B6B37F00064C132 /* FeedbackViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B38FCB1B6B37F00064C132 /* FeedbackViewController.swift */; }; 50B8E6161B40F9E700C17EA1 /* YepDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B8E6151B40F9E700C17EA1 /* YepDownloader.swift */; }; @@ -450,6 +451,7 @@ 50A9D44C1ACD382B000B2599 /* NSDateTimeAgo.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = NSDateTimeAgo.bundle; path = Extensions/NSDateTimeAgo.bundle; sourceTree = ""; }; 50A9D4501ACD383E000B2599 /* NSDate+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSDate+Extension.swift"; path = "Extensions/NSDate+Extension.swift"; sourceTree = ""; }; 50A9D4541ACD393A000B2599 /* YepWaverView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepWaverView.swift; path = Views/AudioWaves/YepWaverView.swift; sourceTree = ""; }; + 50AFBF421BB257D2007D19DA /* ActivityIndicatorTitleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ActivityIndicatorTitleView.swift; path = Views/TitleViews/ActivityIndicatorTitleView.swift; sourceTree = ""; }; 50B38FB61B69CD960064C132 /* Array+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Array+Yep.swift"; path = "Extensions/Array+Yep.swift"; sourceTree = ""; }; 50B38FCB1B6B37F00064C132 /* FeedbackViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FeedbackViewController.swift; path = ViewControllers/Feedback/FeedbackViewController.swift; sourceTree = ""; }; 50B8E6151B40F9E700C17EA1 /* YepDownloader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepDownloader.swift; path = Services/YepDownloader.swift; sourceTree = ""; }; @@ -864,6 +866,7 @@ 500A0B201B451A7400D49358 /* Badge */, 504F85AD1B4D21E400F909BE /* DiscoverFilter */, 8409137E1B7DB178001C89D6 /* FriendRequest */, + 50AFBF441BB257D8007D19DA /* TitleViews */, ); name = Views; sourceTree = ""; @@ -1484,6 +1487,14 @@ name = AudioWaves; sourceTree = ""; }; + 50AFBF441BB257D8007D19DA /* TitleViews */ = { + isa = PBXGroup; + children = ( + 50AFBF421BB257D2007D19DA /* ActivityIndicatorTitleView.swift */, + ); + name = TitleViews; + sourceTree = ""; + }; 50B38FCD1B6B37F90064C132 /* Feedback */ = { isa = PBXGroup; children = ( @@ -2004,6 +2015,7 @@ 5023DE2D1AB685ED00B3EE96 /* DiscoverViewController.swift in Sources */, 0A1CAC701AFA481600826B45 /* SkillHomeHeaderView.swift in Sources */, 0AB8F79A1B15D1DC00F4AF09 /* YepNavigationController.swift in Sources */, + 50AFBF431BB257D2007D19DA /* ActivityIndicatorTitleView.swift in Sources */, 502AE53A1AB871C1005BD199 /* YepAlert.swift in Sources */, 5039594E1B05E6E000797A3E /* ProfileSocialAccountGithubCell.swift in Sources */, 5080EB1F1B93F932006F3C2D /* NSBundle+Yep.swift in Sources */, diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index d495cd27..0709641a 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -15,50 +15,6 @@ class ConversationsViewController: UIViewController { @IBOutlet weak var conversationsTableView: UITableView! - lazy var fetchingTitleView: UIView = { - let view = UIView() - - let helperView = UIView() - helperView.translatesAutoresizingMaskIntoConstraints = false - - view.addSubview(helperView) - - let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray) - activityIndicator.tintColor = UIColor.yepNavgationBarTitleColor() - - activityIndicator.translatesAutoresizingMaskIntoConstraints = false - - activityIndicator.startAnimating() - - helperView.addSubview(activityIndicator) - - let label = UILabel() - label.text = NSLocalizedString("Fetching", comment: "") - label.textColor = UIColor.yepNavgationBarTitleColor() - - label.translatesAutoresizingMaskIntoConstraints = false - - helperView.addSubview(label) - - let helperViewCenterX = NSLayoutConstraint(item: helperView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0) - let helperViewCenterY = NSLayoutConstraint(item: helperView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0) - - NSLayoutConstraint.activateConstraints([helperViewCenterX, helperViewCenterY]) - - let viewsDictionary = [ - "activityIndicator": activityIndicator, - "label": label, - ] - - let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|[activityIndicator]-[label]|", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: viewsDictionary) - let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:|[activityIndicator]|", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: viewsDictionary) - - NSLayoutConstraint.activateConstraints(constraintsH) - NSLayoutConstraint.activateConstraints(constraintsV) - - return view - }() - let cellIdentifier = "ConversationCell" var realm: Realm! @@ -122,15 +78,14 @@ class ConversationsViewController: UIViewController { isFetchingUnreadMessages.bindListener(Listener.isFetchingUnreadMessages) { [weak self] isFetching in dispatch_async(dispatch_get_main_queue()) { - if isFetching { - self?.navigationItem.titleView = self?.fetchingTitleView - self?.fetchingTitleView.frame = CGRect(x: 0, y: 0, width: 120, height: 30) - //self?.fetchingTitleView.backgroundColor = UIColor.redColor() - } else { - //self?.title = "Yep" - } - //self?.title = isFetching ? NSLocalizedString("Fetching", comment: "") : "Yep" println("isFetchingUnreadMessages: \(isFetching)") + + if isFetching { + self?.navigationItem.titleView = ActivityIndicatorTitleView(frame: CGRect(x: 0, y: 0, width: 120, height: 30)) + + } else { + self?.navigationItem.titleView = nil + } } } diff --git a/Yep/Views/TitleViews/ActivityIndicatorTitleView.swift b/Yep/Views/TitleViews/ActivityIndicatorTitleView.swift new file mode 100644 index 00000000..3ae5814f --- /dev/null +++ b/Yep/Views/TitleViews/ActivityIndicatorTitleView.swift @@ -0,0 +1,77 @@ +// +// ActivityIndicatorTitleView.swift +// Yep +// +// Created by nixzhu on 15/9/23. +// Copyright © 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +class ActivityIndicatorTitleView: UIView { + + private var activityIndicator: UIActivityIndicatorView? + private var titleLabel: UILabel? + + var title: String? { + willSet { + self.titleLabel?.text = newValue + } + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override init(frame: CGRect) { + super.init(frame: frame) + + makeUI() + } + + func makeUI() { + + let helperView = UIView() + helperView.translatesAutoresizingMaskIntoConstraints = false + + addSubview(helperView) + + let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray) + activityIndicator.tintColor = UIColor.yepNavgationBarTitleColor() + + activityIndicator.translatesAutoresizingMaskIntoConstraints = false + + activityIndicator.startAnimating() + + self.activityIndicator = activityIndicator + + helperView.addSubview(activityIndicator) + + let label = UILabel() + label.text = NSLocalizedString("Fetching", comment: "") + label.textColor = UIColor.yepNavgationBarTitleColor() + + label.translatesAutoresizingMaskIntoConstraints = false + + self.titleLabel = label + + helperView.addSubview(label) + + let helperViewCenterX = NSLayoutConstraint(item: helperView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0) + let helperViewCenterY = NSLayoutConstraint(item: helperView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0) + + NSLayoutConstraint.activateConstraints([helperViewCenterX, helperViewCenterY]) + + let viewsDictionary = [ + "activityIndicator": activityIndicator, + "label": label, + ] + + let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|[activityIndicator]-[label]|", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: viewsDictionary) + let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:|[activityIndicator]|", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: viewsDictionary) + + NSLayoutConstraint.activateConstraints(constraintsH) + NSLayoutConstraint.activateConstraints(constraintsV) + } +} +