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

234 lines
8.3 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
2021-11-02 03:26:18 +05:30
@State private var presentingEditFavorites = false
2021-11-07 22:22:42 +05:30
@State private var favoritesChanged = false
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
var favoritesObserver: Any?
#if !os(tvOS)
@Default(.favorites) private var favorites
#endif
2022-11-19 04:09:52 +05:30
#if os(iOS)
@Default(.homeRecentDocumentsItems) private var homeRecentDocumentsItems
#endif
2022-11-10 22:41:28 +05:30
@Default(.homeHistoryItems) private var homeHistoryItems
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) {
HStack {
#if os(tvOS)
Group {
2022-11-12 07:09:44 +05:30
if showOpenActionsInHome {
2022-12-16 14:03:06 +05:30
AccentButton(text: "Open Video", imageSystemName: "globe") {
2022-11-12 01:58:40 +05:30
NavigationModel.shared.presentingOpenVideos = true
}
2022-11-12 07:09:44 +05:30
}
2022-12-16 14:03:06 +05:30
AccentButton(text: "Locations", imageSystemName: "globe") {
2022-12-12 03:45:56 +05:30
NavigationModel.shared.presentingAccounts = true
}
2022-12-16 14:03:06 +05:30
AccentButton(text: "Settings", imageSystemName: "gear") {
2022-12-11 03:07:14 +05:30
NavigationModel.shared.presentingSettings = true
}
}
2022-11-12 07:09:44 +05:30
#else
2022-12-11 03:07:14 +05:30
if showOpenActionsInHome {
2022-12-16 14:03:06 +05:30
AccentButton(text: "Files", imageSystemName: "folder") {
2022-12-11 03:07:14 +05:30
NavigationModel.shared.presentingFileImporter = true
2021-11-07 22:22:42 +05:30
}
2022-12-16 14:03:06 +05:30
AccentButton(text: "Paste", imageSystemName: "doc.on.clipboard.fill") {
2022-12-11 03:07:14 +05:30
OpenVideosModel.shared.openURLsFromClipboard(playbackMode: .playNow)
2021-11-02 03:26:18 +05:30
}
2022-12-16 14:03:06 +05:30
AccentButton(imageSystemName: "ellipsis") {
2022-12-11 03:07:14 +05:30
NavigationModel.shared.presentingOpenVideos = true
}
2022-12-11 03:07:14 +05:30
.frame(maxWidth: 40)
}
#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 {
#if os(tvOS)
ForEach(Defaults[.favorites]) { item in
2022-12-11 20:30:20 +05:30
FavoriteItemView(item: item)
2022-12-11 03:07:14 +05:30
}
#else
ForEach(favorites) { item in
2022-12-11 20:30:20 +05:30
FavoriteItemView(item: item)
2022-12-11 03:07:14 +05:30
#if os(macOS)
.workaroundForVerticalScrollingBug()
2022-11-19 19:21:53 +05:30
#endif
2022-11-19 04:09:52 +05:30
}
#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(iOS)
if homeRecentDocumentsItems > 0 {
2022-11-12 01:58:40 +05:30
VStack {
2022-11-15 16:52:27 +05:30
HStack {
2022-12-17 20:48:14 +05:30
NavigationLink(destination: DocumentsView()) {
HStack {
Text("Documents")
.font(.title3.bold())
Image(systemName: "chevron.right")
.imageScale(.small)
}
.lineLimit(1)
}
.padding(.leading, 15)
2022-12-11 03:07:14 +05:30
2022-11-15 16:52:27 +05:30
Spacer()
2022-12-11 03:07:14 +05:30
2022-11-15 16:52:27 +05:30
Button {
2022-12-11 03:07:14 +05:30
recentDocumentsID = UUID()
2022-11-15 16:52:27 +05:30
} label: {
2022-12-11 03:07:14 +05:30
Label("Refresh", systemImage: "arrow.clockwise")
2022-11-15 16:52:27 +05:30
.font(.headline)
.labelStyle(.iconOnly)
2022-11-19 04:09:52 +05:30
.foregroundColor(.secondary)
2022-11-15 16:52:27 +05:30
}
}
2022-11-09 19:04:04 +05:30
2022-12-11 03:07:14 +05:30
RecentDocumentsView(limit: homeRecentDocumentsItems)
.id(recentDocumentsID)
2022-11-12 01:58:40 +05:30
}
2022-12-11 03:07:14 +05:30
.frame(maxWidth: .infinity, alignment: .leading)
#if os(tvOS)
.padding(.trailing, 40)
#else
.padding(.trailing, 15)
#endif
2022-11-09 19:04:04 +05:30
}
2022-12-11 03:07:14 +05:30
#endif
2022-11-09 19:04:04 +05:30
2022-12-11 03:07:14 +05:30
if homeHistoryItems > 0 {
VStack {
HStack {
sectionLabel("History")
Spacer()
Button {
navigation.presentAlert(
Alert(
title: Text("Are you sure you want to clear history of watched videos?"),
message: Text("It cannot be reverted"),
primaryButton: .destructive(Text("Clear All")) {
PlayerModel.shared.removeHistory()
historyID = UUID()
},
secondaryButton: .cancel()
)
)
} label: {
Label("Clear History", systemImage: "trash")
.font(.headline)
.labelStyle(.iconOnly)
.foregroundColor(.secondary)
}
.buttonStyle(.plain)
}
.frame(maxWidth: .infinity, alignment: .leading)
#if os(tvOS)
.padding(.trailing, 40)
#else
.padding(.trailing, 15)
#endif
HistoryView(limit: homeHistoryItems)
2022-12-18 04:38:30 +05:30
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
2022-12-11 03:07:14 +05:30
.id(historyID)
2021-11-07 22:22:42 +05:30
}
}
2022-08-28 23:30:43 +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 {
Defaults.observe(.favorites) { _ in
2022-08-28 23:30:43 +05:30
favoritesChanged.toggle()
}
2022-12-11 03:07:14 +05:30
.tieToLifetime(of: accounts)
2021-11-02 03:26:18 +05:30
}
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)
#endif
#if os(iOS)
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
#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)
}
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 {
2022-11-09 19:04:04 +05:30
HomeView()
.injectFixtureEnvironmentObjects()
.tabItem {
2022-11-09 19:04:04 +05:30
Label("Home", systemImage: "house")
}
}
2021-11-02 03:26:18 +05:30
}
}