Skip to content

Commit

Permalink
make DataManager as EnvironmentObject for ViewModels
Browse files Browse the repository at this point in the history
  • Loading branch information
guanlisheng committed Sep 26, 2024
1 parent 22e69ed commit 985e77b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
18 changes: 12 additions & 6 deletions MMEX/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ struct ContentView: View {
TabContentView(selectedTab: $selectedTab, isDocumentPickerPresented: $isDocumentPickerPresented, databaseURL: url)
}
} else {
// Use @StateObject to manage the lifecycle of InfotableViewModel
let infotableViewModel = InfotableViewModel(dataManager: dataManager)
// iPhone layout: Tabs at the bottom
TabView(selection: $selectedTab) {
// Latest Transactions Tab
NavigationView {
TransactionListView2(viewModel: InfotableViewModel(databaseURL: url)) // Summary and Edit feature
TransactionListView2(viewModel: infotableViewModel) // Summary and Edit feature
.navigationBarTitle("Latest Transactions", displayMode: .inline)
}
.tabItem {
Expand All @@ -46,7 +48,7 @@ struct ContentView: View {

// Insights module
NavigationView {
InsightsView(viewModel: InsightsViewModel(databaseURL: url))
InsightsView(viewModel: InsightsViewModel(dataManager: dataManager))
.navigationBarTitle("Reports and Insights", displayMode: .inline)
}
.tabItem {
Expand Down Expand Up @@ -79,7 +81,7 @@ struct ContentView: View {

// Settings Tab
NavigationView {
SettingsView(viewModel: InfotableViewModel(databaseURL: url)) // Payees, Accounts, Currency
SettingsView(viewModel: infotableViewModel) // Payees, Accounts, Currency
.navigationBarTitle("Settings", displayMode: .inline)
}
.tabItem {
Expand Down Expand Up @@ -196,17 +198,21 @@ struct SidebarView: View {
struct TabContentView: View {
@Binding var selectedTab: Int
@Binding var isDocumentPickerPresented: Bool
@EnvironmentObject var dataManager: DataManager // Access DataManager from environment

var databaseURL: URL

var body: some View {
// Use @StateObject to manage the lifecycle of InfotableViewModel
let infotableViewModel = InfotableViewModel(dataManager: dataManager)
// Here we ensure that there's no additional NavigationStack or NavigationView
Group {
switch selectedTab {
case 0:
TransactionListView2(viewModel: InfotableViewModel(databaseURL: databaseURL)) // Summary and Edit feature
TransactionListView2(viewModel: infotableViewModel) // Summary and Edit feature
.navigationBarTitle("Latest Transactions", displayMode: .inline)
case 1:
InsightsView(viewModel: InsightsViewModel(databaseURL: databaseURL))
InsightsView(viewModel: InsightsViewModel(dataManager: dataManager))
.navigationBarTitle("Reports and Insights", displayMode: .inline)
case 2:
TransactionAddView2(selectedTab: $selectedTab)
Expand All @@ -215,7 +221,7 @@ struct TabContentView: View {
ManagementView(isDocumentPickerPresented: $isDocumentPickerPresented)
.navigationBarTitle("Management", displayMode: .inline)
case 4:
SettingsView(viewModel: InfotableViewModel(databaseURL: databaseURL))
SettingsView(viewModel: infotableViewModel)
.navigationBarTitle("Settings", displayMode: .inline)
default:
EmptyView()
Expand Down
11 changes: 6 additions & 5 deletions MMEX/ViewModels/InfotableViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import SQLite
import Combine

class InfotableViewModel: ObservableObject {
let databaseURL: URL

// for the view to observe
@Published var baseCurrencyId: Int64 = 0
@Published var defaultAccountId: Int64 = 0
Expand All @@ -33,9 +31,8 @@ class InfotableViewModel: ObservableObject {
@Published var txns: [TransactionData] = []
@Published var txns_per_day: [String: [TransactionData]] = [:]

init(databaseURL: URL) {
self.databaseURL = databaseURL
self.dataManager = DataManager(databaseURL: databaseURL)
init(dataManager: DataManager) {
self.dataManager = dataManager

self.infotableRepo = self.dataManager.getInfotableRepository()
self.transactionRepo = self.dataManager.getTransactionRepository()
Expand Down Expand Up @@ -69,6 +66,10 @@ class InfotableViewModel: ObservableObject {
func getSchemaVersion() -> Int32 {
return self.dataManager.getSchemaVersion() ?? 0
}

func getDatabaseURL() -> URL {
return self.dataManager.databaseURL!
}

// Set up individual bindings for each @Published property
private func setupBindings() {
Expand Down
8 changes: 4 additions & 4 deletions MMEX/ViewModels/InsightsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
import Combine

class InsightsViewModel: ObservableObject {
let databaseURL: URL
private var dataManager: DataManager

// Published properties for the view to observe
@Published var stats: [TransactionData] = []
Expand All @@ -19,8 +19,8 @@ class InsightsViewModel: ObservableObject {

private var cancellables = Set<AnyCancellable>()

init(databaseURL: URL) {
self.databaseURL = databaseURL
init(dataManager: DataManager) {
self.dataManager = dataManager
self.startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date()) ?? Date()
self.endDate = Date()

Expand All @@ -37,7 +37,7 @@ class InsightsViewModel: ObservableObject {
}

func loadTransactions() {
let repository = DataManager(databaseURL: self.databaseURL).getTransactionRepository()
let repository = dataManager.getTransactionRepository()

// Fetch transactions asynchronously
DispatchQueue.global(qos: .background).async {
Expand Down
2 changes: 1 addition & 1 deletion MMEX/Views/InsightsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ struct InsightsView: View {
}

#Preview {
InsightsView(viewModel: InsightsViewModel(databaseURL: URL(string: "path/to/database")!))
InsightsView(viewModel: InsightsViewModel(dataManager: DataManager(databaseURL: URL(string: "path/to/database")!)))
}
4 changes: 2 additions & 2 deletions MMEX/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct SettingsView: View {
HStack {
Text("Database File")
Spacer()
// Text(viewModel.databaseURL.lastPathComponent)
Text(viewModel.getDatabaseURL().lastPathComponent)
}
HStack {
Text("Schema Version")
Expand Down Expand Up @@ -110,5 +110,5 @@ enum DefaultPayeeSetting: String, CaseIterable, Identifiable {
}

#Preview {
SettingsView(viewModel: InfotableViewModel(databaseURL: URL(string: "path/to/database")!))
SettingsView(viewModel: InfotableViewModel(dataManager: DataManager(databaseURL: URL(string: "path/to/database")!)))
}

0 comments on commit 985e77b

Please sign in to comment.