@@ -240,7 +240,7 @@ class ConversationViewController: BaseViewController {
NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector : " cleanForLogout " , name : EditProfileViewController . Notification . Logout , object : nil )
YepUserDefaults . avatarURLString . bindListener ( " ConversationViewController " ) { _ in
YepUserDefaults . avatarURLString . bindListener ( " ConversationViewController " ) { [ unowned self ] _ in
self . reloadConversationCollectionView ( )
}
@@ -271,7 +271,7 @@ class ConversationViewController: BaseViewController {
lastTimeMessagesCount = messages . count
messageToolbar . textSendAction = { messageToolbar in
messageToolbar . textSendAction = { [ unowned self ] messageToolbar in
let text = messageToolbar . messageTextView . text !
self . cleanTextInput ( )
@@ -294,10 +294,10 @@ class ConversationViewController: BaseViewController {
} )
} else if let withGroup = self . conversation . withGroup {
sendText ( text , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { message in
sendText ( text , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { [ unowned self ] message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
@@ -319,7 +319,7 @@ class ConversationViewController: BaseViewController {
// p r i n t l n ( " U p d a t e w a v e r " )
audioRecorder . updateMeters ( )
var normalizedValue = pow ( 10 , audioRecorder . averagePowerForChannel ( 0 ) / 40 )
let normalizedValue = pow ( 10 , audioRecorder . averagePowerForChannel ( 0 ) / 40 )
waver . level = CGFloat ( normalizedValue )
}
@@ -328,7 +328,7 @@ class ConversationViewController: BaseViewController {
// MARK: A u d i o S e n d
messageToolbar . voiceSendBeginAction = { messageToolbar in
messageToolbar . voiceSendBeginAction = { [ unowned self ] messageToolbar in
self . view . addSubview ( self . waverView )
self . swipeUpView . hidden = false
self . view . bringSubviewToFront ( self . swipeUpView )
@@ -351,13 +351,13 @@ class ConversationViewController: BaseViewController {
}
}
messageToolbar . voiceSendCancelAction = { messageToolbar in
messageToolbar . voiceSendCancelAction = { [ unowned self ] messageToolbar in
self . swipeUpView . hidden = true
self . waverView . removeFromSuperview ( )
YepAudioService . sharedManager . endRecord ( )
}
messageToolbar . voiceSendEndAction = { messageToolbar in
messageToolbar . voiceSendEndAction = { [ unowned self ] messageToolbar in
self . swipeUpView . hidden = true
self . waverView . removeFromSuperview ( )
if YepAudioService . sharedManager . audioRecorder ? . currentTime < 0.5 {
@@ -398,7 +398,7 @@ class ConversationViewController: BaseViewController {
if let fileURL = YepAudioService . sharedManager . audioFileURL {
if let withFriend = self . conversation . withFriend {
sendAudioInFilePath ( fileURL . path ! , orFileData : nil , metaData : metaData , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : { message -> Void in
sendAudioInFilePath ( fileURL . path ! , orFileData : nil , metaData : metaData , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : { [ unowned self ] message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
if let realm = message . realm {
@@ -410,7 +410,7 @@ class ConversationViewController: BaseViewController {
}
realm . commitWrite ( )
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
}
@@ -424,7 +424,7 @@ class ConversationViewController: BaseViewController {
} )
} else if let withGroup = self . conversation . withGroup {
sendAudioInFilePath ( fileURL . path ! , orFileData : nil , metaData : metaData , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { ( message ) -> Void in
sendAudioInFilePath ( fileURL . path ! , orFileData : nil , metaData : metaData , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { [ unowned self ] message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
if let realm = message . realm {
@@ -436,7 +436,7 @@ class ConversationViewController: BaseViewController {
}
realm . commitWrite ( )
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
}
@@ -454,7 +454,7 @@ class ConversationViewController: BaseViewController {
// MARK: M e s s a g e T o o l b a r S t a t e T r a n s i t i o n s
messageToolbar . stateTransitionAction = { ( messageToolbar , previousState , currentState ) in
messageToolbar . stateTransitionAction = { [ unowned self ] ( messageToolbar , previousState , currentState ) in
switch ( previousState , currentState ) {
@@ -884,15 +884,19 @@ class ConversationViewController: BaseViewController {
messageIDs = _messageIDs
}
updateConversationCollectionViewWithMessageIDs ( messageIDs , scrollToBottom : false , success : { success in
updateConversationCollectionViewWithMessageIDs ( messageIDs , scrollToBottom : false , success : { _ in
} )
}
func updateConversationCollectionViewWithMessageIDs ( messageIDs : [ String ] ? , scrollToBottom : Bool , success : ( Bool ) -> Void ) {
let keyboardAndToolBarHeight = messageToolbarBottomConstraint . constant + CGRectGetHeight ( messageToolbar . bounds )
adjustConversationCollectionViewWithMessageIDs ( messageIDs , adjustHeight : keyboardAndToolBarHeight , scrollToBottom : scrollToBottom ) { finished in
success ( finished )
if navigationController ? . topViewController = = self { // 防 止 p o p / p u s h 后 , 原 来 未 释 放 的 V C 也 执 行 这 下 面 的 代 码
let keyboardAndToolBarHeight = messageToolbarBottomConstraint . constant + CGRectGetHeight ( messageToolbar . bounds )
adjustConversationCollectionViewWithMessageIDs ( messageIDs , adjustHeight : keyboardAndToolBarHeight , scrollToBottom : scrollToBottom ) { finished in
success ( finished )
}
}
}
@@ -910,12 +914,11 @@ class ConversationViewController: BaseViewController {
let lastDisplayedMessagesRange = displayedMessagesRange
displayedMessagesRange . length += newMessagesCount
var indexPaths = [ NSIndexPath ] ( )
if newMessagesCount > 0 {
/*
/ / v a r i n d e x P a t h s = [ N S I n d e x P a t h ] ( )
/ / TODO: 下 面 插 入 逻 辑 的 假 设 有 问 题 , 对 方 的 新 消 息 并 不 会 一 直 排 在 最 后 一 个
f o r i i n 0 . . < n e w M e s s a g e s C o u n t {
l e t i n d e x P a t h = N S I n d e x P a t h ( f o r I t e m : l a s t D i s p l a y e d M e s s a g e s R a n g e . l e n g t h + i , i n S e c t i o n : 0 )
@@ -935,34 +938,35 @@ class ConversationViewController: BaseViewController {
if let messageIDs = messageIDs {
if messageIDs . filter ( { $0 . isEmpty } ) . count > 0 {
println ( " have sectionDate " )
conversationCollectionView . reloadData ( )
var indexPaths = [ NSIndexPath ] ( )
} else {
for messageID in messageIDs {
if let
message = messageWithMessageID ( messageID , inRealm : realm ) ,
index = messages . indexOf ( message ) ,
indexPath = NSIndexPath ( forItem : index - displayedMessagesRange . location , inSection : 0 ) {
println ( " insert item: \( indexPath . item ) " )
var indexPaths = [ NSIndexPath ] ( )
indexPaths . append ( indexPath )
for messageID in messageIDs {
if let
message = messageWithMessageID ( messageID , inRealm : realm ) ,
index = messages . indexOf ( message ) ,
indexPath = NSIndexPath ( forItem : index - displayedMessagesRange . location , inSection : 0 ) {
println ( " insert item: \( indexPath . item ) " )
indexPaths . append ( indexPath )
} else {
println ( " unknown message " )
}
} else {
println ( " unknown message " )
}
conversationCollectionView . insertItemsAtIndexPaths ( indexPaths )
}
conversationCollectionView . insertItemsAtIndexPaths ( indexPaths )
} else {
println ( " self message " )
conversationCollectionView . reloadData ( )
var indexPaths = [ NSIndexPath ] ( )
for i in 0. . < newMessagesCount {
let indexPath = NSIndexPath ( forItem : lastDisplayedMessagesRange . length + i , inSection : 0 )
indexPaths . append ( indexPath )
}
conversationCollectionView . insertItemsAtIndexPaths ( indexPaths )
}
}
@@ -1000,7 +1004,7 @@ class ConversationViewController: BaseViewController {
if ( totleMessagesHeight > conversationCollectionView . frame . size . height ) {
// p r i n t l n ( " N e w M e s s a g e s c r o l l " )
UIView . animateWithDuration ( 0.2 , delay : 0.0 , options : UIViewAnimationOptions . CurveEaseInOut , animations : { ( ) -> Void in
UIView . animateWithDuration ( 0.2 , delay : 0.0 , options : UIViewAnimationOptions . CurveEaseInOut , animations : { [ unowned self ] in
if ( useableSpace > 0 ) {
let contentToScroll = newMessagesTotalHeight - useableSpace
@@ -1139,7 +1143,7 @@ class ConversationViewController: BaseViewController {
conversationCollectionViewContentOffsetBeforeKeyboardWillShow = conversationCollectionView . contentOffset
}
UIView . animateWithDuration ( animationDuration , delay : 0 , options : UIViewAnimationOptions ( animationCurveValue << 16 ) , animations : { ( ) -> Void in
UIView . animateWithDuration ( animationDuration , delay : 0 , options : UIViewAnimationOptions ( animationCurveValue << 16 ) , animations : { [ unowned self ] in
self . messageToolbarBottomConstraint . constant = newHeight
@@ -1179,7 +1183,7 @@ class ConversationViewController: BaseViewController {
self . view . layoutIfNeeded ( )
} , completion : { ( finished ) -> Void in
} , completion : { [ unowned self ] finished in
if keyboard {
self . keyboardShowTimes -= 1
}
@@ -1206,7 +1210,7 @@ class ConversationViewController: BaseViewController {
conversationCollectionViewContentOffsetBeforeKeyboardWillHide = conversationCollectionView . contentOffset
}
UIView . animateWithDuration ( animationDuration , delay : 0 , options : UIViewAnimationOptions ( animationCurveValue << 16 ) , animations : { ( ) -> Void in
UIView . animateWithDuration ( animationDuration , delay : 0 , options : UIViewAnimationOptions ( animationCurveValue << 16 ) , animations : { [ unowned self ] in
var contentOffset = self . conversationCollectionViewContentOffsetBeforeKeyboardWillHide
@@ -1225,7 +1229,7 @@ class ConversationViewController: BaseViewController {
self . view . layoutIfNeeded ( )
} , completion : { ( finished ) -> Void in
} , completion : { _ in
} )
}
@@ -1401,14 +1405,14 @@ class ConversationViewController: BaseViewController {
let nvc = segue . destinationViewController as ! UINavigationController
let vc = nvc . topViewController as ! PickLocationViewController
vc . sendLocationAction = { coordinate in
vc . sendLocationAction = { [ unowned self ] coordinate in
if let withFriend = self . conversation . withFriend {
sendLocationWithCoordinate ( coordinate , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : { message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
@@ -1424,7 +1428,7 @@ class ConversationViewController: BaseViewController {
sendLocationWithCoordinate ( coordinate , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
@@ -1506,7 +1510,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
case MessageMediaType . Image . rawValue :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatLeftImageCellIdentifier , forIndexPath : indexPath ) as ! ChatLeftImageCell
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : {
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : { [ unowned self ] in
self . performSegueWithIdentifier ( " showMessageMedia " , sender : message )
@@ -1519,7 +1523,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
let audioPlayedDuration = audioPlayedDurationOfMessage ( message )
cell . configureWithMessage ( message , audioPlayedDuration : audioPlayedDuration , audioBubbleTapAction : { message in
cell . configureWithMessage ( message , audioPlayedDuration : audioPlayedDuration , audioBubbleTapAction : { [ unowned self ] message in
self . playMessageAudioWithMessage ( message )
@@ -1530,7 +1534,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
case MessageMediaType . Video . rawValue :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatLeftVideoCellIdentifier , forIndexPath : indexPath ) as ! ChatLeftVideoCell
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : {
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : { [ unowned self ] in
self . performSegueWithIdentifier ( " showMessageMedia " , sender : message )
@@ -1541,7 +1545,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
case MessageMediaType . Location . rawValue :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatLeftLocationCellIdentifier , forIndexPath : indexPath ) as ! ChatLeftLocationCell
cell . configureWithMessage ( message , mediaTapAction : {
cell . configureWithMessage ( message , mediaTapAction : { [ unowned self ] in
if let coordinate = message . coordinate {
let locationCoordinate = CLLocationCoordinate2D ( latitude : coordinate . latitude , longitude : coordinate . longitude )
let mapItem = MKMapItem ( placemark : MKPlacemark ( coordinate : locationCoordinate , addressDictionary : nil ) )
@@ -1570,7 +1574,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
case MessageMediaType . Image . rawValue :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatRightImageCellIdentifier , forIndexPath : indexPath ) as ! ChatRightImageCell
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : {
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : { [ unowned self ] in
if message . sendState = = MessageSendState . Failed . rawValue {
@@ -1600,7 +1604,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
let audioPlayedDuration = audioPlayedDurationOfMessage ( message )
cell . configureWithMessage ( message , audioPlayedDuration : audioPlayedDuration , audioBubbleTapAction : { message in
cell . configureWithMessage ( message , audioPlayedDuration : audioPlayedDuration , audioBubbleTapAction : { [ unowned self ] message in
if let message = message {
if message . sendState = = MessageSendState . Failed . rawValue {
@@ -1631,7 +1635,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
case MessageMediaType . Video . rawValue :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatRightVideoCellIdentifier , forIndexPath : indexPath ) as ! ChatRightVideoCell
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : {
cell . configureWithMessage ( message , messageImagePreferredWidth : messageImagePreferredWidth , messageImagePreferredHeight : messageImagePreferredHeight , messageImagePreferredAspectRatio : messageImagePreferredAspectRatio , mediaTapAction : { [ unowned self ] in
if message . sendState = = MessageSendState . Failed . rawValue {
@@ -1659,7 +1663,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
case MessageMediaType . Location . rawValue :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatRightLocationCellIdentifier , forIndexPath : indexPath ) as ! ChatRightLocationCell
cell . configureWithMessage ( message , mediaTapAction : {
cell . configureWithMessage ( message , mediaTapAction : { [ unowned self ] in
if message . sendState = = MessageSendState . Failed . rawValue {
@@ -1695,7 +1699,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
default :
let cell = collectionView . dequeueReusableCellWithReuseIdentifier ( chatRightTextCellIdentifier , forIndexPath : indexPath ) as ! ChatRightTextCell
cell . configureWithMessage ( message , textContentLabelWidth : textContentLabelWidthOfMessage ( message ) , mediaTapAction : {
cell . configureWithMessage ( message , textContentLabelWidth : textContentLabelWidthOfMessage ( message ) , mediaTapAction : { [ unowned self ] in
if message . sendState = = MessageSendState . Failed . rawValue {
@@ -1809,7 +1813,7 @@ extension ConversationViewController: PullToRefreshViewDelegate {
func pulllToRefreshViewDidRefresh ( pulllToRefreshView : PullToRefreshView ) {
delay ( 1 ) {
pulllToRefreshView . endRefreshingAndDoFurtherAction ( ) {
pulllToRefreshView . endRefreshingAndDoFurtherAction ( ) { [ unowned self ] in
let lastDisplayedMessagesRange = self . displayedMessagesRange
@@ -1957,7 +1961,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat
if let withFriend = conversation . withFriend {
sendImageInFilePath ( nil , orFileData : imageData , metaData : metaData , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : { message -> Void in
sendImageInFilePath ( nil , orFileData : imageData , metaData : metaData , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : { [ unowned self ] message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
@@ -1973,7 +1977,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat
}
}
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
@@ -1986,7 +1990,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat
} )
} else if let withGroup = conversation . withGroup {
sendImageInFilePath ( nil , orFileData : imageData , metaData : nil , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { message -> Void in
sendImageInFilePath ( nil , orFileData : imageData , metaData : nil , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : { [ unowned self ] message in
dispatch_async ( dispatch_get_main_queue ( ) ) {
if let messageImageURL = NSFileManager . saveMessageImageData ( imageData , withName : messageImageName ) {
@@ -2001,7 +2005,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat
}
}
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
@@ -2036,7 +2040,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat
let messageVideoName = NSUUID ( ) . UUIDString
let afterCreatedMessageAction = { ( message : Message ) in
let afterCreatedMessageAction = { [ unowned self ] ( message : Message ) in
dispatch_async ( dispatch_get_main_queue ( ) ) {
if let videoData = NSData ( contentsOfURL : videoURL ) {
@@ -2061,27 +2065,27 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat
}
}
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { success in
self . updateConversationCollectionViewWithMessageIDs ( nil , scrollToBottom : true , success : { _ in
} )
}
}
}
if let withFriend = conversation . withFriend {
sendVideoInFilePath ( videoURL . path ! , orFileData : nil , metaData : metaData , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : afterCreatedMessageAction , failureHandler : { ( reason , errorMessage ) -> ( ) in
sendVideoInFilePath ( videoURL . path ! , orFileData : nil , metaData : metaData , toRecipient : withFriend . userID , recipientType : " User " , afterCreatedMessage : afterCreatedMessageAction , failureHandler : { ( reason , errorMessage ) in
defaultFailureHandler ( reason , errorMessage )
// TODO: s e n d V i d e o 错 误 提 醒
} , completion : { success -> Void in
} , completion : { success in
println ( " sendVideo to friend: \( success ) " )
} )
} else if let withGroup = conversation . withGroup {
sendVideoInFilePath ( videoURL . path ! , orFileData : nil , metaData : nil , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : afterCreatedMessageAction , failureHandler : { ( reason , errorMessage ) -> ( ) in
sendVideoInFilePath ( videoURL . path ! , orFileData : nil , metaData : nil , toRecipient : withGroup . groupID , recipientType : " Circle " , afterCreatedMessage : afterCreatedMessageAction , failureHandler : { ( reason , errorMessage ) in
defaultFailureHandler ( reason , errorMessage )
// TODO: s e n d V i d e o 错 误 提 醒
} , completion : { success -> Void in
} , completion : { success in
println ( " sendVideo to group: \( success ) " )
} )
}