From e9fb4841270926768fadefdf2bfd36251b058cc9 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sat, 9 Oct 2021 20:03:01 -0600 Subject: [PATCH] Fix not being able to add multiple transactions --- Twigs/TabbedBudgetView.swift | 8 ++++++-- Twigs/Transaction/AddTransactionView.swift | 10 ++++++---- Twigs/Transaction/TransactionDataStore.swift | 19 +++++++------------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Twigs/TabbedBudgetView.swift b/Twigs/TabbedBudgetView.swift index 4341579..f44d2b4 100644 --- a/Twigs/TabbedBudgetView.swift +++ b/Twigs/TabbedBudgetView.swift @@ -18,8 +18,12 @@ struct TabbedBudgetView: View { var body: some View { TabView { TransactionListView(self.budget) - .sheet(isPresented: $isAddingTransaction, content: { - AddTransactionView(budgetId: self.budget.id) + .sheet(isPresented: $isAddingTransaction, + onDismiss: { + isAddingTransaction = false + }, + content: { + AddTransactionView(showSheet: self.$isAddingTransaction, budgetId: self.budget.id) .navigationBarTitle("add_transaction") }) .tabItem { diff --git a/Twigs/Transaction/AddTransactionView.swift b/Twigs/Transaction/AddTransactionView.swift index c685581..f9e1baa 100644 --- a/Twigs/Transaction/AddTransactionView.swift +++ b/Twigs/Transaction/AddTransactionView.swift @@ -10,7 +10,7 @@ import SwiftUI import Combine struct AddTransactionView: View { - @Environment(\.presentationMode) var presentationMode + @Binding var showSheet: Bool @EnvironmentObject var authDataStore: AuthenticationDataStore @EnvironmentObject var transactionDataStore: TransactionDataStore @State var title: String = "" @@ -29,7 +29,7 @@ struct AddTransactionView: View { var stateContent: AnyView { switch transactionDataStore.transaction { case .success(_): - self.presentationMode.wrappedValue.dismiss() + self.showSheet = false return AnyView(EmptyView()) case .failure(.loading): return AnyView(EmbeddedLoadingView()) @@ -52,7 +52,7 @@ struct AddTransactionView: View { stateContent .navigationBarItems( leading: Button("cancel") { - self.presentationMode.wrappedValue.dismiss() + self.showSheet = false }, trailing: Button("save") { let amount = Double(self.amount) ?? 0.0 @@ -71,6 +71,7 @@ struct AddTransactionView: View { } .onDisappear { _ = self.transactionDataStore.getTransactions(self.budgetId, categoryId: self.categoryId) + self.transactionDataStore.clearSelectedTransaction() self.title = "" self.description = "" self.date = Date() @@ -80,7 +81,8 @@ struct AddTransactionView: View { } } - init(budgetId: String, categoryId: String = "") { + init(showSheet: Binding, budgetId: String, categoryId: String = "") { + self._showSheet = showSheet self._budgetId = State(initialValue: budgetId) self._categoryId = State(initialValue: categoryId) } diff --git a/Twigs/Transaction/TransactionDataStore.swift b/Twigs/Transaction/TransactionDataStore.swift index b558f9c..5986362 100644 --- a/Twigs/Transaction/TransactionDataStore.swift +++ b/Twigs/Transaction/TransactionDataStore.swift @@ -14,17 +14,9 @@ class TransactionDataStore: ObservableObject { private var sumRequests: [String:AnyCancellable] = [:] @Published var transactions: [String:Result<[Transaction], NetworkError>] = ["": .failure(.loading)] - var transaction: Result = .failure(.unknown) { - didSet { - self.objectWillChange.send() - } - } + @Published var transaction: Result = .failure(.unknown) - var sums: [String:Result] = [:] { - didSet { - self.objectWillChange.send() - } - } + @Published var sums: [String:Result] = [:] func getTransactions(_ budgetId: String, categoryId: String? = nil, from: Date? = nil, count: Int? = nil, page: Int? = nil) -> String { let requestId = "\(budgetId)-\(categoryId ?? "all")" @@ -138,12 +130,15 @@ class TransactionDataStore: ObservableObject { return sumId } + func clearSelectedTransaction() { + self.transaction = .failure(.unknown) + } + func reset() { self.transaction = .failure(.unknown) self.transactions = ["": .failure(.loading)] } - - let objectWillChange = ObservableObjectPublisher() + private let transactionRepository: TransactionRepository init(_ transactionRepository: TransactionRepository) { self.transactionRepository = transactionRepository