Skip to content

Commit a69f652

Browse files
committed
continue watching button working i think
1 parent a0588fe commit a69f652

4 files changed

Lines changed: 41 additions & 10 deletions

File tree

Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct CustomVideoPlayer: UIViewControllerRepresentable {
2121

2222
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
2323
// yes? Like the plural of the famous american rapper ye? -IBHRAD
24-
// low taper fade the meme is massive
24+
// low taper fade the meme is massive -cranci
2525
}
2626
}
2727

@@ -156,7 +156,7 @@ struct CustomMediaPlayer: View {
156156
.padding(.horizontal, 32)
157157
}
158158
Spacer()
159-
if duration - currentTime <= duration * 0.10 && currentTime != duration && showWatchNextButton {
159+
if duration - currentTime <= duration * 0.10 && currentTime != duration && showWatchNextButton && duration != 0 {
160160
Button(action: {
161161
player.pause()
162162
presentationMode.wrappedValue.dismiss()

Sora/Views/MediaInfoView/EpisodeCell/CircularProgressBar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct CircularProgressBar: View {
2424
.rotationEffect(Angle(degrees: 270.0))
2525
.animation(.linear, value: progress)
2626

27-
if progress >= 0.90 {
27+
if progress >= 0.9 {
2828
Image(systemName: "checkmark")
2929
.font(.system(size: 12))
3030
} else {

Sora/Views/MediaInfoView/EpisodeCell/EpisodeCell.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct EpisodeCell: View {
2626
@State private var currentProgress: Double = 0.0
2727

2828
private func markAsWatched() {
29-
UserDefaults.standard.set(9999999.0, forKey: "lastPlayedTime_\(episode)")
30-
UserDefaults.standard.set(9999999.0, forKey: "totalTime_\(episode)")
29+
UserDefaults.standard.set(99999999.0, forKey: "lastPlayedTime_\(episode)")
30+
UserDefaults.standard.set(99999999.0, forKey: "totalTime_\(episode)")
3131
updateProgress()
3232
}
3333

Sora/Views/MediaInfoView/MediaInfoView.swift

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ struct MediaInfoView: View {
107107
.frame(width: 20, height: 20)
108108
.foregroundColor(.primary)
109109
}
110-
.padding(5)
110+
.padding(4)
111111
.background(Capsule().fill(Color.accentColor.opacity(0.4)))
112112
}
113113
}
@@ -139,11 +139,12 @@ struct MediaInfoView: View {
139139

140140
HStack {
141141
Button(action: {
142+
playFirstUnwatchedEpisode()
142143
}) {
143144
HStack {
144145
Image(systemName: "play.fill")
145146
.foregroundColor(.primary)
146-
Text("Start Watching")
147+
Text(startWatchingText)
147148
.font(.headline)
148149
.foregroundColor(.primary)
149150
}
@@ -205,7 +206,6 @@ struct MediaInfoView: View {
205206
if !isFetchingEpisode {
206207
isFetchingEpisode = true
207208
fetchStream(href: ep.href)
208-
DropManager.shared.showDrop(title: "Fetching Stream", subtitle: "", duration: 0.5, icon: UIImage(systemName: "arrow.triangle.2.circlepath"))
209209
}
210210
}
211211
.disabled(isFetchingEpisode)
@@ -267,6 +267,35 @@ struct MediaInfoView: View {
267267
}
268268
}
269269

270+
private var startWatchingText: String {
271+
for ep in episodeLinks {
272+
let lastPlayedTime = UserDefaults.standard.double(forKey: "lastPlayedTime_\(ep.href)")
273+
let totalTime = UserDefaults.standard.double(forKey: "totalTime_\(ep.href)")
274+
let progress = totalTime > 0 ? lastPlayedTime / totalTime : 0
275+
if progress >= 0.1 && (totalTime - lastPlayedTime) > (totalTime * 0.1) {
276+
return "Continue Watching Episode \(ep.number)"
277+
}
278+
}
279+
return "Start Watching"
280+
}
281+
282+
private func playFirstUnwatchedEpisode() {
283+
for ep in episodeLinks {
284+
let lastPlayedTime = UserDefaults.standard.double(forKey: "lastPlayedTime_\(ep.href)")
285+
let totalTime = UserDefaults.standard.double(forKey: "totalTime_\(ep.href)")
286+
let progress = totalTime > 0 ? lastPlayedTime / totalTime : 0
287+
if progress >= 0.1 && (totalTime - lastPlayedTime) > (totalTime * 0.1) {
288+
selectedEpisodeNumber = ep.number
289+
fetchStream(href: ep.href)
290+
return
291+
}
292+
}
293+
if let firstEpisode = episodeLinks.first {
294+
selectedEpisodeNumber = firstEpisode.number
295+
fetchStream(href: firstEpisode.href)
296+
}
297+
}
298+
270299
private func generateRanges() -> [Range<Int>] {
271300
let chunkSize = episodeChunkSize
272301
let totalEpisodes = episodeLinks.count
@@ -297,8 +326,7 @@ struct MediaInfoView: View {
297326
self.isLoading = false
298327
self.isRefetching = false
299328
}
300-
}
301-
else {
329+
} else {
302330
jsController.fetchDetails(url: href) { items, episodes in
303331
if let item = items.first {
304332
self.synopsis = item.description
@@ -320,6 +348,7 @@ struct MediaInfoView: View {
320348
}
321349

322350
func fetchStream(href: String) {
351+
DropManager.shared.showDrop(title: "Fetching Stream", subtitle: "", duration: 0.5, icon: UIImage(systemName: "arrow.triangle.2.circlepath"))
323352
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
324353
Task {
325354
do {
@@ -397,6 +426,8 @@ struct MediaInfoView: View {
397426
selectNextEpisode()
398427
}
399428
)
429+
print(Int(selectedEpisodeNumber))
430+
print(selectedEpisodeNumber)
400431
let hostingController = UIHostingController(rootView: customMediaPlayer)
401432
hostingController.modalPresentationStyle = .fullScreen
402433
Logger.shared.log("Opening custom media player with url: \(url)")

0 commit comments

Comments
 (0)