yattee/Shared/Home/FavoriteItemView.swift

543 lines
19 KiB
Swift
Raw Permalink Normal View History

2021-11-01 21:56:18 +00:00
import Defaults
import Siesta
import SwiftUI
import UniformTypeIdentifiers
struct FavoriteItemView: View {
2022-12-11 15:00:20 +00:00
var item: FavoriteItem
@Binding var favoritesChanged: Bool
2021-11-01 21:56:18 +00:00
2022-12-11 15:00:20 +00:00
@Environment(\.navigationStyle) private var navigationStyle
2021-11-01 21:56:18 +00:00
@StateObject private var store = FavoriteResourceObserver()
@ObservedObject private var accounts = AccountsModel.shared
private var playlists = PlaylistsModel.shared
2021-11-05 22:44:52 +00:00
private var favoritesModel = FavoritesModel.shared
2022-12-11 15:00:20 +00:00
private var navigation = NavigationModel.shared
2023-05-25 12:28:29 +00:00
@ObservedObject private var player = PlayerModel.shared
@ObservedObject private var watchModel = WatchModel.shared
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@State private var visibleWatches = [Watch]()
@Default(.hideShorts) private var hideShorts
@Default(.hideWatched) private var hideWatched
@Default(.widgetsSettings) private var widgetsSettings
2023-05-26 23:03:25 +00:00
@Default(.visibleSections) private var visibleSections
2021-11-01 21:56:18 +00:00
init(item: FavoriteItem, favoritesChanged: Binding<Bool>) {
2021-11-01 21:56:18 +00:00
self.item = item
_favoritesChanged = favoritesChanged
2021-11-01 21:56:18 +00:00
}
var body: some View {
2021-11-07 20:51:22 +00:00
Group {
if isVisible {
VStack(alignment: .leading, spacing: 2) {
2022-12-11 15:00:20 +00:00
itemControl
2023-05-25 12:28:29 +00:00
.contextMenu { contextMenu }
2021-11-07 20:51:22 +00:00
.contentShape(Rectangle())
#if os(tvOS)
.padding(.leading, 40)
#else
.padding(.leading, 15)
#endif
if limitedItems.isEmpty, !(resource?.isLoading ?? false) {
VStack(alignment: .leading) {
Text(emptyItemsText)
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(.secondary)
if hideShorts || hideWatched {
AccentButton(text: "Disable filters", maxWidth: nil, verticalPadding: 0, minHeight: 30) {
hideShorts = false
hideWatched = false
reloadVisibleWatches()
}
}
}
.padding(.vertical, 10)
2023-05-25 12:28:29 +00:00
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
} else {
Group {
switch widgetListingStyle {
case .horizontalCells:
HorizontalCells(items: limitedItems)
case .list:
ListView(items: limitedItems)
.padding(.vertical, 10)
#if os(tvOS)
.padding(.leading, 40)
#else
2023-06-07 20:30:10 +00:00
.padding(.horizontal, 15)
2023-05-25 12:28:29 +00:00
#endif
}
}
.environment(\.inChannelView, inChannelView)
2023-05-25 12:28:29 +00:00
}
2021-11-01 21:56:18 +00:00
}
2021-11-07 20:51:22 +00:00
.contentShape(Rectangle())
2022-12-11 15:00:20 +00:00
.onAppear {
2023-05-25 12:28:29 +00:00
if item.section == .history {
reloadVisibleWatches()
} else {
resource?.addObserver(store)
DispatchQueue.main.async {
self.loadCacheAndResource()
}
2023-05-25 12:28:29 +00:00
}
2022-12-11 15:00:20 +00:00
}
.onDisappear {
resource?.removeObservers(ownedBy: store)
}
.onChange(of: player.currentVideo) { _ in if !player.presentingPlayer { reloadVisibleWatches() } }
.onChange(of: hideShorts) { _ in if !player.presentingPlayer { reloadVisibleWatches() } }
.onChange(of: hideWatched) { _ in if !player.presentingPlayer { reloadVisibleWatches() } }
// Delay is necessary to update the list with the new items.
.onChange(of: favoritesChanged) { _ in if !player.presentingPlayer { Delay.by(1.0) { reloadVisibleWatches() } } }
.onChange(of: player.presentingPlayer) { _ in
if player.presentingPlayer {
resource?.removeObservers(ownedBy: store)
} else {
resource?.addObserver(store)
}
}
2021-11-07 20:51:22 +00:00
}
2021-11-01 21:56:18 +00:00
}
2023-05-25 12:28:29 +00:00
.id(watchModel.historyToken)
2021-12-06 18:13:49 +00:00
.onChange(of: accounts.current) { _ in
DispatchQueue.main.async {
loadCacheAndResource(force: true)
}
2022-12-16 11:31:43 +00:00
}
2023-05-25 12:28:29 +00:00
.onChange(of: watchModel.historyToken) { _ in
if !player.presentingPlayer {
reloadVisibleWatches()
}
2023-05-25 12:28:29 +00:00
}
}
var emptyItemsText: String {
var filterText = ""
if hideShorts && hideWatched {
filterText = "(watched and shorts hidden)"
} else if hideShorts {
filterText = "(shorts hidden)"
} else if hideWatched {
filterText = "(watched hidden)"
}
return "No videos to show".localized() + " " + filterText.localized()
}
2023-05-25 12:28:29 +00:00
var contextMenu: some View {
Group {
if item.section == .history {
Section {
Button {
navigation.presentAlert(
Alert(
title: Text("Are you sure you want to clear history of watched videos?"),
message: Text("This cannot be reverted"),
primaryButton: .destructive(Text("Clear All")) {
PlayerModel.shared.removeHistory()
visibleWatches = []
},
secondaryButton: .cancel()
)
)
} label: {
Label("Clear History", systemImage: "trash")
}
}
}
Button {
favoritesModel.remove(item)
} label: {
Label("Remove from Favorites", systemImage: "trash")
}
#if os(tvOS)
Button("Cancel", role: .cancel) {}
#endif
}
}
func reloadVisibleWatches() {
DispatchQueue.main.async {
guard item.section == .history else { return }
2023-05-25 12:28:29 +00:00
visibleWatches = []
2023-05-25 12:28:29 +00:00
let watches = Array(
watches
.filter { $0.videoID != player.currentVideo?.videoID && itemVisible(.init(video: $0.video)) }
.prefix(favoritesModel.limit(item))
)
let last = watches.last
for watch in watches {
player.loadHistoryVideoDetails(watch) {
guard let video = player.historyVideo(watch.videoID), itemVisible(.init(video: video)) else { return }
visibleWatches.append(watch)
if watch == last {
visibleWatches.sort { $0.watchedAt ?? Date() > $1.watchedAt ?? Date() }
}
}
2023-05-25 12:28:29 +00:00
}
}
}
var limitedItems: [ContentItem] {
var items: [ContentItem]
if item.section == .history {
items = visibleWatches.map { ContentItem(video: player.historyVideo($0.videoID) ?? $0.video) }
} else {
items = store.contentItems.filter { itemVisible($0) }
}
return Array(items.prefix(favoritesModel.limit(item)))
}
func itemVisible(_ item: ContentItem) -> Bool {
if hideWatched, watch(item)?.finished ?? false {
return false
}
guard hideShorts, item.contentType == .video, let video = item.video else {
return true
}
return !video.short
}
func watch(_ item: ContentItem) -> Watch? {
2023-06-07 20:35:57 +00:00
guard let id = item.video?.videoID else { return nil }
return watches.first { $0.videoID == id }
2023-05-25 12:28:29 +00:00
}
var widgetListingStyle: WidgetListingStyle {
favoritesModel.listingStyle(item)
2022-12-16 11:31:43 +00:00
}
func loadCacheAndResource(force: Bool = false) {
2022-12-17 15:18:14 +00:00
guard let resource else { return }
2022-12-16 11:31:43 +00:00
var onSuccess: (Entity<Any>) -> Void = { _ in }
var contentItems = [ContentItem]()
switch item.section {
case .subscriptions:
let feed = FeedCacheModel.shared.retrieveFeed(account: accounts.current)
contentItems = ContentItem.array(of: feed)
onSuccess = { response in
if let videos: [Video] = response.typedContent() {
FeedCacheModel.shared.storeFeed(account: accounts.current, videos: videos)
DispatchQueue.main.async {
store.contentItems = contentItems
}
2022-12-16 11:31:43 +00:00
}
}
case let .channel(_, id, name):
2023-01-27 20:02:02 +00:00
var channel = Channel(app: .invidious, id: id, name: name)
if let cache = ChannelsCacheModel.shared.retrieve(channel.cacheKey),
let cacheChannel = cache.channel,
!cacheChannel.videos.isEmpty
2023-01-27 20:02:02 +00:00
{
contentItems = ContentItem.array(of: cacheChannel.videos)
2022-12-16 11:31:43 +00:00
}
onSuccess = { response in
DispatchQueue.main.async {
if let channel: Channel = response.typedContent() {
2023-03-12 21:52:54 +00:00
ChannelsCacheModel.shared.store(channel)
store.contentItems = ContentItem.array(of: channel.videos)
} else if let videos: [Video] = response.typedContent() {
channel.videos = videos
ChannelsCacheModel.shared.store(channel)
store.contentItems = ContentItem.array(of: videos)
} else if let channelPage: ChannelPage = response.typedContent() {
if let channel = channelPage.channel {
ChannelsCacheModel.shared.store(channel)
}
2023-03-12 21:52:54 +00:00
store.contentItems = channelPage.results
}
2022-12-16 11:31:43 +00:00
}
}
2022-12-16 19:37:05 +00:00
case let .channelPlaylist(_, id, title):
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(.init(id: id, title: title)),
2022-12-16 11:31:43 +00:00
!cache.videos.isEmpty
{
contentItems = ContentItem.array(of: cache.videos)
2022-12-10 02:01:59 +00:00
}
2022-12-16 11:31:43 +00:00
onSuccess = { response in
if let playlist: ChannelPlaylist = response.typedContent() {
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist)
DispatchQueue.main.async {
store.contentItems = contentItems
}
2022-12-16 11:31:43 +00:00
}
}
case let .playlist(_, id):
let playlists = PlaylistsCacheModel.shared.retrievePlaylists(account: accounts.current)
if let playlist = playlists.first(where: { $0.id == id }) {
contentItems = ContentItem.array(of: playlist.videos)
}
DispatchQueue.main.async {
store.contentItems = contentItems
}
2022-12-16 11:31:43 +00:00
default:
contentItems = []
DispatchQueue.main.async {
store.contentItems = contentItems
}
2022-12-16 11:31:43 +00:00
}
if force {
resource.load().onSuccess(onSuccess)
} else {
resource.loadIfNeeded()?.onSuccess(onSuccess)
2022-12-10 02:01:59 +00:00
}
}
2023-05-25 12:28:29 +00:00
var navigatableItem: Bool {
2023-05-26 23:03:25 +00:00
switch item.section {
case .history:
return false
case .trending:
return visibleSections.contains(.trending)
case .subscriptions:
return visibleSections.contains(.subscriptions) && accounts.signedIn
case .popular:
return visibleSections.contains(.popular) && accounts.app.supportsPopular
default:
return true
}
2023-05-25 12:28:29 +00:00
}
var inChannelView: Bool {
switch item.section {
case .channel:
return true
default:
return false
}
}
2022-12-11 15:00:20 +00:00
var itemControl: some View {
VStack {
2023-05-25 12:28:29 +00:00
if navigatableItem {
#if os(tvOS)
2022-12-11 15:00:20 +00:00
itemButton
2023-05-25 12:28:29 +00:00
#else
if itemIsNavigationLink {
itemNavigationLink
} else {
itemButton
}
#endif
} else {
itemLabel
.foregroundColor(.secondary)
}
2022-12-11 15:00:20 +00:00
}
}
var itemButton: some View {
Button(action: itemButtonAction) {
itemLabel
.foregroundColor(.accentColor)
}
2023-05-26 22:06:39 +00:00
#if !os(tvOS)
2022-12-11 15:00:20 +00:00
.buttonStyle(.plain)
2023-05-26 22:06:39 +00:00
#endif
2022-12-11 15:00:20 +00:00
}
var itemNavigationLink: some View {
NavigationLink(destination: itemNavigationLinkDestination) {
itemLabel
}
}
var itemIsNavigationLink: Bool {
switch item.section {
case .channel:
return navigationStyle == .tab
case .channelPlaylist:
return navigationStyle == .tab
2022-12-11 22:22:38 +00:00
case .playlist:
return navigationStyle == .tab
2022-12-11 15:00:20 +00:00
case .subscriptions:
return navigationStyle == .tab
case .popular:
return navigationStyle == .tab
default:
return false
}
}
@ViewBuilder var itemNavigationLinkDestination: some View {
switch item.section {
case let .channel(_, id, name):
ChannelVideosView(channel: .init(app: .invidious, id: id, name: name))
case let .channelPlaylist(_, id, title):
ChannelPlaylistView(playlist: .init(id: id, title: title))
case let .playlist(_, id):
ChannelPlaylistView(playlist: .init(id: id, title: label))
case .subscriptions:
SubscriptionsView()
case .popular:
PopularView()
default:
EmptyView()
2022-12-11 15:00:20 +00:00
}
}
func itemButtonAction() {
switch item.section {
case let .channel(_, id, name):
2022-12-13 23:07:32 +00:00
NavigationModel.shared.openChannel(.init(app: .invidious, id: id, name: name), navigationStyle: navigationStyle)
2022-12-11 15:00:20 +00:00
case let .channelPlaylist(_, id, title):
NavigationModel.shared.openChannelPlaylist(.init(id: id, title: title), navigationStyle: navigationStyle)
case .subscriptions:
navigation.hideViewsAboveBrowser()
navigation.tabSelection = .subscriptions
case .popular:
navigation.hideViewsAboveBrowser()
navigation.tabSelection = .popular
case let .trending(country, category):
navigation.hideViewsAboveBrowser()
Defaults[.trendingCountry] = .init(rawValue: country) ?? .us
Defaults[.trendingCategory] = category.isNil ? .default : (.init(rawValue: category!) ?? .default)
navigation.tabSelection = .trending
case let .searchQuery(text, _, _, _):
navigation.hideViewsAboveBrowser()
navigation.openSearchQuery(text)
2022-12-11 22:30:28 +00:00
case let .playlist(_, id):
2022-12-11 15:00:20 +00:00
navigation.tabSelection = .playlist(id)
2023-05-25 12:28:29 +00:00
case .history:
print("should not happen")
2022-12-11 15:00:20 +00:00
}
}
var itemLabel: some View {
HStack {
Text(label)
.font(.title3.bold())
2023-05-25 12:28:29 +00:00
if navigatableItem {
Image(systemName: "chevron.right")
.imageScale(.small)
}
2022-12-11 15:00:20 +00:00
}
.lineLimit(1)
.padding(.trailing, 10)
}
2021-11-07 20:51:22 +00:00
private var isVisible: Bool {
switch item.section {
case .subscriptions:
2022-12-17 13:24:18 +00:00
return accounts.app.supportsSubscriptions && !accounts.isEmpty && !accounts.current.anonymous
2021-11-07 20:51:22 +00:00
case .popular:
return accounts.app.supportsPopular
2022-12-11 15:00:20 +00:00
case let .channel(appType, _, _):
guard let appType = VideosApp.AppType(rawValue: appType) else { return false }
return accounts.app.appType == appType
case let .channelPlaylist(appType, _, _):
guard let appType = VideosApp.AppType(rawValue: appType) else { return false }
return accounts.app.appType == appType
2022-12-11 22:30:28 +00:00
case let .playlist(accountID, _):
return accounts.current?.id == accountID
2021-11-07 20:51:22 +00:00
default:
return true
2021-11-01 21:56:18 +00:00
}
}
2021-11-05 22:44:52 +00:00
private var resource: Resource? {
switch item.section {
2023-05-25 12:28:29 +00:00
case .history:
return nil
2024-07-06 09:48:49 +00:00
2021-11-05 22:44:52 +00:00
case .subscriptions:
if accounts.app.supportsSubscriptions {
2022-12-10 02:01:59 +00:00
return accounts.api.feed(1)
2021-11-05 22:44:52 +00:00
}
case .popular:
if accounts.app.supportsPopular {
return accounts.api.popular
}
case let .trending(country, category):
let trendingCountry = Country(rawValue: country)!
2021-11-11 21:07:13 +00:00
let trendingCategory = category.isNil ? nil : TrendingCategory(rawValue: category!)
2021-11-05 22:44:52 +00:00
return accounts.api.trending(country: trendingCountry, category: trendingCategory)
2022-12-11 15:00:20 +00:00
case let .channel(_, id, _):
2021-11-05 22:44:52 +00:00
return accounts.api.channelVideos(id)
2022-12-11 15:00:20 +00:00
case let .channelPlaylist(_, id, _):
2021-11-05 22:44:52 +00:00
return accounts.api.channelPlaylist(id)
2022-12-11 22:30:28 +00:00
case let .playlist(_, id):
2021-11-05 22:44:52 +00:00
return accounts.api.playlist(id)
2021-11-09 17:43:15 +00:00
case let .searchQuery(text, date, duration, order):
return accounts.api.search(
.init(
query: text,
sortBy: SearchQuery.SortOrder(rawValue: order) ?? .uploadDate,
date: SearchQuery.Date(rawValue: date),
duration: SearchQuery.Duration(rawValue: duration)
),
page: nil
)
2021-11-05 22:44:52 +00:00
}
return nil
}
private var label: String {
2022-12-11 22:30:28 +00:00
switch item.section {
case let .playlist(_, id):
2022-11-18 23:06:13 +00:00
return playlists.find(id: id)?.title ?? "Playlist".localized()
2022-12-11 22:30:28 +00:00
default:
return item.section.label.localized()
2021-11-01 21:56:18 +00:00
}
}
}
2022-12-11 15:00:20 +00:00
struct FavoriteItemView_Previews: PreviewProvider {
struct PreviewWrapper: View {
@State private var favoritesChanged = false
var body: some View {
NavigationView {
VStack {
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Search: resistance body upper band workout")), favoritesChanged: $favoritesChanged)
.environment(\.navigationStyle, .tab)
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Marques")), favoritesChanged: $favoritesChanged)
.environment(\.navigationStyle, .sidebar)
}
2022-12-11 15:00:20 +00:00
}
}
}
static var previews: some View {
PreviewWrapper()
}
2022-12-11 15:00:20 +00:00
}