Implement category creation

This commit is contained in:
William Brawner 2022-05-17 21:40:08 -06:00
parent aaa5c08fb5
commit 191197a037
3 changed files with 63 additions and 19 deletions

View file

@ -15,7 +15,6 @@ struct CategoryDetailsView: View {
@EnvironmentObject var apiService: TwigsApiService @EnvironmentObject var apiService: TwigsApiService
let budget: Budget let budget: Budget
@State var sum: Int? = 0 @State var sum: Int? = 0
@State var editingCategory: Bool = false
var spent: Int { var spent: Int {
get { get {
if case let .success(sum) = categoryDataStore.sum { if case let .success(sum) = categoryDataStore.sum {
@ -49,12 +48,14 @@ struct CategoryDetailsView: View {
await categoryDataStore.sum(categoryId: category.id) await categoryDataStore.sum(categoryId: category.id)
} }
.navigationBarItems(trailing: Button(action: { .navigationBarItems(trailing: Button(action: {
self.editingCategory = true Task {
await dataStore.edit(category)
}
}) { }) {
Text("edit") Text("edit")
}) })
.sheet(isPresented: self.$editingCategory, onDismiss: { .sheet(isPresented: self.$dataStore.editingCategory, onDismiss: {
self.editingCategory = false self.dataStore.cancelEditCategory()
}, content: { }, content: {
CategoryFormSheet(categoryForm: CategoryForm( CategoryFormSheet(categoryForm: CategoryForm(
category: category, category: category,

View file

@ -13,29 +13,60 @@ import TwigsCore
struct CategoryListView: View { struct CategoryListView: View {
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
@State var requestId: String = "" @State var requestId: String = ""
private var budgetId: String {
get {
if case let .success(budget) = dataStore.budget {
return budget.id
} else {
return ""
}
}
}
@ViewBuilder @ViewBuilder
var body: some View { var body: some View {
InlineLoadingView( List {
data: $dataStore.categories, InlineLoadingView(
action: { await self.dataStore.getCategories(budgetId: budget.id, expense: nil, archived: nil, count: nil, page: nil) }, data: $dataStore.categories,
errorTextLocalizedStringKey: "Failed to load categories" action: { await self.dataStore.getCategories(budgetId: budget.id, expense: nil, archived: nil, count: nil, page: nil) },
) { categories in errorTextLocalizedStringKey: "Failed to load categories"
List { ) { categories in
Section { if categories.isEmpty {
ForEach(categories.filter { !$0.archived }) { category in Text("no_categories")
CategoryListItemView(CategoryDataStore(dataStore.apiService), budget: budget, category: category) } else {
} Section {
} ForEach(categories.filter { !$0.archived }) { category in
if categories.contains(where: { $0.archived }) {
Section("Archived") {
ForEach(categories.filter { $0.archived }) { category in
CategoryListItemView(CategoryDataStore(dataStore.apiService), budget: budget, category: category) CategoryListItemView(CategoryDataStore(dataStore.apiService), budget: budget, category: category)
} }
} }
if categories.contains(where: { $0.archived }) {
Section("Archived") {
ForEach(categories.filter { $0.archived }) { category in
CategoryListItemView(CategoryDataStore(dataStore.apiService), budget: budget, category: category)
}
}
}
} }
} }
}.refreshable {
await dataStore.getCategories()
} }
.navigationBarItems(trailing: Button(action: {
Task {
await dataStore.edit(TwigsCore.Category(budgetId: budgetId))
}
}, label: {
Image(systemName: "plus").padding()
}))
.sheet(isPresented: self.$dataStore.editingCategory, onDismiss: {
self.dataStore.cancelEditCategory()
}, content: {
CategoryFormSheet(categoryForm: CategoryForm(
category: TwigsCore.Category(budgetId: budgetId),
dataStore: dataStore,
budgetId: budgetId
))
})
} }
private let budget: Budget private let budget: Budget

View file

@ -28,6 +28,7 @@ class DataStore : ObservableObject {
@Published var overview: AsyncData<BudgetOverview> = .empty @Published var overview: AsyncData<BudgetOverview> = .empty
@Published var showBudgetSelection: Bool = true @Published var showBudgetSelection: Bool = true
@Published var editingBudget: Bool = false @Published var editingBudget: Bool = false
@Published var editingCategory: Bool = false
init( init(
_ apiService: TwigsApiService _ apiService: TwigsApiService
@ -176,7 +177,14 @@ class DataStore : ObservableObject {
} }
@Published var selectedCategory: TwigsCore.Category? = nil @Published var selectedCategory: TwigsCore.Category? = nil
func getCategories(budgetId: String? = nil, expense: Bool? = nil, archived: Bool? = false, count: Int? = nil, page: Int? = nil) async { func getCategories() async {
guard case let .success(budget) = self.budget else {
return
}
await self.getCategories(budgetId: budget.id)
}
func getCategories(budgetId: String, expense: Bool? = nil, archived: Bool? = false, count: Int? = nil, page: Int? = nil) async {
self.categories = .loading self.categories = .loading
do { do {
let categories = try await apiService.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page) let categories = try await apiService.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page)
@ -196,6 +204,7 @@ class DataStore : ObservableObject {
savedCategory = try await self.apiService.createCategory(category) savedCategory = try await self.apiService.createCategory(category)
} }
self.category = .success(savedCategory) self.category = .success(savedCategory)
self.editingCategory = false
if case let .success(categories) = self.categories { if case let .success(categories) = self.categories {
var updatedCategories = categories.filter(withoutId: category.id) var updatedCategories = categories.filter(withoutId: category.id)
updatedCategories.append(savedCategory) updatedCategories.append(savedCategory)
@ -211,6 +220,7 @@ class DataStore : ObservableObject {
do { do {
try await self.apiService.deleteCategory(category.id) try await self.apiService.deleteCategory(category.id)
self.category = .empty self.category = .empty
self.editingCategory = false
if case let .success(categories) = self.categories { if case let .success(categories) = self.categories {
self.categories = .success(categories.filter(withoutId: category.id)) self.categories = .success(categories.filter(withoutId: category.id))
} }
@ -220,10 +230,12 @@ class DataStore : ObservableObject {
} }
func edit(_ category: TwigsCore.Category) async { func edit(_ category: TwigsCore.Category) async {
self.editingCategory = true
self.category = .editing(category) self.category = .editing(category)
} }
func cancelEditCategory() { func cancelEditCategory() {
self.editingCategory = false
if let category = self.selectedCategory { if let category = self.selectedCategory {
self.category = .success(category) self.category = .success(category)
} else { } else {