2021-09-25 13:48:22 +05:30
|
|
|
import Siesta
|
2021-06-12 04:19:42 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SubscriptionsView: View {
|
2021-09-25 13:48:22 +05:30
|
|
|
@StateObject private var store = Store<[Video]>()
|
2021-06-26 17:07:24 +05:30
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
var feed: Resource? {
|
|
|
|
accounts.api.feed
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-06-26 17:07:24 +05:30
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
var videos: [ContentItem] {
|
|
|
|
ContentItem.array(of: store.collection)
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
var body: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
PlayerControlsView {
|
|
|
|
SignInRequiredView(title: "Subscriptions") {
|
2021-10-22 04:59:10 +05:30
|
|
|
VerticalCells(items: videos)
|
2021-10-06 01:50:09 +05:30
|
|
|
.onAppear {
|
|
|
|
loadResources()
|
|
|
|
}
|
2021-10-20 02:57:04 +05:30
|
|
|
.onChange(of: accounts.current) { _ in
|
2021-10-06 01:50:09 +05:30
|
|
|
loadResources(force: true)
|
|
|
|
}
|
2022-01-05 21:55:57 +05:30
|
|
|
#if os(iOS)
|
|
|
|
.refreshControl { refreshControl in
|
|
|
|
loadResources(force: true) {
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2021-11-02 03:26:18 +05:30
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .automatic) {
|
|
|
|
FavoriteButton(item: FavoriteItem(section: .subscriptions))
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 21:55:57 +05:30
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
|
|
|
|
#endif
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2022-01-05 21:55:57 +05:30
|
|
|
private func loadResources(force: Bool = false, onCompletion: @escaping () -> Void = {}) {
|
2021-10-21 03:51:50 +05:30
|
|
|
feed?.addObserver(store)
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-11-15 04:36:01 +05:30
|
|
|
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
|
2022-01-05 21:55:57 +05:30
|
|
|
loadFeed(force: force, onCompletion: onCompletion)
|
2021-11-15 04:36:01 +05:30
|
|
|
}
|
|
|
|
} else {
|
2022-01-05 21:55:57 +05:30
|
|
|
loadFeed(force: force, onCompletion: onCompletion)
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
} else {
|
2022-01-05 21:55:57 +05:30
|
|
|
loadFeed(force: force, onCompletion: onCompletion)
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:55:57 +05:30
|
|
|
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-12 04:19:42 +05:30
|
|
|
}
|
|
|
|
}
|