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

@@ -3,6 +3,7 @@ import Foundation
struct FavoriteItem: Codable, Equatable, Identifiable, Defaults.Serializable {
enum Section: Codable, Equatable, Defaults.Serializable {
case history
case subscriptions
case popular
case trending(String, String?)
@@ -13,6 +14,8 @@ struct FavoriteItem: Codable, Equatable, Identifiable, Defaults.Serializable {
var label: String {
switch self {
case .history:
return "History"
case .subscriptions:
return "Subscriptions"
case .popular:
@@ -50,4 +53,8 @@ struct FavoriteItem: Codable, Equatable, Identifiable, Defaults.Serializable {
var id = UUID().uuidString
var section: Section
var widgetSettingsKey: String {
"favorites-\(id)"
}
}

View File

@@ -6,6 +6,7 @@ struct FavoritesModel {
@Default(.showFavoritesInHome) var showFavoritesInHome
@Default(.favorites) var all
@Default(.widgetsSettings) var widgetsSettings
var isEnabled: Bool {
showFavoritesInHome
@@ -74,9 +75,47 @@ struct FavoritesModel {
func addableItems() -> [FavoriteItem] {
let allItems = [
FavoriteItem(section: .subscriptions),
FavoriteItem(section: .popular)
FavoriteItem(section: .popular),
FavoriteItem(section: .history)
]
return allItems.filter { item in !all.contains { $0.section == item.section } }
}
func listingStyle(_ item: FavoriteItem) -> WidgetListingStyle {
widgetSettings(item).listingStyle
}
func limit(_ item: FavoriteItem) -> Int {
min(WidgetSettings.maxLimit(listingStyle(item)), widgetSettings(item).limit)
}
func setListingStyle(_ style: WidgetListingStyle, _ item: FavoriteItem) {
if let index = widgetsSettings.firstIndex(where: { $0.id == item.widgetSettingsKey }) {
var settings = widgetsSettings[index]
settings.listingStyle = style
widgetsSettings[index] = settings
} else {
let settings = WidgetSettings(id: item.widgetSettingsKey, listingStyle: style)
widgetsSettings.append(settings)
}
}
func setLimit(_ limit: Int, _ item: FavoriteItem) {
if let index = widgetsSettings.firstIndex(where: { $0.id == item.widgetSettingsKey }) {
var settings = widgetsSettings[index]
let limit = min(max(1, limit), WidgetSettings.maxLimit(settings.listingStyle))
settings.limit = limit
widgetsSettings[index] = settings
} else {
var settings = WidgetSettings(id: item.widgetSettingsKey, limit: limit)
let limit = min(max(1, limit), WidgetSettings.maxLimit(settings.listingStyle))
settings.limit = limit
widgetsSettings.append(settings)
}
}
func widgetSettings(_ item: FavoriteItem) -> WidgetSettings {
widgetsSettings.first { $0.id == item.widgetSettingsKey } ?? WidgetSettings(id: item.widgetSettingsKey)
}
}

View File

@@ -223,6 +223,7 @@ final class FeedModel: ObservableObject, CacheModel {
try? self.backgroundContext.save()
self.calculateUnwatchedFeed()
WatchModel.shared.watchesChanged()
}
}

View File

@@ -10,18 +10,21 @@ extension PlayerModel {
historyVideos.first { $0.videoID == id }
}
func loadHistoryVideoDetails(_ watch: Watch) {
func loadHistoryVideoDetails(_ watch: Watch, onCompletion: @escaping () -> Void = {}) {
guard historyVideo(watch.videoID).isNil else {
onCompletion()
return
}
if !Video.VideoID.isValid(watch.videoID), let url = URL(string: watch.videoID) {
historyVideos.append(.local(url))
onCompletion()
return
}
if let video = VideosCacheModel.shared.retrieveVideo(watch.video.cacheKey) {
historyVideos.append(video)
onCompletion()
return
}
@@ -35,6 +38,7 @@ extension PlayerModel {
if let video: Video = response.typedContent() {
VideosCacheModel.shared.storeVideo(video)
self.historyVideos.append(video)
onCompletion()
}
}
.onCompletion { _ in
@@ -107,13 +111,19 @@ extension PlayerModel {
try? self.context.save()
FeedModel.shared.calculateUnwatchedFeed()
WatchModel.shared.watchesChanged()
}
}
func removeAllWatches() {
let watchesFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Watch")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: watchesFetchRequest)
_ = try? context.execute(deleteRequest)
_ = try? context.save()
do {
try context.executeAndMergeChanges(deleteRequest)
try context.save()
} catch let error as NSError {
logger.info(.init(stringLiteral: error.localizedDescription))
}
}
}

View File

@@ -83,6 +83,7 @@ final class NavigationModel: ObservableObject {
@Published var presentingSettings = false
@Published var presentingAccounts = false
@Published var presentingWelcomeScreen = false
@Published var presentingHomeSettings = false
@Published var presentingChannelSheet = false
@Published var channelPresentedInSheet: Channel!

11
Model/WatchModel.swift Normal file
View File

@@ -0,0 +1,11 @@
import SwiftUI
final class WatchModel: ObservableObject {
static let shared = WatchModel()
@Published var historyToken = UUID()
func watchesChanged() {
historyToken = UUID()
}
}