yattee/Shared/Subscriptions/SubscriptionsView.swift

157 lines
4.7 KiB
Swift
Raw Permalink Normal View History

import Defaults
2022-12-16 08:35:10 +00:00
import Siesta
2022-12-10 02:01:59 +00:00
import SwiftUI
struct SubscriptionsView: View {
enum Page: String, CaseIterable, Defaults.Serializable {
case feed
case channels
2022-12-10 02:01:59 +00:00
}
@Default(.subscriptionsViewPage) private var subscriptionsViewPage
2022-12-12 00:18:29 +00:00
@Default(.subscriptionsListingStyle) private var subscriptionsListingStyle
2022-12-12 23:39:50 +00:00
@ObservedObject private var feed = FeedModel.shared
2022-12-16 08:35:10 +00:00
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
2022-12-12 23:39:50 +00:00
2022-12-10 02:01:59 +00:00
var body: some View {
2022-12-10 21:37:14 +00:00
SignInRequiredView(title: "Subscriptions".localized()) {
switch subscriptionsViewPage {
case .feed:
FeedView()
case .channels:
ChannelsView()
#if os(tvOS)
.ignoresSafeArea(.all, edges: .horizontal)
#endif
}
}
2022-12-12 00:18:29 +00:00
.environment(\.listingStyle, subscriptionsListingStyle)
#if os(iOS)
2022-12-12 00:18:29 +00:00
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
2023-05-25 15:13:01 +00:00
HStack {
Picker("Page", selection: $subscriptionsViewPage) {
Label("Feed", systemImage: "film").tag(Page.feed)
Label("Channels", systemImage: "person.3.fill").tag(Page.channels)
}
.pickerStyle(.segmented)
.labelStyle(.titleOnly)
2023-05-25 15:13:01 +00:00
}
.frame(maxWidth: 500)
2022-12-12 00:18:29 +00:00
}
2022-12-16 08:35:10 +00:00
ToolbarItem(placement: .navigationBarTrailing) {
subscriptionsMenu
}
2022-12-16 08:35:10 +00:00
ToolbarItem {
RequestErrorButton(error: requestError)
}
2022-12-12 00:18:29 +00:00
}
#endif
#if os(macOS)
.toolbar {
2023-04-22 15:59:28 +00:00
ToolbarItemGroup {
2022-12-12 00:18:29 +00:00
ListingStyleButtons(listingStyle: $subscriptionsListingStyle)
HideWatchedButtons()
2023-05-23 16:54:53 +00:00
HideShortsButtons()
2022-12-13 12:14:20 +00:00
toggleWatchedButton
2023-04-22 15:59:28 +00:00
.id(feed.watchedId)
2022-12-13 21:32:49 +00:00
playUnwatchedButton
2023-04-22 15:59:28 +00:00
.id(feed.watchedId)
2022-12-13 21:32:49 +00:00
}
}
#endif
}
2022-12-10 20:08:03 +00:00
2022-12-16 08:35:10 +00:00
var requestError: RequestError? {
subscriptionsViewPage == .channels ? subscriptions.error : feed.error
}
#if os(iOS)
var subscriptionsMenu: some View {
Menu {
2022-12-12 00:18:29 +00:00
if subscriptionsViewPage == .feed {
ListingStyleButtons(listingStyle: $subscriptionsListingStyle)
Section {
HideWatchedButtons()
HideShortsButtons()
}
2023-02-25 15:42:18 +00:00
playUnwatchedButton
2022-12-13 12:14:20 +00:00
toggleWatchedButton
}
2022-12-13 12:14:20 +00:00
2022-12-11 22:15:56 +00:00
Section {
SettingsButtons()
}
} label: {
HStack {
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
2023-05-25 15:13:01 +00:00
.imageScale(.large)
2022-12-10 02:01:59 +00:00
}
.transaction { t in t.animation = nil }
2022-12-10 02:01:59 +00:00
}
}
2022-12-11 22:40:08 +00:00
var menuLabel: some View {
HStack {
Image(systemName: subscriptionsViewPage == .channels ? "person.3.fill" : "film")
2022-12-12 00:18:29 +00:00
.imageScale(.small)
2022-12-11 22:40:08 +00:00
Text(subscriptionsViewPage.rawValue.capitalized.localized())
.font(.headline)
}
2022-12-10 02:01:59 +00:00
}
#endif
2022-12-13 12:14:20 +00:00
var playUnwatchedButton: some View {
Button {
feed.playUnwatchedFeed()
} label: {
Label("Play all unwatched", systemImage: "play")
}
2024-02-26 13:12:11 +00:00
.help("Play all unwatched")
2022-12-13 12:14:20 +00:00
.disabled(!feed.canPlayUnwatchedFeed)
}
@ViewBuilder var toggleWatchedButton: some View {
if feed.canMarkAllFeedAsWatched {
markAllFeedAsWatchedButton
} else {
markAllFeedAsUnwatchedButton
}
}
var markAllFeedAsWatchedButton: some View {
Button {
feed.markAllFeedAsWatched()
} label: {
Label("Mark all as watched", systemImage: "checkmark.circle.fill")
}
2024-02-26 13:12:11 +00:00
.help("Mark all as watched")
2022-12-13 12:14:20 +00:00
.disabled(!feed.canMarkAllFeedAsWatched)
}
var markAllFeedAsUnwatchedButton: some View {
Button {
feed.markAllFeedAsUnwatched()
} label: {
Label("Mark all as unwatched", systemImage: "checkmark.circle")
}
2024-02-26 13:12:11 +00:00
.help("Mark all as unwatched")
2022-12-13 12:14:20 +00:00
}
2022-12-10 02:01:59 +00:00
}
struct SubscriptionsView_Previews: PreviewProvider {
2022-12-10 02:01:59 +00:00
static var previews: some View {
NavigationView {
SubscriptionsView()
}
2022-12-10 02:01:59 +00:00
}
}