From a399f9cfaea7ac7dfa23481d60957d077ea8a3db Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 30 Apr 2015 14:27:41 +0800 Subject: [PATCH] add ConversationTitleView for ConversationViewController --- Yep.xcodeproj/project.pbxproj | 12 ++++ Yep/Realm/Models.swift | 15 +++++ .../ConversationViewController.swift | 8 +++ .../ConversationTitleView.swift | 67 +++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 Yep/Views/ConversationTitle/ConversationTitleView.swift diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index b5c8e4a4..3ea6e16c 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -139,6 +139,7 @@ 50E114F91ABC137A00F13000 /* YepServiceSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E114F81ABC137A00F13000 /* YepServiceSync.swift */; }; 50E114FC1ABC549300F13000 /* ContactsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E114FA1ABC549300F13000 /* ContactsCell.swift */; }; 50E114FD1ABC549300F13000 /* ContactsCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50E114FB1ABC549300F13000 /* ContactsCell.xib */; }; + 50E61CEB1AF1F69D00908A9D /* ConversationTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E61CEA1AF1F69D00908A9D /* ConversationTitleView.swift */; }; 50EB8C5E1AE78AB9001AC1EE /* YepAsset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50EB8C5D1AE78AB9001AC1EE /* YepAsset.swift */; }; 50EB8CBF1AE89ED8001AC1EE /* ChatLeftVideoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50EB8CBD1AE89ED8001AC1EE /* ChatLeftVideoCell.swift */; }; 50EB8CC01AE89ED8001AC1EE /* ChatLeftVideoCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50EB8CBE1AE89ED8001AC1EE /* ChatLeftVideoCell.xib */; }; @@ -299,6 +300,7 @@ 50E114F81ABC137A00F13000 /* YepServiceSync.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepServiceSync.swift; path = Services/YepServiceSync.swift; sourceTree = ""; }; 50E114FA1ABC549300F13000 /* ContactsCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContactsCell.swift; path = Views/Cells/Contacts/ContactsCell.swift; sourceTree = ""; }; 50E114FB1ABC549300F13000 /* ContactsCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = ContactsCell.xib; path = Views/Cells/Contacts/ContactsCell.xib; sourceTree = ""; }; + 50E61CEA1AF1F69D00908A9D /* ConversationTitleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConversationTitleView.swift; path = Views/ConversationTitle/ConversationTitleView.swift; sourceTree = ""; }; 50EB8C5D1AE78AB9001AC1EE /* YepAsset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepAsset.swift; path = Helpers/YepAsset.swift; sourceTree = ""; }; 50EB8CBD1AE89ED8001AC1EE /* ChatLeftVideoCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ChatLeftVideoCell.swift; path = Views/Cells/ChatLeftVideo/ChatLeftVideoCell.swift; sourceTree = ""; }; 50EB8CBE1AE89ED8001AC1EE /* ChatLeftVideoCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = ChatLeftVideoCell.xib; path = Views/Cells/ChatLeftVideo/ChatLeftVideoCell.xib; sourceTree = ""; }; @@ -527,6 +529,7 @@ 503315A11AC126140008A209 /* MessageToolbar */, 508D16BE1ADCAF0900270E3F /* PullToRefresh */, 50EB8CCC1AEA1AC5001AC1EE /* Media */, + 50E61CEC1AF1F6A600908A9D /* ConversationTitle */, ); name = Views; sourceTree = ""; @@ -968,6 +971,14 @@ name = Contacts; sourceTree = ""; }; + 50E61CEC1AF1F6A600908A9D /* ConversationTitle */ = { + isa = PBXGroup; + children = ( + 50E61CEA1AF1F69D00908A9D /* ConversationTitleView.swift */, + ); + name = ConversationTitle; + sourceTree = ""; + }; 50EB8CC11AE89EDE001AC1EE /* ChatLeftVideo */ = { isa = PBXGroup; children = ( @@ -1247,6 +1258,7 @@ 506923141ADE025F00D27574 /* YepRefreshView.swift in Sources */, 503CAB431AC00CB100DFE830 /* ConversationViewController.swift in Sources */, 50553F931ADBA50600F80B59 /* NSDate+Yep.swift in Sources */, + 50E61CEB1AF1F69D00908A9D /* ConversationTitleView.swift in Sources */, 5023DE331AB688F600B3EE96 /* UIColor+Yep.swift in Sources */, 503315971AC1017E0008A209 /* ChatLeftTextCell.swift in Sources */, 0A8F54B11AB67D11004AD60E /* AppDelegate.swift in Sources */, diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index b5ebe049..aa3a7712 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -201,6 +201,21 @@ func groupWithGroupID(groupID: String) -> Group? { return Group.objectsWithPredicate(predicate).firstObject() as? Group } +func nameOfConversation(conversation: Conversation) -> String? { + if conversation.type == ConversationType.OneToOne.rawValue { + if let withFriend = conversation.withFriend { + return withFriend.nickname + } + + } else if conversation.type == ConversationType.Group.rawValue { + if let withGroup = conversation.withGroup { + return withGroup.groupName + } + } + + return nil +} + func avatarWithAvatarURLString(avatarURLString: String) -> Avatar? { let predicate = NSPredicate(format: "avatarURLString = %@", avatarURLString) return Avatar.objectsWithPredicate(predicate).firstObject() as? Avatar diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 4c76b947..a9dbc2f2 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -60,6 +60,13 @@ class ConversationViewController: UIViewController { } } + lazy var titleView: ConversationTitleView = { + let titleView = ConversationTitleView(frame: CGRect(origin: CGPointZero, size: CGSize(width: 150, height: 44))) + titleView.nameLabel.text = nameOfConversation(self.conversation) + titleView.stateInfoLabel.text = "Hello world" + return titleView + }() + lazy var pullToRefreshView = PullToRefreshView() @IBOutlet weak var conversationCollectionView: UICollectionView! @@ -148,6 +155,7 @@ class ConversationViewController: UIViewController { displayedMessagesRange = NSRange(location: 0, length: Int(messages.count)) } + navigationItem.titleView = titleView let undoBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Undo, target: self, action: "undoMessageSend") navigationItem.rightBarButtonItem = undoBarButtonItem diff --git a/Yep/Views/ConversationTitle/ConversationTitleView.swift b/Yep/Views/ConversationTitle/ConversationTitleView.swift new file mode 100644 index 00000000..52043e81 --- /dev/null +++ b/Yep/Views/ConversationTitle/ConversationTitleView.swift @@ -0,0 +1,67 @@ +// +// ConversationTitleView.swift +// Yep +// +// Created by NIX on 15/4/30. +// Copyright (c) 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +class ConversationTitleView: UIView { + + lazy var nameLabel: UILabel = { + let label = UILabel() + label.textAlignment = .Center + label.font = UIFont(name: "Helvetica-Bold", size: 15)! + return label + }() + + lazy var stateInfoLabel: UILabel = { + let label = UILabel() + label.textAlignment = .Center + label.font = UIFont(name: "Helvetica-Light", size: 10)! + label.textColor = UIColor.grayColor() + return label + }() + + override func didMoveToSuperview() { + super.didMoveToSuperview() + + makeUI() + } + + func makeUI() { + addSubview(nameLabel) + addSubview(stateInfoLabel) + + let helperView = UIView() + + addSubview(helperView) + + nameLabel.setTranslatesAutoresizingMaskIntoConstraints(false) + stateInfoLabel.setTranslatesAutoresizingMaskIntoConstraints(false) + + helperView.setTranslatesAutoresizingMaskIntoConstraints(false) + + let viewsDictionary = [ + "nameLabel": nameLabel, + "stateInfoLabel": stateInfoLabel, + "helperView": helperView, + ] + + let helperViewCenterX = NSLayoutConstraint(item: helperView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0) + let helperViewCenterY = NSLayoutConstraint(item: helperView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0) + let helperViewTop = NSLayoutConstraint(item: helperView, attribute: .Top, relatedBy: .Equal, toItem: nameLabel, attribute: .Top, multiplier: 1, constant: 0) + let helperViewBottom = NSLayoutConstraint(item: helperView, attribute: .Bottom, relatedBy: .Equal, toItem: stateInfoLabel, attribute: .Bottom, multiplier: 1, constant: 0) + + NSLayoutConstraint.activateConstraints([helperViewCenterX, helperViewCenterY, helperViewTop, helperViewBottom]) + + let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:[nameLabel(24)][stateInfoLabel(12)]", options: .AlignAllCenterX | .AlignAllLeading | .AlignAllTrailing, metrics: nil, views: viewsDictionary) + + let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|[nameLabel]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary) + + NSLayoutConstraint.activateConstraints(constraintsV) + NSLayoutConstraint.activateConstraints(constraintsH) + } +}