mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-05 02:25:45 +02:00
refactor: Pull out seek back calculation into testable class
This commit is contained in:
parent
3b285db21a
commit
890852bef5
4 changed files with 260 additions and 1 deletions
46
ios/App/Shared/player/util/PlayerTimeUtils.swift
Normal file
46
ios/App/Shared/player/util/PlayerTimeUtils.swift
Normal file
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// PlayerTimeUtils.swift
|
||||
// Audiobookshelf
|
||||
//
|
||||
// Created by Ron Heft on 9/20/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class PlayerTimeUtils {
|
||||
|
||||
private init() {}
|
||||
|
||||
static func calcSeekBackTime(currentTime: TimeInterval, lastPlayedMs: Double?) -> TimeInterval {
|
||||
let sinceLastPlayed = timeSinceLastPlayed(lastPlayedMs)
|
||||
let timeToSeekBack = timeToSeekBackForSinceLastPlayed(sinceLastPlayed)
|
||||
return currentTime.advanced(by: -timeToSeekBack)
|
||||
}
|
||||
|
||||
static internal func timeSinceLastPlayed(_ lastPlayedMs: Double?) -> TimeInterval? {
|
||||
guard let lastPlayedMs = lastPlayedMs else { return nil }
|
||||
let lastPlayed = Date(timeIntervalSince1970: lastPlayedMs / 1000)
|
||||
return lastPlayed.timeIntervalSinceNow
|
||||
}
|
||||
|
||||
static internal func timeToSeekBackForSinceLastPlayed(_ sinceLastPlayed: TimeInterval?) -> TimeInterval {
|
||||
if let sinceLastPlayed = sinceLastPlayed {
|
||||
if sinceLastPlayed < 6 {
|
||||
return 2
|
||||
} else if sinceLastPlayed < 12 {
|
||||
return 10
|
||||
} else if sinceLastPlayed < 30 {
|
||||
return 15
|
||||
} else if sinceLastPlayed < 180 {
|
||||
return 20
|
||||
} else if sinceLastPlayed < 3600 {
|
||||
return 25
|
||||
} else {
|
||||
return 29
|
||||
}
|
||||
} else {
|
||||
return 5
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue