yattee/Shared/Views/SubscriptionsView.swift

57 lines
1.5 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import Siesta
2021-06-11 22:49:42 +00:00
import SwiftUI
struct SubscriptionsView: View {
2021-09-25 08:18:22 +00:00
@StateObject private var store = Store<[Video]>()
2021-06-26 11:37:24 +00:00
2021-10-16 22:48:58 +00:00
@EnvironmentObject<AccountsModel> private var accounts
2021-10-20 22:21:50 +00:00
var feed: Resource? {
accounts.api.feed
2021-06-28 10:43:07 +00:00
}
2021-06-26 11:37:24 +00:00
var videos: [ContentItem] {
ContentItem.array(of: store.collection)
}
2021-06-28 10:43:07 +00:00
var body: some View {
PlayerControlsView {
SignInRequiredView(title: "Subscriptions") {
VerticalCells(items: videos)
.onAppear {
loadResources()
}
.onChange(of: accounts.current) { _ in
loadResources(force: true)
}
}
2021-09-25 08:18:22 +00:00
}
2021-11-01 21:56:18 +00:00
.toolbar {
ToolbarItem(placement: .automatic) {
FavoriteButton(item: FavoriteItem(section: .subscriptions))
}
}
2021-09-25 08:18:22 +00:00
}
fileprivate func loadResources(force: Bool = false) {
2021-10-20 22:21:50 +00:00
feed?.addObserver(store)
2021-09-25 08:18:22 +00:00
if accounts.app == .invidious {
// Invidious for some reason won't refresh feed until homepage is loaded
if let request = force ? accounts.api.home?.load() : accounts.api.home?.loadIfNeeded() {
request.onSuccess { _ in
loadFeed(force: force)
}
} else {
2021-09-25 08:18:22 +00:00
loadFeed(force: force)
2021-06-28 10:43:07 +00:00
}
2021-09-25 08:18:22 +00:00
} else {
loadFeed(force: force)
}
}
fileprivate func loadFeed(force: Bool = false) {
2021-10-20 22:21:50 +00:00
_ = force ? feed?.load() : feed?.loadIfNeeded()
2021-06-11 22:49:42 +00:00
}
}