Moving Downloading Audio file logic to view from Behaviour

This commit is contained in:
Sumanth Nadigadda 2021-06-21 22:33:39 +05:30
parent 975f3fa012
commit b318474da1

View File

@ -60,16 +60,7 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior {
Self.activeAudioPlayerDelegate?.pause()
} else {
// Download binary
downloadAudioFile(information: information) { url in
// TODO: Actually pause/play
Self.activeAudioPlayerDelegate?.audioFileURL = url
Self.activeAudioPlayerDelegate?.togglePlayPause()
// TODO: Remove Activity Indicator.
}
Self.activeAudioPlayerDelegate?.play()
}
// TODO: Tell Template to update this cell (needs to be built). Currently it updates all cells.
@ -81,63 +72,6 @@ public class PagePlayAudioBehavior: PageCustomActionHandlerBehavior {
// TODO: Stop player
}
//--------------------------------------------------
// MARK: - Download Audio File
//--------------------------------------------------
func downloadAudioFile(information:[AnyHashable: Any]?,_ completion: @escaping (URL) -> ()) {
guard let extraparams = information?["extraParameters"] as? [AnyHashable:Any] else {return}
guard let requestParams = MVMCoreRequestParameters.init(pageType: "getVoiceMailMessage", extraParameters: extraparams) else { return }
if let voicemailID = extraparams["messageID"] as? String, checkIfFileExists(voicemailID: voicemailID) {
let vmURL = self.constructVoicemailFilePathWithID(voicemailID: voicemailID)
completion(vmURL)
} else {
MVMCoreLoadHandler.sharedGlobal()?.sendRequest(requestParams, locationForError: "PlayAudioRequest", requestFinished: { response, error in
guard let voicemailJSON = response as? [String:AnyHashable] else { return }
guard let voicemail = voicemailJSON["bin"] as? String, let voicemailID = voicemailJSON["vmMessageId"] as? String else { return }
let audioURL = self.saveVoicemailToDocumentDirectory(voicemail: voicemail, voicemailID: voicemailID)
completion(audioURL)
})
}
}
func checkIfFileExists(voicemailID:String) -> Bool {
var cachePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
cachePath?.appendPathComponent("FiOS.FDV.Voicemails", isDirectory: false)
let fileName = cachePath?.appendingPathComponent("VM_\(voicemailID).wmv")
return FileManager.default.fileExists(atPath: fileName?.path ?? "")
}
func constructVoicemailFilePathWithID(voicemailID:String) -> URL {
let voicemailUrl = URL(string: "fileNotFound")!
///Setting Voicemail Path
var cachePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
cachePath?.appendPathComponent("FiOS.FDV.Voicemails", isDirectory: false)
if let path = cachePath?.path, !FileManager.default.fileExists(atPath: path) {
do{
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
}
}
let fileName = cachePath?.appendingPathComponent("VM_\(voicemailID).wmv")
return fileName ?? voicemailUrl
}
func saveVoicemailToDocumentDirectory(voicemail:String, voicemailID:String) -> URL {
let voicemailUrl = URL(string: "fileNotFound")!
let fileName = self.constructVoicemailFilePathWithID(voicemailID: voicemailID)
let voicemailData = Data(base64Encoded: voicemail)
do{
try voicemailData?.write(to: fileName, options: .completeFileProtection)
return fileName
} catch let error as NSError {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: error.debugDescription)
}
return voicemailUrl
}
// func saveVoicemailToDocumentDirectory(voicemail:String) -> URL {
// let voicemailUrl = URL(string: "fileNotFound")!