1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Home/HomeView.swift

234 lines
7.7 KiB
Swift
Raw Normal View History

2021-11-02 03:26:18 +05:30
import Defaults
import Siesta
import SwiftUI
import UniformTypeIdentifiers
2022-11-09 19:04:04 +05:30
struct HomeView: View {
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var player = PlayerModel.shared
2021-11-02 03:26:18 +05:30
2023-05-25 17:58:29 +05:30
@State private var presentingHomeSettings = false
2021-11-07 22:22:42 +05:30
@State private var favoritesChanged = false
@State private var updateTask: Task<Void, Never>?
2021-11-07 22:22:42 +05:30
2022-11-15 16:52:27 +05:30
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@State private var historyID = UUID()
2022-11-19 04:09:52 +05:30
#if os(iOS)
@State private var recentDocumentsID = UUID()
#endif
2022-11-15 16:52:27 +05:30
2021-11-07 22:22:42 +05:30
#if !os(tvOS)
@Default(.favorites) private var favorites
2023-05-25 17:58:29 +05:30
@Default(.widgetsSettings) private var widgetsSettings
2022-11-19 04:09:52 +05:30
#endif
2022-11-12 01:58:40 +05:30
@Default(.showFavoritesInHome) private var showFavoritesInHome
@Default(.showOpenActionsInHome) private var showOpenActionsInHome
2022-12-19 06:07:09 +05:30
@Default(.showQueueInHome) private var showQueueInHome
2021-11-07 22:22:42 +05:30
2022-11-09 19:04:04 +05:30
private var navigation: NavigationModel { .shared }
2021-11-02 03:26:18 +05:30
var body: some View {
2022-12-11 03:07:14 +05:30
ScrollView(.vertical, showsIndicators: false) {
2023-05-25 17:58:29 +05:30
VStack {
2023-06-08 02:49:10 +05:30
#if !os(tvOS)
HStack {
2022-11-12 07:09:44 +05:30
if showOpenActionsInHome {
2023-05-25 17:58:29 +05:30
AccentButton(text: "Files", imageSystemName: "folder") {
NavigationModel.shared.presentingFileImporter = true
}
AccentButton(text: "Paste", imageSystemName: "doc.on.clipboard.fill") {
OpenVideosModel.shared.openURLsFromClipboard(playbackMode: .playNow)
}
AccentButton(imageSystemName: "ellipsis") {
2022-11-12 01:58:40 +05:30
NavigationModel.shared.presentingOpenVideos = true
}
2023-05-25 17:58:29 +05:30
.frame(maxWidth: 40)
2022-11-12 07:09:44 +05:30
}
2023-06-08 02:49:10 +05:30
}
#endif
2023-05-25 17:58:29 +05:30
#if os(tvOS)
HStack {
2023-06-08 02:49:10 +05:30
if showOpenActionsInHome {
Button {
NavigationModel.shared.presentingOpenVideos = true
} label: {
Label("Open Video", systemImage: "globe")
}
}
Button {
NavigationModel.shared.presentingAccounts = true
} label: {
Label("Locations", systemImage: "globe")
}
2023-05-25 17:58:29 +05:30
Spacer()
HideWatchedButtons()
HideShortsButtons()
2023-06-08 02:49:10 +05:30
Button {
NavigationModel.shared.presentingSettings = true
} label: {
Label("Settings", systemImage: "gear")
}
2022-12-11 03:07:14 +05:30
}
2023-06-08 02:49:10 +05:30
#if os(tvOS)
.font(.caption)
.imageScale(.small)
#endif
2022-12-11 03:07:14 +05:30
#endif
}
.padding(.top, 15)
2022-12-19 04:40:05 +05:30
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
2022-12-11 03:07:14 +05:30
#endif
2022-12-19 04:40:05 +05:30
2022-12-19 06:07:09 +05:30
if showQueueInHome {
QueueView()
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
}
2022-12-11 03:07:14 +05:30
if !accounts.current.isNil, showFavoritesInHome {
2023-05-25 17:58:29 +05:30
VStack(alignment: .leading) {
2022-12-23 00:06:35 +05:30
#if os(tvOS)
ForEach(Defaults[.favorites]) { item in
FavoriteItemView(item: item, favoritesChanged: $favoritesChanged)
2022-12-23 00:06:35 +05:30
}
#else
ForEach(favorites) { item in
FavoriteItemView(item: item, favoritesChanged: $favoritesChanged)
2022-12-23 00:06:35 +05:30
#if os(macOS)
.workaroundForVerticalScrollingBug()
#endif
}
#endif
}
2022-12-11 03:07:14 +05:30
}
2022-11-19 04:09:52 +05:30
2022-12-11 03:07:14 +05:30
#if !os(tvOS)
Color.clear.padding(.bottom, 60)
2021-11-02 03:26:18 +05:30
#endif
2022-12-11 03:07:14 +05:30
}
.onAppear {
Task {
for await _ in Defaults.updates(.favorites) {
favoritesChanged.toggle()
}
for await _ in Defaults.updates(.widgetsSettings) {
favoritesChanged.toggle()
}
2023-05-25 17:58:29 +05:30
}
2021-11-02 03:26:18 +05:30
}
.onDisappear {
updateTask?.cancel()
}
.onChange(of: player.presentingPlayer) { _ in
if player.presentingPlayer {
updateTask?.cancel()
} else {
Task {
for await _ in Defaults.updates(.favorites) {
favoritesChanged.toggle()
}
for await _ in Defaults.updates(.widgetsSettings) {
favoritesChanged.toggle()
}
}
}
}
2022-12-11 03:07:14 +05:30
.redrawOn(change: favoritesChanged)
#if os(tvOS)
.edgesIgnoringSafeArea(.horizontal)
#else
.navigationTitle("Home")
#endif
#if os(macOS)
.background(Color.secondaryBackground)
.frame(minWidth: 360)
2023-05-25 17:58:29 +05:30
.toolbar {
ToolbarItemGroup(placement: .automatic) {
HideWatchedButtons()
HideShortsButtons()
HomeSettingsButton()
}
}
2022-12-11 03:07:14 +05:30
#endif
#if os(iOS)
2023-05-23 22:36:49 +05:30
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
homeMenu
}
}
2022-12-11 03:07:14 +05:30
#endif
#if !os(macOS)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
favoritesChanged.toggle()
}
#endif
2021-11-02 03:26:18 +05:30
}
2022-11-19 04:09:52 +05:30
func sectionLabel(_ label: String) -> some View {
2022-11-19 04:36:13 +05:30
Text(label.localized())
2022-11-19 04:09:52 +05:30
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
.font(.title3.bold())
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(.secondary)
}
2023-05-23 22:36:49 +05:30
2023-05-25 17:58:29 +05:30
#if os(iOS)
2023-05-23 22:36:49 +05:30
var homeMenu: some View {
Menu {
Section {
HideWatchedButtons()
HideShortsButtons()
}
2023-05-25 17:58:29 +05:30
Section {
Button {
navigation.presentingHomeSettings = true
} label: {
Label("Home Settings", systemImage: "gear")
}
}
2023-05-23 22:36:49 +05:30
} label: {
HStack(spacing: 12) {
Text("Home")
.foregroundColor(.primary)
.font(.headline)
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
}
.transaction { t in t.animation = nil }
}
}
#endif
2021-11-02 03:26:18 +05:30
}
2022-11-19 04:09:52 +05:30
struct Home_Previews: PreviewProvider {
2021-11-02 03:26:18 +05:30
static var previews: some View {
TabView {
2023-05-23 22:36:49 +05:30
NavigationView {
HomeView()
.injectFixtureEnvironmentObjects()
.tabItem {
Label("Home", systemImage: "house")
}
}
}
2021-11-02 03:26:18 +05:30
}
}