2022-12-12 05:48:29 +05:30
|
|
|
import Defaults
|
2021-06-28 16:13:07 +05:30
|
|
|
import Siesta
|
2021-06-11 18:06:26 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
2021-07-28 04:10:04 +05:30
|
|
|
struct PopularView: View {
|
2021-09-25 13:48:22 +05:30
|
|
|
@StateObject private var store = Store<[Video]>()
|
2021-06-12 03:24:00 +05:30
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
@ObservedObject private var accounts = AccountsModel.shared
|
2021-06-12 02:41:59 +05:30
|
|
|
|
2022-12-16 14:05:10 +05:30
|
|
|
@State private var error: RequestError?
|
|
|
|
|
2022-12-12 05:48:29 +05:30
|
|
|
@Default(.popularListingStyle) private var popularListingStyle
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
var resource: Resource? {
|
|
|
|
accounts.api.popular
|
2021-06-28 16:13:07 +05:30
|
|
|
}
|
2021-06-14 23:35:02 +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 {
|
2023-06-08 02:02:54 +05:30
|
|
|
VerticalCells(items: videos) { if shouldDisplayHeader { header } }
|
2022-12-11 03:07:14 +05:30
|
|
|
.onAppear {
|
|
|
|
resource?.addObserver(store)
|
2022-12-16 14:05:10 +05:30
|
|
|
resource?.loadIfNeeded()?
|
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-01-07 04:30:40 +05:30
|
|
|
}
|
2022-12-12 05:48:29 +05:30
|
|
|
.environment(\.listingStyle, popularListingStyle)
|
2022-12-11 03:07:14 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
.navigationTitle("Popular")
|
|
|
|
.background(
|
|
|
|
Button("Refresh") {
|
|
|
|
resource?.load()
|
2022-12-16 14:05:10 +05:30
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-12-11 03:07:14 +05:30
|
|
|
}
|
|
|
|
.keyboardShortcut("r")
|
|
|
|
.opacity(0)
|
|
|
|
)
|
2022-01-07 04:30:40 +05:30
|
|
|
#endif
|
2022-01-05 21:55:57 +05:30
|
|
|
#if os(iOS)
|
2022-12-12 05:48:29 +05:30
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
popularMenu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
2022-09-02 22:32:19 +05:30
|
|
|
.refreshable {
|
2022-09-04 20:54:07 +05:30
|
|
|
DispatchQueue.main.async {
|
|
|
|
resource?.load()
|
2022-12-16 14:05:10 +05:30
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-09-04 20:54:07 +05:30
|
|
|
}
|
2022-01-05 21:55:57 +05:30
|
|
|
}
|
2023-07-01 22:08:11 +05:30
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
2022-01-05 21:55:57 +05:30
|
|
|
#endif
|
2022-12-12 05:48:29 +05:30
|
|
|
#if os(macOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem {
|
|
|
|
ListingStyleButtons(listingStyle: $popularListingStyle)
|
|
|
|
}
|
2023-02-25 21:12:18 +05:30
|
|
|
|
2023-05-23 22:18:39 +05:30
|
|
|
ToolbarItem {
|
|
|
|
HideWatchedButtons()
|
|
|
|
}
|
|
|
|
|
2023-02-25 21:12:18 +05:30
|
|
|
ToolbarItem {
|
2023-05-23 22:24:53 +05:30
|
|
|
HideShortsButtons()
|
2023-02-25 21:12:18 +05:30
|
|
|
}
|
2022-08-28 23:30:43 +05:30
|
|
|
}
|
2022-12-12 05:48:29 +05:30
|
|
|
#else
|
2023-05-08 01:15:18 +05:30
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
2022-12-16 14:05:10 +05:30
|
|
|
resource?.loadIfNeeded()?
|
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
2022-12-12 05:48:29 +05:30
|
|
|
}
|
2022-08-28 23:30:43 +05:30
|
|
|
#endif
|
2021-06-11 18:06:26 +05:30
|
|
|
}
|
2022-12-12 05:48:29 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
private var popularMenu: some View {
|
|
|
|
Menu {
|
|
|
|
ListingStyleButtons(listingStyle: $popularListingStyle)
|
|
|
|
|
2023-02-25 21:12:18 +05:30
|
|
|
Section {
|
2023-05-23 22:18:39 +05:30
|
|
|
HideWatchedButtons()
|
2023-05-23 22:24:53 +05:30
|
|
|
HideShortsButtons()
|
2023-02-25 21:12:18 +05:30
|
|
|
}
|
|
|
|
|
2022-12-12 05:48:29 +05:30
|
|
|
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
|
2023-04-23 00:37:30 +05:30
|
|
|
|
|
|
|
var shouldDisplayHeader: Bool {
|
|
|
|
#if os(tvOS)
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var header: some View {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
ListingStyleButtons(listingStyle: $popularListingStyle)
|
2023-05-23 22:18:39 +05:30
|
|
|
HideWatchedButtons()
|
2023-05-23 22:24:53 +05:30
|
|
|
HideShortsButtons()
|
2023-04-23 00:37:30 +05:30
|
|
|
|
|
|
|
Button {
|
|
|
|
resource?.load()
|
|
|
|
.onFailure { self.error = $0 }
|
|
|
|
.onSuccess { _ in self.error = nil }
|
|
|
|
} label: {
|
|
|
|
Label("Refresh", systemImage: "arrow.clockwise")
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.imageScale(.small)
|
|
|
|
.font(.caption)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.leading, 30)
|
|
|
|
.padding(.bottom, 15)
|
|
|
|
.padding(.trailing, 30)
|
|
|
|
}
|
2022-12-12 05:48:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
struct PopularView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NavigationView {
|
|
|
|
PopularView()
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 18:06:26 +05:30
|
|
|
}
|