yattee/Shared/Views/PopularView.swift

66 lines
1.8 KiB
Swift
Raw Normal View History

2021-06-28 10:43:07 +00:00
import Siesta
2021-06-11 12:36:26 +00:00
import SwiftUI
2021-07-27 22:40:04 +00:00
struct PopularView: View {
2021-09-25 08:18:22 +00:00
@StateObject private var store = Store<[Video]>()
2021-06-11 21:54:00 +00:00
2021-10-16 22:48:58 +00:00
@EnvironmentObject<AccountsModel> private var accounts
2021-06-11 21:11:59 +00:00
2021-10-20 22:21:50 +00:00
var resource: Resource? {
accounts.api.popular
2021-06-28 10:43:07 +00:00
}
2021-06-14 18:05:02 +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 {
VerticalCells(items: videos)
.onAppear {
2021-10-20 22:21:50 +00:00
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if !os(tvOS)
.navigationTitle("Popular")
#endif
}
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: .popular))
}
}
2022-01-06 23:00:40 +00:00
.background(
Button("Refresh") {
resource?.load()
}
.keyboardShortcut("r")
2022-01-07 11:12:56 +00:00
.opacity(0)
2022-01-06 23:00:40 +00:00
)
#endif
#if os(iOS)
.refreshControl { refreshControl in
resource?.load().onCompletion { _ in
refreshControl.endRefreshing()
}
2022-09-02 17:02:19 +00:00
.onFailure { error in
2022-10-12 16:49:47 +00:00
NavigationModel.shared.presentAlert(title: "Could not refresh Popular", message: error.userMessage)
2022-09-02 17:02:19 +00:00
}
}
.backport
.refreshable {
DispatchQueue.main.async {
resource?.load()
}
}
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
#endif
2022-08-28 18:00:43 +00:00
#if !os(macOS)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
resource?.loadIfNeeded()
}
#endif
2021-06-11 12:36:26 +00:00
}
}