some fixes

This commit is contained in:
2025-10-20 00:50:21 +02:00
parent bf0ffe8632
commit b72ea4f417
2 changed files with 48 additions and 6 deletions

View File

@@ -300,6 +300,8 @@ private struct DirectoryView: View {
.onAppear { .onAppear {
if manager.currentDirectory != path { if manager.currentDirectory != path {
manager.changeDirectory(to: path) manager.changeDirectory(to: path)
} else {
manager.refreshDirectory()
} }
} }
.fileImporter( .fileImporter(

View File

@@ -115,6 +115,7 @@ final class TimeSyncManager: NSObject, ObservableObject {
private var pendingListPath: String? private var pendingListPath: String?
private var pendingListOperationID: UUID? private var pendingListOperationID: UUID?
private var simpleOperationID: UUID? private var simpleOperationID: UUID?
private var pendingDirectoryRequest: (path: String, operationID: UUID)?
private struct UploadState { private struct UploadState {
let id: UUID let id: UUID
@@ -402,6 +403,7 @@ final class TimeSyncManager: NSObject, ObservableObject {
pendingListPath = nil pendingListPath = nil
pendingListOperationID = nil pendingListOperationID = nil
simpleOperationID = nil simpleOperationID = nil
pendingDirectoryRequest = nil
isFileBusy = false isFileBusy = false
currentDirectory = "/" currentDirectory = "/"
directoryEntries = [] directoryEntries = []
@@ -675,19 +677,51 @@ final class TimeSyncManager: NSObject, ObservableObject {
} }
private func requestDirectory(path: String) { private func requestDirectory(path: String) {
let normalizedPath = normalizedPath(path)
guard let commandCharacteristic = fileCommandCharacteristic else { guard let commandCharacteristic = fileCommandCharacteristic else {
fileErrorMessage = FileServiceError.characteristicUnavailable.localizedDescription let opID = pendingDirectoryRequest?.operationID ?? UUID()
pendingDirectoryRequest = (path: normalizedPath, operationID: opID)
pendingListPath = normalizedPath
pendingListData.removeAll()
isFileBusy = true
pendingListOperationID = opID
updateOperation(id: opID, title: "Loading", message: normalizedPath, progress: nil, indeterminate: true)
return return
} }
pendingListPath = path
let opID: UUID
if let pending = pendingDirectoryRequest, pending.path == normalizedPath {
opID = pending.operationID
pendingDirectoryRequest = nil
} else {
opID = UUID()
}
pendingListPath = normalizedPath
pendingListData.removeAll() pendingListData.removeAll()
isFileBusy = true isFileBusy = true
let opID = UUID()
pendingListOperationID = opID pendingListOperationID = opID
updateOperation(id: opID, title: "Loading", message: path, progress: nil, indeterminate: true) updateOperation(id: opID, title: "Loading", message: normalizedPath, progress: nil, indeterminate: true)
let payload = payloadFor(path: path) let payload = payloadFor(path: normalizedPath)
enqueueFileCommand(.listDirectory, payload: payload, characteristic: commandCharacteristic) enqueueFileCommand(.listDirectory, payload: payload, characteristic: commandCharacteristic)
} }
@discardableResult
private func flushPendingDirectoryRequest() -> Bool {
guard let pending = pendingDirectoryRequest,
let commandCharacteristic = fileCommandCharacteristic else { return false }
pendingDirectoryRequest = nil
pendingListPath = pending.path
pendingListData.removeAll()
isFileBusy = true
pendingListOperationID = pending.operationID
updateOperation(id: pending.operationID, title: "Loading", message: pending.path, progress: nil, indeterminate: true)
let payload = payloadFor(path: pending.path)
enqueueFileCommand(.listDirectory, payload: payload, characteristic: commandCharacteristic)
return true
}
} }
// MARK: - CBCentralManagerDelegate // MARK: - CBCentralManagerDelegate
@@ -824,6 +858,10 @@ extension TimeSyncManager: CBPeripheralDelegate {
connectionState = .ready connectionState = .ready
statusMessage = "Connected to Cardboy." statusMessage = "Connected to Cardboy."
} }
if fileResponseCharacteristic?.isNotifying == true {
_ = flushPendingDirectoryRequest()
}
} }
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) { func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
@@ -833,7 +871,9 @@ extension TimeSyncManager: CBPeripheralDelegate {
} }
if characteristic.uuid == fileResponseUUID, characteristic.isNotifying { if characteristic.uuid == fileResponseUUID, characteristic.isNotifying {
refreshDirectory() if !flushPendingDirectoryRequest() {
refreshDirectory()
}
} }
} }