From 0e03bba96eeec4ba11ccdbeb4e4f3a09f904ae2b Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Mon, 5 Oct 2020 10:51:26 +1100 Subject: [PATCH] Change loader design --- .../Loki/Components/VoiceMessageView.swift | 64 ++++++++++++------- .../Cells/OWSMessageBubbleView.m | 44 +------------ .../ConversationView/ConversationViewItem.m | 2 +- 3 files changed, 43 insertions(+), 67 deletions(-) diff --git a/Signal/src/Loki/Components/VoiceMessageView.swift b/Signal/src/Loki/Components/VoiceMessageView.swift index 6f603622e..56a3784d1 100644 --- a/Signal/src/Loki/Components/VoiceMessageView.swift +++ b/Signal/src/Loki/Components/VoiceMessageView.swift @@ -1,19 +1,12 @@ import Accelerate -@objc(LKVoiceMessageViewDelegate) -protocol VoiceMessageViewDelegate { - - func showLoader() - func hideLoader() -} - @objc(LKVoiceMessageView) final class VoiceMessageView : UIView { private let voiceMessage: TSAttachment private let isOutgoing: Bool + private var isLoading = false private var volumeSamples: [Float] = [] { didSet { updateShapeLayers() } } - private var progress: CGFloat = 0 - @objc var delegate: VoiceMessageViewDelegate? + @objc var progress: CGFloat = 0 { didSet { updateShapeLayers() } } @objc var duration: Int = 0 { didSet { updateDurationLabel() } } @objc var isPlaying = false { didSet { updateToggleImageView() } } @@ -40,10 +33,11 @@ final class VoiceMessageView : UIView { }() // MARK: Settings - private let vMargin: CGFloat = 0 + private let leadingInset: CGFloat = 0 private let sampleSpacing: CGFloat = 1 + private let targetSampleCount = 48 private let toggleContainerSize: CGFloat = 32 - private let leadingInset: CGFloat = 0 + private let vMargin: CGFloat = 0 @objc public static let contentHeight: CGFloat = 40 @@ -69,27 +63,24 @@ final class VoiceMessageView : UIView { guard let url = (voiceMessage as? TSAttachmentStream)?.originalMediaURL else { return print("[Loki] Couldn't get URL for voice message.") } - let targetSampleCount = 48 if let cachedVolumeSamples = Storage.getVolumeSamples(for: voiceMessage.uniqueId!), cachedVolumeSamples.count == targetSampleCount { + self.hideLoader() self.volumeSamples = cachedVolumeSamples - self.delegate?.hideLoader() } else { let voiceMessageID = voiceMessage.uniqueId! AudioUtilities.getVolumeSamples(for: url, targetSampleCount: targetSampleCount).done(on: DispatchQueue.main) { [weak self] volumeSamples in guard let self = self else { return } + self.hideLoader() self.volumeSamples = volumeSamples Storage.write { transaction in Storage.setVolumeSamples(for: voiceMessageID, to: volumeSamples, using: transaction) } - self.durationLabel.alpha = 1 - self.delegate?.hideLoader() }.catch(on: DispatchQueue.main) { error in print("[Loki] Couldn't sample audio file due to error: \(error).") } } } else { - durationLabel.alpha = 0 - delegate?.showLoader() + showLoader() } } @@ -121,14 +112,30 @@ final class VoiceMessageView : UIView { } // MARK: UI & Updating - override func layoutSubviews() { - super.layoutSubviews() - updateShapeLayers() + private func showLoader() { + isLoading = true + Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] timer in + guard let self = self else { return timer.invalidate() } + if self.isLoading { + self.updateFakeVolumeSamples() + } else { + timer.invalidate() + } + } + updateFakeVolumeSamples() + } + + private func updateFakeVolumeSamples() { + let fakeVolumeSamples = (0.. +@interface OWSMessageBubbleView () @property (nonatomic) OWSBubbleView *bubbleView; @@ -50,10 +50,6 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, nullable) OWSContactShareButtonsView *contactShareButtonsView; -@property (nonatomic) UIView *loader; - -@property (nonatomic) BOOL isAnimating; - @end #pragma mark - @@ -112,10 +108,6 @@ NS_ASSUME_NONNULL_BEGIN self.linkPreviewView = [[LinkPreviewView alloc] initWithDraftDelegate:nil]; self.footerView = [OWSMessageFooterView new]; - - self.loader = [UIView new]; - self.loader.backgroundColor = [LKColors.text colorWithAlphaComponent:0.6f]; - self.loader.alpha = 0.0f; } - (OWSMessageTextView *)newTextView @@ -422,9 +414,6 @@ NS_ASSUME_NONNULL_BEGIN addObject:[bodyMediaView autoSetDimension:ALDimensionHeight toSize:bodyMediaSize.CGSizeValue.height]]; } - [self.bubbleView addSubview:self.loader]; - [self.loader autoPinEdgesToSuperviewEdges]; - [self insertContactShareButtonsIfNecessary]; [self updateBubbleColor]; @@ -672,34 +661,6 @@ NS_ASSUME_NONNULL_BEGIN } } -- (void)showLoader -{ - self.isAnimating = YES; - self.loader.alpha = 1.0f; - [self animateLoader]; -} - -- (void)animateLoader -{ - __weak OWSMessageBubbleView *weakSelf = self; - self.loader.frame = CGRectMake(0.0f, 0.0f, 0.0f, self.frame.size.height); - [UIView animateWithDuration:2 animations:^{ - if (weakSelf != nil) { - weakSelf.loader.frame = CGRectMake(0.0f, 0.0f, weakSelf.frame.size.width, weakSelf.frame.size.height); - } - } completion:^(BOOL isFinished) { - if (weakSelf != nil && weakSelf.isAnimating) { - [weakSelf animateLoader]; - } - }]; -} - -- (void)hideLoader -{ - self.isAnimating = NO; - self.loader.alpha = 0.0f; -} - #pragma mark - Subviews - (void)configureBodyTextView @@ -880,8 +841,7 @@ NS_ASSUME_NONNULL_BEGIN LKVoiceMessageView *voiceMessageView = [[LKVoiceMessageView alloc] initWithVoiceMessage:attachment isOutgoing:self.isOutgoing]; [voiceMessageView setDuration:(int)self.viewItem.audioDurationSeconds]; - [voiceMessageView updateForProgress:self.viewItem.audioProgressSeconds / self.viewItem.audioDurationSeconds]; - [voiceMessageView setDelegate:self]; + [voiceMessageView setProgress:self.viewItem.audioProgressSeconds / self.viewItem.audioDurationSeconds]; [voiceMessageView initialize]; self.viewItem.lastAudioMessageView = voiceMessageView; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index ffe6064b6..8e1f90a7c 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -485,7 +485,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) self.audioProgressSeconds = progress; - [self.lastAudioMessageView updateForProgress:progress / duration]; + [self.lastAudioMessageView setProgress:progress / duration]; } #pragma mark - Displayable Text