Remember previously opened budget to reopen upon launch
This commit is contained in:
parent
980e0baa1f
commit
7b99811cd9
4 changed files with 24 additions and 61 deletions
|
@ -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) {
|
||||
|
|
|
@ -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<Budget, NetworkError> = .failure(.loading) {
|
||||
didSet {
|
||||
self.objectWillChange.send()
|
||||
UserDefaults.standard.set(budget?.id, forKey: LAST_BUDGET)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,26 +43,12 @@ class BudgetsDataStore: ObservableObject {
|
|||
return
|
||||
}
|
||||
}, receiveValue: { (budgets) in
|
||||
self.budgets = .success(budgets)
|
||||
})
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}, receiveValue: { (budget) in
|
||||
self.budget = .success(budget)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -75,6 +57,6 @@ class BudgetsDataStore: ObservableObject {
|
|||
}
|
||||
|
||||
private let budgetRepository: BudgetRepository
|
||||
// Needed since the default implementation is currently broken
|
||||
let objectWillChange = ObservableObjectPublisher()
|
||||
}
|
||||
|
||||
private let LAST_BUDGET = "LAST_BUDGET"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue