diff --git a/Twigs/Budget/BudgetListsView.swift b/Twigs/Budget/BudgetListsView.swift index a5d5415..cb92e2d 100644 --- a/Twigs/Budget/BudgetListsView.swift +++ b/Twigs/Budget/BudgetListsView.swift @@ -39,24 +39,19 @@ struct BudgetListsView: View { } struct BudgetListItemView: View { + @EnvironmentObject var budgetsDataStore: BudgetsDataStore var budget: Budget var body: some View { NavigationLink( - destination: TabbedBudgetView(budget) + budget.name, + tag: budget, + selection: $budgetsDataStore.budget, + destination: { + TabbedBudgetView(budget) .navigationBarTitle(budget.name) - ) { - VStack(alignment: .leading) { - Text(verbatim: budget.name) - .lineLimit(1) - if budget.description?.isEmpty == false { - Text(verbatim: budget.description!) - .font(.subheadline) - .foregroundColor(.secondary) - .lineLimit(1) - } } - } + ) } init (_ budget: Budget) { diff --git a/Twigs/Budget/BudgetsDataStore.swift b/Twigs/Budget/BudgetsDataStore.swift index 24211bd..8bd6983 100644 --- a/Twigs/Budget/BudgetsDataStore.swift +++ b/Twigs/Budget/BudgetsDataStore.swift @@ -11,14 +11,10 @@ import Combine class BudgetsDataStore: ObservableObject { private var currentRequest: AnyCancellable? = nil - var budgets: Result<[Budget], NetworkError> = .failure(.loading) { + @Published var budgets: Result<[Budget], NetworkError> = .failure(.loading) + @Published var budget: Budget? = nil { didSet { - self.objectWillChange.send() - } - } - var budget: Result = .failure(.loading) { - didSet { - self.objectWillChange.send() + UserDefaults.standard.set(budget?.id, forKey: LAST_BUDGET) } } @@ -47,34 +43,20 @@ class BudgetsDataStore: ObservableObject { return } }, receiveValue: { (budgets) in - self.budgets = .success(budgets) - }) - } - - func getBudget(_ id: String) { - self.budget = .failure(.loading) - - self.currentRequest = self.budgetRepository.getBudget(id) - .receive(on: DispatchQueue.main) - .sink(receiveCompletion: { (status) in - switch status { - case .finished: - self.currentRequest = nil - return - case .failure(let error): - self.budget = .failure(error) - return + self.budgets = .success(budgets.sorted(by: { $0.name < $1.name })) + if let id = UserDefaults.standard.string(forKey: LAST_BUDGET) { + if let budget = budgets.first(where: { $0.id == id }) { + self.budget = budget + } } - }, receiveValue: { (budget) in - self.budget = .success(budget) }) } - + init(_ budgetRepository: BudgetRepository) { self.budgetRepository = budgetRepository } private let budgetRepository: BudgetRepository - // Needed since the default implementation is currently broken - let objectWillChange = ObservableObjectPublisher() } + +private let LAST_BUDGET = "LAST_BUDGET" diff --git a/Twigs/TabbedBudgetView.swift b/Twigs/TabbedBudgetView.swift index c44f8dd..7f5052b 100644 --- a/Twigs/TabbedBudgetView.swift +++ b/Twigs/TabbedBudgetView.swift @@ -9,6 +9,7 @@ import SwiftUI struct TabbedBudgetView: View { + @EnvironmentObject var budgetDataStore: BudgetsDataStore @EnvironmentObject var categoryDataStore: CategoryDataStore let budget: Budget @State var isAddingTransaction = false @@ -18,7 +19,7 @@ struct TabbedBudgetView: View { TabView { BudgetDetailsView(budget: self.budget) .tabItem { - Image(systemName: "briefcase.circle.fill") + Image(systemName: "chart.line.uptrend.xyaxis.circle.fill") Text("Overview") } .tag(0) diff --git a/Twigs/Transaction/TransactionDetailsView.swift b/Twigs/Transaction/TransactionDetailsView.swift index c3cab3b..ef4ce32 100644 --- a/Twigs/Transaction/TransactionDetailsView.swift +++ b/Twigs/Transaction/TransactionDetailsView.swift @@ -38,7 +38,7 @@ struct TransactionDetailsView: View { Spacer().frame(height: 20.0) LabeledField(label: "notes", value: transaction.description, showDivider: true) CategoryLineItem(transaction.categoryId) - BudgetLineItem(transaction.budgetId) + BudgetLineItem() UserLineItem(transaction.createdBy) }.padding() } @@ -116,25 +116,10 @@ struct CategoryLineItem: View { } struct BudgetLineItem: View { - var body: some View { - stateContent.onAppear { - budgetDataStore.getBudget(budgetId) - } - } - - var stateContent: AnyView { - switch budgetDataStore.budget { - case .success(let budget): - return AnyView(LabeledField(label: "budget", value: budget.name, showDivider: true)) - default: - return AnyView(LabeledField(label: "budget", value: "", showDivider: true)) - } - } - @EnvironmentObject var budgetDataStore: BudgetsDataStore - let budgetId: String - init(_ budgetId: String) { - self.budgetId = budgetId + + var body: some View { + LabeledField(label: "budget", value: budgetDataStore.budget?.name ?? "", showDivider: true) } }