From de372199ce68e176f59c640dec1c39b5fa11abde Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 27 May 2015 16:37:29 +0800 Subject: [PATCH] add audioBubbleTapAction to ChatLeftAudioCell & ChatRightAudioCell for only tap on Audio Bubble to play or pause --- .../ConversationViewController.swift | 57 ++++++++++++++++++- .../ChatLeftAudio/ChatLeftAudioCell.swift | 15 ++++- .../ChatRightAudio/ChatRightAudioCell.swift | 15 ++++- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index f7c6b06b..34ec0784 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -907,6 +907,49 @@ class ConversationViewController: BaseViewController { } } + func playMessageAudioWithMessage(message: Message) { + + if let audioPlayer = YepAudioService.sharedManager.audioPlayer { + if let playingMessage = YepAudioService.sharedManager.playingMessage { + if audioPlayer.playing { + + audioPlayer.pause() + + if let playbackTimer = YepAudioService.sharedManager.playbackTimer { + playbackTimer.invalidate() + } + + if let sender = playingMessage.fromFriend, playingMessageIndex = messages.indexOf(playingMessage) { + + let indexPath = NSIndexPath(forItem: playingMessageIndex - displayedMessagesRange.location, inSection: 0) + + if sender.friendState != UserFriendState.Me.rawValue { + if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatLeftAudioCell { + cell.playing = false + } + + } else { + if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatRightAudioCell { + cell.playing = false + } + } + } + + if message.messageID == playingMessage.messageID { + return + } + } + } + } + + let audioPlayedDuration = audioPlayedDurationOfMessage(message) as NSTimeInterval + YepAudioService.sharedManager.playAudioWithMessage(message, beginFromTime: audioPlayedDuration, delegate: self) { + let playbackTimer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: "updateAudioPlaybackProgress:", userInfo: nil, repeats: true) + YepAudioService.sharedManager.playbackTimer = playbackTimer + } + } + + // MARK: Keyboard func handleKeyboardWillShowNotification(notification: NSNotification) { @@ -1247,6 +1290,8 @@ class ConversationViewController: BaseViewController { } } } + + } // MARK: UIGestureRecognizerDelegate @@ -1336,7 +1381,11 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatLeftAudioCellIdentifier, forIndexPath: indexPath) as! ChatLeftAudioCell let audioPlayedDuration = audioPlayedDurationOfMessage(message) - cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration) + + cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { message in + + self.playMessageAudioWithMessage(message) + }) return cell @@ -1382,7 +1431,11 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightAudioCellIdentifier, forIndexPath: indexPath) as! ChatRightAudioCell let audioPlayedDuration = audioPlayedDurationOfMessage(message) - cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration) + + cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { message in + + self.playMessageAudioWithMessage(message) + }) return cell diff --git a/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift b/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift index 111b56ac..7f8a0dad 100644 --- a/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift +++ b/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift @@ -43,6 +43,9 @@ class ChatLeftAudioCell: UICollectionViewCell { @IBOutlet weak var playButton: UIButton! + typealias AudioBubbleTapAction = (message: Message) -> Void + var audioBubbleTapAction: AudioBubbleTapAction? + override func awakeFromNib() { super.awakeFromNib() @@ -56,9 +59,19 @@ class ChatLeftAudioCell: UICollectionViewCell { playButton.userInteractionEnabled = false playButton.tintColor = UIColor.darkGrayColor() + + bubbleImageView.userInteractionEnabled = true + let tap = UITapGestureRecognizer(target: self, action: "tapMediaView") + bubbleImageView.addGestureRecognizer(tap) } - func configureWithMessage(message: Message, audioPlayedDuration: Double) { + func tapMediaView() { + audioBubbleTapAction?(message: message) + } + + func configureWithMessage(message: Message, audioPlayedDuration: Double, audioBubbleTapAction: AudioBubbleTapAction?) { + + self.audioBubbleTapAction = audioBubbleTapAction self.message = message diff --git a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift index 4ce0f071..dbbcf801 100644 --- a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift +++ b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift @@ -42,6 +42,9 @@ class ChatRightAudioCell: UICollectionViewCell { @IBOutlet weak var playButton: UIButton! + typealias AudioBubbleTapAction = (message: Message) -> Void + var audioBubbleTapAction: AudioBubbleTapAction? + override func awakeFromNib() { super.awakeFromNib() @@ -54,9 +57,19 @@ class ChatRightAudioCell: UICollectionViewCell { audioDurationLabel.textColor = UIColor.whiteColor() playButton.userInteractionEnabled = false + + bubbleImageView.userInteractionEnabled = true + let tap = UITapGestureRecognizer(target: self, action: "tapMediaView") + bubbleImageView.addGestureRecognizer(tap) } - func configureWithMessage(message: Message, audioPlayedDuration: Double) { + func tapMediaView() { + audioBubbleTapAction?(message: message) + } + + func configureWithMessage(message: Message, audioPlayedDuration: Double, audioBubbleTapAction: AudioBubbleTapAction?) { + + self.audioBubbleTapAction = audioBubbleTapAction self.message = message