diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 182f0efa..4e241073 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -72,6 +72,7 @@ 5033159D1AC101C80008A209 /* ChatRightTextCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5033159B1AC101C80008A209 /* ChatRightTextCell.xib */; }; 503315A01AC126100008A209 /* MessageToolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5033159F1AC126100008A209 /* MessageToolbar.swift */; }; 503CAB431AC00CB100DFE830 /* ConversationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 503CAB421AC00CB100DFE830 /* ConversationViewController.swift */; }; + 504F41251ACE2D1D00FBB19A /* SampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 504F41241ACE2D1D00FBB19A /* SampleView.swift */; }; 506EC20A1AC95BFC00CE42B3 /* UIFont+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 506EC2091AC95BFC00CE42B3 /* UIFont+Yep.swift */; }; 5076C56A1AC46B9F00B22952 /* FayeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5076C5691AC46B9F00B22952 /* FayeService.swift */; }; 508D17401ACCD2740092D666 /* YepButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508D173F1ACCD2740092D666 /* YepButton.swift */; }; @@ -185,6 +186,7 @@ 5033159B1AC101C80008A209 /* ChatRightTextCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = ChatRightTextCell.xib; path = Views/Cells/ChatRightText/ChatRightTextCell.xib; sourceTree = ""; }; 5033159F1AC126100008A209 /* MessageToolbar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MessageToolbar.swift; path = Views/MessageToolbar/MessageToolbar.swift; sourceTree = ""; }; 503CAB421AC00CB100DFE830 /* ConversationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConversationViewController.swift; path = ViewControllers/Conversation/ConversationViewController.swift; sourceTree = ""; }; + 504F41241ACE2D1D00FBB19A /* SampleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SampleView.swift; path = Views/AudioWaves/SampleView.swift; sourceTree = ""; }; 506EC2091AC95BFC00CE42B3 /* UIFont+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIFont+Yep.swift"; path = "Extensions/UIFont+Yep.swift"; sourceTree = ""; }; 5076C5691AC46B9F00B22952 /* FayeService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FayeService.swift; path = Services/FayeService.swift; sourceTree = ""; }; 508D173F1ACCD2740092D666 /* YepButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepButton.swift; path = Views/Buttons/YepButton.swift; sourceTree = ""; }; @@ -659,6 +661,7 @@ isa = PBXGroup; children = ( 50A9D4521ACD393A000B2599 /* SquareWaveView.swift */, + 504F41241ACE2D1D00FBB19A /* SampleView.swift */, 50A9D4531ACD393A000B2599 /* Waver.swift */, 50A9D4541ACD393A000B2599 /* YepWaverView.swift */, ); @@ -958,6 +961,7 @@ 502AE5271AB72D06005BD199 /* YepNetworking.swift in Sources */, 5076C56A1AC46B9F00B22952 /* FayeService.swift in Sources */, 5099B9C71AB99F7B002F940B /* RegisterPickAvatarViewController.swift in Sources */, + 504F41251ACE2D1D00FBB19A /* SampleView.swift in Sources */, 502AE5291AB81076005BD199 /* YepService.swift in Sources */, 502AE51E1AB712D2005BD199 /* RegisterPickNameViewController.swift in Sources */, 50E114F91ABC137A00F13000 /* YepServiceSync.swift in Sources */, diff --git a/Yep/Configs/YepConfig.swift b/Yep/Configs/YepConfig.swift index 58273202..1c746aa6 100644 --- a/Yep/Configs/YepConfig.swift +++ b/Yep/Configs/YepConfig.swift @@ -33,4 +33,12 @@ class YepConfig { class func messageImageViewAspectRatio() -> CGFloat { return 4.0 / 3.0 } + + class func audioSampleWidth() -> CGFloat { + return 2 + } + + class func audioSampleGap() -> CGFloat { + return 1 + } } \ No newline at end of file diff --git a/Yep/Services/YepAudioService.swift b/Yep/Services/YepAudioService.swift index 720150a9..c50418ea 100644 --- a/Yep/Services/YepAudioService.swift +++ b/Yep/Services/YepAudioService.swift @@ -91,6 +91,9 @@ class YepAudioService: NSObject { if self.audioPlayer.play() { println("Do Play audio \(error)") } + + } else { + println("play audio \(error)") } } } diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 11820d53..625d6547 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -588,6 +588,27 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi cell.avatarImageView.image = roundImage } } + + if !message.metaData.isEmpty { + + if let data = message.metaData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { + if let metaDataDict = decodeJSON(data) { + + if let audioSamples = metaDataDict["audio_samples"] as? [CGFloat] { + cell.sampleViewWidthConstraint.constant = CGFloat(audioSamples.count) * (YepConfig.audioSampleWidth() + YepConfig.audioSampleGap()) - YepConfig.audioSampleGap() // 最后最后一个 gap 不要 + cell.sampleView.samples = audioSamples + + if let audioDuration = metaDataDict["audio_duration"] as? Double { + cell.audioDurationLabel.text = NSString(format: "%.1f\"", audioDuration) as String + } + } + } + + } else { + cell.sampleViewWidthConstraint.constant = 15 * (YepConfig.audioSampleWidth() + YepConfig.audioSampleGap()) + cell.audioDurationLabel.text = "" + } + } return cell @@ -644,6 +665,27 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi } } + if !message.metaData.isEmpty { + + if let data = message.metaData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { + if let metaDataDict = decodeJSON(data) { + + if let audioSamples = metaDataDict["audio_samples"] as? [CGFloat] { + cell.sampleViewWidthConstraint.constant = CGFloat(audioSamples.count) * (YepConfig.audioSampleWidth() + YepConfig.audioSampleGap()) - YepConfig.audioSampleGap() // 最后一个 gap 不要 + cell.sampleView.samples = audioSamples + + if let audioDuration = metaDataDict["audio_duration"] as? Double { + cell.audioDurationLabel.text = NSString(format: "%.1f\"", audioDuration) as String + } + } + } + + } else { + cell.sampleViewWidthConstraint.constant = 15 * (YepConfig.audioSampleWidth() + YepConfig.audioSampleGap()) + cell.audioDurationLabel.text = "" + } + } + return cell default: diff --git a/Yep/Views/AudioWaves/SampleView.swift b/Yep/Views/AudioWaves/SampleView.swift new file mode 100644 index 00000000..0db05d96 --- /dev/null +++ b/Yep/Views/AudioWaves/SampleView.swift @@ -0,0 +1,73 @@ +// +// SampleView.swift +// Yep +// +// Created by NIX on 15/4/3. +// Copyright (c) 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +@IBDesignable +class SampleView: UIView { + + var samples: [CGFloat]? { + didSet { + setNeedsLayout() + } + } + + @IBInspectable var sampleColor: UIColor = UIColor.yepTintColor() { + willSet { + waveLayer.strokeColor = newValue.CGColor + } + } + + let sampleWidth: CGFloat = YepConfig.audioSampleWidth() + let sampleGap = YepConfig.audioSampleGap() + + lazy var waveLayer: CAShapeLayer = { + let layer = CAShapeLayer() + layer.lineWidth = self.sampleWidth + layer.strokeColor = self.sampleColor.CGColor + return layer + }() + + override func didMoveToSuperview() { + super.didMoveToSuperview() + + backgroundColor = UIColor.clearColor() + + layer.addSublayer(waveLayer) + } + + override func layoutSubviews() { + super.layoutSubviews() + + let wavePath = UIBezierPath() + + if let samples = samples { + + if samples.count > 0 { + + let viewHeight = self.bounds.height + + for (index, percent) in enumerate(samples) { + + let x = CGFloat(index) * sampleWidth + sampleGap * CGFloat(index) + + let sampleHeight = percent * viewHeight + + wavePath.moveToPoint(CGPointMake(x, viewHeight / 2.0 - sampleHeight / 2.0)) + wavePath.addLineToPoint(CGPointMake(x, sampleHeight / 2.0 + viewHeight / 2.0)) + } + + waveLayer.path = wavePath.CGPath + } + + } else { + samples = [0.05, 0.05, 0.1, 0.2, 0.3, 0.6, 0.2, 0.7, 0.9, 0.7, 0.6, 0.3, 0.1, 0.1, 0.05] // count = 15 + } + } + +} diff --git a/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift b/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift index d4f661e3..1e6546ec 100644 --- a/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift +++ b/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.swift @@ -14,10 +14,19 @@ class ChatLeftAudioCell: UICollectionViewCell { @IBOutlet weak var bubbleImageView: UIImageView! + @IBOutlet weak var sampleView: SampleView! + @IBOutlet weak var sampleViewWidthConstraint: NSLayoutConstraint! + + @IBOutlet weak var audioDurationLabel: UILabel! + override func awakeFromNib() { super.awakeFromNib() bubbleImageView.tintColor = UIColor.leftBubbleTintColor() + + sampleView.sampleColor = UIColor.rightBubbleTintColor() + + audioDurationLabel.textColor = UIColor.blackColor() } } diff --git a/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.xib b/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.xib index 720d02ff..31f26e9d 100644 --- a/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.xib +++ b/Yep/Views/Cells/ChatLeftAudio/ChatLeftAudioCell.xib @@ -36,13 +36,16 @@ - + + + + @@ -53,15 +56,14 @@ - + - + - @@ -77,8 +79,11 @@ + + + diff --git a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift index 8d8d086b..05bb8114 100644 --- a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift +++ b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.swift @@ -13,11 +13,20 @@ class ChatRightAudioCell: UICollectionViewCell { @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var bubbleImageView: UIImageView! + + @IBOutlet weak var sampleView: SampleView! + @IBOutlet weak var sampleViewWidthConstraint: NSLayoutConstraint! + + @IBOutlet weak var audioDurationLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() bubbleImageView.tintColor = UIColor.rightBubbleTintColor() + + sampleView.sampleColor = UIColor.leftBubbleTintColor() + + audioDurationLabel.textColor = UIColor.whiteColor() } } diff --git a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib index 307d46b0..b1783ca1 100644 --- a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib +++ b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib @@ -37,13 +37,16 @@ - + + + + @@ -52,14 +55,13 @@ - + - - + @@ -78,8 +80,11 @@ + + +