Add recent documents to home on iOS

This commit is contained in:
Arkadiusz Fal
2022-11-18 23:39:52 +01:00
parent 8ec06b0d59
commit bc1571a746
8 changed files with 148 additions and 20 deletions

View File

@@ -22,10 +22,13 @@ extension Defaults.Keys {
static let enableReturnYouTubeDislike = Key<Bool>("enableReturnYouTubeDislike", default: false)
static let showHome = Key<Bool>("showHome", default: true)
static let showDocuments = Key<Bool>("showDocuments", default: true)
static let showOpenActionsInHome = Key<Bool>("showOpenActionsInHome", default: true)
static let showOpenActionsToolbarItem = Key<Bool>("showOpenActionsToolbarItem", default: false)
static let showFavoritesInHome = Key<Bool>("showFavoritesInHome", default: true)
#if os(iOS)
static let showDocuments = Key<Bool>("showDocuments", default: false)
static let homeRecentDocumentsItems = Key<Int>("homeRecentDocumentsItems", default: 3)
#endif
static let homeHistoryItems = Key<Int>("homeHistoryItems", default: 10)
static let favorites = Key<[FavoriteItem]>("favorites", default: [])

View File

@@ -7,16 +7,7 @@ struct DocumentsView: View {
BrowserPlayerControls {
ScrollView(.vertical, showsIndicators: false) {
if model.directoryContents.isEmpty {
VStack(alignment: .center, spacing: 20) {
HStack {
Image(systemName: "doc")
Text("No documents")
}
Text("Share files from Finder on a Mac\nor iTunes on Windows")
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity)
.foregroundColor(.secondary)
NoDocumentsView()
} else {
ForEach(model.sortedDirectoryContents, id: \.absoluteString) { url in
let video = Video.local(model.replacePrivateVar(url) ?? url)

View File

@@ -0,0 +1,22 @@
import SwiftUI
struct NoDocumentsView: View {
var body: some View {
VStack(alignment: .center, spacing: 20) {
HStack {
Image(systemName: "doc")
Text("No documents")
}
Text("Share files from Finder on a Mac\nor iTunes on Windows")
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity)
.foregroundColor(.secondary)
}
}
struct NoDocumentsView_Previews: PreviewProvider {
static var previews: some View {
NoDocumentsView()
}
}

View File

@@ -0,0 +1,36 @@
import Defaults
import SwiftUI
struct RecentDocumentsView: View {
var limit = 3
let model = DocumentsModel.shared
var body: some View {
LazyVStack {
if recentDocuments.isEmpty {
NoDocumentsView()
} else {
ForEach(recentDocuments, id: \.absoluteString) { url in
let video = Video.local(model.replacePrivateVar(url) ?? url)
PlayerQueueRow(
item: PlayerQueueItem(video)
)
.contextMenu {
VideoContextMenuView(video: video)
}
}
}
}
.padding(.horizontal, 15)
}
var recentDocuments: [URL] {
model.recentDocuments(limit)
}
}
struct RecentDocumentsView_Previews: PreviewProvider {
static var previews: some View {
RecentDocumentsView()
}
}

View File

@@ -15,12 +15,18 @@ struct HomeView: View {
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@State private var historyID = UUID()
#if os(iOS)
@State private var recentDocumentsID = UUID()
#endif
var favoritesObserver: Any?
#if !os(tvOS)
@Default(.favorites) private var favorites
#endif
#if os(iOS)
@Default(.homeRecentDocumentsItems) private var homeRecentDocumentsItems
#endif
@Default(.homeHistoryItems) private var homeHistoryItems
@Default(.showFavoritesInHome) private var showFavoritesInHome
@Default(.showOpenActionsInHome) private var showOpenActionsInHome
@@ -87,10 +93,33 @@ struct HomeView: View {
#endif
}
if homeRecentDocumentsItems > 0 {
VStack {
HStack {
sectionLabel("Recent Documents")
Spacer()
Button {
recentDocumentsID = UUID()
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
.font(.headline)
.labelStyle(.iconOnly)
.foregroundColor(.secondary)
}
}
RecentDocumentsView(limit: homeRecentDocumentsItems)
.id(recentDocumentsID)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
if homeHistoryItems > 0 {
VStack {
HStack {
Text("History")
sectionLabel("History")
Spacer()
Button {
navigation.presentAlert(
@@ -108,17 +137,11 @@ struct HomeView: View {
Label("Clear History", systemImage: "trash")
.font(.headline)
.labelStyle(.iconOnly)
.foregroundColor(.secondary)
}
}
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
.font(.title3.bold())
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(.secondary)
HistoryView(limit: homeHistoryItems)
.id(historyID)
@@ -157,9 +180,21 @@ struct HomeView: View {
#endif
}
}
func sectionLabel(_ label: String) -> some View {
Text(label)
#if os(tvOS)
.padding(.horizontal, 40)
#else
.padding(.horizontal, 15)
#endif
.font(.title3.bold())
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(.secondary)
}
}
struct Favorites_Previews: PreviewProvider {
struct Home_Previews: PreviewProvider {
static var previews: some View {
TabView {
HomeView()

View File

@@ -8,6 +8,7 @@ struct BrowsingSettings: View {
#endif
@Default(.accountPickerDisplaysAnonymousAccounts) private var accountPickerDisplaysAnonymousAccounts
#if os(iOS)
@Default(.homeRecentDocumentsItems) private var homeRecentDocumentsItems
@Default(.lockPortraitWhenBrowsing) private var lockPortraitWhenBrowsing
#endif
@Default(.thumbnailsQuality) private var thumbnailsQuality
@@ -24,6 +25,9 @@ struct BrowsingSettings: View {
@EnvironmentObject<AccountsModel> private var accounts
@State private var homeHistoryItemsText = ""
#if os(iOS)
@State private var homeRecentDocumentsItemsText = ""
#endif
#if os(macOS)
@State private var presentingEditFavoritesSheet = false
#endif
@@ -87,6 +91,22 @@ struct BrowsingSettings: View {
}
.multilineTextAlignment(.trailing)
HStack {
Text("Recent documents")
TextField("Recent documents", text: $homeRecentDocumentsItemsText)
.labelsHidden()
#if !os(macOS)
.keyboardType(.numberPad)
#endif
.onAppear {
homeRecentDocumentsItemsText = String(homeRecentDocumentsItems)
}
.onChange(of: homeRecentDocumentsItemsText) { newValue in
homeRecentDocumentsItems = Int(newValue) ?? 3
}
}
.multilineTextAlignment(.trailing)
if !accounts.isEmpty {
Toggle("Show Favorites", isOn: $showFavoritesInHome)