yattee/Model/Favorites/FavoriteItem.swift

61 lines
2.1 KiB
Swift
Raw Normal View History

2021-11-01 21:56:18 +00:00
import Defaults
import Foundation
struct FavoriteItem: Codable, Equatable, Identifiable, Defaults.Serializable {
enum Section: Codable, Equatable, Defaults.Serializable {
2023-05-25 12:28:29 +00:00
case history
2021-11-01 21:56:18 +00:00
case subscriptions
case popular
case trending(String, String?)
2022-12-11 15:00:20 +00:00
case channel(String, String, String)
2022-12-11 22:30:28 +00:00
case playlist(String, String)
2022-12-11 15:00:20 +00:00
case channelPlaylist(String, String, String)
2021-11-09 17:43:15 +00:00
case searchQuery(String, String, String, String)
2021-11-01 21:56:18 +00:00
var label: String {
switch self {
2023-05-25 12:28:29 +00:00
case .history:
return "History"
2021-11-01 21:56:18 +00:00
case .subscriptions:
return "Subscriptions"
case .popular:
return "Popular"
case let .trending(country, category):
let trendingCountry = Country(rawValue: country)!
2021-11-11 21:07:13 +00:00
let trendingCategory = category.isNil ? nil : TrendingCategory(rawValue: category!)
2021-11-07 23:34:42 +00:00
return "\(trendingCountry.flag) \(trendingCountry.id) \(trendingCategory?.name ?? "Trending")"
2022-12-11 15:00:20 +00:00
case let .channel(_, _, name):
2021-11-01 21:56:18 +00:00
return name
2022-12-11 15:00:20 +00:00
case let .channelPlaylist(_, _, name):
2021-11-01 21:56:18 +00:00
return name
2021-11-09 17:43:15 +00:00
case let .searchQuery(text, date, duration, order):
var label = "Search: \"\(text)\""
if !date.isEmpty, let date = SearchQuery.Date(rawValue: date), date != .any {
label += " from \(date == .today ? date.name : " this \(date.name)")"
}
if !order.isEmpty, let order = SearchQuery.SortOrder(rawValue: order), order != .relevance {
label += " by \(order.name)"
}
if !duration.isEmpty {
label += " (\(duration))"
}
return label
2021-11-01 21:56:18 +00:00
default:
return ""
}
}
}
static func == (lhs: FavoriteItem, rhs: FavoriteItem) -> Bool {
lhs.section == rhs.section
}
var id = UUID().uuidString
var section: Section
2023-05-25 12:28:29 +00:00
var widgetSettingsKey: String {
"favorites-\(id)"
}
2021-11-01 21:56:18 +00:00
}