From 58a310156bd09ac536f65aa12122ea1f045c0116 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Tue, 8 Feb 2022 22:01:58 -0600 Subject: - Disable button when no command is selected. - Add confirmation for sending command. --- iosApp/iosApp/Details/Commands/UnitCommandsView.swift | 11 ++++++++++- iosApp/iosApp/Details/Commands/UnitCommandsViewModel.swift | 12 ++++++++++-- iosApp/iosApp/Localizable.strings | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/iosApp/iosApp/Details/Commands/UnitCommandsView.swift b/iosApp/iosApp/Details/Commands/UnitCommandsView.swift index 9e4b280..eef3ae4 100644 --- a/iosApp/iosApp/Details/Commands/UnitCommandsView.swift +++ b/iosApp/iosApp/Details/Commands/UnitCommandsView.swift @@ -39,10 +39,19 @@ struct UnitCommandsView: View { }.pickerStyle(InlinePickerStyle()) Button { - unitCommandsViewModel.sendCommand() + unitCommandsViewModel.showConfirmation = true } label: { Label("send-command", systemImage: "paperplane") } + .disabled(unitCommandsViewModel.notSelected) + .actionSheet(isPresented: $unitCommandsViewModel.showConfirmation) { + ActionSheet(title: Text("send-command-confirm"), buttons: [ + .cancel(), + .destructive(Text("send-command")) { + unitCommandsViewModel.sendCommand() + } + ]) + } } } } diff --git a/iosApp/iosApp/Details/Commands/UnitCommandsViewModel.swift b/iosApp/iosApp/Details/Commands/UnitCommandsViewModel.swift index aac82c9..d7298c4 100644 --- a/iosApp/iosApp/Details/Commands/UnitCommandsViewModel.swift +++ b/iosApp/iosApp/Details/Commands/UnitCommandsViewModel.swift @@ -25,11 +25,19 @@ class UnitCommandsViewModel: ObservableObject { @Published var selected: Command? @Published var selectedId: Int = 0 { didSet { - selected = commands.first(where: { + if let selected = commands.first(where: { Int(truncating: $0.id!) == selectedId - }) + }) { + self.selected = selected + notSelected = false + } else { + self.selected = nil + notSelected = true + } } } + @Published var notSelected: Bool = true + @Published var showConfirmation: Bool = false func fetchCommands(id: Int32) { commandsController.fetchCommands(deviceId: id) { commands, error in diff --git a/iosApp/iosApp/Localizable.strings b/iosApp/iosApp/Localizable.strings index 4d1e1f2..76e7bd4 100644 --- a/iosApp/iosApp/Localizable.strings +++ b/iosApp/iosApp/Localizable.strings @@ -52,3 +52,4 @@ "maps-url-template" = "https://www.google.com/maps/place/{y},{x}?z=19"; "send-command" = "Send command"; +"send-command-confirm" = "Are you sure you want to send the command?"; -- cgit v1.2.3