Fix not being able to add multiple transactions

This commit is contained in:
William Brawner 2021-10-09 20:03:01 -06:00
parent 85f462cd07
commit e9fb484127
3 changed files with 19 additions and 18 deletions

View file

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

View file

@ -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<Bool>, budgetId: String, categoryId: String = "") {
self._showSheet = showSheet
self._budgetId = State(initialValue: budgetId)
self._categoryId = State(initialValue: categoryId)
}

View file

@ -14,17 +14,9 @@ class TransactionDataStore: ObservableObject {
private var sumRequests: [String:AnyCancellable] = [:]
@Published var transactions: [String:Result<[Transaction], NetworkError>] = ["": .failure(.loading)]
var transaction: Result<Transaction, NetworkError> = .failure(.unknown) {
didSet {
self.objectWillChange.send()
}
}
@Published var transaction: Result<Transaction, NetworkError> = .failure(.unknown)
var sums: [String:Result<BalanceResponse, NetworkError>] = [:] {
didSet {
self.objectWillChange.send()
}
}
@Published var sums: [String:Result<BalanceResponse, NetworkError>] = [:]
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