mirror of
https://github.com/yattee/yattee.git
synced 2025-10-17 21:08:16 +00:00
Use Siesta framework
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct ChannelView: View {
|
||||
@ObservedObject private var provider = ChannelVideosProvider()
|
||||
@EnvironmentObject private var state: AppState
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
|
||||
var id: String
|
||||
|
||||
var resource: Resource {
|
||||
InvidiousAPI.shared.channelVideos(id)
|
||||
}
|
||||
|
||||
init(id: String) {
|
||||
self.id = id
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VideosListView(videos: videos)
|
||||
}
|
||||
|
||||
var listRowInsets: EdgeInsets {
|
||||
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
|
||||
}
|
||||
|
||||
var videos: [Video] {
|
||||
if state.channelID != provider.channelID {
|
||||
provider.videos = []
|
||||
provider.channelID = state.channelID
|
||||
provider.load()
|
||||
}
|
||||
|
||||
return provider.videos
|
||||
VideosListView(videos: store.collection)
|
||||
.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,16 @@
|
||||
import AVKit
|
||||
import Foundation
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct PlayerView: View {
|
||||
@ObservedObject private var provider: VideoDetailsProvider
|
||||
@ObservedObject private var store = Store<Video>()
|
||||
|
||||
let resource: Resource
|
||||
|
||||
init(id: String) {
|
||||
provider = VideoDetailsProvider(id)
|
||||
resource = InvidiousAPI.shared.video(id)
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@@ -14,18 +18,16 @@ struct PlayerView: View {
|
||||
pvc?
|
||||
.edgesIgnoringSafeArea(.all)
|
||||
}
|
||||
.task {
|
||||
Task {
|
||||
provider.load()
|
||||
}
|
||||
.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
var pvc: PlayerViewController? {
|
||||
guard provider.video != nil else {
|
||||
guard store.item != nil else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return PlayerViewController(video: provider.video!)
|
||||
return PlayerViewController(video: store.item!)
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,19 @@
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct PlaylistsView: View {
|
||||
@EnvironmentObject private var state: AppState
|
||||
|
||||
@ObservedObject private var provider = PlaylistsProvider()
|
||||
@ObservedObject private var store = Store<[Playlist]>()
|
||||
|
||||
@State private var selectedPlaylist: Playlist?
|
||||
|
||||
var resource: Resource {
|
||||
InvidiousAPI.shared.playlists
|
||||
}
|
||||
|
||||
init() {
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Section {
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
@@ -19,39 +26,34 @@ struct PlaylistsView: View {
|
||||
}
|
||||
.padding(.bottom, 5)
|
||||
|
||||
Spacer()
|
||||
|
||||
VStack {
|
||||
if selectedPlaylist != nil {
|
||||
VideosView(videos: selectedPlaylist!.videos)
|
||||
if currentPlaylist != nil {
|
||||
VideosView(videos: currentPlaylist!.videos)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.task {
|
||||
Task {
|
||||
provider.load { playlists in
|
||||
selectedPlaylist = playlists.first
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
var playlists: [Playlist] {
|
||||
if provider.playlists.isEmpty {
|
||||
provider.load()
|
||||
}
|
||||
|
||||
return provider.playlists
|
||||
var currentPlaylist: Playlist? {
|
||||
selectedPlaylist ?? store.collection.first
|
||||
}
|
||||
|
||||
var selectPlaylistButton: some View {
|
||||
Button(selectedPlaylist?.title ?? "Select playlist") {
|
||||
guard selectedPlaylist != nil else {
|
||||
Button(currentPlaylist?.title ?? "Select playlist") {
|
||||
guard currentPlaylist != nil else {
|
||||
return
|
||||
}
|
||||
|
||||
selectedPlaylist = playlists.next(after: selectedPlaylist!)
|
||||
selectedPlaylist = store.collection.next(after: currentPlaylist!)
|
||||
}
|
||||
.contextMenu {
|
||||
ForEach(provider.playlists) { playlist in
|
||||
ForEach(store.collection) { playlist in
|
||||
Button(playlist.title) {
|
||||
selectedPlaylist = playlist
|
||||
}
|
||||
@@ -59,17 +61,3 @@ struct PlaylistsView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element: Equatable {
|
||||
func next(after element: Element) -> Element? {
|
||||
let idx = firstIndex(of: element)
|
||||
|
||||
if idx == nil {
|
||||
return first
|
||||
}
|
||||
|
||||
let next = index(after: idx!)
|
||||
|
||||
return self[next == endIndex ? startIndex : next]
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,19 @@
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct PopularVideosView: View {
|
||||
@ObservedObject private var provider = PopularVideosProvider()
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
|
||||
var resource = InvidiousAPI.shared.popular
|
||||
|
||||
init() {
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VideosView(videos: videos)
|
||||
}
|
||||
|
||||
var videos: [Video] {
|
||||
if provider.videos.isEmpty {
|
||||
provider.load()
|
||||
}
|
||||
|
||||
return provider.videos
|
||||
VideosView(videos: store.collection)
|
||||
.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +1,29 @@
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct SearchView: View {
|
||||
@ObservedObject private var provider = SearchedVideosProvider()
|
||||
@EnvironmentObject private var profile: Profile
|
||||
@EnvironmentObject private var state: AppState
|
||||
|
||||
@State private var query = ""
|
||||
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
|
||||
var body: some View {
|
||||
VideosView(videos: videos)
|
||||
.environmentObject(state)
|
||||
.environmentObject(profile)
|
||||
VideosView(videos: store.collection)
|
||||
.searchable(text: $query)
|
||||
.onChange(of: query) { newQuery in
|
||||
queryChanged(query, newQuery)
|
||||
}
|
||||
}
|
||||
|
||||
var videos: [Video] {
|
||||
provider.load(query)
|
||||
func queryChanged(_ old: String, _ new: String) {
|
||||
let oldResource = resource(old)
|
||||
oldResource.removeObservers(ownedBy: store)
|
||||
|
||||
return provider.videos
|
||||
let resource = resource(new)
|
||||
resource.addObserver(store)
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
|
||||
func resource(_ query: String) -> Resource {
|
||||
InvidiousAPI.shared.search(query)
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,18 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SubscriptionsView: View {
|
||||
@ObservedObject private var provider = SubscriptionVideosProvider()
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
|
||||
var resource = InvidiousAPI.shared.subscriptions
|
||||
|
||||
init() {
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VideosView(videos: videos)
|
||||
}
|
||||
|
||||
var videos: [Video] {
|
||||
if provider.videos.isEmpty {
|
||||
provider.load()
|
||||
}
|
||||
|
||||
return provider.videos
|
||||
VideosView(videos: store.collection)
|
||||
.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,31 +1,27 @@
|
||||
import SwiftUI
|
||||
|
||||
struct TrendingCountrySelectionView: View {
|
||||
@Environment(\.presentationMode) private var presentationMode
|
||||
@ObservedObject private var provider = TrendingCountriesProvider()
|
||||
|
||||
@State private var query: String = ""
|
||||
|
||||
@ObservedObject private var store = Store<[Country]>()
|
||||
@Binding var selectedCountry: Country
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var body: some View {
|
||||
ScrollView(.vertical) {
|
||||
ForEach(countries) { country in
|
||||
ForEach(store.collection) { country in
|
||||
Button(country.name) {
|
||||
selectedCountry = country
|
||||
presentationMode.wrappedValue.dismiss()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
.frame(width: 800)
|
||||
}
|
||||
.searchable(text: $query)
|
||||
.searchable(text: $query, prompt: Text("Country name or two letter code"))
|
||||
.onChange(of: query) { newQuery in
|
||||
store.replace(Country.search(newQuery))
|
||||
}
|
||||
.background(.thinMaterial)
|
||||
}
|
||||
|
||||
var countries: [Country] {
|
||||
provider.load(query)
|
||||
|
||||
return provider.countries
|
||||
}
|
||||
}
|
||||
|
@@ -1,15 +1,23 @@
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct TrendingView: View {
|
||||
@EnvironmentObject private var state: AppState
|
||||
|
||||
@ObservedObject private var videosProvider = TrendingVideosProvider()
|
||||
|
||||
@SceneStorage("category") var category: TrendingCategory = .default
|
||||
@SceneStorage("country") var country: Country = .pl
|
||||
|
||||
@State private var category: TrendingCategory = .default
|
||||
@State private var country: Country = .pl
|
||||
@State private var selectingCountry = false
|
||||
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
|
||||
var resource: Resource {
|
||||
InvidiousAPI.shared.trending(category: category, country: country)
|
||||
}
|
||||
|
||||
init() {
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Section {
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
@@ -24,26 +32,20 @@ struct TrendingView: View {
|
||||
}
|
||||
.scaleEffect(0.85)
|
||||
|
||||
VideosView(videos: videos)
|
||||
VideosView(videos: store.collection)
|
||||
}
|
||||
}.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
var videos: [Video] {
|
||||
videosProvider.load(category: category, country: country)
|
||||
|
||||
return videosProvider.videos
|
||||
}
|
||||
|
||||
var categoryButton: some View {
|
||||
Button(category.name) {
|
||||
category = category.next()
|
||||
setCategory(category.next())
|
||||
}
|
||||
.contextMenu {
|
||||
ForEach(TrendingCategory.allCases) { category in
|
||||
Button(category.name) {
|
||||
self.category = category
|
||||
}
|
||||
Button(category.name) { setCategory(category) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,9 +58,23 @@ struct TrendingView: View {
|
||||
var countryButton: some View {
|
||||
Button(country.rawValue) {
|
||||
selectingCountry.toggle()
|
||||
resource.removeObservers(ownedBy: store)
|
||||
}
|
||||
.fullScreenCover(isPresented: $selectingCountry) {
|
||||
.fullScreenCover(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
|
||||
TrendingCountrySelectionView(selectedCountry: $country)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func setCategory(_ category: TrendingCategory) {
|
||||
resource.removeObservers(ownedBy: store)
|
||||
self.category = category
|
||||
resource.addObserver(store)
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
|
||||
fileprivate func setCountry(_ country: Country) {
|
||||
self.country = country
|
||||
resource.addObserver(store)
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import SwiftUI
|
||||
|
||||
struct VideoCellView: View {
|
||||
var video: Video
|
||||
|
||||
var body: some View {
|
||||
NavigationLink(destination: PlayerView(id: video.id)) {
|
||||
VStack(alignment: .leading) {
|
||||
|
Reference in New Issue
Block a user