From 03a47b06ee5a5e2b5b97b54a185968e6253311ed Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 11 Aug 2022 18:49:04 -0500 Subject: [PATCH] Fix:iOS sleep timer chapter end #291 --- ios/App/App/plugins/AbsAudioPlayer.swift | 10 +++++---- ios/App/Podfile | 12 +++++------ ios/App/Shared/player/PlayerHandler.swift | 25 ++++++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ios/App/App/plugins/AbsAudioPlayer.swift b/ios/App/App/plugins/AbsAudioPlayer.swift index d7f00e28..4f576811 100644 --- a/ios/App/App/plugins/AbsAudioPlayer.swift +++ b/ios/App/App/plugins/AbsAudioPlayer.swift @@ -134,24 +134,26 @@ public class AbsAudioPlayer: CAPPlugin { @objc func setSleepTimer(_ call: CAPPluginCall) { guard let timeString = call.getString("time") else { return call.resolve([ "success": false ]) } guard let time = Int(timeString) else { return call.resolve([ "success": false ]) } + let timeSeconds = time / 1000 NSLog("chapter time: \(call.getBool("isChapterTime", false))") if call.getBool("isChapterTime", false) { - let timeToPause = time / 1000 - Int(PlayerHandler.getCurrentTime() ?? 0) + let timeToPause = timeSeconds - Int(PlayerHandler.getCurrentTime() ?? 0) if timeToPause < 0 { return call.resolve([ "success": false ]) } - NSLog("oof \(timeToPause)") - + PlayerHandler.sleepTimerChapterStopTime = timeSeconds PlayerHandler.remainingSleepTime = timeToPause return call.resolve([ "success": true ]) } - PlayerHandler.remainingSleepTime = time / 1000 + PlayerHandler.sleepTimerChapterStopTime = nil + PlayerHandler.remainingSleepTime = timeSeconds call.resolve([ "success": true ]) } @objc func cancelSleepTimer(_ call: CAPPluginCall) { PlayerHandler.remainingSleepTime = nil + PlayerHandler.sleepTimerChapterStopTime = nil call.resolve() } @objc func getSleepTimerTime(_ call: CAPPluginCall) { diff --git a/ios/App/Podfile b/ios/App/Podfile index 89125e80..dcdae730 100644 --- a/ios/App/Podfile +++ b/ios/App/Podfile @@ -9,12 +9,12 @@ install! 'cocoapods', :disable_input_output_paths => true def capacitor_pods pod 'Capacitor', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' - pod 'CapacitorApp', :path => '..\..\node_modules\@capacitor\app' - pod 'CapacitorDialog', :path => '..\..\node_modules\@capacitor\dialog' - pod 'CapacitorHaptics', :path => '..\..\node_modules\@capacitor\haptics' - pod 'CapacitorNetwork', :path => '..\..\node_modules\@capacitor\network' - pod 'CapacitorStatusBar', :path => '..\..\node_modules\@capacitor\status-bar' - pod 'CapacitorStorage', :path => '..\..\node_modules\@capacitor\storage' + pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app' + pod 'CapacitorDialog', :path => '../../node_modules/@capacitor/dialog' + pod 'CapacitorHaptics', :path => '../../node_modules/@capacitor/haptics' + pod 'CapacitorNetwork', :path => '../../node_modules/@capacitor/network' + pod 'CapacitorStatusBar', :path => '../../node_modules/@capacitor/status-bar' + pod 'CapacitorStorage', :path => '../../node_modules/@capacitor/storage' end target 'App' do diff --git a/ios/App/Shared/player/PlayerHandler.swift b/ios/App/Shared/player/PlayerHandler.swift index 3384e5fa..8aed62e8 100644 --- a/ios/App/Shared/player/PlayerHandler.swift +++ b/ios/App/Shared/player/PlayerHandler.swift @@ -13,6 +13,7 @@ class PlayerHandler { private static var timer: Timer? private static var lastSyncTime:Double = 0.0 + public static var sleepTimerChapterStopTime: Int? = nil private static var _remainingSleepTime: Int? = nil public static var remainingSleepTime: Int? { get { @@ -127,18 +128,28 @@ class PlayerHandler { private static func tick() { if !paused { listeningTimePassedSinceLastSync += 1 + + if remainingSleepTime != nil { + if sleepTimerChapterStopTime != nil { + let timeUntilChapterEnd = Double(sleepTimerChapterStopTime ?? 0) - (getCurrentTime() ?? 0) + if timeUntilChapterEnd <= 0 { + paused = true + remainingSleepTime = nil + } else { + remainingSleepTime = Int(timeUntilChapterEnd.rounded()) + } + } else { + if remainingSleepTime! <= 0 { + paused = true + } + remainingSleepTime! -= 1 + } + } } if listeningTimePassedSinceLastSync >= 5 { syncProgress() } - - if remainingSleepTime != nil { - if remainingSleepTime! == 0 { - paused = true - } - remainingSleepTime! -= 1 - } } public static func syncProgress() { if session == nil { return }