yattee/Shared/Player/PlaybackBar.swift

75 lines
2.1 KiB
Swift
Raw Normal View History

2021-08-22 19:13:33 +00:00
import Foundation
import SwiftUI
struct PlaybackBar: View {
let video: Video
2021-08-23 21:31:51 +00:00
@Environment(\.dismiss) private var dismiss
2021-09-25 08:18:22 +00:00
@EnvironmentObject private var playback: PlaybackModel
2021-08-23 21:31:51 +00:00
2021-08-22 19:13:33 +00:00
var body: some View {
HStack {
closeButton
2021-08-23 21:31:51 +00:00
.frame(width: 60, alignment: .leading)
2021-08-22 19:13:33 +00:00
2021-09-13 20:41:16 +00:00
Text(playbackStatus)
2021-08-22 19:13:33 +00:00
.foregroundColor(.gray)
.font(.caption2)
2021-08-23 21:31:51 +00:00
.frame(minWidth: 60, maxWidth: .infinity)
2021-08-22 19:13:33 +00:00
2021-08-23 21:31:51 +00:00
VStack {
2021-09-25 08:18:22 +00:00
if playback.stream != nil {
2021-08-23 21:31:51 +00:00
Text(currentStreamString)
} else {
2021-09-13 20:41:16 +00:00
if video.live {
Image(systemName: "dot.radiowaves.left.and.right")
} else {
Image(systemName: "bolt.horizontal.fill")
}
2021-08-23 21:31:51 +00:00
}
}
.foregroundColor(.gray)
.font(.caption2)
.frame(width: 60, alignment: .trailing)
.fixedSize(horizontal: true, vertical: true)
2021-08-22 19:13:33 +00:00
}
.padding(4)
.background(.black)
}
var currentStreamString: String {
2021-09-25 08:18:22 +00:00
playback.stream != nil ? "\(playback.stream!.resolution.height)p" : ""
2021-08-22 19:13:33 +00:00
}
2021-09-13 20:41:16 +00:00
var playbackStatus: String {
2021-09-25 08:18:22 +00:00
guard playback.time != nil else {
if playback.live {
2021-09-13 20:41:16 +00:00
return "LIVE"
} else {
return "loading..."
}
2021-08-22 19:13:33 +00:00
}
2021-09-25 08:18:22 +00:00
let remainingSeconds = video.length - playback.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 "finishes at \(timeFinishAtString)"
}
var closeButton: some View {
Button(action: { dismiss() }) {
2021-09-18 20:36:42 +00:00
Image(systemName: "xmark.circle.fill")
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)
}
}