Fix updating watch history

This commit is contained in:
Arkadiusz Fal 2022-12-15 22:39:42 +01:00
parent 12005e63e1
commit 404e2e6768
3 changed files with 72 additions and 3 deletions

View File

@ -48,6 +48,10 @@ extension PlayerModel {
let id = currentVideo.videoID let id = currentVideo.videoID
let time = backend.currentTime let time = backend.currentTime
let seconds = time?.seconds ?? 0 let seconds = time?.seconds ?? 0
let duration = playerTime.duration.seconds
if seconds < 3 {
return
}
let watchFetchRequest = Watch.fetchRequest() let watchFetchRequest = Watch.fetchRequest()
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String) watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String)
@ -64,8 +68,6 @@ extension PlayerModel {
let duration = self.playerTime.duration.seconds let duration = self.playerTime.duration.seconds
if results?.isEmpty ?? true { if results?.isEmpty ?? true {
if seconds < 3, duration > 3 { return }
watch = Watch(context: self.backgroundContext) watch = Watch(context: self.backgroundContext)
watch.videoID = id watch.videoID = id
watch.appName = currentVideo.app.rawValue watch.appName = currentVideo.app.rawValue

View File

@ -58,7 +58,7 @@ extension Watch {
var progress: Double { var progress: Double {
guard videoDuration.isFinite, !videoDuration.isZero else { guard videoDuration.isFinite, !videoDuration.isZero else {
return 0 return 100
} }
let progress = (stoppedAt / videoDuration) * 100 let progress = (stoppedAt / videoDuration) * 100

View File

@ -0,0 +1,67 @@
import Defaults
import Foundation
import SwiftUI
struct WatchView: View {
var watch: Watch?
var videoID: Video.ID
var duration: Double
@Default(.watchedVideoBadgeColor) private var watchedVideoBadgeColor
var backgroundContext = PersistenceController.shared.container.newBackgroundContext()
var body: some View {
#if os(tvOS)
if finished {
image
}
#else
Button(action: toggleWatch) {
image
}
.opacity(finished ? 1 : 0.4)
.buttonStyle(.plain)
#endif
}
var image: some View {
Image(systemName: imageSystemName)
.foregroundColor(Color(
watchedVideoBadgeColor == .colorSchemeBased ? "WatchProgressBarColor" :
watchedVideoBadgeColor == .red ? "AppRedColor" : "AppBlueColor"
))
.background(backgroundColor)
.clipShape(Circle())
.imageScale(.large)
}
func toggleWatch() {
if finished, let watch {
PlayerModel.shared.removeWatch(watch)
} else {
Watch.markAsWatched(videoID: watch?.videoID ?? videoID, account: AccountsModel.shared.current, duration: watch?.videoDuration ?? duration, context: backgroundContext)
}
FeedModel.shared.calculateUnwatchedFeed()
}
var imageSystemName: String {
finished ? "checkmark.circle.fill" : "circle"
}
var backgroundColor: Color {
finished ? .white : .clear
}
var finished: Bool {
guard let watch else { return false }
return watch.finished
}
}
struct WatchView_Previews: PreviewProvider {
static var previews: some View {
WatchView(videoID: "abc", duration: 10)
}
}