-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaylistView.swift
179 lines (158 loc) · 4.5 KB
/
PlaylistView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Copyright (c) SRG SSR. All rights reserved.
//
// License information is available from the LICENSE file.
//
import PillarboxPlayer
import SwiftUI
private struct MediaCell: View {
let media: Media
var body: some View {
VStack(alignment: .leading) {
Text(media.title)
if let subtitle = media.subtitle {
Text(subtitle)
.font(.subheadline)
.foregroundColor(.secondary)
}
}
.accessibilityElement()
.accessibilityLabel(media.title)
}
}
private struct Toolbar: View {
@ObservedObject private var player: Player
@ObservedObject private var model: PlaylistViewModel
@State private var isSelectionPresented = false
private var repeatModeImageName: String {
switch player.repeatMode {
case .off:
"repeat.circle"
case .one:
"repeat.1.circle.fill"
case .all:
"repeat.circle.fill"
}
}
private var repeatModeAccessibilityLabel: String {
switch player.repeatMode {
case .off:
"Repeat none"
case .one:
"Repeat current"
case .all:
"Repeat all"
}
}
var body: some View {
HStack {
previousButton()
Spacer()
managementButtons()
Spacer()
nextButton()
}
.padding()
.frame(maxWidth: .infinity)
.sheet(isPresented: $isSelectionPresented) {
PlaylistSelectionView(model: model)
}
}
init(model: PlaylistViewModel) {
self.player = model.player
self.model = model
}
@ViewBuilder
private func previousButton() -> some View {
Button(action: player.returnToPrevious) {
Image(systemName: "arrow.left")
}
.hoverEffect()
.accessibilityLabel("Previous")
.disabled(!player.canReturnToPrevious())
}
@ViewBuilder
private func managementButtons() -> some View {
HStack(spacing: 30) {
Button(action: toggleRepeatMode) {
Image(systemName: repeatModeImageName)
}
.hoverEffect()
.accessibilityLabel(repeatModeAccessibilityLabel)
Button(action: model.shuffle) {
Image(systemName: "shuffle")
}
.hoverEffect()
.accessibilityLabel("Shuffle")
.disabled(model.isEmpty)
Button(action: add) {
Image(systemName: "plus")
}
.hoverEffect()
.accessibilityLabel("Add")
Button(action: model.trash) {
Image(systemName: "trash")
}
.hoverEffect()
.accessibilityLabel("Delete all")
.disabled(model.isEmpty)
}
}
@ViewBuilder
private func nextButton() -> some View {
Button(action: player.advanceToNext) {
Image(systemName: "arrow.right")
}
.hoverEffect()
.accessibilityLabel("Next")
.disabled(!player.canAdvanceToNext())
}
private func toggleRepeatMode() {
switch player.repeatMode {
case .off:
player.repeatMode = .all
case .one:
player.repeatMode = .off
case .all:
player.repeatMode = .one
}
}
private func add() {
isSelectionPresented.toggle()
}
}
struct PlaylistView: View {
let medias: [Media]
@StateObject private var model = PlaylistViewModel.persisted ?? PlaylistViewModel()
var body: some View {
VStack(spacing: 0) {
PlaybackView(player: model.player, layout: $model.layout)
.supportsPictureInPicture()
#if os(iOS)
if model.layout != .maximized {
Toolbar(model: model)
List($model.medias, id: \.self, editActions: .all, selection: $model.currentMedia) { $media in
MediaCell(media: media)
}
}
#endif
}
.animation(.defaultLinear, value: model.layout)
.onAppear {
model.medias = medias
model.play()
}
.enabledForInAppPictureInPicture(persisting: model)
.tracked(name: "playlist")
}
}
extension PlaylistView: SourceCodeViewable {
static let filePath = #file
}
#Preview {
PlaylistView(medias: [
URLMedia.onDemandVideoLocalHLS,
URLMedia.shortOnDemandVideoHLS,
URLMedia.dvrVideoHLS
])
}