Skip to content

Commit a2bae01

Browse files
committed
feat: add option to wait for sync to finish (fixes #309)
Signed-off-by: Tommy van der Vorst <tommy@pixelspark.nl>
1 parent e7a23e1 commit a2bae01

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

Localizable.xcstrings

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36570,6 +36570,52 @@
3657036570
}
3657136571
}
3657236572
},
36573+
"Wait inside the app" : {
36574+
"localizations" : {
36575+
"de" : {
36576+
"stringUnit" : {
36577+
"state" : "translated",
36578+
"value" : "Warten Sie in der App"
36579+
}
36580+
},
36581+
"es" : {
36582+
"stringUnit" : {
36583+
"state" : "translated",
36584+
"value" : "Espera dentro de la app"
36585+
}
36586+
},
36587+
"it" : {
36588+
"stringUnit" : {
36589+
"state" : "translated",
36590+
"value" : "Attendi all'interno dell'app"
36591+
}
36592+
},
36593+
"ja" : {
36594+
"stringUnit" : {
36595+
"state" : "translated",
36596+
"value" : "アプリ内でお待ちください"
36597+
}
36598+
},
36599+
"nl" : {
36600+
"stringUnit" : {
36601+
"state" : "translated",
36602+
"value" : "Wacht in de app"
36603+
}
36604+
},
36605+
"uk" : {
36606+
"stringUnit" : {
36607+
"state" : "translated",
36608+
"value" : "Зачекайте в додатку"
36609+
}
36610+
},
36611+
"zh-Hans" : {
36612+
"stringUnit" : {
36613+
"state" : "translated",
36614+
"value" : "请在应用内等待"
36615+
}
36616+
}
36617+
}
36618+
},
3657336619
"Wait until the folder is done synchronizing and try again." : {
3657436620
"localizations" : {
3657536621
"de" : {

Sushitrain/BackgroundManager.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum BackgroundTaskType: String, Codable, Equatable {
4343

4444
enum ContinuedTaskType {
4545
case time(seconds: Double)
46+
case timeOrFinished(seconds: Double)
4647
}
4748

4849
#if os(iOS)
@@ -170,8 +171,13 @@ enum ContinuedTaskType {
170171
var run = BackgroundSyncRun(started: Date.now, taskType: .continued)
171172
// Perform the requested continued task
172173
switch taskType {
173-
case .time(seconds: let duration):
174+
case .time(seconds: let duration), .timeOrFinished(seconds: let duration):
174175
let start = Date.now
176+
var stopWhenFinished = false
177+
if case .timeOrFinished(_) = taskType {
178+
stopWhenFinished = true
179+
}
180+
175181
var shouldContinue = true
176182
task.expirationHandler = {
177183
Log.info("Continued processing task expired")
@@ -180,6 +186,17 @@ enum ContinuedTaskType {
180186

181187
do {
182188
while shouldContinue {
189+
if stopWhenFinished && appState.isFinished {
190+
// Wait another second to see if we're still finished
191+
continuedTask.updateTitle(continuedTask.title, subtitle: String(localized: "Finished"))
192+
try await Task.sleep(for: .seconds(1))
193+
if appState.isFinished {
194+
shouldContinue = false
195+
continuedTask.updateTitle(continuedTask.title, subtitle: String(localized: "Finishing up..."))
196+
break
197+
}
198+
}
199+
183200
let remaining = Int64(duration - Date.now.timeIntervalSince(start))
184201
if remaining <= 0 {
185202
shouldContinue = false

Sushitrain/StartView.swift

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,18 @@ struct StartView: View {
303303
#if os(iOS)
304304
.contextMenu {
305305
if !self.appState.isFinished {
306-
Button("Wait for completion", systemImage: "hourglass.circle") {
307-
self.showWaitScreen = true
306+
if #available(iOS 26, *) {
307+
self.continueInBackgroundMenu(untilFinished: true)
308+
}
309+
else {
310+
Button("Wait for completion", systemImage: "hourglass.circle") {
311+
self.showWaitScreen = true
312+
}
308313
}
309314
}
310315

311316
if #available(iOS 26, *) {
312-
self.continueInBackgroundMenu()
317+
self.continueInBackgroundMenu(untilFinished: false)
313318
}
314319
}
315320
#endif
@@ -425,32 +430,40 @@ struct StartView: View {
425430
}
426431

427432
#if os(iOS)
428-
@ViewBuilder @available(iOS 26, *) private func continueInBackgroundMenu() -> some View {
429-
Menu("Synchronize in the background", systemImage: "gearshape.2.fill") {
433+
@ViewBuilder @available(iOS 26, *) private func continueInBackgroundMenu(untilFinished: Bool) -> some View {
434+
Menu(untilFinished ? "Wait for completion" : "Synchronize in the background", systemImage: "gearshape.2.fill") {
430435
Button("For 10 seconds") {
431-
self.startBackgroundSyncFor(.time(seconds: 10))
436+
self.startBackgroundSyncFor(untilFinished ? .timeOrFinished(seconds: 10) : .time(seconds: 10))
432437
}.disabled(backgroundManager.runningContinuedTask != nil)
433438

434439
Button("For 1 minute") {
435-
self.startBackgroundSyncFor(.time(seconds: 60))
440+
self.startBackgroundSyncFor(untilFinished ? .timeOrFinished(seconds: 60) : .time(seconds: 60))
436441
}.disabled(backgroundManager.runningContinuedTask != nil)
437442

438443
Button("For 10 minutes") {
439-
self.startBackgroundSyncFor(.time(seconds: 10 * 60))
444+
self.startBackgroundSyncFor(untilFinished ? .timeOrFinished(seconds: 10 * 60) : .time(seconds: 10 * 60))
440445
}.disabled(backgroundManager.runningContinuedTask != nil)
441446

442447
Button("For 1 hour") {
443-
self.startBackgroundSyncFor(.time(seconds: 3600))
448+
self.startBackgroundSyncFor(untilFinished ? .timeOrFinished(seconds: 3600) : .time(seconds: 3600))
444449
}.disabled(backgroundManager.runningContinuedTask != nil)
450+
451+
Divider()
452+
453+
Button("Wait inside the app", systemImage: "hourglass.circle") {
454+
self.showWaitScreen = true
455+
}
445456
}
446457
}
447458

448459
@available(iOS 26, *) private func startBackgroundSyncFor(_ type: ContinuedTaskType) {
449-
do {
450-
try backgroundManager.startContinuedSync(type)
451-
}
452-
catch {
453-
self.showError = error
460+
withAnimation {
461+
do {
462+
try backgroundManager.startContinuedSync(type)
463+
}
464+
catch {
465+
self.showError = error
466+
}
454467
}
455468
}
456469
#endif

0 commit comments

Comments
 (0)