yattee/Shared/Views/SubscriptionsView.swift

82 lines
2.4 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)
}
#if os(iOS)
.refreshControl { refreshControl in
loadResources(force: true) {
refreshControl.endRefreshing()
}
}
#endif
}
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))
}
}
2022-01-06 23:00:40 +00:00
#if !os(tvOS)
.background(
Button("Refresh") {
loadResources(force: true)
}
.keyboardShortcut("r")
2022-01-07 11:12:56 +00:00
.opacity(0)
2022-01-06 23:00:40 +00:00
)
#endif
#if os(iOS)
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
#endif
2021-09-25 08:18:22 +00:00
}
private func loadResources(force: Bool = false, onCompletion: @escaping () -> Void = {}) {
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, onCompletion: onCompletion)
}
} else {
loadFeed(force: force, onCompletion: onCompletion)
2021-06-28 10:43:07 +00:00
}
2021-09-25 08:18:22 +00:00
} else {
loadFeed(force: force, onCompletion: onCompletion)
2021-09-25 08:18:22 +00:00
}
}
private func loadFeed(force: Bool = false, onCompletion: @escaping () -> Void = {}) {
if let request = force ? feed?.load() : feed?.loadIfNeeded() {
request.onCompletion { _ in
onCompletion()
}
} else {
onCompletion()
}
2021-06-11 22:49:42 +00:00
}
}