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

@@ -28,7 +28,6 @@ extension Defaults.Keys {
static let showFavoritesInHome = Key<Bool>("showFavoritesInHome", default: true)
#if os(iOS)
static let showDocuments = Key<Bool>("showDocuments", default: false)
static let homeRecentDocumentsItems = Key<Int>("homeRecentDocumentsItems", default: 3)
#endif
static let homeHistoryItems = Key<Int>("homeHistoryItems", default: 10)
static let favorites = Key<[FavoriteItem]>("favorites", default: [])
@@ -258,6 +257,7 @@ extension Defaults.Keys {
static let hideShorts = Key<Bool>("hideShorts", default: false)
static let hideWatched = Key<Bool>("hideWatched", default: false)
static let showInspector = Key<ShowInspectorSetting>("showInspector", default: .onlyLocal)
static let widgetsSettings = Key<[WidgetSettings]>("widgetsSettings", default: [])
}
enum ResolutionSetting: String, CaseIterable, Defaults.Serializable {
@@ -445,3 +445,60 @@ enum FullScreenRotationSetting: String, CaseIterable, Defaults.Serializable {
self != .disabled
}
}
struct WidgetSettings: Defaults.Serializable {
static let defaultLimit = 10
static let maxLimit: [WidgetListingStyle: Int] = [
.horizontalCells: 50,
.list: 50
]
static var bridge = WidgetSettingsBridge()
var id: String
var listingStyle = WidgetListingStyle.horizontalCells
var limit = Self.defaultLimit
var viewID: String {
"\(id)-\(listingStyle.rawValue)-\(limit)"
}
static func maxLimit(_ style: WidgetListingStyle) -> Int {
Self.maxLimit[style] ?? Self.defaultLimit
}
}
struct WidgetSettingsBridge: Defaults.Bridge {
typealias Value = WidgetSettings
typealias Serializable = [String: String]
func serialize(_ value: Value?) -> Serializable? {
guard let value else { return nil }
return [
"id": value.id,
"listingStyle": value.listingStyle.rawValue,
"limit": String(value.limit)
]
}
func deserialize(_ object: Serializable?) -> Value? {
guard let object, let id = object["id"], !id.isEmpty else { return nil }
var listingStyle = WidgetListingStyle.horizontalCells
if let style = object["listingStyle"] {
listingStyle = WidgetListingStyle(rawValue: style) ?? .horizontalCells
}
let limit = Int(object["limit"] ?? "\(WidgetSettings.defaultLimit)") ?? WidgetSettings.defaultLimit
return Value(
id: id,
listingStyle: listingStyle,
limit: limit
)
}
}
enum WidgetListingStyle: String, CaseIterable, Defaults.Serializable {
case horizontalCells
case list
}

View File

@@ -62,10 +62,6 @@ private struct LoadMoreContentHandler: EnvironmentKey {
static let defaultValue: LoadMoreContentHandlerType = {}
}
private struct ScrollViewBottomPaddingKey: EnvironmentKey {
static let defaultValue: Double = 30
}
extension EnvironmentValues {
var inChannelView: Bool {
get { self[InChannelViewKey.self] }
@@ -97,11 +93,6 @@ extension EnvironmentValues {
set { self[LoadMoreContentHandler.self] = newValue }
}
var scrollViewBottomPadding: Double {
get { self[ScrollViewBottomPaddingKey.self] }
set { self[ScrollViewBottomPaddingKey.self] = newValue }
}
var listingStyle: ListingStyle {
get { self[ListingStyleKey.self] }
set { self[ListingStyleKey.self] = newValue }

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? {

View File

@@ -73,6 +73,37 @@ struct ContentView: View {
AccountsView()
}
)
.background(
EmptyView().sheet(isPresented: $navigation.presentingHomeSettings) {
#if os(macOS)
VStack(alignment: .leading) {
Button("Done") {
navigation.presentingHomeSettings = false
}
.padding()
.keyboardShortcut(.cancelAction)
HomeSettings()
}
.frame(width: 500, height: 800)
#else
NavigationView {
HomeSettings()
#if os(iOS)
.toolbar {
ToolbarItem(placement: .navigation) {
Button {
navigation.presentingHomeSettings = false
} label: {
Text("Done")
}
}
}
#endif
}
#endif
}
)
#if !os(tvOS)
.fileImporter(
isPresented: $navigation.presentingFileImporter,

View File

@@ -64,7 +64,6 @@ struct PlaylistsView: View {
SignInRequiredView(title: "Playlists".localized()) {
VStack {
VerticalCells(items: items, allowEmpty: true) { if shouldDisplayHeader { header } }
.environment(\.scrollViewBottomPadding, 70)
.environment(\.currentPlaylistID, currentPlaylist?.id)
.environment(\.listingStyle, playlistListingStyle)

View File

@@ -9,19 +9,13 @@ struct BrowsingSettings: View {
@Default(.accountPickerDisplaysAnonymousAccounts) private var accountPickerDisplaysAnonymousAccounts
@Default(.showUnwatchedFeedBadges) private var showUnwatchedFeedBadges
#if os(iOS)
@Default(.homeRecentDocumentsItems) private var homeRecentDocumentsItems
@Default(.lockPortraitWhenBrowsing) private var lockPortraitWhenBrowsing
@Default(.showDocuments) private var showDocuments
#endif
@Default(.thumbnailsQuality) private var thumbnailsQuality
@Default(.channelOnThumbnail) private var channelOnThumbnail
@Default(.timeOnThumbnail) private var timeOnThumbnail
@Default(.showHome) private var showHome
@Default(.showFavoritesInHome) private var showFavoritesInHome
@Default(.showQueueInHome) private var showQueueInHome
@Default(.showOpenActionsInHome) private var showOpenActionsInHome
@Default(.showOpenActionsToolbarItem) private var showOpenActionsToolbarItem
@Default(.homeHistoryItems) private var homeHistoryItems
@Default(.visibleSections) private var visibleSections
@Default(.playerButtonSingleTapGesture) private var playerButtonSingleTapGesture
@Default(.playerButtonDoubleTapGesture) private var playerButtonDoubleTapGesture
@@ -32,12 +26,11 @@ struct BrowsingSettings: View {
@ObservedObject private var accounts = AccountsModel.shared
@State private var homeHistoryItemsText = ""
#if os(iOS)
@State private var homeRecentDocumentsItemsText = ""
#endif
#if os(macOS)
@State private var presentingEditFavoritesSheet = false
@State private var presentingHomeSettingsSheet = false
#endif
var body: some View {
@@ -83,79 +76,32 @@ struct BrowsingSettings: View {
}
}
private var homeSettings: some View {
Section(header: SettingsHeader(text: "Home".localized())) {
#if !os(tvOS)
if !accounts.isEmpty {
Toggle("Show Home", isOn: $showHome)
}
#endif
Toggle("Show Open Videos quick actions", isOn: $showOpenActionsInHome)
Toggle("Show Next in Queue", isOn: $showQueueInHome)
#if os(iOS)
HStack {
Text("Recent Documents")
TextField("Recent Documents", text: $homeRecentDocumentsItemsText)
.multilineTextAlignment(.trailing)
.labelsHidden()
#if !os(macOS)
.keyboardType(.numberPad)
#endif
.onAppear {
homeRecentDocumentsItemsText = String(homeRecentDocumentsItems)
}
.onChange(of: homeRecentDocumentsItemsText) { newValue in
homeRecentDocumentsItems = Int(newValue) ?? 3
}
}
#endif
HStack {
Text("Recent History")
TextField("Recent History", text: $homeHistoryItemsText)
.multilineTextAlignment(.trailing)
.labelsHidden()
#if !os(macOS)
.keyboardType(.numberPad)
#endif
.onAppear {
homeHistoryItemsText = String(homeHistoryItems)
@ViewBuilder private var homeSettings: some View {
if !accounts.isEmpty {
Section(header: SettingsHeader(text: "Home".localized())) {
#if os(macOS)
Button {
presentingHomeSettingsSheet = true
} label: {
Text("Home Settings")
}
.onChange(of: homeHistoryItemsText) { newValue in
homeHistoryItems = Int(newValue) ?? 10
}
}
if !accounts.isEmpty {
Toggle("Show Favorites", isOn: $showFavoritesInHome)
Group {
#if os(macOS)
Button {
presentingEditFavoritesSheet = true
} label: {
Text("Edit Favorites…")
}
.sheet(isPresented: $presentingEditFavoritesSheet) {
VStack(alignment: .leading) {
Button("Done") {
presentingEditFavoritesSheet = false
}
.padding()
.keyboardShortcut(.cancelAction)
EditFavorites()
.sheet(isPresented: $presentingHomeSettingsSheet) {
VStack(alignment: .leading) {
Button("Done") {
presentingHomeSettingsSheet = false
}
.frame(width: 500, height: 300)
.padding()
.keyboardShortcut(.cancelAction)
HomeSettings()
}
#else
NavigationLink(destination: LazyView(EditFavorites())) {
Text("Edit Favorites…")
}
#endif
}
.disabled(!showFavoritesInHome)
.frame(width: 500, height: 800)
}
#else
NavigationLink(destination: LazyView(HomeSettings())) {
Text("Home Settings")
}
#endif
}
}
}

View File

@@ -1,119 +0,0 @@
import Defaults
import SwiftUI
struct EditFavorites: View {
private var playlistsModel = PlaylistsModel.shared
private var model = FavoritesModel.shared
@Default(.favorites) private var favorites
var body: some View {
Group {
#if os(tvOS)
ScrollView {
VStack {
editor
}
}
.frame(width: 1000)
#else
List {
editor
}
#endif
}
.navigationTitle("Favorites")
}
var editor: some View {
Group {
Section(header: Text("Favorites")) {
if favorites.isEmpty {
Text("Favorites is empty")
.foregroundColor(.secondary)
}
ForEach(favorites) { item in
HStack {
Text(label(item))
Spacer()
HStack(spacing: 30) {
Button {
model.moveUp(item)
} label: {
Label("Move Up", systemImage: "arrow.up")
}
Button {
model.moveDown(item)
} label: {
Label("Move Down", systemImage: "arrow.down")
}
Button {
model.remove(item)
} label: {
Label("Remove", systemImage: "trash")
}
}
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
}
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
#if os(tvOS)
Divider()
.padding(20)
#endif
if !model.addableItems().isEmpty {
Section(header: Text("Available")) {
ForEach(model.addableItems()) { item in
HStack {
Text(label(item))
Spacer()
Button {
model.add(item)
} label: {
Label("Add to Favorites", systemImage: "heart")
#if os(tvOS)
.font(.system(size: 30))
#endif
}
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
}
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
}
}
.labelStyle(.iconOnly)
}
func label(_ item: FavoriteItem) -> String {
switch item.section {
case let .playlist(_, id):
return playlistsModel.find(id: id)?.title ?? "Playlist".localized()
default:
return item.section.label.localized()
}
}
}
struct EditFavorites_Previews: PreviewProvider {
static var previews: some View {
EditFavorites()
.injectFixtureEnvironmentObjects()
}
}

View File

@@ -0,0 +1,302 @@
import Defaults
import SwiftUI
struct HomeSettings: View {
private var model = FavoritesModel.shared
@Default(.favorites) private var favorites
@Default(.showHome) private var showHome
@Default(.showFavoritesInHome) private var showFavoritesInHome
@Default(.showQueueInHome) private var showQueueInHome
@Default(.showOpenActionsInHome) private var showOpenActionsInHome
@Default(.showOpenActionsToolbarItem) private var showOpenActionsToolbarItem
@ObservedObject private var accounts = AccountsModel.shared
var body: some View {
Group {
#if os(tvOS)
ScrollView {
LazyVStack {
homeSettings
.padding(.horizontal)
editor
}
}
.frame(width: 1000)
#else
List {
homeSettings
editor
}
#endif
}
.navigationTitle("Home Settings")
}
var editor: some View {
Group {
Section(header: SettingsHeader(text: "Favorites")) {
if favorites.isEmpty {
Text("Favorites is empty")
.padding(.vertical)
.foregroundColor(.secondary)
}
ForEach(favorites) { item in
FavoriteItemEditor(item: item)
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
if !model.addableItems().isEmpty {
Section(header: SettingsHeader(text: "Available")) {
ForEach(model.addableItems()) { item in
HStack {
FavoriteItemLabel(item: item)
Spacer()
Button {
model.add(item)
} label: {
Label("Add to Favorites", systemImage: "heart")
#if os(tvOS)
.font(.system(size: 30))
#endif
}
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
}
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
}
}
.labelStyle(.iconOnly)
}
private var homeSettings: some View {
Section(header: SettingsHeader(text: "Home".localized())) {
#if !os(tvOS)
if !accounts.isEmpty {
Toggle("Show Home", isOn: $showHome)
}
#endif
Toggle("Show Open Videos quick actions", isOn: $showOpenActionsInHome)
Toggle("Show Next in Queue", isOn: $showQueueInHome)
if !accounts.isEmpty {
Toggle("Show Favorites", isOn: $showFavoritesInHome)
}
}
}
}
struct FavoriteItemLabel: View {
var item: FavoriteItem
var body: some View {
Text(label)
.fontWeight(.bold)
}
var label: String {
switch item.section {
case let .playlist(_, id):
return PlaylistsModel.shared.find(id: id)?.title ?? "Playlist".localized()
default:
return item.section.label.localized()
}
}
}
struct FavoriteItemEditor: View {
var item: FavoriteItem
private var model: FavoritesModel { .shared }
@State private var listingStyle = WidgetListingStyle.horizontalCells
@State private var limit = 3
@State private var presentingRemoveAlert = false
var body: some View {
VStack(alignment: .leading) {
HStack {
FavoriteItemLabel(item: item)
Spacer()
HStack(spacing: 10) {
FavoriteItemEditorButton {
Label("Move Up", systemImage: "arrow.up")
} onTapGesture: {
model.moveUp(item)
}
FavoriteItemEditorButton {
Label("Move Down", systemImage: "arrow.down")
} onTapGesture: {
model.moveDown(item)
}
FavoriteItemEditorButton(color: .init("AppRedColor")) {
Label("Remove", systemImage: "trash")
} onTapGesture: {
presentingRemoveAlert = true
}
.alert(isPresented: $presentingRemoveAlert) {
Alert(
title: Text(
String(
format: "Are you sure you want to remove %@ from Favorites?".localized(),
item.section.label.localized()
)
),
message: Text("This cannot be reverted"),
primaryButton: .destructive(Text("Remove")) {
model.remove(item)
},
secondaryButton: .cancel()
)
}
}
}
listingStylePicker
.padding(.vertical, 5)
limitInput
#if !os(iOS)
Divider()
#endif
}
.onAppear(perform: setupEditor)
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
}
var listingStylePicker: some View {
Picker("Listing Style", selection: $listingStyle) {
Text("Cells").tag(WidgetListingStyle.horizontalCells)
Text("List").tag(WidgetListingStyle.list)
}
.onChange(of: listingStyle) { newValue in
model.setListingStyle(newValue, item)
limit = min(limit, WidgetSettings.maxLimit(newValue))
}
.labelsHidden()
.pickerStyle(.segmented)
}
var limitInput: some View {
HStack {
Text("Limit")
Spacer()
#if !os(tvOS)
limitMinusButton
.disabled(limit == 1)
#endif
#if os(tvOS)
let textFieldWidth = 100.00
#else
let textFieldWidth = 30.00
#endif
TextField("Limit", value: $limit, formatter: NumberFormatter())
#if !os(macOS)
.keyboardType(.numberPad)
#endif
.labelsHidden()
.frame(width: textFieldWidth, alignment: .trailing)
.multilineTextAlignment(.center)
.onChange(of: limit) { newValue in
let value = min(limit, WidgetSettings.maxLimit(listingStyle))
if newValue <= 0 || newValue != value {
limit = value
} else {
model.setLimit(value, item)
}
}
#if !os(tvOS)
limitPlusButton
.disabled(limit == WidgetSettings.maxLimit(listingStyle))
#endif
}
}
#if !os(tvOS)
var limitMinusButton: some View {
FavoriteItemEditorButton {
Label("Minus", systemImage: "minus")
} onTapGesture: {
limit = max(1, limit - 1)
}
}
var limitPlusButton: some View {
FavoriteItemEditorButton {
Label("Plus", systemImage: "plus")
} onTapGesture: {
limit = max(1, limit + 1)
}
}
#endif
func setupEditor() {
listingStyle = model.listingStyle(item)
limit = model.limit(item)
}
}
struct FavoriteItemEditorButton<LabelView: View>: View {
var color = Color.accentColor
var label: LabelView
var onTapGesture: () -> Void = {}
init(
color: Color = .accentColor,
@ViewBuilder label: () -> LabelView,
onTapGesture: @escaping () -> Void = {}
) {
self.color = color
self.label = label()
self.onTapGesture = onTapGesture
}
var body: some View {
#if os(tvOS)
Button(action: onTapGesture) {
label
}
#else
label
.imageScale(.medium)
.labelStyle(.iconOnly)
.padding(7)
.frame(minWidth: 40, minHeight: 40)
.foregroundColor(color)
.accessibilityAddTraits(.isButton)
#if os(iOS)
.background(RoundedRectangle(cornerRadius: 4).strokeBorder(lineWidth: 1).foregroundColor(color))
#endif
.contentShape(Rectangle())
.onTapGesture(perform: onTapGesture)
#endif
}
}
struct HomeSettings_Previews: PreviewProvider {
static var previews: some View {
HomeSettings()
.injectFixtureEnvironmentObjects()
}
}

View File

@@ -0,0 +1,32 @@
import SwiftUI
struct ListView: View {
var items: [ContentItem]
var limit: Int? = 10
var body: some View {
LazyVStack(alignment: .leading) {
ForEach(limitedItems) { item in
ContentItemView(item: item)
.environment(\.listingStyle, .list)
.environment(\.noListingDividers, limit == 1)
.transition(.opacity)
}
}
}
var limitedItems: [ContentItem] {
if let limit, limit >= 0 {
return Array(items.prefix(limit))
}
return items
}
}
struct ListView_Previews: PreviewProvider {
static var previews: some View {
ListView(items: [.init(video: .fixture)])
}
}

View File

@@ -6,7 +6,6 @@ struct VerticalCells<Header: View>: View {
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
@Environment(\.scrollViewBottomPadding) private var scrollViewBottomPadding
@Environment(\.loadMoreContentHandler) private var loadMoreContentHandler
@Environment(\.listingStyle) private var listingStyle
@@ -15,14 +14,25 @@ struct VerticalCells<Header: View>: View {
var edgesIgnoringSafeArea = Edge.Set.horizontal
let header: Header?
init(items: [ContentItem], allowEmpty: Bool = false, edgesIgnoringSafeArea: Edge.Set = .horizontal, @ViewBuilder header: @escaping () -> Header? = { nil }) {
@State private var gridSize = CGSize.zero
init(
items: [ContentItem],
allowEmpty: Bool = false,
edgesIgnoringSafeArea: Edge.Set = .horizontal,
@ViewBuilder header: @escaping () -> Header? = { nil }
) {
self.items = items
self.allowEmpty = allowEmpty
self.edgesIgnoringSafeArea = edgesIgnoringSafeArea
self.header = header()
}
init(items: [ContentItem], allowEmpty: Bool = false) where Header == EmptyView {
init(
items: [ContentItem],
allowEmpty: Bool = false
) where Header == EmptyView {
self.init(items: items, allowEmpty: allowEmpty) { EmptyView() }
}
@@ -37,9 +47,6 @@ struct VerticalCells<Header: View>: View {
}
}
.padding()
#if !os(tvOS)
Color.clear.padding(.bottom, scrollViewBottomPadding)
#endif
}
.animation(nil)
.edgesIgnoringSafeArea(edgesIgnoringSafeArea)

View File

@@ -53,6 +53,7 @@ struct WatchView: View {
}
FeedModel.shared.calculateUnwatchedFeed()
WatchModel.shared.watchesChanged()
}
var imageSystemName: String {

View File

@@ -5,6 +5,9 @@ struct AccentButton: View {
var imageSystemName: String?
var maxWidth: CGFloat? = .infinity
var bold = true
var verticalPadding = 10.0
var horizontalPadding = 10.0
var minHeight = 45.0
var action: () -> Void = {}
var body: some View {
@@ -18,9 +21,9 @@ struct AccentButton: View {
.fontWeight(bold ? .bold : .regular)
}
}
.padding(.vertical, 10)
.padding(.horizontal, 10)
.frame(minHeight: 45)
.padding(.vertical, verticalPadding)
.padding(.horizontal, horizontalPadding)
.frame(minHeight: minHeight)
.frame(maxWidth: maxWidth)
.contentShape(Rectangle())
}

View File

@@ -0,0 +1,21 @@
import SwiftUI
struct HomeSettingsButton: View {
var navigation = NavigationModel.shared
var body: some View {
Button {
navigation.presentingHomeSettings = true
} label: {
Label("Home Settings", systemImage: "gear")
}
.font(.caption)
.imageScale(.small)
}
}
struct HomeSettingsButton_Previews: PreviewProvider {
static var previews: some View {
HomeSettingsButton()
}
}

View File

@@ -160,6 +160,7 @@ struct VideoContextMenuView: View {
Button {
Watch.markAsWatched(videoID: video.videoID, account: accounts.current, duration: video.length, context: backgroundContext)
FeedModel.shared.calculateUnwatchedFeed()
WatchModel.shared.watchesChanged()
} label: {
Label("Mark as watched", systemImage: "checkmark.circle.fill")
}

View File

@@ -47,6 +47,7 @@ struct YatteeApp: App {
let persistenceController = PersistenceController.shared
var favorites: FavoritesModel { .shared }
var playerControls: PlayerControlsModel { .shared }
var body: some Scene {
@@ -180,5 +181,22 @@ struct YatteeApp: App {
#endif
URLBookmarkModel.shared.refreshAll()
migrateHomeHistoryItems()
}
func migrateHomeHistoryItems() {
guard Defaults[.homeHistoryItems] > 0 else { return }
if favorites.addableItems().contains(where: { $0.section == .history }) {
let historyItem = FavoriteItem(section: .history)
favorites.add(historyItem)
favorites.setListingStyle(.list, historyItem)
favorites.setLimit(Defaults[.homeHistoryItems], historyItem)
print("migrated home history items: \(favorites.limit(historyItem))")
}
Defaults[.homeHistoryItems] = -1
}
}