From 3cb949279107fa15f55411df1faaf7cbd5856ae8 Mon Sep 17 00:00:00 2001 From: cranci1 <100066266+cranci1@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:57:38 +0100 Subject: [PATCH] addde hold cell --- .../EpisodeCell/EpisodeCell.swift | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Sora/Views/MediaInfoView/EpisodeCell/EpisodeCell.swift b/Sora/Views/MediaInfoView/EpisodeCell/EpisodeCell.swift index d69506b..427f16a 100644 --- a/Sora/Views/MediaInfoView/EpisodeCell/EpisodeCell.swift +++ b/Sora/Views/MediaInfoView/EpisodeCell/EpisodeCell.swift @@ -23,6 +23,25 @@ struct EpisodeCell: View { @State private var episodeTitle: String = "" @State private var episodeImageUrl: String = "" @State private var isLoading: Bool = true + @State private var currentProgress: Double = 0.0 + + private func markAsWatched() { + UserDefaults.standard.set(9999999.0, forKey: "lastPlayedTime_\(episode)") + UserDefaults.standard.set(9999999.0, forKey: "totalTime_\(episode)") + updateProgress() + } + + private func resetProgress() { + UserDefaults.standard.set(0.0, forKey: "lastPlayedTime_\(episode)") + UserDefaults.standard.set(0.0, forKey: "totalTime_\(episode)") + updateProgress() + } + + private func updateProgress() { + let lastPlayedTime = UserDefaults.standard.double(forKey: "lastPlayedTime_\(episode)") + let totalTime = UserDefaults.standard.double(forKey: "totalTime_\(episode)") + currentProgress = totalTime > 0 ? lastPlayedTime / totalTime : 0 + } var body: some View { HStack { @@ -51,11 +70,21 @@ struct EpisodeCell: View { Spacer() - CircularProgressBar(progress: progress) + CircularProgressBar(progress: currentProgress) .frame(width: 40, height: 40) } + .contextMenu { + Button(action: markAsWatched) { + Label("Mark as Watched", systemImage: "checkmark.circle") + } + + Button(action: resetProgress) { + Label("Reset Progress", systemImage: "arrow.counterclockwise") + } + } .onAppear { fetchEpisodeDetails() + updateProgress() } } @@ -82,15 +111,17 @@ struct EpisodeCell: View { } guard let data = data else { - print("No data received") DispatchQueue.main.async { self.isLoading = false } return } - UserDefaults.standard.set(data, forKey: cacheKey) - self.parseEpisodeDetails(data: data) + DispatchQueue.main.async { + self.parseEpisodeDetails(data: data) + UserDefaults.standard.set(data, forKey: cacheKey) + self.isLoading = false + } }.resume() }