Disabling UpdateButton as long its executing its action

This commit is contained in:
dankito 2020-09-07 01:32:16 +02:00
parent 4d781aea65
commit de2b5152ef
3 changed files with 24 additions and 9 deletions

View File

@ -3,16 +3,18 @@ import SwiftUI
struct UpdateButton: View { struct UpdateButton: View {
private let action: (Any?) -> Void private let action: (Any?, @escaping () -> Void) -> Void
private let actionParameter: Any? 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) 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.action = action
self.actionParameter = actionParameter self.actionParameter = actionParameter
@ -21,15 +23,26 @@ struct UpdateButton: View {
var body: some View { var body: some View {
Button( Button(
action: { self.action(self.actionParameter) }, action: { self.executeAction() },
label: { Image(systemName: "arrow.2.circlepath") } label: { Image(systemName: "arrow.2.circlepath") }
) )
.disabled(isExecutingAction)
} }
private func executeAction() {
isExecutingAction = true
action(self.actionParameter) {
self.isExecutingAction = false
}
}
} }
struct UpdateButton_Previews: PreviewProvider { struct UpdateButton_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
UpdateButton( { _ in } ) UpdateButton( { _, _ in } )
} }
} }

View File

@ -159,12 +159,14 @@ struct AccountTransactionsDialog: View {
Alert(title: message.title, message: message.message, dismissButton: message.primaryButton) Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
} }
.showNavigationBarTitle(LocalizedStringKey(title)) .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 presenter.updateSelectedBankAccountTransactionsAsync { response in
executingDone()
if response.isSuccessful { if response.isSuccessful {
self.filterTransactions(self.searchText) self.filterTransactions(self.searchText)
} }

View File

@ -37,8 +37,8 @@ struct AccountsTab: View {
} }
.systemGroupedBackground() .systemGroupedBackground()
.showNavigationBarTitle("Accounts") .showNavigationBarTitle("Accounts")
.navigationBarItems(leading: data.hasAtLeastOneAccountBeenAdded == false ? nil : UpdateButton { _ in .navigationBarItems(leading: data.hasAtLeastOneAccountBeenAdded == false ? nil : UpdateButton { _, executingDone in
self.presenter.updateAccountsTransactionsAsync { _ in } self.presenter.updateAccountsTransactionsAsync { _ in executingDone() }
}) })
} }