yattee/Model/Favorites/FavoritesModel.swift

126 lines
3.6 KiB
Swift
Raw Normal View History

2021-11-01 21:56:18 +00:00
import Defaults
import Foundation
struct FavoritesModel {
2023-04-22 13:08:33 +00:00
static let shared = Self()
2021-11-01 21:56:18 +00:00
2022-11-11 20:28:40 +00:00
@Default(.showFavoritesInHome) var showFavoritesInHome
2021-11-01 21:56:18 +00:00
@Default(.favorites) var all
2023-05-25 12:28:29 +00:00
@Default(.widgetsSettings) var widgetsSettings
2021-12-02 20:35:25 +00:00
var isEnabled: Bool {
2022-11-11 20:28:40 +00:00
showFavoritesInHome
2021-12-02 20:35:25 +00:00
}
2021-11-01 21:56:18 +00:00
func contains(_ item: FavoriteItem) -> Bool {
all.contains { $0 == item }
}
func toggle(_ item: FavoriteItem) {
2023-06-17 12:09:51 +00:00
if contains(item) {
remove(item)
} else {
add(item)
}
2021-11-01 21:56:18 +00:00
}
func add(_ item: FavoriteItem) {
all.append(item)
}
func remove(_ item: FavoriteItem) {
if let index = all.firstIndex(where: { $0 == item }) {
all.remove(at: index)
}
}
func canMoveUp(_ item: FavoriteItem) -> Bool {
if let index = all.firstIndex(where: { $0 == item }) {
return index > all.startIndex
}
return false
}
func canMoveDown(_ item: FavoriteItem) -> Bool {
if let index = all.firstIndex(where: { $0 == item }) {
return index < all.endIndex - 1
}
return false
}
func moveUp(_ item: FavoriteItem) {
guard canMoveUp(item) else {
return
}
if let from = all.firstIndex(where: { $0 == item }) {
all.move(
fromOffsets: IndexSet(integer: from),
toOffset: from - 1
)
}
}
func moveDown(_ item: FavoriteItem) {
guard canMoveDown(item) else {
return
}
if let from = all.firstIndex(where: { $0 == item }) {
all.move(
fromOffsets: IndexSet(integer: from),
toOffset: from + 2
)
}
}
func addableItems() -> [FavoriteItem] {
let allItems = [
FavoriteItem(section: .subscriptions),
2023-05-25 12:28:29 +00:00
FavoriteItem(section: .popular),
FavoriteItem(section: .history)
2021-11-01 21:56:18 +00:00
]
return allItems.filter { item in !all.contains { $0.section == item.section } }
}
2023-05-25 12:28:29 +00:00
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)
}
2021-11-01 21:56:18 +00:00
}