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 {
|
2022-02-16 20:23:11 +00:00
|
|
|
BrowserPlayerControls {
|
2022-09-04 15:28:30 +00:00
|
|
|
SignInRequiredView(title: "Subscriptions".localized()) {
|
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)
|
|
|
|
}
|
2022-01-05 16:25:57 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.refreshControl { refreshControl in
|
|
|
|
loadResources(force: true) {
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 17:02:19 +00:00
|
|
|
.backport
|
|
|
|
.refreshable {
|
2022-09-04 15:24:07 +00:00
|
|
|
await loadResources(force: true)
|
2022-09-02 17:02:19 +00:00
|
|
|
}
|
2022-01-05 16:25:57 +00:00
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2022-11-10 21:51:30 +00:00
|
|
|
|
|
|
|
#if !os(tvOS)
|
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
|
|
|
.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
|
2022-01-05 16:25:57 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
|
|
|
|
#endif
|
2022-08-28 18:00:43 +00:00
|
|
|
#if !os(macOS)
|
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
|
|
|
loadResources()
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:25:57 +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
|
|
|
|
2021-11-14 23:06:01 +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
|
2022-01-05 16:25:57 +00:00
|
|
|
loadFeed(force: force, onCompletion: onCompletion)
|
2021-11-14 23:06:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-05 16:25:57 +00:00
|
|
|
loadFeed(force: force, onCompletion: onCompletion)
|
2021-06-28 10:43:07 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
} else {
|
2022-01-05 16:25:57 +00:00
|
|
|
loadFeed(force: force, onCompletion: onCompletion)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:25:57 +00:00
|
|
|
private func loadFeed(force: Bool = false, onCompletion: @escaping () -> Void = {}) {
|
|
|
|
if let request = force ? feed?.load() : feed?.loadIfNeeded() {
|
|
|
|
request.onCompletion { _ in
|
|
|
|
onCompletion()
|
|
|
|
}
|
2022-09-02 17:02:19 +00:00
|
|
|
.onFailure { error in
|
|
|
|
NavigationModel.shared.presentAlert(title: "Could not refresh Subscriptions", message: error.userMessage)
|
|
|
|
}
|
2022-01-05 16:25:57 +00:00
|
|
|
} else {
|
|
|
|
onCompletion()
|
|
|
|
}
|
2021-06-11 22:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-26 10:51:00 +00:00
|
|
|
|
|
|
|
struct SubscriptonsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
SubscriptionsView()
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|