-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirectoryView.swift
291 lines (273 loc) · 9.53 KB
/
DirectoryView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// DirectoryView.swift
//
// Created by Speedyfriend67 on 27.06.24
//
import SwiftUI
#if canImport(UIKit)
extension View {
func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
#endif
struct DirectoryView: View {
@ObservedObject private var viewModel: FileManagerViewModel
@State private var showingActionSheet = false
@State private var showingAddItemSheet = false
@State private var showingRenameCopySheet = false
@State private var isAddingDirectory = false
@State private var isRenaming = false
@State private var statusMessage = ""
@State private var showingStatusAlert = false
@State private var showingDeleteAlert = false
@State private var itemToDelete: FileSystemItem?
@State private var itemToRenameOrCopy: FileSystemItem?
@State private var newName: String = ""
@State private var showingSortOptions = false
@State private var showingSearchBar = false
@State private var isEditing = false
@State private var progress: Double = 0.0
init(directory: URL) {
_viewModel = ObservedObject(wrappedValue: FileManagerViewModel(directory: directory))
}
var body: some View {
VStack {
if showingSearchBar {
searchBarView
}
if viewModel.isSearching {
ProgressView("Searching...")
.padding()
}
fileListView
}
.navigationBarTitle(viewModel.directory.path, displayMode: .inline)
.navigationBarItems(leading: editButton, trailing: trailingNavBarItems)
.actionSheet(isPresented: $showingActionSheet) {
actionSheet
}
.sheet(isPresented: $showingAddItemSheet) {
AddItemView(isPresented: $showingAddItemSheet, isDirectory: isAddingDirectory, existingNames: viewModel.items.map { $0.name }) { name in
if isAddingDirectory {
viewModel.createFolder(named: name)
} else {
viewModel.createFile(named: name)
}
}
}
.sheet(isPresented: $showingRenameCopySheet) {
RenameCopyView(newName: $newName, isPresented: $showingRenameCopySheet, isRename: isRenaming) { name in
if let item = itemToRenameOrCopy {
if isRenaming {
viewModel.renameFile(at: item.url, to: name)
} else {
viewModel.copyFile(at: item.url, to: name)
}
}
}
}
.alert(isPresented: $showingStatusAlert) {
Alert(
title: Text("Status"),
message: Text(statusMessage),
dismissButton: .default(Text("OK"))
)
}
.alert(isPresented: $showingDeleteAlert) {
Alert(
title: Text("Confirm Delete"),
message: Text("Are you sure you want to delete \(itemToDelete?.name ?? "this item")?"),
primaryButton: .destructive(Text("Delete")) {
if let item = itemToDelete {
viewModel.deleteFile(at: item.url)
}
},
secondaryButton: .cancel()
)
}
.sheet(item: $viewModel.selectedFile) { selectedFile in
FilePermissionView(viewModel: viewModel, fileURL: selectedFile.url)
}
}
private var searchBarView: some View {
VStack {
Picker("Search Scope", selection: $viewModel.searchScope) {
Text(SearchScope.current.title).tag(SearchScope.current)
Text(SearchScope.root.title).tag(SearchScope.root)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
SearchBar(text: $viewModel.searchQuery, onSearchButtonClicked: {
hideKeyboard()
if viewModel.searchScope == .root {
viewModel.loadRootFiles()
}
})
.padding(.horizontal)
}
}
private var fileListView: some View {
List(selection: $viewModel.selectedItems) {
ForEach(viewModel.filteredItems) { item in
NavigationLink(destination: destinationView(for: item)) {
fileRow(for: item)
}
}
}
}
private func fileRow(for item: FileSystemItem) -> some View {
HStack {
if isEditing {
Image(systemName: viewModel.selectedItems.contains(item.id) ? "checkmark.circle.fill" : "circle")
}
if item.isDirectory {
Image(systemName: "folder")
} else if item.isSymlink {
Image(systemName: "link")
} else {
Image(systemName: "doc")
}
VStack(alignment: .leading) {
Text(item.name)
if !item.isDirectory {
Text(viewModel.formattedFileSize(item.size))
}
}
Spacer()
Button(action: {
viewModel.showFilePermissions(for: item)
}) {
Image(systemName: "info.circle")
}
.buttonStyle(BorderlessButtonStyle())
.padding(.leading, 10)
}
.contextMenu {
Button(action: {
itemToRenameOrCopy = item
isRenaming = true
showingRenameCopySheet = true
}) {
Text("Rename")
Image(systemName: "pencil")
}
Button(action: {
itemToRenameOrCopy = item
isRenaming = false
showingRenameCopySheet = true
}) {
Text("Copy")
Image(systemName: "doc.on.doc")
}
Button(action: {
itemToDelete = item
showingDeleteAlert = true
}) {
Text("Delete")
Image(systemName: "trash")
}
}
}
private var editButton: some View {
Button(action: {
withAnimation {
isEditing.toggle()
viewModel.selectedItems.removeAll()
}
}) {
Text(isEditing ? "Done" : "Edit")
}
}
private var trailingNavBarItems: some View {
HStack {
Button(action: {
showingActionSheet = true
}) {
Image(systemName: "plus")
}
Button(action: {
withAnimation {
showingSearchBar.toggle()
}
}) {
Image(systemName: "magnifyingglass")
}
Menu {
ForEach(SortOption.allCases, id: \.self) { option in
Button(action: {
viewModel.sortOption = option
}) {
Label(option.title, systemImage: option.icon)
}
}
Button(action: {
viewModel.showHiddenFiles.toggle()
}) {
Label(viewModel.showHiddenFiles ? "Hide Hidden Files" : "Show Hidden Files", systemImage: viewModel.showHiddenFiles ? "eye.slash" : "eye")
}
} label: {
HStack {
Text("Sort")
Image(systemName: "arrow.up.arrow.down")
}
}
}
}
private var actionSheet: ActionSheet {
ActionSheet(title: Text("Add New Item"), message: Text("What would you like to add?"), buttons: [
.default(Text("Folder")) {
isAddingDirectory = true
showingAddItemSheet = true
},
.default(Text("File")) {
isAddingDirectory = false
showingAddItemSheet = true
},
.cancel()
])
}
private func destinationView(for item: FileSystemItem) -> some View {
if item.isDirectory {
if item.isSymlink {
if let resolvedURL = resolveSymlink(at: item.url) {
return AnyView(DirectoryView(directory: resolvedURL))
} else {
return AnyView(Text("Invalid symlink: \(item.name)"))
}
} else {
return AnyView(DirectoryView(directory: item.url))
}
} else if item.isTextFile {
return AnyView(TextFileView(fileURL: item.url))
} else if item.isImageFile {
return AnyView(ImageMetadataView(fileURL: item.url))
} else if item.isPlistFile {
return AnyView(PlistEditorView(fileURL: item.url))
} else if item.isHexFile {
return AnyView(HexEditorView(fileURL: item.url))
} else if item.isVideoFile {
return AnyView(VideoPlayerView(fileURL: item.url))
} else if item.isAudioFile {
return AnyView(AudioPlayerView(fileURL: item.url))
} else {
return AnyView(FileDetailView(fileURL: item.url))
}
}
private func resolveSymlink(at url: URL) -> URL? {
do {
let destination = try FileManager.default.destinationOfSymbolicLink(atPath: url.path)
let resolvedURL = URL(fileURLWithPath: destination)
// Check if the resolved URL exists
if FileManager.default.fileExists(atPath: resolvedURL.path) {
return resolvedURL
} else {
print("Resolved symlink does not exist: \(resolvedURL.path)")
return nil
}
} catch {
print("Failed to resolve symlink: \(error.localizedDescription)")
return nil
}
}
}