Skip to content

Commit

Permalink
feat: 🗑️ Delete files and folders (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Jun 24, 2024
1 parent f122518 commit 95f3cc8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Reconnect/Model/BrowserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,20 @@ class BrowserModel {
}
}

func delete(path: String) {
Task {
do {
if path.isDirectory {
try await fileServer.rmdir(path: path)
} else {
try await fileServer.remove(path: path)
}
update()
} catch {
print("Failed to delete item at path '\(path)' with error \(error).")
lastError = error
}
}
}

}
42 changes: 42 additions & 0 deletions Reconnect/PLP/FileServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,48 @@ class FileServer {
}
}

func syncQueue_rmdir(path: String) throws {
dispatchPrecondition(condition: .onQueue(workQueue))
let result = client.rmdir(path)
guard result.rawValue == 0 else {
throw ReconnectError.rfsvError(result)
}
}

func rmdir(path: String) async throws {
return try await withCheckedThrowingContinuation { continuation in
workQueue.async {
do {
try self.syncQueue_rmdir(path: path)
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}

func syncQueue_remove(path: String) throws {
dispatchPrecondition(condition: .onQueue(workQueue))
let result = client.remove(path)
guard result.rawValue == 0 else {
throw ReconnectError.rfsvError(result)
}
}

func remove(path: String) async throws {
return try await withCheckedThrowingContinuation { continuation in
workQueue.async {
do {
try self.syncQueue_remove(path: path)
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}

func devlist() -> [String] {
var devbits: UInt32 = 0
client.devlist(&devbits)
Expand Down
6 changes: 6 additions & 0 deletions Reconnect/Views/BrowserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ struct BrowserView: View {
model.navigate(to: items.first!)
}
.disabled(items.count != 1 || !(items.first?.isDirectory ?? false))
Divider()
Button("Delete") {
for item in items {
model.delete(path: item)
}
}
} primaryAction: { items in
guard
items.count == 1,
Expand Down

0 comments on commit 95f3cc8

Please sign in to comment.