From 72242d2cf74ca2dc591103e7b91da3247350998d Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 18 Jul 2020 17:23:49 +0200 Subject: [PATCH] Fixed that loaded accounts didn't get displayed in AccountsTab by introducing App Data --- .../BankingiOSApp/ContentView.swift | 4 +++- .../BankingiOSApp/persistence/AppData.swift | 20 ++++++++++++++++ .../BankingiOSApp/ui/views/AccountsTab.swift | 24 ++++++++----------- 3 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 ui/BankingiOSApp/BankingiOSApp/persistence/AppData.swift diff --git a/ui/BankingiOSApp/BankingiOSApp/ContentView.swift b/ui/BankingiOSApp/BankingiOSApp/ContentView.swift index 609af1b8..75be055a 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ContentView.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ContentView.swift @@ -2,11 +2,13 @@ import SwiftUI struct ContentView: View { + @ObservedObject var data: AppData = AppData() + @State private var selection = 0 var body: some View { TabView(selection: $selection) { - AccountsTab() + AccountsTab(data: data) .tabItem { VStack { Image("first") diff --git a/ui/BankingiOSApp/BankingiOSApp/persistence/AppData.swift b/ui/BankingiOSApp/BankingiOSApp/persistence/AppData.swift new file mode 100644 index 00000000..11a6ec19 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/persistence/AppData.swift @@ -0,0 +1,20 @@ +import Foundation +import BankingUiSwift + + +class AppData : ObservableObject { + + @Inject private var presenter: BankingPresenterSwift + + @Published var banks: [BUCCustomer] = [] + + + init() { + banks = presenter.customers + + presenter.addAccountsChangedListener { banks in + self.banks = banks + } + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift index 247724a8..a76faeaa 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/AccountsTab.swift @@ -6,23 +6,14 @@ struct AccountsTab: View { @Inject private var presenter: BankingPresenterSwift - @State var banks: [BUCCustomer] = [] - - - init() { - self.banks = presenter.customers - } + @ObservedObject var data: AppData var body: some View { - presenter.addAccountsChangedListener { (customers) in // i think this will add a lot of listeners but i am not allowed to use this code in init() - self.banks = customers - } - - return NavigationView { + NavigationView { VStack { - if self.banks.isEmpty == false { - List(banks, id: \.id) { bank in + if data.banks.isEmpty == false { + List(data.banks, id: \.id) { bank in BankListItem(bank: bank) } } @@ -40,7 +31,12 @@ struct AccountsTab: View { struct AccountsTab_Previews: PreviewProvider { + static var previews: some View { - AccountsTab() + let data = AppData() + data.banks = previewBanks + + return AccountsTab(data: data) } + }