2022-12-15 21:39:42 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct WatchView: View {
|
|
|
|
var watch: Watch?
|
|
|
|
var videoID: Video.ID
|
|
|
|
var duration: Double
|
|
|
|
|
|
|
|
@Default(.watchedVideoBadgeColor) private var watchedVideoBadgeColor
|
2023-04-22 16:04:28 +00:00
|
|
|
@Default(.showToggleWatchedStatusButton) private var showToggleWatchedStatusButton
|
2022-12-15 21:39:42 +00:00
|
|
|
|
|
|
|
var backgroundContext = PersistenceController.shared.container.newBackgroundContext()
|
|
|
|
|
|
|
|
var body: some View {
|
2023-04-22 16:04:28 +00:00
|
|
|
if showToggleWatchedStatusButton {
|
|
|
|
#if os(tvOS)
|
|
|
|
if finished {
|
|
|
|
image
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Button(action: toggleWatch) {
|
|
|
|
image
|
|
|
|
}
|
|
|
|
.opacity(finished ? 1 : 0.4)
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
#endif
|
|
|
|
} else {
|
2022-12-15 21:39:42 +00:00
|
|
|
if finished {
|
|
|
|
image
|
|
|
|
}
|
2023-04-22 16:04:28 +00:00
|
|
|
}
|
2022-12-15 21:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-12-21 17:13:41 +00:00
|
|
|
if let account = AccountsModel.shared.current {
|
|
|
|
Watch.markAsWatched(videoID: watch?.videoID ?? videoID, account: account, duration: watch?.videoDuration ?? duration, context: backgroundContext)
|
|
|
|
}
|
2022-12-15 21:39:42 +00:00
|
|
|
}
|
2022-12-16 21:22:59 +00:00
|
|
|
|
|
|
|
FeedModel.shared.calculateUnwatchedFeed()
|
2023-05-25 12:28:29 +00:00
|
|
|
WatchModel.shared.watchesChanged()
|
2022-12-15 21:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|