Skip to content

Commit b5ee780

Browse files
committed
more functionality, unified preview
1 parent faa4afc commit b5ee780

File tree

3 files changed

+86
-63
lines changed

3 files changed

+86
-63
lines changed

Django Files/API/Files.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
public struct DFFile: Codable {
10+
public struct DFFile: Codable, Hashable, Equatable {
1111
public let id: Int
1212
public let user: Int
1313
public let size: Int
@@ -66,6 +66,16 @@ public struct DFFile: Codable {
6666
formatter.timeStyle = .short
6767
return formatter.string(from: date)
6868
}
69+
70+
// Add hash implementation for Hashable conformance
71+
public func hash(into hasher: inout Hasher) {
72+
hasher.combine(id)
73+
}
74+
75+
// Add equality implementation for Equatable conformance
76+
public static func == (lhs: DFFile, rhs: DFFile) -> Bool {
77+
return lhs.id == rhs.id
78+
}
6979
}
7080

7181
public struct DFFilesResponse: Codable {

Django Files/Views/FileContextMenu.swift

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import SwiftUI
22

33
struct FileContextMenuButtons: View {
4+
5+
var isPreviewing: Bool = false
6+
47
var onPreview: () -> Void
58
var onCopyShareLink: () -> Void
69
var onCopyRawLink: () -> Void
7-
var onSetPrivate: () -> Void
10+
var onTogglePrivate: () -> Void
811
var onShowInMaps: () -> Void
912

1013
var body: some View {
1114
Group {
12-
Button {
13-
onPreview()
14-
} label: {
15-
Label("Open Preview", systemImage: "arrow.up.forward.app")
15+
if !isPreviewing {
16+
Button {
17+
onPreview()
18+
} label: {
19+
Label("Open Preview", systemImage: "arrow.up.forward.app")
20+
}
1621
}
1722

1823
Button {
@@ -28,7 +33,7 @@ struct FileContextMenuButtons: View {
2833
}
2934

3035
Button {
31-
onSetPrivate()
36+
onTogglePrivate()
3237
} label: {
3338
Label("Set Private", systemImage: "lock")
3439
}
@@ -40,4 +45,4 @@ struct FileContextMenuButtons: View {
4045
}
4146
}
4247
}
43-
}
48+
}

Django Files/Views/FileList.swift

+63-55
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ struct FileListView: View {
1919
@State private var isLoading = true
2020
@State private var errorMessage: String? = nil
2121

22+
@State private var previewFile: Bool = true
23+
@State private var selectedFile: DFFile? = nil
24+
@State private var navigationPath = NavigationPath()
25+
2226
var body: some View {
2327
ZStack {
2428
if isLoading && files.isEmpty {
@@ -53,39 +57,31 @@ struct FileListView: View {
5357
}
5458
.padding()
5559
} else {
56-
NavigationStack {
60+
NavigationStack(path: $navigationPath) {
5761
List {
5862
ForEach(files, id: \.id) { file in
59-
NavigationLink {
60-
ContentPreview(mimeType: file.mime, fileURL: URL(string: file.raw))
61-
.toolbar {
62-
ToolbarItem(placement: .navigationBarTrailing) {
63-
Menu {
64-
FileContextMenuButtons(
65-
onPreview: {
66-
67-
},
68-
onCopyShareLink: {
69-
// Open Maps and center it on this item.
70-
},
71-
onCopyRawLink: {
72-
// Open Maps and center it on this item.
73-
},
74-
onSetPrivate: {
75-
// Add this item to a list of favorites.
76-
},
77-
onShowInMaps: {
78-
// Open Maps and center it on this item.
79-
}
80-
)
81-
} label: {
82-
Image(systemName: "ellipsis.circle")
63+
NavigationLink(value: file) {
64+
FileRowView(file: file)
65+
.contextMenu {
66+
FileContextMenuButtons(
67+
isPreviewing: false,
68+
onPreview: {
69+
selectedFile = file
70+
},
71+
onCopyShareLink: {
72+
UIPasteboard.general.string = file.url
73+
},
74+
onCopyRawLink: {
75+
UIPasteboard.general.string = file.raw
76+
},
77+
onTogglePrivate: {
78+
// Add this item to a list of favorites.
79+
},
80+
onShowInMaps: {
81+
// Open Maps and center it on this item.
8382
}
84-
}
83+
)
8584
}
86-
87-
} label: {
88-
FileRowView(file: file)
8985
}
9086
.id(file.id)
9187

@@ -106,6 +102,35 @@ struct FileListView: View {
106102
}
107103
}
108104
}
105+
.navigationDestination(for: DFFile.self) { file in
106+
ContentPreview(mimeType: file.mime, fileURL: URL(string: file.raw))
107+
.toolbar {
108+
ToolbarItem(placement: .navigationBarTrailing) {
109+
Menu {
110+
FileContextMenuButtons(
111+
isPreviewing: true,
112+
onPreview: {
113+
// No action needed since this is already the preview screen.
114+
},
115+
onCopyShareLink: {
116+
UIPasteboard.general.string = file.url
117+
},
118+
onCopyRawLink: {
119+
UIPasteboard.general.string = file.raw
120+
},
121+
onTogglePrivate: {
122+
// Add this item to a list of favorites.
123+
},
124+
onShowInMaps: {
125+
// Open Maps and center it on this item.
126+
}
127+
)
128+
} label: {
129+
Image(systemName: "ellipsis.circle")
130+
}
131+
}
132+
}
133+
}
109134

110135
.listStyle(.plain)
111136
.refreshable {
@@ -132,7 +157,12 @@ struct FileListView: View {
132157
}
133158
}
134159
}
135-
160+
.onChange(of: selectedFile) { oldValue, newValue in
161+
if let file = newValue {
162+
navigationPath.append(file)
163+
selectedFile = nil // Reset after navigation
164+
}
165+
}
136166
}
137167
}
138168
.onAppear {
@@ -219,9 +249,6 @@ struct CustomLabel: LabelStyle {
219249

220250
struct FileRowView: View {
221251
let file: DFFile
222-
223-
@State private var showPreview: Bool = false
224-
225252
private func getIcon() -> String {
226253
switch file.mime {
227254
case "image/jpeg":
@@ -254,27 +281,8 @@ struct FileRowView: View {
254281
.foregroundColor(.secondary)
255282
}
256283
}
257-
.contextMenu {
258-
FileContextMenuButtons(
259-
onPreview: {
260-
showPreview = true
261-
},
262-
onCopyShareLink: {
263-
// Open Maps and center it on this item.
264-
},
265-
onCopyRawLink: {
266-
// Open Maps and center it on this item.
267-
},
268-
onSetPrivate: {
269-
// Add this item to a list of favorites.
270-
},
271-
onShowInMaps: {
272-
// Open Maps and center it on this item.
273-
}
274-
)
275-
}
276-
.sheet(isPresented: $showPreview) {
277-
ContentPreview(mimeType: file.mime, fileURL: URL(string: file.raw))
278-
}
284+
// .sheet(isPresented: $showPreview) {
285+
// ContentPreview(mimeType: file.mime, fileURL: URL(string: file.raw))
286+
// }
279287
}
280288
}

0 commit comments

Comments
 (0)