2021-11-05 19:57:22 +00:00
|
|
|
import Defaults
|
2021-10-05 20:20:09 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct NowPlayingView: View {
|
2021-11-02 23:02:02 +00:00
|
|
|
enum ViewSection: CaseIterable {
|
|
|
|
case nowPlaying, playingNext, playedPreviously, related
|
|
|
|
}
|
|
|
|
|
|
|
|
var sections = ViewSection.allCases
|
2021-10-22 15:00:09 +00:00
|
|
|
var inInfoViewController = false
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
|
2021-11-05 19:57:22 +00:00
|
|
|
@Default(.saveHistory) private var saveHistory
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
var body: some View {
|
2021-10-22 15:00:09 +00:00
|
|
|
if inInfoViewController {
|
2021-10-05 20:20:09 +00:00
|
|
|
content
|
|
|
|
.background(.thinMaterial)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 24))
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var content: some View {
|
2021-10-23 10:13:05 +00:00
|
|
|
List {
|
|
|
|
Group {
|
2021-11-02 23:02:02 +00:00
|
|
|
if sections.contains(.nowPlaying), let item = player.currentItem {
|
2021-10-23 10:13:05 +00:00
|
|
|
Section(header: Text("Now Playing")) {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button {
|
|
|
|
player.presentPlayer()
|
|
|
|
} label: {
|
|
|
|
VideoBanner(video: item.video)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onPlayPauseCommand(perform: player.togglePlay)
|
|
|
|
}
|
|
|
|
|
2021-11-02 23:02:02 +00:00
|
|
|
if sections.contains(.playingNext) {
|
|
|
|
Section(header: Text("Playing Next")) {
|
|
|
|
if player.queue.isEmpty {
|
|
|
|
Text("Playback queue is empty")
|
|
|
|
.padding([.vertical, .leading], 40)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
|
2021-11-02 23:02:02 +00:00
|
|
|
ForEach(player.queue) { item in
|
|
|
|
Button {
|
|
|
|
player.advanceToItem(item)
|
|
|
|
player.presentPlayer()
|
|
|
|
} label: {
|
|
|
|
VideoBanner(video: item.video)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Delete", role: .destructive) {
|
|
|
|
player.remove(item)
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
}
|
2021-11-02 23:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sections.contains(.related), !player.currentVideo.isNil, !player.currentVideo!.related.isEmpty {
|
|
|
|
Section(header: inInfoViewController ? AnyView(EmptyView()) : AnyView(Text("Related"))) {
|
|
|
|
ForEach(player.currentVideo!.related) { video in
|
|
|
|
Button {
|
|
|
|
player.playNow(video)
|
|
|
|
player.presentPlayer()
|
|
|
|
} label: {
|
|
|
|
VideoBanner(video: video)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Play Next") {
|
|
|
|
player.playNext(video)
|
|
|
|
}
|
|
|
|
Button("Play Last") {
|
|
|
|
player.enqueueVideo(video)
|
|
|
|
}
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-10-23 10:13:05 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:05:19 +00:00
|
|
|
|
2021-11-05 19:57:22 +00:00
|
|
|
if sections.contains(.playedPreviously), saveHistory, !player.history.isEmpty {
|
2021-10-23 10:13:05 +00:00
|
|
|
Section(header: Text("Played Previously")) {
|
|
|
|
ForEach(player.history) { item in
|
|
|
|
Button {
|
|
|
|
player.playHistory(item)
|
|
|
|
player.presentPlayer()
|
|
|
|
} label: {
|
|
|
|
VideoBanner(video: item.video, playbackTime: item.playbackTime, videoDuration: item.videoDuration)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
2021-11-05 18:23:28 +00:00
|
|
|
Button("Remove", role: .destructive) {
|
2021-10-23 10:13:05 +00:00
|
|
|
player.removeHistory(item)
|
|
|
|
}
|
|
|
|
|
2021-11-05 18:23:28 +00:00
|
|
|
Button("Remove All", role: .destructive) {
|
2021-10-23 10:13:05 +00:00
|
|
|
player.removeHistoryItems()
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
|
|
|
|
.padding(.vertical, 20)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
.padding(.horizontal, inInfoViewController ? 40 : 0)
|
|
|
|
.listStyle(.grouped)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 560, maxHeight: .infinity, alignment: .leading)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func header(_ text: String) -> some View {
|
|
|
|
Text(text)
|
2021-10-22 15:00:09 +00:00
|
|
|
.font((inInfoViewController ? Font.system(size: 40) : .title3).bold())
|
2021-10-05 20:20:09 +00:00
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NowPlayingView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NowPlayingView()
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-10-22 15:00:09 +00:00
|
|
|
|
|
|
|
NowPlayingView(inInfoViewController: true)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|