From ac13ac60259d9e33e43c9bf5a07751de4e7a296e Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Mon, 19 Apr 2021 14:03:51 -0400 Subject: [PATCH] implementing a delegate pattern. --- MVMCoreUI/Behaviors/PlayAudioBehavior.swift | 59 ++++++++++++--------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/MVMCoreUI/Behaviors/PlayAudioBehavior.swift b/MVMCoreUI/Behaviors/PlayAudioBehavior.swift index 76d7e64b..572fac0b 100644 --- a/MVMCoreUI/Behaviors/PlayAudioBehavior.swift +++ b/MVMCoreUI/Behaviors/PlayAudioBehavior.swift @@ -13,6 +13,8 @@ public protocol PagePlayAudioBehaviorConsumerProtocol { func pause() func stop() var isPlaying: Bool { get } + var messageID: String? { get } + var audioFileURL: URL? { get set } } public class PagePlayAudioBehaviorModel: PageBehaviorModelProtocol { @@ -27,7 +29,7 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior { // MARK: - Active Model //-------------------------------------------------- - public static var activeAudioPlayer: PagePlayAudioBehaviorConsumerProtocol? + public static var activeAudioPlayerDelegate: PagePlayAudioBehaviorConsumerProtocol? //-------------------------------------------------- // MARK: - Delegate @@ -50,31 +52,27 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior { // Either play or pause public func handleAction(type actionType: String?, information: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?) -> Bool { - // TODO: Has to grab the specific model that is active (expanded). Identify model holder of this action. - - // TODO: Update the model. play -> pause OR pause -> play - - // TODO: Download binary - - // TODO: Store audio file in cache using some key (messageID???) to store it. - - // TODO: Actually pause/play + // TODO: Impose Activity Indicator. + // Update the model. play -> pause OR pause -> play + if Self.activeAudioPlayerDelegate?.isPlaying ?? false { + Self.activeAudioPlayerDelegate?.pause() + + } else { + + // Download binary + downloadAudioFile { url in + + // TODO: Actually pause/play + Self.activeAudioPlayerDelegate?.audioFileURL = url + Self.activeAudioPlayerDelegate?.togglePlayPause() + + // TODO: Remove Activity Indicator. + } + } + // TODO: Tell Template to update this cell (needs to be built). Currently it updates all cells. - -// let templateModel = (delegate?.moleculeDelegate as? PageProtocol)?.pageModel as? TemplateModelProtocol - Self.activeAudioPlayer?.play() -// let consumers: [PagePlayAudioBehaviorConsumerProtocol] = templateModel!.allMoleculesOfType() -// for consumer in consumers { -// consumer.playPause() -// } - - // Tell template to update. -// MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in - // TODO: move to protocol function instead -// (self?.delegate?.moleculeDelegate as? ViewController)?.handleNewData() -// }) - + return true } @@ -85,6 +83,19 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior { //-------------------------------------------------- // MARK: - Download Audio File //-------------------------------------------------- + + func downloadAudioFile(_ completion: @escaping (URL) -> ()) { + + // TODO: Check cache. Return if available +// let audioData = MVMCoreCache.shared()?.getCachedData(withName: PagePlayAudioBehavior.activeAudioPlayerDelegate!.messageID!) + + guard let soundFileURL = Bundle.main.url(forResource: "sampleAudio", withExtension: "wav") else { return } + + // TODO: Store audio file in cache using some key (messageID???) to store it. +// MVMCoreCache.shared()?.addData(toCache: soundFileURL.dataRepresentation, withName: PagePlayAudioBehavior.activeAudioPlayerDelegate!.messageID!) + completion(soundFileURL) +// return soundFileURL + } /* // https://oneconfluence.verizon.com/pages/viewpage.action?spaceKey=EIM&title=FDV+API