2022-06-18 12:39:49 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
final class NetworkStateModel: ObservableObject {
|
2022-09-01 23:05:31 +00:00
|
|
|
static var shared = NetworkStateModel()
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
@Published var pausedForCache = false
|
|
|
|
@Published var cacheDuration = 0.0
|
|
|
|
@Published var bufferingState = 0.0
|
|
|
|
|
2022-09-01 23:05:31 +00:00
|
|
|
private var player: PlayerModel! { .shared }
|
|
|
|
private let controlsOverlayModel = ControlOverlaysModel.shared
|
|
|
|
|
|
|
|
var osdVisible: Bool {
|
|
|
|
guard let player = player else { return false }
|
|
|
|
return player.isPlaying && ((player.activeBackend == .mpv && pausedForCache) || player.isSeeking)
|
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
|
|
|
|
var fullStateText: String? {
|
|
|
|
guard let bufferingStateText = bufferingStateText,
|
|
|
|
let cacheDurationText = cacheDurationText
|
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "\(bufferingStateText) (\(cacheDurationText))"
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufferingStateText: String? {
|
|
|
|
guard detailsAvailable else { return nil }
|
|
|
|
return String(format: "%.0f%%", bufferingState)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cacheDurationText: String? {
|
|
|
|
guard detailsAvailable else { return nil }
|
|
|
|
return String(format: "%.2fs", cacheDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
var detailsAvailable: Bool {
|
|
|
|
guard let player = player else { return false }
|
|
|
|
return player.activeBackend.supportsNetworkStateBufferingDetails
|
|
|
|
}
|
|
|
|
|
|
|
|
var needsUpdates: Bool {
|
|
|
|
if let player = player {
|
2022-09-01 23:05:31 +00:00
|
|
|
return !player.currentItem.isNil && (pausedForCache || player.isSeeking || player.isLoadingVideo || controlsOverlayModel.presenting)
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 22:40:46 +00:00
|
|
|
return false
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
}
|