Implement budget editing

This commit is contained in:
William Brawner 2022-05-18 19:44:33 -06:00
parent 191197a037
commit 3d06b61ddc
4 changed files with 44 additions and 17 deletions

View file

@ -35,6 +35,26 @@ struct BudgetDetailsView: View {
.refreshable {
await dataStore.loadOverview(self.budget)
}
.navigationBarItems(trailing: Button(action: {
dataStore.editBudget()
}, label: { Text("edit") }))
.sheet(
isPresented: $dataStore.editingBudget,
onDismiss: { Task {
await dataStore.cancelEditBudget()
}},
content: {
NavigationView {
BudgetFormView(budget)
.navigationBarItems(leading: Button(action: { Task {
await dataStore.cancelEditBudget()
}}, label: {
Text("cancel")
})
.navigationTitle("edit_budget"))
}
}
)
}
}
}

View file

@ -11,22 +11,14 @@ import TwigsCore
struct BudgetFormView: View {
@EnvironmentObject var dataStore: DataStore
@State private var title = ""
@State private var description = ""
@State private var title: String
@State private var description: String
@State private var showingAlert = false
private var budgetId: String {
get {
if case let .editing(budget) = dataStore.budget {
return budget.id
} else {
return ""
}
}
}
private var budgetId: String
@ViewBuilder
var stateContent: some View {
switch dataStore.category {
switch dataStore.budget {
case .success(_):
EmptyView()
case .saving(_):
@ -68,6 +60,12 @@ struct BudgetFormView: View {
}
})
}
init(_ budget: Budget? = nil) {
self.budgetId = budget?.id ?? ""
self.title = budget?.name ?? ""
self.description = budget?.description ?? ""
}
}
struct BudgetFormView_Previews: PreviewProvider {

View file

@ -22,6 +22,10 @@ class DataStore : ObservableObject {
if case let .success(budget) = self.budget {
UserDefaults.standard.set(budget.id, forKey: LAST_BUDGET)
self.showBudgetSelection = false
self.editingBudget = false
}
if case .editing(_) = self.budget {
self.editingBudget = true
}
}
}
@ -63,7 +67,6 @@ class DataStore : ObservableObject {
func newBudget() {
self.budget = .editing(Budget(id: "", name: "", description: "", currencyCode: ""))
self.editingBudget = true
}
func save(_ budget: Budget) async {
@ -150,7 +153,6 @@ class DataStore : ObservableObject {
}
func selectBudget(_ budget: Budget?) async {
self.editingBudget = false
if let budget = budget {
self.budget = .success(budget)
await loadOverview(budget)
@ -164,6 +166,13 @@ class DataStore : ObservableObject {
self.recurringTransactions = .empty
}
}
func editBudget() {
guard case let .success(budget) = self.budget else {
return
}
self.budget = .editing(budget)
}
@Published var categories: AsyncData<[TwigsCore.Category]> = .empty
@Published var category: AsyncData<TwigsCore.Category> = .empty {

View file

@ -16,7 +16,8 @@ struct TabbedBudgetView: View {
@ViewBuilder
var mainView: some View {
if case let .success(budget) = dataStore.budget {
switch self.dataStore.budget {
case .success(let budget), .editing(let budget), .saving(let budget):
TabView(selection: $tabSelection) {
NavigationView {
BudgetDetailsView(budget: budget)
@ -66,7 +67,7 @@ struct TabbedBudgetView: View {
.tag(3)
.keyboardShortcut("4")
}
} else {
default:
ActivityIndicator(isAnimating: .constant(true), style: .large)
}
}
@ -78,7 +79,6 @@ struct TabbedBudgetView: View {
.environmentObject(dataStore)
.interactiveDismissDisabled(true)
})
.sheet(isPresented: $dataStore.showBudgetSelection,
content: {
NavigationView {