Skip to content

Commit 73d7d9d

Browse files
committed
set album on upload
1 parent 30ab4d4 commit 73d7d9d

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

Django Files/Views/FileUploadView.swift

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ struct FileUploadView: View {
2323

2424
@State private var uploadPrivate: Bool = false
2525

26+
// Album selection states
27+
@State private var albums: [DFAlbum] = []
28+
@State private var searchText: String = ""
29+
@State private var selectedAlbum: DFAlbum?
30+
@State private var isLoadingAlbums: Bool = false
31+
@FocusState private var isSearchFocused: Bool
32+
2633
// Audio recording states
2734
@State private var audioRecorder: AVAudioRecorder?
2835
@State private var isRecording: Bool = false
@@ -32,6 +39,60 @@ struct FileUploadView: View {
3239
NavigationView {
3340
VStack(spacing: 20) {
3441
Toggle("Make Private", isOn: $uploadPrivate)
42+
43+
// Album Selection
44+
VStack(alignment: .leading) {
45+
TextField("Search Albums", text: $searchText)
46+
.textFieldStyle(RoundedBorderTextFieldStyle())
47+
.focused($isSearchFocused)
48+
.onChange(of: searchText) { _, _ in
49+
if searchText.isEmpty {
50+
selectedAlbum = nil
51+
}
52+
}
53+
54+
if !searchText.isEmpty && (isSearchFocused || selectedAlbum == nil) {
55+
ScrollView {
56+
LazyVStack(alignment: .leading) {
57+
ForEach(albums.filter { album in
58+
album.name.localizedCaseInsensitiveContains(searchText)
59+
}) { album in
60+
Button(action: {
61+
selectedAlbum = album
62+
searchText = album.name
63+
isSearchFocused = false
64+
}) {
65+
Text(album.name)
66+
.foregroundColor(.primary)
67+
.padding(.vertical, 8)
68+
.frame(maxWidth: .infinity, alignment: .leading)
69+
}
70+
Divider()
71+
}
72+
}
73+
}
74+
.frame(maxHeight: 200)
75+
.background(Color(.systemBackground))
76+
.cornerRadius(8)
77+
.shadow(radius: 2)
78+
}
79+
80+
if let album = selectedAlbum {
81+
HStack {
82+
Text("Selected Album: \(album.name)")
83+
.foregroundColor(.secondary)
84+
Spacer()
85+
Button(action: {
86+
selectedAlbum = nil
87+
searchText = ""
88+
}) {
89+
Image(systemName: "xmark.circle.fill")
90+
.foregroundColor(.gray)
91+
}
92+
}
93+
.padding(.vertical, 4)
94+
}
95+
}
3596

3697
// Audio Recording Button
3798
Button(action: {
@@ -133,6 +194,9 @@ struct FileUploadView: View {
133194
await uploadPhotos(newValue)
134195
}
135196
}
197+
.task {
198+
await loadAlbums()
199+
}
136200
}
137201
}
138202

@@ -147,7 +211,11 @@ struct FileUploadView: View {
147211
uploadProgress = progress
148212
}
149213

150-
_ = await api.uploadFile(url: tempURL, privateUpload: uploadPrivate, taskDelegate: delegate)
214+
if let albumId = selectedAlbum?.id {
215+
_ = await api.uploadFile(url: tempURL, albums: String(albumId), privateUpload: uploadPrivate, taskDelegate: delegate)
216+
} else {
217+
_ = await api.uploadFile(url: tempURL, privateUpload: uploadPrivate, taskDelegate: delegate)
218+
}
151219
try? FileManager.default.removeItem(at: tempURL)
152220
}
153221

@@ -170,7 +238,11 @@ struct FileUploadView: View {
170238
uploadProgress = (Double(index) + progress) / totalItems
171239
}
172240

173-
_ = await api.uploadFile(url: tempURL, privateUpload: uploadPrivate, taskDelegate: delegate)
241+
if let albumId = selectedAlbum?.id {
242+
_ = await api.uploadFile(url: tempURL, albums: String(albumId), privateUpload: uploadPrivate, taskDelegate: delegate)
243+
} else {
244+
_ = await api.uploadFile(url: tempURL, privateUpload: uploadPrivate, taskDelegate: delegate)
245+
}
174246
try? FileManager.default.removeItem(at: tempURL)
175247
}
176248
}
@@ -191,7 +263,11 @@ struct FileUploadView: View {
191263
uploadProgress = (Double(index) + progress) / totalFiles
192264
}
193265

194-
_ = await api.uploadFile(url: url, privateUpload: uploadPrivate, taskDelegate: delegate)
266+
if let albumId = selectedAlbum?.id {
267+
_ = await api.uploadFile(url: url, albums: String(albumId), privateUpload: uploadPrivate, taskDelegate: delegate)
268+
} else {
269+
_ = await api.uploadFile(url: url, privateUpload: uploadPrivate, taskDelegate: delegate)
270+
}
195271
}
196272

197273
isUploading = false
@@ -256,13 +332,26 @@ struct FileUploadView: View {
256332
uploadProgress = progress
257333
}
258334

259-
_ = await api.uploadFile(url: url, privateUpload: uploadPrivate, taskDelegate: delegate)
335+
if let albumId = selectedAlbum?.id {
336+
_ = await api.uploadFile(url: url, albums: String(albumId), privateUpload: uploadPrivate, taskDelegate: delegate)
337+
} else {
338+
_ = await api.uploadFile(url: url, privateUpload: uploadPrivate, taskDelegate: delegate)
339+
}
260340
try? FileManager.default.removeItem(at: url)
261341

262342
isUploading = false
263343
recordingURL = nil
264344
dismiss()
265345
}
346+
347+
private func loadAlbums() async {
348+
isLoadingAlbums = true
349+
let api = DFAPI(url: URL(string: server.url)!, token: server.token)
350+
if let response = await api.getAlbums() {
351+
albums = response.albums
352+
}
353+
isLoadingAlbums = false
354+
}
266355
}
267356

268357
class UploadProgressDelegate: NSObject, URLSessionTaskDelegate {

0 commit comments

Comments
 (0)