1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Model/Favorites/FavoritesModel.swift

135 lines
3.9 KiB
Swift
Raw Normal View History

2021-11-02 03:26:18 +05:30
import Defaults
import Foundation
struct FavoritesModel {
2023-04-22 18:38:33 +05:30
static let shared = Self()
2021-11-02 03:26:18 +05:30
2022-11-12 01:58:40 +05:30
@Default(.showFavoritesInHome) var showFavoritesInHome
2021-11-02 03:26:18 +05:30
@Default(.favorites) var all
2023-05-25 17:58:29 +05:30
@Default(.widgetsSettings) var widgetsSettings
2021-12-03 02:05:25 +05:30
var isEnabled: Bool {
2022-11-12 01:58:40 +05:30
showFavoritesInHome
2021-12-03 02:05:25 +05:30
}
2021-11-02 03:26:18 +05:30
func contains(_ item: FavoriteItem) -> Bool {
all.contains { $0 == item }
}
func toggle(_ item: FavoriteItem) {
2023-06-17 17:39:51 +05:30
if contains(item) {
remove(item)
} else {
add(item)
}
2021-11-02 03:26:18 +05:30
}
func add(_ item: FavoriteItem) {
2024-02-02 04:24:16 +05:30
if contains(item) { return }
2021-11-02 03:26:18 +05:30
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 17:58:29 +05:30
FavoriteItem(section: .popular),
FavoriteItem(section: .history)
2021-11-02 03:26:18 +05:30
]
return allItems.filter { item in !all.contains { $0.section == item.section } }
}
2023-05-25 17:58:29 +05:30
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)
}
2024-02-02 04:24:16 +05:30
func updateWidgetSettings(_ settings: WidgetSettings) {
if let index = widgetsSettings.firstIndex(where: { $0.id == settings.id }) {
widgetsSettings[index] = settings
} else {
widgetsSettings.append(settings)
}
}
2021-11-02 03:26:18 +05:30
}