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

308 lines
10 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 {
2022-12-11 20:30:20 +05:30
var item: FavoriteItem
2021-11-02 03:26:18 +05:30
2022-12-11 20:30:20 +05:30
@Environment(\.navigationStyle) private var navigationStyle
2021-11-02 03:26:18 +05:30
@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
@ObservedObject private var accounts = AccountsModel.shared
private var playlists = PlaylistsModel.shared
2021-11-06 04:14:52 +05:30
private var favoritesModel = FavoritesModel.shared
2022-12-11 20:30:20 +05:30
private var navigation = NavigationModel.shared
2021-11-02 03:26:18 +05:30
2022-12-11 20:30:20 +05:30
init(item: FavoriteItem) {
2021-11-02 03:26:18 +05:30
self.item = item
}
var body: some View {
2021-11-08 02:21:22 +05:30
Group {
if isVisible {
VStack(alignment: .leading, spacing: 2) {
2022-12-11 20:30:20 +05:30
itemControl
2021-11-08 02:21:22 +05:30
.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)
.environment(\.inChannelView, inChannelView)
2021-11-02 03:26:18 +05:30
}
2021-11-08 02:21:22 +05:30
.contentShape(Rectangle())
2022-12-11 20:30:20 +05:30
.onAppear {
resource?.addObserver(store)
2022-12-16 17:01:43 +05:30
loadCacheAndResource()
2022-12-11 20:30:20 +05:30
}
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-16 17:01:43 +05:30
loadCacheAndResource(force: true)
}
}
func loadCacheAndResource(force: Bool = false) {
2022-12-17 20:48:14 +05:30
guard let resource else { return }
2022-12-16 17:01:43 +05:30
var onSuccess: (Entity<Any>) -> Void = { _ in }
var contentItems = [ContentItem]()
switch item.section {
case .subscriptions:
let feed = FeedCacheModel.shared.retrieveFeed(account: accounts.current)
contentItems = ContentItem.array(of: feed)
onSuccess = { response in
if let videos: [Video] = response.typedContent() {
FeedCacheModel.shared.storeFeed(account: accounts.current, videos: videos)
}
}
case let .channel(_, id, name):
let channel = Channel(app: .invidious, id: id, name: name)
if let cache = ChannelsCacheModel.shared.retrieve(channel.cacheKey) {
contentItems = ContentItem.array(of: cache.videos)
}
onSuccess = { response in
if let channel: Channel = response.typedContent() {
ChannelsCacheModel.shared.store(channel)
}
}
2022-12-17 01:07:05 +05:30
case let .channelPlaylist(_, id, title):
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(.init(id: id, title: title)),
2022-12-16 17:01:43 +05:30
!cache.videos.isEmpty
{
contentItems = ContentItem.array(of: cache.videos)
2022-12-10 07:31:59 +05:30
}
2022-12-16 17:01:43 +05:30
onSuccess = { response in
if let playlist: ChannelPlaylist = response.typedContent() {
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist)
}
}
case let .playlist(_, id):
let playlists = PlaylistsCacheModel.shared.retrievePlaylists(account: accounts.current)
if let playlist = playlists.first(where: { $0.id == id }) {
contentItems = ContentItem.array(of: playlist.videos)
}
default:
contentItems = []
}
if !contentItems.isEmpty {
store.contentItems = contentItems
}
if force {
resource.load().onSuccess(onSuccess)
} else {
resource.loadIfNeeded()?.onSuccess(onSuccess)
2022-12-10 07:31:59 +05:30
}
}
var inChannelView: Bool {
switch item.section {
case .channel:
return true
default:
return false
}
}
2022-12-11 20:30:20 +05:30
var itemControl: some View {
VStack {
#if os(tvOS)
itemButton
#else
if itemIsNavigationLink {
itemNavigationLink
} else {
itemButton
}
#endif
}
}
var itemButton: some View {
Button(action: itemButtonAction) {
itemLabel
.foregroundColor(.accentColor)
}
.buttonStyle(.plain)
}
var itemNavigationLink: some View {
NavigationLink(destination: itemNavigationLinkDestination) {
itemLabel
}
}
var itemIsNavigationLink: Bool {
switch item.section {
case .channel:
return navigationStyle == .tab
case .channelPlaylist:
return navigationStyle == .tab
2022-12-12 03:52:38 +05:30
case .playlist:
return navigationStyle == .tab
2022-12-11 20:30:20 +05:30
case .subscriptions:
return navigationStyle == .tab
case .popular:
return navigationStyle == .tab
default:
return false
}
}
@ViewBuilder var itemNavigationLinkDestination: some View {
switch item.section {
case let .channel(_, id, name):
ChannelVideosView(channel: .init(app: .invidious, id: id, name: name))
case let .channelPlaylist(_, id, title):
ChannelPlaylistView(playlist: .init(id: id, title: title))
case let .playlist(_, id):
ChannelPlaylistView(playlist: .init(id: id, title: label))
case .subscriptions:
SubscriptionsView()
case .popular:
PopularView()
default:
EmptyView()
2022-12-11 20:30:20 +05:30
}
}
func itemButtonAction() {
switch item.section {
case let .channel(_, id, name):
2022-12-14 04:37:32 +05:30
NavigationModel.shared.openChannel(.init(app: .invidious, id: id, name: name), navigationStyle: navigationStyle)
2022-12-11 20:30:20 +05:30
case let .channelPlaylist(_, id, title):
NavigationModel.shared.openChannelPlaylist(.init(id: id, title: title), navigationStyle: navigationStyle)
case .subscriptions:
navigation.hideViewsAboveBrowser()
navigation.tabSelection = .subscriptions
case .popular:
navigation.hideViewsAboveBrowser()
navigation.tabSelection = .popular
case let .trending(country, category):
navigation.hideViewsAboveBrowser()
Defaults[.trendingCountry] = .init(rawValue: country) ?? .us
Defaults[.trendingCategory] = category.isNil ? .default : (.init(rawValue: category!) ?? .default)
navigation.tabSelection = .trending
case let .searchQuery(text, _, _, _):
navigation.hideViewsAboveBrowser()
navigation.openSearchQuery(text)
2022-12-12 04:00:28 +05:30
case let .playlist(_, id):
2022-12-11 20:30:20 +05:30
navigation.tabSelection = .playlist(id)
}
}
var itemLabel: some View {
HStack {
Text(label)
.font(.title3.bold())
Image(systemName: "chevron.right")
.imageScale(.small)
}
.lineLimit(1)
.padding(.trailing, 10)
}
2021-11-08 02:21:22 +05:30
private var isVisible: Bool {
switch item.section {
case .subscriptions:
2022-12-17 18:54:18 +05:30
return accounts.app.supportsSubscriptions && !accounts.isEmpty && !accounts.current.anonymous
2021-11-08 02:21:22 +05:30
case .popular:
return accounts.app.supportsPopular
2022-12-11 20:30:20 +05:30
case let .channel(appType, _, _):
guard let appType = VideosApp.AppType(rawValue: appType) else { return false }
return accounts.app.appType == appType
case let .channelPlaylist(appType, _, _):
guard let appType = VideosApp.AppType(rawValue: appType) else { return false }
return accounts.app.appType == appType
2022-12-12 04:00:28 +05:30
case let .playlist(accountID, _):
return accounts.current?.id == accountID
2021-11-08 02:21:22 +05:30
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)
2022-12-11 20:30:20 +05:30
case let .channel(_, id, _):
2021-11-06 04:14:52 +05:30
return accounts.api.channelVideos(id)
2022-12-11 20:30:20 +05:30
case let .channelPlaylist(_, id, _):
2021-11-06 04:14:52 +05:30
return accounts.api.channelPlaylist(id)
2022-12-12 04:00:28 +05:30
case let .playlist(_, id):
2021-11-06 04:14:52 +05:30
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 {
2022-12-12 04:00:28 +05:30
switch item.section {
case let .playlist(_, id):
2022-11-19 04:36:13 +05:30
return playlists.find(id: id)?.title ?? "Playlist".localized()
2022-12-12 04:00:28 +05:30
default:
return item.section.label.localized()
2021-11-02 03:26:18 +05:30
}
}
}
2022-12-11 20:30:20 +05:30
struct FavoriteItemView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
VStack {
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Search: resistance body upper band workout")))
.environment(\.navigationStyle, .tab)
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Marques")))
.environment(\.navigationStyle, .sidebar)
}
}
}
}