2022-12-12 00:18:29 +00:00
|
|
|
import Defaults
|
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
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
2021-06-11 21:11:59 +00:00
|
|
|
|
2022-12-16 08:35:10 +00:00
|
|
|
@State private var error: RequestError?
|
|
|
|
|
2022-12-12 00:18:29 +00:00
|
|
|
@Default(.popularListingStyle) private var popularListingStyle
|
|
|
|
|
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
|
|
|
|
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-12-10 21:37:14 +00:00
|
|
|
VerticalCells(items: videos)
|
|
|
|
.onAppear {
|
|
|
|
resource?.addObserver(store)
|
2022-12-16 08:35:10 +00:00
|
|
|
resource?.loadIfNeeded()?
|
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-01-06 23:00:40 +00:00
|
|
|
}
|
2022-12-12 00:18:29 +00:00
|
|
|
.environment(\.listingStyle, popularListingStyle)
|
2022-12-10 21:37:14 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.navigationTitle("Popular")
|
|
|
|
.background(
|
|
|
|
Button("Refresh") {
|
|
|
|
resource?.load()
|
2022-12-16 08:35:10 +00:00
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-12-10 21:37:14 +00:00
|
|
|
}
|
|
|
|
.keyboardShortcut("r")
|
|
|
|
.opacity(0)
|
|
|
|
)
|
2022-01-06 23:00:40 +00:00
|
|
|
#endif
|
2022-01-05 16:25:57 +00:00
|
|
|
#if os(iOS)
|
2022-12-12 00:18:29 +00:00
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
popularMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
2022-01-05 16:25:57 +00:00
|
|
|
.refreshControl { refreshControl in
|
|
|
|
resource?.load().onCompletion { _ in
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
}
|
2022-12-16 08:35:10 +00:00
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-09-02 17:02:19 +00:00
|
|
|
}
|
|
|
|
.backport
|
|
|
|
.refreshable {
|
2022-09-04 15:24:07 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
resource?.load()
|
2022-12-16 08:35:10 +00:00
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-09-04 15:24:07 +00:00
|
|
|
}
|
2022-01-05 16:25:57 +00:00
|
|
|
}
|
|
|
|
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
|
|
|
|
#endif
|
2022-12-12 00:18:29 +00:00
|
|
|
#if os(macOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem {
|
|
|
|
ListingStyleButtons(listingStyle: $popularListingStyle)
|
|
|
|
}
|
2022-08-28 18:00:43 +00:00
|
|
|
}
|
2022-12-12 00:18:29 +00:00
|
|
|
#else
|
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
2022-12-16 08:35:10 +00:00
|
|
|
resource?.loadIfNeeded()?
|
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-12-12 00:18:29 +00:00
|
|
|
}
|
2022-08-28 18:00:43 +00:00
|
|
|
#endif
|
2021-06-11 12:36:26 +00:00
|
|
|
}
|
2022-12-12 00:18:29 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
private var popularMenu: some View {
|
|
|
|
Menu {
|
|
|
|
ListingStyleButtons(listingStyle: $popularListingStyle)
|
|
|
|
|
|
|
|
Section {
|
|
|
|
SettingsButtons()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack(spacing: 12) {
|
|
|
|
HStack(spacing: 6) {
|
|
|
|
Image(systemName: "arrow.up.right.circle.fill")
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
.imageScale(.small)
|
|
|
|
|
|
|
|
Text("Popular")
|
|
|
|
.font(.headline)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
}
|
|
|
|
|
|
|
|
Image(systemName: "chevron.down.circle.fill")
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
.imageScale(.small)
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = nil }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PopularView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NavigationView {
|
|
|
|
PopularView()
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 12:36:26 +00:00
|
|
|
}
|