Implement category creation
This commit is contained in:
parent
aaa5c08fb5
commit
191197a037
3 changed files with 63 additions and 19 deletions
|
@ -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,
|
||||||
|
|
|
@ -13,15 +13,27 @@ 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 {
|
||||||
|
List {
|
||||||
InlineLoadingView(
|
InlineLoadingView(
|
||||||
data: $dataStore.categories,
|
data: $dataStore.categories,
|
||||||
action: { await self.dataStore.getCategories(budgetId: budget.id, expense: nil, archived: nil, count: nil, page: nil) },
|
action: { await self.dataStore.getCategories(budgetId: budget.id, expense: nil, archived: nil, count: nil, page: nil) },
|
||||||
errorTextLocalizedStringKey: "Failed to load categories"
|
errorTextLocalizedStringKey: "Failed to load categories"
|
||||||
) { categories in
|
) { categories in
|
||||||
List {
|
if categories.isEmpty {
|
||||||
|
Text("no_categories")
|
||||||
|
} else {
|
||||||
Section {
|
Section {
|
||||||
ForEach(categories.filter { !$0.archived }) { category in
|
ForEach(categories.filter { !$0.archived }) { category in
|
||||||
CategoryListItemView(CategoryDataStore(dataStore.apiService), budget: budget, category: category)
|
CategoryListItemView(CategoryDataStore(dataStore.apiService), budget: budget, category: category)
|
||||||
|
@ -36,6 +48,25 @@ struct CategoryListView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}.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
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue