From c1bd70085a25aa801508d03796638aee643f77f3 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 6 Aug 2020 23:33:44 +0200 Subject: [PATCH] Extracted TanProcedurePicker --- .../BankingiOSApp/ui/TanProcedurePicker.swift | 69 +++++++++++++++++++ .../ui/views/EnterTanDialog.swift | 27 +------- 2 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/TanProcedurePicker.swift diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/TanProcedurePicker.swift b/ui/BankingiOSApp/BankingiOSApp/ui/TanProcedurePicker.swift new file mode 100644 index 00000000..4d7f56a2 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/TanProcedurePicker.swift @@ -0,0 +1,69 @@ +import SwiftUI +import BankingUiSwift + + +struct TanProcedurePicker: View { + + private let bank: Customer + + private let selectedTanProcedureChanged: (TanProcedure) -> Void + + + private var customersTanProcedures: [TanProcedure] = [] + + @State private var selectedTanProcedureIndex: Int + + private var selectedTanProcedureIndexBinding: Binding { + Binding( + get: { self.selectedTanProcedureIndex }, + set: { + if (self.selectedTanProcedureIndex != $0) { // only if TAN procedure has really changed + self.selectedTanProcedureIndex = $0 + self.dispatchSelectedTanProcedureChanged(self.customersTanProcedures[$0]) + } + }) + } + + + init(_ bank: Customer, _ initiallySelectedTanProcedure: TanProcedure? = nil, selectedTanProcedureChanged: @escaping (TanProcedure) -> Void) { + self.bank = bank + + self.selectedTanProcedureChanged = selectedTanProcedureChanged + + + self.customersTanProcedures = bank.supportedTanProcedures.filter( {$0.type != .chiptanusb } ) // USB tan generators are not supported on iOS + + let selectedTanProcedureType = initiallySelectedTanProcedure?.type ?? bank.selectedTanProcedure?.type + + _selectedTanProcedureIndex = State(initialValue: customersTanProcedures.firstIndex(where: { $0.type == selectedTanProcedureType } ) + ?? bank.supportedTanProcedures.firstIndex(where: { $0.type != .chiptanmanuell && $0.type != .chiptanusb } ) + ?? 0) + } + + + var body: some View { + Picker("TAN procedure", selection: selectedTanProcedureIndexBinding) { + ForEach(0 ..< self.customersTanProcedures.count) { index in + Text(self.customersTanProcedures[index].displayName) + } + } + } + + + private func dispatchSelectedTanProcedureChanged(_ selectedTanProcedure: TanProcedure) { + // do async as at this point Picker dialog gets dismissed -> this EnterTanDialog would never get dismissed (and dismiss has to be called before callback.changeTanProcedure()) + DispatchQueue.main.async { + self.selectedTanProcedureChanged(selectedTanProcedure) + } + } + +} + + +struct TanProcedurePicker_Previews: PreviewProvider { + + static var previews: some View { + TanProcedurePicker(previewBanks[0]) { _ in } + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift index 149f43b4..b66b6c63 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift @@ -13,21 +13,6 @@ struct EnterTanDialog: View { private var customer: Customer - private var customersTanProcedures: [TanProcedure] = [] - - @State private var selectedTanProcedureIndex: Int - - private var selectedTanProcedureIndexBinding: Binding { - Binding( - get: { self.selectedTanProcedureIndex }, - set: { - if (self.selectedTanProcedureIndex != $0) { // only if TAN procedure has really changed - self.selectedTanProcedureIndex = $0 - self.selectedTanProcedureChanged(self.customersTanProcedures[$0]) - } - }) - } - private var customersTanMedia: [TanMedium] = [] @State private var selectedTanMediumIndex = 0 @@ -65,12 +50,6 @@ struct EnterTanDialog: View { self.tanChallenge = state.tanChallenge self.customer = state.customer - self.customersTanProcedures = customer.supportedTanProcedures.filter( {$0.type != .chiptanusb } ) // USB tan generators are not supported on iOS - - _selectedTanProcedureIndex = State(initialValue: customersTanProcedures.firstIndex(where: { $0.type == state.tanChallenge.tanProcedure.type } ) - ?? state.customer.supportedTanProcedures.firstIndex(where: { $0.type != .chiptanmanuell && $0.type != .chiptanusb } ) - ?? 0) - self.customersTanMedia = customer.tanMediaSorted self.showSelectTanMediumView = self.customersTanMedia.count > 1 // TODO: use isOpticalTanProcedure && tanMedia.count > 1 @@ -90,10 +69,8 @@ struct EnterTanDialog: View { var body: some View { Form { Section { - Picker("TAN procedure", selection: selectedTanProcedureIndexBinding) { - ForEach(0 ..< self.customersTanProcedures.count) { index in - Text(self.customersTanProcedures[index].displayName) - } + TanProcedurePicker(customer, state.tanChallenge.tanProcedure) { selectedTanProcedure in + self.selectedTanProcedureChanged(selectedTanProcedure) } if showSelectTanMediumView {