1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 06:10:32 +05:30
yattee/Shared/Home/FavoriteItemView.swift

143 lines
4.3 KiB
Swift
Raw Normal View History

2021-11-02 03:26:18 +05:30
import Defaults
import Siesta
import SwiftUI
import UniformTypeIdentifiers
struct FavoriteItemView: View {
let item: FavoriteItem
@StateObject private var store = FavoriteResourceObserver()
2021-11-06 04:14:52 +05:30
@Default(.favorites) private var favorites
2021-11-02 03:26:18 +05:30
@Binding private var dragging: FavoriteItem?
@ObservedObject private var accounts = AccountsModel.shared
private var playlists = PlaylistsModel.shared
2021-11-06 04:14:52 +05:30
private var favoritesModel = FavoritesModel.shared
2021-11-02 03:26:18 +05:30
init(
item: FavoriteItem,
dragging: Binding<FavoriteItem?>
) {
self.item = item
_dragging = dragging
}
var body: some View {
2021-11-08 02:21:22 +05:30
Group {
if isVisible {
VStack(alignment: .leading, spacing: 2) {
Text(label)
.font(.title3.bold())
.foregroundColor(.secondary)
.contextMenu {
Button {
favoritesModel.remove(item)
} label: {
Label("Remove from Favorites", systemImage: "trash")
}
}
.contentShape(Rectangle())
#if os(tvOS)
.padding(.leading, 40)
#else
.padding(.leading, 15)
#endif
HorizontalCells(items: store.contentItems)
2021-11-02 03:26:18 +05:30
}
2021-11-08 02:21:22 +05:30
.contentShape(Rectangle())
#if os(macOS)
.opacity(dragging?.id == item.id ? 0.5 : 1)
#endif
.onAppear {
resource?.addObserver(store)
2022-12-10 07:31:59 +05:30
if item.section == .subscriptions {
cacheFeed(resource?.loadIfNeeded())
} else {
resource?.loadIfNeeded()
}
}
2021-11-08 02:21:22 +05:30
}
2021-11-02 03:26:18 +05:30
}
2021-12-06 23:43:49 +05:30
.onChange(of: accounts.current) { _ in
resource?.addObserver(store)
2022-12-10 07:31:59 +05:30
if item.section == .subscriptions {
cacheFeed(resource?.load())
} else {
resource?.load()
}
}
}
private func cacheFeed(_ request: Request?) {
request?.onSuccess { response in
if let videos: [Video] = response.typedContent() {
FeedCacheModel.shared.storeFeed(account: accounts.current, videos: videos)
}
2021-12-06 23:43:49 +05:30
}
2021-11-08 02:21:22 +05:30
}
2021-11-02 03:26:18 +05:30
2021-11-08 02:21:22 +05:30
private var isVisible: Bool {
switch item.section {
case .subscriptions:
2021-11-12 02:37:13 +05:30
return accounts.app.supportsSubscriptions && accounts.signedIn
2021-11-08 02:21:22 +05:30
case .popular:
return accounts.app.supportsPopular
default:
return true
2021-11-02 03:26:18 +05:30
}
}
2021-11-06 04:14:52 +05:30
private var resource: Resource? {
switch item.section {
case .subscriptions:
if accounts.app.supportsSubscriptions {
2022-12-10 07:31:59 +05:30
return accounts.api.feed(1)
2021-11-06 04:14:52 +05:30
}
case .popular:
if accounts.app.supportsPopular {
return accounts.api.popular
}
case let .trending(country, category):
let trendingCountry = Country(rawValue: country)!
2021-11-12 02:37:13 +05:30
let trendingCategory = category.isNil ? nil : TrendingCategory(rawValue: category!)
2021-11-06 04:14:52 +05:30
return accounts.api.trending(country: trendingCountry, category: trendingCategory)
case let .channel(id, _):
return accounts.api.channelVideos(id)
case let .channelPlaylist(id, _):
return accounts.api.channelPlaylist(id)
case let .playlist(id):
return accounts.api.playlist(id)
2021-11-09 23:13:15 +05:30
case let .searchQuery(text, date, duration, order):
return accounts.api.search(
.init(
query: text,
sortBy: SearchQuery.SortOrder(rawValue: order) ?? .uploadDate,
date: SearchQuery.Date(rawValue: date),
duration: SearchQuery.Duration(rawValue: duration)
),
page: nil
)
2021-11-06 04:14:52 +05:30
}
return nil
}
private var label: String {
2021-11-02 03:26:18 +05:30
if case let .playlist(id) = item.section {
2022-11-19 04:36:13 +05:30
return playlists.find(id: id)?.title ?? "Playlist".localized()
2021-11-02 03:26:18 +05:30
}
2022-11-19 04:36:13 +05:30
return item.section.label.localized()
2021-11-02 03:26:18 +05:30
}
}