diff --git a/Twigs/Budget/BudgetDetailsView.swift b/Twigs/Budget/BudgetDetailsView.swift index 9d013f0..c2bc2c1 100644 --- a/Twigs/Budget/BudgetDetailsView.swift +++ b/Twigs/Budget/BudgetDetailsView.swift @@ -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) } } diff --git a/Twigs/TabbedBudgetView.swift b/Twigs/TabbedBudgetView.swift index f44d2b4..c44f8dd 100644 --- a/Twigs/TabbedBudgetView.swift +++ b/Twigs/TabbedBudgetView.swift @@ -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) { diff --git a/Twigs/Transaction/TransactionDataStore.swift b/Twigs/Transaction/TransactionDataStore.swift index 5986362..69dca16 100644 --- a/Twigs/Transaction/TransactionDataStore.swift +++ b/Twigs/Transaction/TransactionDataStore.swift @@ -16,7 +16,7 @@ class TransactionDataStore: ObservableObject { @Published var transaction: Result = .failure(.unknown) - @Published var sums: [String:Result] = [:] + @Published var sums: [String:Result] = ["": .failure(.loading)] func getTransactions(_ budgetId: String, categoryId: String? = nil, from: Date? = nil, count: Int? = nil, page: Int? = nil) -> String { let requestId = "\(budgetId)-\(categoryId ?? "all")"