Skip to content

Commit 79e1f42

Browse files
committed
fix: changing folder type and direction doesn't work (fixes #310)
Signed-off-by: Tommy van der Vorst <tommy@pixelspark.nl>
1 parent 056d06f commit 79e1f42

File tree

1 file changed

+77
-61
lines changed

1 file changed

+77
-61
lines changed

Sushitrain/FolderView.swift

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -276,47 +276,58 @@ struct FolderStatusView: View {
276276
}
277277

278278
struct FolderSyncTypePicker: View {
279+
private enum FolderSyncType: String {
280+
case allFiles = "allFiles"
281+
case selectedFiles = "selectedFiles"
282+
}
283+
279284
@Environment(AppState.self) private var appState
280285
@State private var changeProhibited = true
286+
@State private var folderSyncType: FolderSyncType? = nil
281287
var folder: SushitrainFolder
282288

283289
var body: some View {
284290
if folder.exists() {
285-
Picker(
286-
"Selection",
287-
selection: Binding(
288-
get: { folder.isSelective() }, set: { s in try? folder.setSelective(s) })
289-
) {
290-
Text("All files").tag(false)
291-
Text("Selected files").tag(true)
291+
Picker("Selection", selection: $folderSyncType) {
292+
Text("All files").tag(FolderSyncType.allFiles)
293+
Text("Selected files").tag(FolderSyncType.selectedFiles)
294+
}
295+
.onChange(of: folderSyncType) { _, nv in
296+
try? self.folder.setSelective(nv == .selectedFiles)
292297
}
293298
.pickerStyle(.menu)
294299
.disabled(changeProhibited)
295300
.onAppear {
296-
// Only allow changes to selection mode when folder is idle
297-
if !folder.isIdleOrSyncing {
298-
changeProhibited = true
299-
return
300-
}
301+
self.update()
302+
}
303+
}
304+
}
301305

302-
// Prohibit change in selection mode when there are extraneous files
303-
Task.detached {
304-
var hasExtra: ObjCBool = false
305-
do {
306-
let _ = try folder.hasExtraneousFiles(&hasExtra)
307-
let hasExtraFinal = hasExtra
308-
DispatchQueue.main.async {
309-
changeProhibited = hasExtraFinal.boolValue
310-
}
311-
}
312-
catch {
313-
Log.warn(
314-
"Error calling hasExtraneousFiles: \(error.localizedDescription)"
315-
)
316-
DispatchQueue.main.async {
317-
changeProhibited = true
318-
}
319-
}
306+
private func update() {
307+
self.folderSyncType = self.folder.isSelective() ? .selectedFiles : .allFiles
308+
309+
// Only allow changes to selection mode when folder is idle
310+
if !folder.isIdleOrSyncing {
311+
changeProhibited = true
312+
return
313+
}
314+
315+
// Prohibit change in selection mode when there are extraneous files
316+
Task.detached {
317+
var hasExtra: ObjCBool = false
318+
do {
319+
let _ = try folder.hasExtraneousFiles(&hasExtra)
320+
let hasExtraFinal = hasExtra
321+
DispatchQueue.main.async {
322+
changeProhibited = hasExtraFinal.boolValue
323+
}
324+
}
325+
catch {
326+
Log.warn(
327+
"Error calling hasExtraneousFiles: \(error.localizedDescription)"
328+
)
329+
DispatchQueue.main.async {
330+
changeProhibited = true
320331
}
321332
}
322333
}
@@ -327,49 +338,54 @@ struct FolderDirectionPicker: View {
327338
@Environment(AppState.self) private var appState
328339
var folder: SushitrainFolder
329340
@State private var changeProhibited: Bool = true
341+
@State private var folderType: String? = nil
330342

331343
var body: some View {
332344
if folder.exists() {
333-
Picker(
334-
"Direction",
335-
selection: Binding(
336-
get: { folder.folderType() }, set: { s in try? folder.setFolderType(s) })
337-
) {
338-
Text("Send and receive").tag(SushitrainFolderTypeSendReceive)
339-
Text("Receive only").tag(SushitrainFolderTypeReceiveOnly)
345+
Picker("Direction", selection: $folderType) {
346+
Text("Send and receive").tag(SushitrainFolderTypeSendReceive as String?)
347+
Text("Receive only").tag(SushitrainFolderTypeReceiveOnly as String?)
340348

341349
// Cannot be selected, but should be here when it is set
342350
if folder.folderType() == SushitrainFolderTypeSendOnly {
343-
Text("Send only").tag(SushitrainFolderTypeSendOnly)
351+
Text("Send only").tag(SushitrainFolderTypeSendOnly as String?)
344352
}
345353
}
346354
.pickerStyle(.menu)
347355
.disabled(changeProhibited)
348356
.onAppear {
349-
// Only allow changes to selection mode when folder is idle
350-
if !folder.isIdleOrSyncing {
351-
changeProhibited = true
352-
return
353-
}
357+
self.update()
358+
}
359+
.onChange(of: folderType) { _, nv in
360+
try? self.folder.setFolderType(nv)
361+
}
362+
}
363+
}
354364

355-
// Prohibit change in selection mode when there are extraneous files
356-
Task.detached {
357-
do {
358-
var hasExtra: ObjCBool = false
359-
let _ = try folder.hasExtraneousFiles(&hasExtra)
360-
let hasExtraFinal = hasExtra
361-
DispatchQueue.main.async {
362-
changeProhibited = hasExtraFinal.boolValue
363-
}
364-
}
365-
catch {
366-
Log.warn(
367-
"Error calling hasExtraneousFiles: \(error.localizedDescription)"
368-
)
369-
DispatchQueue.main.async {
370-
changeProhibited = true
371-
}
372-
}
365+
private func update() {
366+
self.folderType = self.folder.folderType()
367+
// Only allow changes to selection mode when folder is idle
368+
if !folder.isIdleOrSyncing {
369+
changeProhibited = true
370+
return
371+
}
372+
373+
// Prohibit change in selection mode when there are extraneous files
374+
Task.detached {
375+
do {
376+
var hasExtra: ObjCBool = false
377+
let _ = try folder.hasExtraneousFiles(&hasExtra)
378+
let hasExtraFinal = hasExtra
379+
DispatchQueue.main.async {
380+
changeProhibited = hasExtraFinal.boolValue
381+
}
382+
}
383+
catch {
384+
Log.warn(
385+
"Error calling hasExtraneousFiles: \(error.localizedDescription)"
386+
)
387+
DispatchQueue.main.async {
388+
changeProhibited = true
373389
}
374390
}
375391
}

0 commit comments

Comments
 (0)