diff --git a/MVMCoreUI/Atomic/Protocols/MoleculeTreeTraversalProtocol.swift b/MVMCoreUI/Atomic/Protocols/MoleculeTreeTraversalProtocol.swift index 4e7efabb..abe4cdd7 100644 --- a/MVMCoreUI/Atomic/Protocols/MoleculeTreeTraversalProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/MoleculeTreeTraversalProtocol.swift @@ -50,5 +50,4 @@ extension MoleculeTreeTraversalProtocol { return accumulator } } - } diff --git a/MVMCoreUI/Behaviors/PlayAudioBehavior.swift b/MVMCoreUI/Behaviors/PlayAudioBehavior.swift index a9eebd55..76d7e64b 100644 --- a/MVMCoreUI/Behaviors/PlayAudioBehavior.swift +++ b/MVMCoreUI/Behaviors/PlayAudioBehavior.swift @@ -8,9 +8,11 @@ public protocol PagePlayAudioBehaviorConsumerProtocol { - func playPause() + func togglePlayPause() + func play() + func pause() func stop() - func isPlaying() + var isPlaying: Bool { get } } public class PagePlayAudioBehaviorModel: PageBehaviorModelProtocol { @@ -21,6 +23,12 @@ public class PagePlayAudioBehaviorModel: PageBehaviorModelProtocol { } public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior { + //-------------------------------------------------- + // MARK: - Active Model + //-------------------------------------------------- + + public static var activeAudioPlayer: PagePlayAudioBehaviorConsumerProtocol? + //-------------------------------------------------- // MARK: - Delegate //-------------------------------------------------- @@ -39,20 +47,33 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior { // MARK: - Custom Action //-------------------------------------------------- + // Either play or pause public func handleAction(type actionType: String?, information: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?) -> Bool { - let templateModel = (delegate?.moleculeDelegate as? PageProtocol)?.pageModel as? TemplateModelProtocol + // TODO: Has to grab the specific model that is active (expanded). Identify model holder of this action. - let consumers: [PagePlayAudioBehaviorConsumerProtocol] = templateModel!.allMoleculesOfType() - for consumer in consumers { - consumer.playPause() - } + // 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: 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 +// MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in // TODO: move to protocol function instead - (self?.delegate?.moleculeDelegate as? ViewController)?.handleNewData() - }) +// (self?.delegate?.moleculeDelegate as? ViewController)?.handleNewData() +// }) return true } @@ -60,4 +81,112 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior { public func onPageHidden(_ delegateObject: MVMCoreUIDelegateObject?) { // TODO: Stop player } + + //-------------------------------------------------- + // MARK: - Download Audio File + //-------------------------------------------------- +/* + // https://oneconfluence.verizon.com/pages/viewpage.action?spaceKey=EIM&title=FDV+API + + /// The directory to store all downloaded voicemails. + func voicemailDirectory() -> URL? { + + guard let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil } + return documentsURL.appendingPathComponent("fios_vm") + } + + func voicemailListQuery(accountId: String, sourceIP: String, mdn: String, mailBoxNo: String, timeZone: String) { + + let url = URL(string: "")! + var request = URLRequest(url: url) + request.httpMethod = "POST" + + let task = URLSession.shared.dataTask(with: request) { data, response, error in + // do something with the result + // print(data) +// if let data = data { + // TODO: Receive List of MDNs + // if let data = data, let dataString = String(data: data, encoding: .utf8) { + // print("Response data string:\n \(dataString)") + // } + // print(String(data: data, encoding: .utf8)) +// } else { + // print("no data") +// } + } + + task.resume() + + } + + func fetchVoicemail(accountId: String, sourceIP: String, mdn: String, mailBoxNo: String) { + +// guard let messageID = messageID else { return } + + let url = URL(string: "")! + var request = URLRequest(url: url) + request.httpMethod = "POST" + + let task = URLSession.shared.dataTask(with: request) { data, response, error in + // do something with the result + // print(data) +// if let data = data { + //body/VoiceMailMessages/Stream/TN/VMID/Audio/bin + // TODO: Receive List of MDNs + // if let data = data, let dataString = String(data: data, encoding: .utf8) { + // print("Response data string:\n \(dataString)") + // } + // print(String(data: data, encoding: .utf8)) +// } else { + // print("no data") +// } + } + + task.resume() + + // TODO: Fetch Audio file. + } + + func downloadVoicemail(url: URL?) { + + let downloadTask = URLSession.shared.downloadTask(with: url!) { [weak self] url, response, error in + guard error == nil, + let fileURL = url + else { return } + + do { + // "data/user/0/com.verizon.myfios/files/VM_INBOX-8711.wav" + let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) + let savedURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent) + try FileManager.default.moveItem(at: fileURL, to: savedURL) + self?.audioFilePath = savedURL + } catch let error as NSError { + MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription) + } + } + downloadTask.resume() + } + + func fetchAudioFilePath() { + + guard let messageID = messageID, // "INBOX-8711" + let cleanMDN = cleanMDN, // "4124712342" + let destinationURL = voicemailDirectory() + else { return } + + let uri = "com.verizon.fios.voice2/downloadvm?VM_DOWNLOAD_URI_DATA_MESSAGE_ID=\(messageID)&VM_DOWNLOAD_URI_DATA_TN=\(cleanMDN)" + let url = URL(string: uri) + + + // TODO: First check to see if the file is stored on disk, if not, then downloaded. + createDirectoryIfNeeded(url: destinationURL) + + if FileManager.default.fileExists(atPath: destinationURL.path) { + audioFilePath = destinationURL + + } else { + downloadVoicemail(url: url) + } + } + */ }