forked from MacMagazine/app-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChaptersView.swift
More file actions
201 lines (175 loc) · 6.61 KB
/
ChaptersView.swift
File metadata and controls
201 lines (175 loc) · 6.61 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import AnalyticsLibrary
import MacMagazineLibrary
import SwiftUI
import UIComponentsLibrary
struct ChaptersView: View {
@EnvironmentObject private var analytics: AnalyticsManager
@Bindable private var playerManager: PodcastPlayerManager
@Binding private var isShowingChapterDialog: Bool
@State private var backgroundGradientColors: [Color] = [.black, .black]
@State private var isDarkBackground = false
private let backgroundGradientStyle: PodcastBackgroundGradientStyle
init(
playerManager: PodcastPlayerManager,
backgroundGradientStyle: PodcastBackgroundGradientStyle,
isShowingChapterDialog: Binding<Bool>
) {
self.playerManager = playerManager
self.backgroundGradientStyle = backgroundGradientStyle
_isShowingChapterDialog = isShowingChapterDialog
}
var body: some View {
ZStack {
BackgroundView(
chapter: playerManager.currentChapter,
backgroundGradientColors: backgroundGradientColors,
usesGradient: false
)
.ignoresSafeArea()
content
}
.trackScreen(
AnalyticsConstants.Screen.podcastChapters.name,
previous: nil,
analytics: analytics
)
.preferredColorScheme(isDarkBackground ? .dark : .light)
.onAppear {
updateBackgroundGradient(data: playerManager.currentChapter?.artworkData)
}
.onChange(of: playerManager.currentChapter?.artworkData) { _, value in
updateBackgroundGradient(data: value)
}
}
}
// MARK: - UI
private extension ChaptersView {
var content: some View {
ScrollView {
LazyVStack(spacing: 10) {
ForEach(playerManager.chapters, id: \.id) { chapter in
chapterRow(chapter)
}
}
.padding(.horizontal, 16)
.padding(.top, 12)
.padding(.bottom, 24)
}
.scrollIndicators(.hidden)
}
func chapterRow(_ chapter: PodcastChapter) -> some View {
return Button {
playerManager.seek(to: chapter.start.seconds)
isShowingChapterDialog.toggle()
} label: {
HStack(spacing: 12) {
PodcastImageView(
artworkData: chapter.artworkData,
location: .chapter,
fallback: { Rectangle().fill(.clear) }
)
.frame(width: 44, height: 44)
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
VStack(alignment: .leading, spacing: 8) {
Text(chapter.title)
.font(isActive(for: chapter) ? .body.weight(.bold) : .body.weight(.semibold))
.foregroundStyle(.primary)
.multilineTextAlignment(.leading)
HStack(spacing: 12) {
HStack(spacing: 6) {
Image(systemName: "clock")
.font(.system(size: 14))
Text(chapter.startString)
}
Spacer()
HStack(spacing: 6) {
Text(chapter.durationString)
Image(systemName: "microphone")
.font(.system(size: 14))
}
}
.font(.callout)
.foregroundStyle(.secondary)
if isActive(for: chapter) {
TimelineView(.animation) { _ in
progressBar(for: chapter)
}
.transition(.opacity)
}
}
Spacer(minLength: 0)
}
.padding(.vertical, 12)
.padding(.horizontal, 12)
.contentShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
}
.buttonStyle(.plain)
.accessibilityLabel("\(chapter.title), começando em \(chapter.startString) com duração de \(chapter.durationString).")
.background {
cardBackground(for: chapter)
}
.scaleEffect(isActive(for: chapter) ? 1.035 : 1.0)
.animation(.spring(response: 0.35, dampingFraction: 0.85), value: isActive(for: chapter))
}
}
// MARK: - Card background (glass + highlight + “band” que acompanha o tempo)
private extension ChaptersView {
func cardBackground(for chapter: PodcastChapter) -> some View {
return RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(.ultraThinMaterial)
.overlay {
RoundedRectangle(cornerRadius: 16, style: .continuous)
.strokeBorder(
.white.opacity(
isActive(for: chapter) ? 0.35 : (isDarkBackground ? 0.10 : 0.18)
),
lineWidth: isActive(for: chapter) ? 1.2 : 0.5
)
}
}
func chapterTintColor(for chapter: PodcastChapter) -> Color {
chapter.backgroundColor(at: playerManager.currentTime, using: backgroundGradientColors)
}
}
// MARK: - Progress bar
private extension ChaptersView {
func progressBar(for chapter: PodcastChapter) -> some View {
let progress = Double(progress(for: chapter))
return Slider(
value: .constant(progress),
in: 0...1
)
.tint(.white.opacity(isDarkBackground ? 0.75 : 0.65))
.sliderThumbVisibility(.hidden)
.frame(height: 3)
.padding(.top, 2)
.accessibilityHidden(true)
.allowsHitTesting(false)
}
}
// MARK: - Active/progress helpers
private extension ChaptersView {
@MainActor
func isActive(for chapter: PodcastChapter) -> Bool {
playerManager.currentChapter == chapter
}
func progress(for chapter: PodcastChapter) -> CGFloat {
let time = playerManager.currentTime
let start = chapter.start.seconds
let end = chapter.end.seconds
let denom = max(0.001, end - start)
return CGFloat((time - start) / denom)
}
}
// MARK: - Background
private extension ChaptersView {
func updateBackgroundGradient(data: Data? = nil) {
BackgroundViewModel.backgroundGradient(
data: data,
artworkURL: Constants.coverURL,
backgroundGradientStyle: backgroundGradientStyle,
backgroundGradientColors: $backgroundGradientColors,
isDarkBackground: $isDarkBackground
)
}
}