Home Settings

This commit is contained in:
Arkadiusz Fal
2023-05-25 14:28:29 +02:00
parent 12afb31c03
commit 0061bd8c20
26 changed files with 911 additions and 396 deletions

View File

@@ -13,6 +13,16 @@ struct FavoriteItemView: View {
private var playlists = PlaylistsModel.shared
private var favoritesModel = FavoritesModel.shared
private var navigation = NavigationModel.shared
@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
init(item: FavoriteItem) {
self.item = item
@@ -23,13 +33,7 @@ struct FavoriteItemView: View {
if isVisible {
VStack(alignment: .leading, spacing: 2) {
itemControl
.contextMenu {
Button {
favoritesModel.remove(item)
} label: {
Label("Remove from Favorites", systemImage: "trash")
}
}
.contextMenu { contextMenu }
.contentShape(Rectangle())
#if os(tvOS)
.padding(.leading, 40)
@@ -37,20 +41,173 @@ struct FavoriteItemView: View {
.padding(.leading, 15)
#endif
HorizontalCells(items: store.contentItems)
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)
#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
.padding(.leading, 15)
#endif
}
}
.environment(\.inChannelView, inChannelView)
}
}
.contentShape(Rectangle())
.onAppear {
resource?.addObserver(store)
loadCacheAndResource()
if item.section == .history {
reloadVisibleWatches()
} else {
resource?.addObserver(store)
loadCacheAndResource()
}
}
.onChange(of: player.currentVideo) { _ in reloadVisibleWatches() }
.onChange(of: hideShorts) { _ in reloadVisibleWatches() }
.onChange(of: hideWatched) { _ in reloadVisibleWatches() }
}
}
.id(watchModel.historyToken)
.onChange(of: accounts.current) { _ in
resource?.addObserver(store)
loadCacheAndResource(force: true)
}
.onChange(of: watchModel.historyToken) { _ in
Delay.by(0.5) {
reloadVisibleWatches()
}
}
.onAppear {
Defaults.observe(.widgetsSettings) { _ in
watchModel.watchesChanged()
}
.tieToLifetime(of: accounts)
}
}
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()
}
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() {
guard item.section == .history else { return }
visibleWatches = []
let watches = Array(
watches
.filter { $0.videoID != player.currentVideo?.videoID && itemVisible(.init(video: $0.video)) }
.prefix(favoritesModel.limit(item))
)
let last = watches.last
watches.forEach { watch in
player.loadHistoryVideoDetails(watch) {
guard let video = player.historyVideo(watch.videoID), itemVisible(.init(video: video)) else { return }
visibleWatches.append(watch)
guard watch == last else { return }
visibleWatches.sort { $0.watchedAt ?? Date() > $1.watchedAt ?? Date() }
}
}
}
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? {
watches.first { $0.videoID == item.video.videoID }
}
var widgetListingStyle: WidgetListingStyle {
favoritesModel.listingStyle(item)
}
func loadCacheAndResource(force: Bool = false) {
@@ -127,6 +284,10 @@ struct FavoriteItemView: View {
}
}
var navigatableItem: Bool {
item.section != .history
}
var inChannelView: Bool {
switch item.section {
case .channel:
@@ -138,15 +299,20 @@ struct FavoriteItemView: View {
var itemControl: some View {
VStack {
#if os(tvOS)
itemButton
#else
if itemIsNavigationLink {
itemNavigationLink
} else {
if navigatableItem {
#if os(tvOS)
itemButton
}
#endif
#else
if itemIsNavigationLink {
itemNavigationLink
} else {
itemButton
}
#endif
} else {
itemLabel
.foregroundColor(.secondary)
}
}
}
@@ -220,6 +386,8 @@ struct FavoriteItemView: View {
navigation.openSearchQuery(text)
case let .playlist(_, id):
navigation.tabSelection = .playlist(id)
case .history:
print("should not happen")
}
}
@@ -227,8 +395,10 @@ struct FavoriteItemView: View {
HStack {
Text(label)
.font(.title3.bold())
Image(systemName: "chevron.right")
.imageScale(.small)
if navigatableItem {
Image(systemName: "chevron.right")
.imageScale(.small)
}
}
.lineLimit(1)
.padding(.trailing, 10)
@@ -255,6 +425,8 @@ struct FavoriteItemView: View {
private var resource: Resource? {
switch item.section {
case .history:
return nil
case .subscriptions:
if accounts.app.supportsSubscriptions {
return accounts.api.feed(1)

View File

@@ -24,25 +24,19 @@ struct HistoryView: View {
}.foregroundColor(.secondary)
}
} else {
ForEach(visibleWatches, id: \.videoID) { watch in
let video = player.historyVideo(watch.videoID) ?? watch.video
ContentItemView(item: .init(video: video))
.environment(\.listingStyle, .list)
.contextMenu {
VideoContextMenuView(video: video)
}
}
ListView(items: contentItems, limit: limit)
}
}
.animation(nil, value: visibleWatches)
.onAppear(perform: reloadVisibleWatches)
.onChange(of: player.currentVideo) { _ in reloadVisibleWatches() }
}
var contentItems: [ContentItem] {
visibleWatches.map { .init(video: player.historyVideo($0.videoID) ?? $0.video) }
}
func reloadVisibleWatches() {
visibleWatches = Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit))
visibleWatches.forEach(player.loadHistoryVideoDetails)
}
}

