yattee/Shared/Player/PlaybackBar.swift

89 lines
2.5 KiB
Swift
Raw Normal View History

2021-08-22 19:13:33 +00:00
import Foundation
import SwiftUI
struct PlaybackBar: View {
2021-08-23 21:31:51 +00:00
@Environment(\.dismiss) private var dismiss
@Environment(\.inNavigationView) private var inNavigationView
@EnvironmentObject<PlayerModel> private var player
2021-08-23 21:31:51 +00:00
2021-08-22 19:13:33 +00:00
var body: some View {
HStack {
closeButton
.frame(width: 80, alignment: .leading)
2021-08-22 19:13:33 +00:00
if player.currentItem != nil {
Text(playbackStatus)
.foregroundColor(.gray)
.font(.caption2)
.frame(minWidth: 130, maxWidth: .infinity)
2021-08-22 19:13:33 +00:00
VStack {
if player.stream != nil {
Text(currentStreamString)
2021-09-13 20:41:16 +00:00
} else {
if player.currentVideo!.live {
Image(systemName: "dot.radiowaves.left.and.right")
} else {
Image(systemName: "bolt.horizontal.fill")
}
2021-09-13 20:41:16 +00:00
}
2021-08-23 21:31:51 +00:00
}
.foregroundColor(.gray)
.font(.caption2)
.frame(width: 80, alignment: .trailing)
.fixedSize(horizontal: true, vertical: true)
} else {
Spacer()
2021-08-23 21:31:51 +00:00
}
2021-08-22 19:13:33 +00:00
}
.padding(4)
.background(.black)
}
var currentStreamString: String {
"\(player.stream!.resolution.height)p"
2021-08-22 19:13:33 +00:00
}
2021-09-13 20:41:16 +00:00
var playbackStatus: String {
if player.live {
return "LIVE"
}
guard player.time != nil, player.time!.isValid else {
return "loading..."
2021-08-22 19:13:33 +00:00
}
let remainingSeconds = player.currentVideo!.length - player.time!.seconds
2021-08-22 19:13:33 +00:00
2021-08-23 21:31:51 +00:00
if remainingSeconds < 60 {
return "less than a minute"
}
2021-08-22 19:13:33 +00:00
let timeFinishAt = Date.now.addingTimeInterval(remainingSeconds)
let timeFinishAtString = timeFinishAt.formatted(date: .omitted, time: .shortened)
return "ends at \(timeFinishAtString)"
2021-08-22 19:13:33 +00:00
}
var closeButton: some View {
Button {
dismiss()
} label: {
Label("Close", systemImage: inNavigationView ? "chevron.backward.circle.fill" : "chevron.down.circle.fill")
.labelStyle(.iconOnly)
2021-08-22 19:13:33 +00:00
}
.accessibilityLabel(Text("Close"))
.buttonStyle(.borderless)
2021-08-22 19:13:33 +00:00
.foregroundColor(.gray)
.keyboardShortcut(.cancelAction)
}
}
2021-10-13 22:10:29 +00:00
struct PlaybackBar_Previews: PreviewProvider {
static var previews: some View {
PlaybackBar()
.injectFixtureEnvironmentObjects()
}
}