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

211 lines
7.9 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 {
2021-11-02 03:26:18 +05:30
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlaylistsModel> private var playlists
@State private var dragging: FavoriteItem?
@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
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-02-17 01:53:11 +05:30
BrowserPlayerControls {
2021-11-02 03:26:18 +05:30
ScrollView(.vertical, showsIndicators: false) {
2022-11-12 07:09:44 +05:30
HStack {
#if os(tvOS)
Group {
if showOpenActionsInHome {
OpenVideosButton(text: "Open Video", imageSystemName: "globe") {
NavigationModel.shared.presentingOpenVideos = true
}
2022-11-12 01:58:40 +05:30
}
2022-11-12 07:09:44 +05:30
OpenVideosButton(text: "Settings", imageSystemName: "gear") {
NavigationModel.shared.presentingSettings = true
}
}
#else
if showOpenActionsInHome {
2022-11-12 01:58:40 +05:30
OpenVideosButton(text: "Files", imageSystemName: "folder") {
NavigationModel.shared.presentingFileImporter = true
}
OpenVideosButton(text: "Paste", imageSystemName: "doc.on.clipboard.fill") {
OpenVideosModel.shared.openURLsFromClipboard(playbackMode: .playNow)
}
OpenVideosButton(imageSystemName: "ellipsis") {
NavigationModel.shared.presentingOpenVideos = true
}
.frame(maxWidth: 40)
2022-11-12 07:09:44 +05:30
}
2022-11-11 23:20:13 +05:30
#endif
}
2022-11-12 07:09:44 +05:30
#if os(iOS)
.padding(.top, RefreshControl.navigationBarTitleDisplayMode == .inline ? 15 : 0)
#else
.padding(.top, 15)
#endif
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
2022-11-11 23:20:13 +05:30
2022-11-12 01:58:40 +05:30
if !accounts.current.isNil, showFavoritesInHome {
2021-11-02 03:26:18 +05:30
#if os(tvOS)
2021-11-07 22:22:42 +05:30
ForEach(Defaults[.favorites]) { item in
FavoriteItemView(item: item, dragging: $dragging)
}
#else
#if os(iOS)
let first = favorites.first
#endif
2021-11-07 22:22:42 +05:30
ForEach(favorites) { item in
FavoriteItemView(item: item, dragging: $dragging)
#if os(macOS)
.workaroundForVerticalScrollingBug()
#endif
2021-11-02 03:26:18 +05:30
}
#endif
}
2022-11-09 19:04:04 +05:30
#if os(iOS)
if homeRecentDocumentsItems > 0 {
VStack {
HStack {
sectionLabel("Recent Documents")
Spacer()
Button {
recentDocumentsID = UUID()
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
.font(.headline)
.labelStyle(.iconOnly)
.foregroundColor(.secondary)
}
2022-11-19 04:09:52 +05:30
}
RecentDocumentsView(limit: homeRecentDocumentsItems)
.id(recentDocumentsID)
}
.frame(maxWidth: .infinity, alignment: .leading)
2022-11-19 04:09:52 +05:30
}
#endif
2022-11-19 04:09:52 +05:30
2022-11-12 01:58:40 +05:30
if homeHistoryItems > 0 {
VStack {
2022-11-15 16:52:27 +05:30
HStack {
2022-11-19 04:09:52 +05:30
sectionLabel("History")
2022-11-15 16:52:27 +05:30
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)
2022-11-19 04:09:52 +05:30
.foregroundColor(.secondary)
2022-11-15 16:52:27 +05:30
}
}
.frame(maxWidth: .infinity, alignment: .leading)
#if os(tvOS)
.padding(.trailing, 40)
#endif
2022-11-09 19:04:04 +05:30
2022-11-12 01:58:40 +05:30
HistoryView(limit: homeHistoryItems)
2022-11-15 16:52:27 +05:30
.id(historyID)
2022-11-12 01:58:40 +05:30
}
2022-11-09 19:04:04 +05:30
}
2022-11-11 23:20:13 +05:30
#if !os(tvOS)
2022-11-09 19:04:04 +05:30
Color.clear.padding(.bottom, 60)
#endif
2021-11-02 03:26:18 +05:30
}
2021-11-07 22:22:42 +05:30
.onAppear {
Defaults.observe(.favorites) { _ in
favoritesChanged.toggle()
}
.tieToLifetime(of: accounts)
}
2022-08-28 23:30:43 +05:30
2021-11-07 22:22:42 +05:30
.redrawOn(change: favoritesChanged)
2021-11-02 03:26:18 +05:30
#if os(tvOS)
.edgesIgnoringSafeArea(.horizontal)
#else
2022-11-09 19:04:04 +05:30
.navigationTitle("Home")
2021-11-02 03:26:18 +05:30
#endif
#if os(macOS)
.background(Color.secondaryBackground)
2021-11-08 21:59:35 +05:30
.frame(minWidth: 360)
2021-11-02 03:26:18 +05:30
#endif
#if os(iOS)
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
#endif
2022-08-28 23:30:43 +05:30
#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
}
}