implementing a delegate pattern.

This commit is contained in:
Kevin G Christiano 2021-04-19 14:03:51 -04:00
parent 4fea0eb473
commit ac13ac6025

View File

@ -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