Add basic budget details tab

This commit is contained in:
William Brawner 2021-10-09 20:27:54 -06:00
parent e9fb484127
commit 980e0baa1f
3 changed files with 32 additions and 31 deletions

View file

@ -9,25 +9,35 @@
import SwiftUI
struct BudgetDetailsView: View {
@EnvironmentObject var transactionDataStore: TransactionDataStore
@State var sumId: String = ""
let budget: Budget
@ViewBuilder
var body: some View {
ScrollView {
VStack {
// ProgressView(value: .constant(50.0, maxValue: 100))
// CategoryListView(budget)
switch transactionDataStore.sums[sumId] {
case .failure(.loading):
ActivityIndicator(isAnimating: .constant(true), style: .large).onAppear {
if self.sumId == "" {
self.sumId = transactionDataStore.sum(budgetId: self.budget.id)
}
}
case .success(let sum):
Text("Current Balance:")
Text(verbatim: sum.balance.toCurrencyString())
.foregroundColor(sum.balance < 0 ? .red : .green)
default:
Text("An error has ocurred")
}
}
}
}
let dataStoreProvider: DataStoreProvider
let budget: Budget
init(_ dataStoreProvider: DataStoreProvider, budget: Budget) {
self.dataStoreProvider = dataStoreProvider
self.budget = budget
}
}
struct BudgetDetailsView_Previews: PreviewProvider {
static var previews: some View {
BudgetDetailsView(MockDataStoreProvider(), budget: MockBudgetRepository.budget)
BudgetDetailsView(budget: MockBudgetRepository.budget)
}
}

View file

@ -12,11 +12,21 @@ struct TabbedBudgetView: View {
@EnvironmentObject var categoryDataStore: CategoryDataStore
let budget: Budget
@State var isAddingTransaction = false
@State var selectedTab: Int = 0
@State var categoryRequestId: String = ""
var body: some View {
TabView {
BudgetDetailsView(budget: self.budget)
.tabItem {
Image(systemName: "briefcase.circle.fill")
Text("Overview")
}
.tag(0)
.onAppear {
if categoryRequestId == "" {
categoryRequestId = categoryDataStore.getCategories(budgetId: budget.id, archived: false)
}
}
TransactionListView(self.budget)
.sheet(isPresented: $isAddingTransaction,
onDismiss: {
@ -30,32 +40,17 @@ struct TabbedBudgetView: View {
Image(systemName: "dollarsign.circle.fill")
Text("transactions")
}
.tag(0)
.onAppear {
selectedTab = 0
if categoryRequestId == "" {
categoryRequestId = categoryDataStore.getCategories(budgetId: budget.id, archived: false)
}
}
// TODO: Figure out why this is breaking when requestId is set from inside CategoryListView
CategoryListView(self.budget, requestId: categoryRequestId).tabItem {
Image(systemName: "chart.pie.fill")
Text("categories")
}
.tag(1)
.onAppear {
selectedTab = 1
}
ProfileView().tabItem {
Image(systemName: "person.circle.fill")
Text("profile")
}
.tag(2)
.onAppear {
selectedTab = 2
}
}.navigationBarItems(
trailing: HStack {
NavigationLink(destination: EmptyView()) {
@ -69,10 +64,6 @@ struct TabbedBudgetView: View {
}
}
)
.onAppear {
// Prefetch categories to avoid tab switching bug
_ = categoryDataStore.getCategories(budgetId: budget.id)
}
}
init (_ budget: Budget) {

View file

@ -16,7 +16,7 @@ class TransactionDataStore: ObservableObject {
@Published var transaction: Result<Transaction, NetworkError> = .failure(.unknown)
@Published var sums: [String:Result<BalanceResponse, NetworkError>] = [:]
@Published var sums: [String:Result<BalanceResponse, NetworkError>] = ["": .failure(.loading)]
func getTransactions(_ budgetId: String, categoryId: String? = nil, from: Date? = nil, count: Int? = nil, page: Int? = nil) -> String {
let requestId = "\(budgetId)-\(categoryId ?? "all")"