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
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
var videos: [ContentItem] {
|
|
|
|
ContentItem.array(of: store.collection)
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:43:07 +00:00
|
|
|
var body: some View {
|
2021-10-05 20:20:09 +00:00
|
|
|
PlayerControlsView {
|
|
|
|
SignInRequiredView(title: "Subscriptions") {
|
2021-10-21 23:29:10 +00:00
|
|
|
VerticalCells(items: videos)
|
2021-10-05 20:20:09 +00:00
|
|
|
.onAppear {
|
|
|
|
loadResources()
|
|
|
|
}
|
2021-10-19 21:27:04 +00:00
|
|
|
.onChange(of: accounts.current) { _ in
|
2021-10-05 20:20:09 +00:00
|
|
|
loadResources(force: true)
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
.refreshable {
|
|
|
|
loadResources(force: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
if let request = force ? accounts.api.home?.load() : accounts.api.home?.loadIfNeeded() {
|
2021-09-25 08:18:22 +00:00
|
|
|
request.onSuccess { _ in
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|