From de2b5152ef2ed02477baefbae3a0c81cb18cace6 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 7 Sep 2020 01:32:16 +0200 Subject: [PATCH] Disabling UpdateButton as long its executing its action --- .../BankingiOSApp/ui/UpdateButton.swift | 23 +++++++++++++++---- .../ui/views/AccountTransactionsDialog.swift | 6 +++-- .../BankingiOSApp/ui/views/AccountsTab.swift | 4 ++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UpdateButton.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UpdateButton.swift index d3e39a2d..f76bfb47 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UpdateButton.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UpdateButton.swift @@ -3,16 +3,18 @@ import SwiftUI struct UpdateButton: View { - private let action: (Any?) -> Void + private let action: (Any?, @escaping () -> Void) -> Void private let actionParameter: Any? + @State private var isExecutingAction = false - init(_ action: @escaping (Any?) -> Void) { + + init(_ action: @escaping (Any, @escaping () -> Void?) -> Void) { self.init(actionParameter: nil, action) } - init(actionParameter: Any? = nil, _ action: @escaping (Any?) -> Void) { + init(actionParameter: Any? = nil, _ action: @escaping (Any?, @escaping () -> Void) -> Void) { self.action = action self.actionParameter = actionParameter @@ -21,15 +23,26 @@ struct UpdateButton: View { var body: some View { Button( - action: { self.action(self.actionParameter) }, + action: { self.executeAction() }, label: { Image(systemName: "arrow.2.circlepath") } ) + .disabled(isExecutingAction) } + + + private func executeAction() { + isExecutingAction = true + + action(self.actionParameter) { + self.isExecutingAction = false + } + } + } struct UpdateButton_Previews: PreviewProvider { static var previews: some View { - UpdateButton( { _ in } ) + UpdateButton( { _, _ in } ) } } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountTransactionsDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountTransactionsDialog.swift index 8ffb3875..9b920330 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountTransactionsDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountTransactionsDialog.swift @@ -159,12 +159,14 @@ struct AccountTransactionsDialog: View { Alert(title: message.title, message: message.message, dismissButton: message.primaryButton) } .showNavigationBarTitle(LocalizedStringKey(title)) - .navigationBarItems(trailing: UpdateButton { _ in self.updateTransactions() }) + .navigationBarItems(trailing: UpdateButton { _, executingDone in self.updateTransactions(executingDone) }) } - private func updateTransactions() { + private func updateTransactions(_ executingDone: @escaping () -> Void) { presenter.updateSelectedBankAccountTransactionsAsync { response in + executingDone() + if response.isSuccessful { self.filterTransactions(self.searchText) } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift index e93c9066..651872e0 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift @@ -37,8 +37,8 @@ struct AccountsTab: View { } .systemGroupedBackground() .showNavigationBarTitle("Accounts") - .navigationBarItems(leading: data.hasAtLeastOneAccountBeenAdded == false ? nil : UpdateButton { _ in - self.presenter.updateAccountsTransactionsAsync { _ in } + .navigationBarItems(leading: data.hasAtLeastOneAccountBeenAdded == false ? nil : UpdateButton { _, executingDone in + self.presenter.updateAccountsTransactionsAsync { _ in executingDone() } }) }