diff --git a/Model/DocumentsModel.swift b/Model/DocumentsModel.swift index a5a251c4..765638e1 100644 --- a/Model/DocumentsModel.swift +++ b/Model/DocumentsModel.swift @@ -3,24 +3,14 @@ import Foundation final class DocumentsModel: ObservableObject { static var shared = DocumentsModel() - @Published private(set) var directoryURL: URL! @Published private(set) var refreshID = UUID() typealias AreInIncreasingOrder = (URL, URL) -> Bool - init(directoryURL: URL! = nil) { - self.directoryURL = directoryURL - } - private var fileManager: FileManager { .default } - var directoryLabel: String { - guard let directoryURL else { return "Documents" } - return displayLabelForDocument(directoryURL) - } - var sortPredicates: [AreInIncreasingOrder] { [ { self.isDirectory($0) && !self.isDirectory($1) }, @@ -28,8 +18,8 @@ final class DocumentsModel: ObservableObject { ] } - var sortedDirectoryContents: [URL] { - directoryContents.sorted { lhs, rhs in + func sortedDirectoryContents(_ directoryURL: URL) -> [URL] { + directoryContents(directoryURL).sorted { lhs, rhs in for predicate in sortPredicates { if !predicate(lhs, rhs), !predicate(rhs, lhs) { continue @@ -42,9 +32,8 @@ final class DocumentsModel: ObservableObject { } } - var directoryContents: [URL] { - guard let directoryURL else { return [] } - return contents(of: directoryURL) + func directoryContents(_ directoryURL: URL) -> [URL] { + contents(of: directoryURL) } var documentsDirectory: URL? { @@ -157,7 +146,7 @@ final class DocumentsModel: ObservableObject { )) ?? [] } - private func displayLabelForDocument(_ file: URL) -> String { + func displayLabelForDocument(_ file: URL) -> String { let components = file.absoluteString.components(separatedBy: "/Documents/") if components.count == 2 { let component = components[1] @@ -166,27 +155,6 @@ final class DocumentsModel: ObservableObject { return "Document" } - var canGoBack: Bool { - guard let directoryURL, let documentsDirectory else { return false } - return standardizedURL(directoryURL) != documentsDirectory - } - - func goToURL(_ url: URL) { - directoryURL = url - } - - func goBack() { - directoryURL = urlToGoBack - } - - func goToTop() { - directoryURL = documentsDirectory - } - - private var urlToGoBack: URL? { - directoryURL?.deletingLastPathComponent() - } - func standardizedURL(_ url: URL) -> URL? { let standardizedURL = NSString(string: url.absoluteString).standardizingPath return URL(string: standardizedURL) diff --git a/Shared/Documents/DocumentsView.swift b/Shared/Documents/DocumentsView.swift index 9080641b..7b15fc41 100644 --- a/Shared/Documents/DocumentsView.swift +++ b/Shared/Documents/DocumentsView.swift @@ -1,18 +1,28 @@ import SwiftUI struct DocumentsView: View { + var directoryURL: URL? + @ObservedObject private var model = DocumentsModel.shared var body: some View { ScrollView(.vertical, showsIndicators: false) { - if model.directoryContents.isEmpty { + if let url, model.directoryContents(url).isEmpty { NoDocumentsView() - } else { - ForEach(model.sortedDirectoryContents, id: \.absoluteString) { url in - let video = Video.local(model.standardizedURL(url) ?? url) - PlayerQueueRow( - item: PlayerQueueItem(video) - ) + } else if let url { + ForEach(model.sortedDirectoryContents(url), id: \.absoluteString) { url in + let standardizedURL = model.standardizedURL(url) ?? url + let video = Video.local(standardizedURL) + + Group { + if model.isDirectory(standardizedURL) { + NavigationLink(destination: DocumentsView(directoryURL: url)) { + VideoBanner(video: video) + } + } else { + PlayerQueueRow(item: PlayerQueueItem(video)) + } + } .contextMenu { VideoContextMenuView(video: video) } @@ -22,29 +32,7 @@ struct DocumentsView: View { } Color.clear.padding(.bottom, 50) } - .onAppear { - if model.directoryURL.isNil { - model.goToTop() - } - } - .toolbar { - ToolbarItem(placement: .navigationBarLeading) { - if model.canGoBack { - Button { - withAnimation { - model.goBack() - } - } label: { - HStack(spacing: 6) { - Label("Go back", systemImage: "chevron.left") - } - } - .transaction { t in t.animation = .none } - .disabled(!model.canGoBack) - } - } - } - .navigationTitle(model.directoryLabel) + .navigationTitle(directoryLabel) .padding(.horizontal) .navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode) .backport @@ -55,6 +43,15 @@ struct DocumentsView: View { } } + var url: URL? { + directoryURL ?? model.documentsDirectory + } + + var directoryLabel: String { + guard let directoryURL else { return "Documents" } + return model.displayLabelForDocument(directoryURL) + } + func refresh() { withAnimation { model.refresh() diff --git a/Shared/Home/FavoriteItemView.swift b/Shared/Home/FavoriteItemView.swift index 67c1a34e..46974da5 100644 --- a/Shared/Home/FavoriteItemView.swift +++ b/Shared/Home/FavoriteItemView.swift @@ -56,7 +56,7 @@ struct FavoriteItemView: View { } func loadCacheAndResource(force: Bool = false) { - guard var resource else { return } + guard let resource else { return } var onSuccess: (Entity) -> Void = { _ in } var contentItems = [ContentItem]() diff --git a/Shared/Home/HomeView.swift b/Shared/Home/HomeView.swift index 7bb5537a..eba3cf85 100644 --- a/Shared/Home/HomeView.swift +++ b/Shared/Home/HomeView.swift @@ -92,7 +92,16 @@ struct HomeView: View { if homeRecentDocumentsItems > 0 { VStack { HStack { - sectionLabel("Recent Documents") + NavigationLink(destination: DocumentsView()) { + HStack { + Text("Documents") + .font(.title3.bold()) + Image(systemName: "chevron.right") + .imageScale(.small) + } + .lineLimit(1) + } + .padding(.leading, 15) Spacer() diff --git a/Shared/Player/PlayerQueueRow.swift b/Shared/Player/PlayerQueueRow.swift index 4c11ff6c..25c84e00 100644 --- a/Shared/Player/PlayerQueueRow.swift +++ b/Shared/Player/PlayerQueueRow.swift @@ -33,17 +33,6 @@ struct PlayerQueueRow: View { return } - #if os(iOS) - guard !video.localStreamIsDirectory else { - if let url = video.localStream?.localURL { - withAnimation { - DocumentsModel.shared.goToURL(url) - } - } - return - } - #endif - if video.localStreamIsFile, let url = video.localStream?.localURL { URLBookmarkModel.shared.saveBookmark(url) }