yattee/Shared/Favorites/FavoriteItemView.swift

106 lines
2.9 KiB
Swift
Raw Normal View History

2021-11-01 21:56:18 +00:00
import Defaults
import Siesta
import SwiftUI
import UniformTypeIdentifiers
struct FavoriteItemView: View {
let item: FavoriteItem
@StateObject private var store = FavoriteResourceObserver()
2021-11-05 22:44:52 +00:00
@Default(.favorites) private var favorites
2021-11-01 21:56:18 +00:00
@Binding private var dragging: FavoriteItem?
2021-11-05 22:44:52 +00:00
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlaylistsModel> private var playlists
private var favoritesModel = FavoritesModel.shared
2021-11-01 21:56:18 +00:00
init(
item: FavoriteItem,
dragging: Binding<FavoriteItem?>
) {
self.item = item
_dragging = dragging
}
var body: some View {
VStack(alignment: .leading, spacing: 2) {
Text(label)
.font(.title3.bold())
.foregroundColor(.secondary)
.contextMenu {
Button {
2021-11-05 22:44:52 +00:00
favoritesModel.remove(item)
2021-11-01 21:56:18 +00:00
} label: {
Label("Remove from Favorites", systemImage: "trash")
}
}
.contentShape(Rectangle())
#if os(tvOS)
.padding(.leading, 40)
#else
.padding(.leading, 15)
#endif
2021-11-05 22:44:52 +00:00
HorizontalCells(items: store.contentItems)
2021-11-01 21:56:18 +00:00
}
.contentShape(Rectangle())
.opacity(dragging?.id == item.id ? 0.5 : 1)
.onAppear {
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if !os(tvOS)
.onDrag {
dragging = item
return NSItemProvider(object: item.id as NSString)
}
.onDrop(
of: [UTType.text],
delegate: DropFavorite(item: item, favorites: $favorites, current: $dragging)
)
#endif
}
2021-11-05 22:44:52 +00:00
private var resource: Resource? {
switch item.section {
case .subscriptions:
if accounts.app.supportsSubscriptions {
return accounts.api.feed
}
case .popular:
if accounts.app.supportsPopular {
return accounts.api.popular
}
case let .trending(country, category):
let trendingCountry = Country(rawValue: country)!
let trendingCategory = category.isNil ? nil : TrendingCategory(rawValue: category!)!
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)
}
return nil
}
private var label: String {
2021-11-01 21:56:18 +00:00
if case let .playlist(id) = item.section {
2021-11-05 22:44:52 +00:00
return playlists.find(id: id)?.title ?? "Playlist"
2021-11-01 21:56:18 +00:00
}
return item.section.label
}
}