From b1eab23a030a57d9750ddd0965359008dd27453e Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 25 Jan 2023 09:32:35 +0100 Subject: [PATCH] Fixes in countdown edition --- LeCountdown.xcodeproj/project.pbxproj | 2 +- LeCountdown/NewCountdownView.swift | 35 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index 0707caa..2bc76f8 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -114,13 +114,13 @@ C4060DF6297AFEF2003FAB80 /* NewCountdownView.swift */, C438C7C02980228B00BF3EF9 /* CountdownScheduler.swift */, C4060DC3297AE73D003FAB80 /* Assets.xcassets */, + C438C7C829803CA000BF3EF9 /* AppDelegate.swift */, C4060DC8297AE73D003FAB80 /* Persistence.swift */, C438C7C4298024E900BF3EF9 /* NSManagedContext+Extensions.swift */, C4060DF4297AE9A7003FAB80 /* TimeInterval+Extensions.swift */, C4060DCD297AE73D003FAB80 /* Info.plist */, C4060DCA297AE73D003FAB80 /* LeCountdown.xcdatamodeld */, C4060DC5297AE73D003FAB80 /* Preview Content */, - C438C7C829803CA000BF3EF9 /* AppDelegate.swift */, ); path = LeCountdown; sourceTree = ""; diff --git a/LeCountdown/NewCountdownView.swift b/LeCountdown/NewCountdownView.swift index 48eb172..e6d1d10 100644 --- a/LeCountdown/NewCountdownView.swift +++ b/LeCountdown/NewCountdownView.swift @@ -32,6 +32,8 @@ struct CountdownEditView : View { @State var secondsString: String = "" @State var minutesString: String = "" + + @State var deleteConfirmationShown: Bool = false @State var errorShown: Bool = false @State var error: Error? = nil @@ -57,7 +59,19 @@ struct CountdownEditView : View { Text("Image") Text("Sound") } + }.onAppear { + self._initDuration() } + .confirmationDialog("", isPresented: $deleteConfirmationShown, actions: { + Button("Yes", role: .destructive) { + withAnimation { + self._delete() + } + }.keyboardShortcut(.defaultAction) + Button("No", role: .cancel) {} + }, message: { + Text("Do you really want to delete?") + }) .alert("", isPresented: $errorShown, actions: { }, message: { Text(error?.localizedDescription ?? "error") }) @@ -74,6 +88,13 @@ struct CountdownEditView : View { self._save() } } + ToolbarItem(placement: .bottomBar) { + Button { + self.deleteConfirmationShown = true + } label: { + Image(systemName: "trash") + } + } ToolbarItemGroup(placement: .keyboard) { Button { textFieldIsFocused = false @@ -87,6 +108,20 @@ struct CountdownEditView : View { } + fileprivate func _initDuration() { + + let minutes = Int(self.countdown.duration / 60.0) + let seconds = self.countdown.duration - Double(minutes * 60) + + if minutes > 0 { + self.minutesString = self._numberFormatter.string(from: NSNumber(value: minutes)) ?? "" + } + + if seconds > 0 { + self.secondsString = self._numberFormatter.string(from: NSNumber(value: seconds)) ?? "" + } + } + fileprivate let _numberFormatter = NumberFormatter() fileprivate var _seconds: Double {