Fixed that loaded accounts didn't get displayed in AccountsTab by introducing App Data

This commit is contained in:
dankito 2020-07-18 17:23:49 +02:00
parent d29965c77e
commit 72242d2cf7
3 changed files with 33 additions and 15 deletions

View File

@ -2,11 +2,13 @@ import SwiftUI
struct ContentView: View { struct ContentView: View {
@ObservedObject var data: AppData = AppData()
@State private var selection = 0 @State private var selection = 0
var body: some View { var body: some View {
TabView(selection: $selection) { TabView(selection: $selection) {
AccountsTab() AccountsTab(data: data)
.tabItem { .tabItem {
VStack { VStack {
Image("first") Image("first")

View File

@ -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
}
}
}

View File

@ -6,23 +6,14 @@ struct AccountsTab: View {
@Inject private var presenter: BankingPresenterSwift @Inject private var presenter: BankingPresenterSwift
@State var banks: [BUCCustomer] = [] @ObservedObject var data: AppData
init() {
self.banks = presenter.customers
}
var body: some View { 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() NavigationView {
self.banks = customers
}
return NavigationView {
VStack { VStack {
if self.banks.isEmpty == false { if data.banks.isEmpty == false {
List(banks, id: \.id) { bank in List(data.banks, id: \.id) { bank in
BankListItem(bank: bank) BankListItem(bank: bank)
} }
} }
@ -40,7 +31,12 @@ struct AccountsTab: View {
struct AccountsTab_Previews: PreviewProvider { struct AccountsTab_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
AccountsTab() let data = AppData()
data.banks = previewBanks
return AccountsTab(data: data)
} }
} }