View File

@@ -6,7 +6,7 @@ import UniformTypeIdentifiers
struct HomeView: View {
@ObservedObject private var accounts = AccountsModel.shared
@State private var presentingEditFavorites = false
@State private var presentingHomeSettings = false
@State private var favoritesChanged = false
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
@@ -20,9 +20,7 @@ struct HomeView: View {
#if !os(tvOS)
@Default(.favorites) private var favorites
#endif
#if os(iOS)
@Default(.homeRecentDocumentsItems) private var homeRecentDocumentsItems
@Default(.widgetsSettings) private var widgetsSettings
#endif
@Default(.homeHistoryItems) private var homeHistoryItems
@Default(.showFavoritesInHome) private var showFavoritesInHome
@@ -33,33 +31,45 @@ struct HomeView: View {
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
HStack {
#if os(tvOS)
Group {
if showOpenActionsInHome {
AccentButton(text: "Open Video", imageSystemName: "globe") {
NavigationModel.shared.presentingOpenVideos = true
VStack {
HStack {
#if os(tvOS)
Group {
if showOpenActionsInHome {
AccentButton(text: "Open Video", imageSystemName: "globe") {
NavigationModel.shared.presentingOpenVideos = true
}
}
AccentButton(text: "Locations", imageSystemName: "globe") {
NavigationModel.shared.presentingAccounts = true
}
AccentButton(text: "Settings", imageSystemName: "gear") {
NavigationModel.shared.presentingSettings = true
}
}
AccentButton(text: "Locations", imageSystemName: "globe") {
NavigationModel.shared.presentingAccounts = true
#else
if showOpenActionsInHome {
AccentButton(text: "Files", imageSystemName: "folder") {
NavigationModel.shared.presentingFileImporter = true
}
AccentButton(text: "Paste", imageSystemName: "doc.on.clipboard.fill") {
OpenVideosModel.shared.openURLsFromClipboard(playbackMode: .playNow)
}
AccentButton(imageSystemName: "ellipsis") {
NavigationModel.shared.presentingOpenVideos = true
}
.frame(maxWidth: 40)
}
AccentButton(text: "Settings", imageSystemName: "gear") {
NavigationModel.shared.presentingSettings = true
}
}
#else
if showOpenActionsInHome {
AccentButton(text: "Files", imageSystemName: "folder") {
NavigationModel.shared.presentingFileImporter = true
}
AccentButton(text: "Paste", imageSystemName: "doc.on.clipboard.fill") {
OpenVideosModel.shared.openURLsFromClipboard(playbackMode: .playNow)
}
AccentButton(imageSystemName: "ellipsis") {
NavigationModel.shared.presentingOpenVideos = true
}
.frame(maxWidth: 40)
#endif
}
#if os(tvOS)
HStack {
Spacer()
HideWatchedButtons()
HideShortsButtons()
HomeSettingsButton()
}
#endif
}
@@ -80,7 +90,7 @@ struct HomeView: View {
}
if !accounts.current.isNil, showFavoritesInHome {
LazyVStack(alignment: .leading) {
VStack(alignment: .leading) {
#if os(tvOS)
ForEach(Defaults[.favorites]) { item in
FavoriteItemView(item: item)
@@ -96,87 +106,6 @@ struct HomeView: View {
}
}
#if os(iOS)
if homeRecentDocumentsItems > 0 {
VStack {
HStack {
NavigationLink(destination: DocumentsView()) {
HStack {
Text("Documents")
.font(.title3.bold())
Image(systemName: "chevron.right")
.imageScale(.small)
}
.lineLimit(1)
}
.padding(.leading, 15)
Spacer()
Button {
recentDocumentsID = UUID()
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
.font(.headline)
.labelStyle(.iconOnly)
.foregroundColor(.secondary)
}
}
RecentDocumentsView(limit: homeRecentDocumentsItems)
.id(recentDocumentsID)
}
.frame(maxWidth: .infinity, alignment: .leading)
#if os(tvOS)
.padding(.trailing, 40)
#else
.padding(.trailing, 15)
#endif
}
#endif
if homeHistoryItems > 0 {
VStack {
HStack {
sectionLabel("History")
Spacer()
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()
historyID = UUID()
},
secondaryButton: .cancel()
)
)
} label: {
Label("Clear History", systemImage: "trash")
.font(.headline)
.labelStyle(.iconOnly)
.foregroundColor(.secondary)
}
.buttonStyle(.plain)
}
.frame(maxWidth: .infinity, alignment: .leading)
#if os(tvOS)
.padding(.trailing, 40)
#else
.padding(.trailing, 15)
#endif
HistoryView(limit: homeHistoryItems)
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
.id(historyID)
}
}
#if !os(tvOS)
Color.clear.padding(.bottom, 60)
#endif
@@ -186,6 +115,10 @@ struct HomeView: View {
favoritesChanged.toggle()
}
.tieToLifetime(of: accounts)
Defaults.observe(.widgetsSettings) { _ in
favoritesChanged.toggle()
}
.tieToLifetime(of: accounts)
}
.redrawOn(change: favoritesChanged)
@@ -198,6 +131,13 @@ struct HomeView: View {
#if os(macOS)
.background(Color.secondaryBackground)
.frame(minWidth: 360)
.toolbar {
ToolbarItemGroup(placement: .automatic) {
HideWatchedButtons()
HideShortsButtons()
HomeSettingsButton()
}
}
#endif
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
@@ -226,13 +166,20 @@ struct HomeView: View {
.foregroundColor(.secondary)
}
#if os(iOS)
#if os(iOS)
var homeMenu: some View {
Menu {
Section {
HideWatchedButtons()
HideShortsButtons()
}
Section {
Button {
navigation.presentingHomeSettings = true
} label: {
Label("Home Settings", systemImage: "gear")
}
}
} label: {
HStack(spacing: 12) {
Text("Home")

View File

@@ -28,15 +28,8 @@ struct QueueView: View {
}
.buttonStyle(.plain)
LazyVStack(alignment: .leading) {
ForEach(limitedItems) { item in
ContentItemView(item: .init(video: item.video))
.environment(\.listingStyle, .list)
.environment(\.inQueueListing, true)
.environment(\.noListingDividers, limit == 1)
.transition(.opacity)
}
}
ListView(items: items, limit: limit)
.environment(\.inQueueListing, true)
}
}
.padding(.vertical, items.isEmpty ? 0 : 15)
@@ -50,16 +43,8 @@ struct QueueView: View {
return "Next in Queue".localized() + " (\(items.count))"
}
var limitedItems: [ContentItem] {
if let limit {
return Array(items.prefix(limit).map(\.contentItem))
}
return items.map(\.contentItem)
}
var items: [PlayerQueueItem] {
player.queue
var items: [ContentItem] {
player.queue.map(\.contentItem)
}
var limit: Int? {