Skip to content

Commit

Permalink
Update:iOS remove paused timer that synced with the server when curre…
Browse files Browse the repository at this point in the history
…nt media was paused
  • Loading branch information
advplyr committed Nov 12, 2023
1 parent ad5146e commit a7cbdbf
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
23 changes: 0 additions & 23 deletions ios/App/Shared/player/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class AudioPlayer: NSObject {
internal var currentTrackIndex = 0
private var allPlayerItems:[AVPlayerItem] = []

private var pausedTimer: Timer?

// MARK: - Constructor
init(sessionId: String, playWhenReady: Bool = false, playbackRate: Float = 1) {
self.playWhenReady = playWhenReady
Expand Down Expand Up @@ -159,7 +157,6 @@ class AudioPlayer: NSObject {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.closed.rawValue), object: nil)

// Remove timers
self.stopPausedTimer()
self.removeSleepTimer()
}

Expand Down Expand Up @@ -301,21 +298,6 @@ class AudioPlayer: NSObject {
}
}

private func startPausedTimer() {
guard self.pausedTimer == nil else { return }
self.queue.async {
self.pausedTimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { [weak self] timer in
self?.logger.log("PAUSE TIMER: Syncing from server")
Task { await PlayerProgress.shared.syncFromServer() }
}
}
}

private func stopPausedTimer() {
self.pausedTimer?.invalidate()
self.pausedTimer = nil
}

// MARK: - Methods
public func play(allowSeekBack: Bool = false, isInitializing: Bool = false) {
guard self.isInitialized() || isInitializing else { return }
Expand Down Expand Up @@ -345,9 +327,6 @@ class AudioPlayer: NSObject {
private func resumePlayback() {
logger.log("PLAY: Resuming playback")

// Stop the paused timer
self.stopPausedTimer()

self.markAudioSessionAs(active: true)
DispatchQueue.runOnMainQueue {
self.audioPlayer.play()
Expand Down Expand Up @@ -380,8 +359,6 @@ class AudioPlayer: NSObject {

self.status = .paused
updateNowPlaying()

self.startPausedTimer()
}

public func seek(_ to: Double, from: String) {
Expand Down
12 changes: 1 addition & 11 deletions ios/App/Shared/player/PlayerProgress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ class PlayerProgress {
await UIApplication.shared.endBackgroundTask(backgroundToken)
}

public func syncFromServer() async {
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncFromServer")
do {
try await updateLocalSessionFromServerMediaProgress()
} catch {
logger.error("Failed to syncFromServer")
logger.error(error)
}
await UIApplication.shared.endBackgroundTask(backgroundToken)
}


// MARK: - SYNC LOGIC

Expand Down Expand Up @@ -164,6 +153,7 @@ class PlayerProgress {
}
}

// TODO: Unused for now
private func updateLocalSessionFromServerMediaProgress() async throws {
logger.log("updateLocalSessionFromServerMediaProgress: Checking if local media progress was updated on server")
guard let session = try Realm(queue: nil).objects(PlaybackSession.self).last(where: {
Expand Down
6 changes: 4 additions & 2 deletions ios/App/Shared/util/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ class ApiClient {
if (success) {
// Remove sessions from db
try playbackSessions.forEach { session in
if let session = session.thaw() {
try session.delete()
if (!session.isActiveSession) {
if let session = session.thaw() {
try session.delete()
}
}
}
}
Expand Down

0 comments on commit a7cbdbf

Please sign in to comment.