mirror of
https://github.com/yattee/yattee.git
synced 2024-11-09 15:58:20 +00:00
Documents navigation
This commit is contained in:
parent
cf0572a94b
commit
8e5bafba58
@ -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)
|
||||
|
@ -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 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 {
|
||||
ForEach(model.sortedDirectoryContents, id: \.absoluteString) { url in
|
||||
let video = Video.local(model.standardizedURL(url) ?? url)
|
||||
PlayerQueueRow(
|
||||
item: PlayerQueueItem(video)
|
||||
)
|
||||
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()
|
||||
|
@ -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<Any>) -> Void = { _ in }
|
||||
var contentItems = [ContentItem]()
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user