From 2cffa9c83cd3abbaa32e8ede751026cadd9c839d Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Thu, 12 May 2022 18:49:22 -0600 Subject: [PATCH] Fix issues with TransactionFormSheet --- Twigs/Transaction/TransactionDataStore.swift | 17 +++- .../Transaction/TransactionDetailsView.swift | 1 + Twigs/Transaction/TransactionFormSheet.swift | 92 +++++++++++-------- Twigs/Transaction/TransactionListView.swift | 1 - Twigs/en.lproj/Localizable.strings | 1 + Twigs/es.lproj/Localizable.strings | 1 + 6 files changed, 72 insertions(+), 41 deletions(-) diff --git a/Twigs/Transaction/TransactionDataStore.swift b/Twigs/Transaction/TransactionDataStore.swift index c6c42cb..13b1155 100644 --- a/Twigs/Transaction/TransactionDataStore.swift +++ b/Twigs/Transaction/TransactionDataStore.swift @@ -24,8 +24,22 @@ class TransactionDataStore: ObservableObject { } } @Published var selectedTransaction: Transaction? = nil - + private var budgetId: String = "" + private var categoryId: String? = nil + private var from: Date? = nil + private var count: Int? = nil + private var page: Int? = nil + func getTransactions(_ budgetId: String, categoryId: String? = nil, from: Date? = nil, count: Int? = nil, page: Int? = nil) async { + self.budgetId = budgetId + self.categoryId = categoryId + self.from = from + self.count = count + self.page = page + await self.getTransactions() + } + + func getTransactions() async { self.transactions = .loading do { var categoryIds: [String] = [] @@ -57,6 +71,7 @@ class TransactionDataStore: ObservableObject { savedTransaction = try await self.transactionRepository.createTransaction(transaction) } self.transaction = .success(savedTransaction) + await getTransactions() } catch { self.transaction = .error(error, transaction) } diff --git a/Twigs/Transaction/TransactionDetailsView.swift b/Twigs/Transaction/TransactionDetailsView.swift index 3d1f612..a506a95 100644 --- a/Twigs/Transaction/TransactionDetailsView.swift +++ b/Twigs/Transaction/TransactionDetailsView.swift @@ -8,6 +8,7 @@ import SwiftUI import TwigsCore +import XCTest struct TransactionDetailsView: View { @EnvironmentObject var apiService: TwigsApiService diff --git a/Twigs/Transaction/TransactionFormSheet.swift b/Twigs/Transaction/TransactionFormSheet.swift index c169e8d..7a59600 100644 --- a/Twigs/Transaction/TransactionFormSheet.swift +++ b/Twigs/Transaction/TransactionFormSheet.swift @@ -16,54 +16,68 @@ struct TransactionFormSheet: View { @ViewBuilder var body: some View { - switch self.transactionDataStore.transaction { - case .loading: - EmbeddedLoadingView() - default: - Form { - TextField(LocalizedStringKey("prompt_name"), text: $transactionForm.title) - .textInputAutocapitalization(.words) - TextField(LocalizedStringKey("prompt_description"), text: $transactionForm.description) - .textInputAutocapitalization(.sentences) - DatePicker(selection: $transactionForm.date, label: { Text(LocalizedStringKey("prompt_date")) }) - TextField(LocalizedStringKey("prompt_amount"), text: $transactionForm.amount) - .keyboardType(.decimalPad) - Picker(LocalizedStringKey("prompt_type"), selection: $transactionForm.type) { - ForEach(TransactionType.allCases) { type in - Text(type.localizedKey) + NavigationView { + switch self.transactionDataStore.transaction { + case .loading: + EmbeddedLoadingView() + default: + Form { + TextField(LocalizedStringKey("prompt_name"), text: $transactionForm.title) + .textInputAutocapitalization(.words) + TextField(LocalizedStringKey("prompt_description"), text: $transactionForm.description) + .textInputAutocapitalization(.sentences) + DatePicker(selection: $transactionForm.date, label: { Text(LocalizedStringKey("prompt_date")) }) + TextField(LocalizedStringKey("prompt_amount"), text: $transactionForm.amount) + .keyboardType(.decimalPad) + Picker(LocalizedStringKey("prompt_type"), selection: $transactionForm.type) { + ForEach(TransactionType.allCases) { type in + Text(type.localizedKey) + } } - } - BudgetPicker() - CategoryPicker() - if transactionForm.showDelete { - Button(action: { - self.showingAlert = true - }) { - Text(LocalizedStringKey("delete")) - .foregroundColor(.red) + BudgetPicker() + CategoryPicker() + if transactionForm.showDelete { + Button(action: { + self.showingAlert = true + }) { + Text(LocalizedStringKey("delete")) + .foregroundColor(.red) + } + .alert(isPresented:$showingAlert) { + Alert( + title: Text(LocalizedStringKey("confirm_delete")), + message: Text(LocalizedStringKey("cannot_undo")), + primaryButton: .destructive( + Text(LocalizedStringKey("delete")), + action: { Task { await transactionForm.delete() }} + ), + secondaryButton: .cancel() + ) + } + } else { + EmptyView() } - .alert(isPresented:$showingAlert) { - Alert( - title: Text(LocalizedStringKey("confirm_delete")), - message: Text(LocalizedStringKey("cannot_undo")), - primaryButton: .destructive( - Text(LocalizedStringKey("delete")), - action: { Task { await transactionForm.delete() }} - ), - secondaryButton: .cancel() - ) + }.environmentObject(transactionForm) + .task { + await transactionForm.load() } - } else { - EmptyView() - } - }.environmentObject(transactionForm) + .navigationTitle(transactionForm.transactionId.isEmpty ? "add_transaction" : "edit_transaction") + .navigationBarItems( + leading: Button("cancel", action: { transactionForm.transactionList.cancelEdit() }), + trailing: Button("save", action: { + Task { + await transactionForm.save() + } + }) + ) + } } } } struct BudgetPicker: View { @EnvironmentObject var transactionForm: TransactionForm - + @ViewBuilder var body: some View { if case let .success(budgets) = self.transactionForm.budgets { diff --git a/Twigs/Transaction/TransactionListView.swift b/Twigs/Transaction/TransactionListView.swift index 220b72b..cfa0c68 100644 --- a/Twigs/Transaction/TransactionListView.swift +++ b/Twigs/Transaction/TransactionListView.swift @@ -81,7 +81,6 @@ struct TransactionListView: View where Content: View { categoryId: self.category?.id, transaction: nil )) - .navigationBarTitle("add_transaction") }) .navigationBarItems( trailing: HStack { diff --git a/Twigs/en.lproj/Localizable.strings b/Twigs/en.lproj/Localizable.strings index 11f11c5..128cd37 100644 --- a/Twigs/en.lproj/Localizable.strings +++ b/Twigs/en.lproj/Localizable.strings @@ -47,6 +47,7 @@ // MARK: Transactions "transactions" = "Transactions"; "add_transaction" = "Add Transaction"; +"edit_transaction" = "Edit Transaction"; "notes" = "Notes"; "registered_by" = "Registered by"; "no_transactions" = "No transactions found for the given criteria"; diff --git a/Twigs/es.lproj/Localizable.strings b/Twigs/es.lproj/Localizable.strings index a690c26..2740da4 100644 --- a/Twigs/es.lproj/Localizable.strings +++ b/Twigs/es.lproj/Localizable.strings @@ -47,6 +47,7 @@ // MARK: Transactions "transactions" = "Transacciones"; "add_transaction" = "Agregar Transacción"; +"edit_transaction" = "Editar Transacción"; "notes" = "Notas"; "registered_by" = "Registrado por"; "no_transactions" = "No se encontraron transacciones con el criterio dado";