Skip to content

Commit

Permalink
Currency basic view
Browse files Browse the repository at this point in the history
  • Loading branch information
guanlisheng committed Sep 17, 2024
1 parent 9e46417 commit 2cb5050
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 15 deletions.
16 changes: 16 additions & 0 deletions MMEX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
A3462F642C9426F500F79145 /* InsightsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3462F632C9426F500F79145 /* InsightsViewModel.swift */; };
A3462F662C94854800F79145 /* ExportableEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3462F652C94854800F79145 /* ExportableEntity.swift */; };
A3462F6A2C948CDB00F79145 /* ExportableEntityDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3462F692C948CDB00F79145 /* ExportableEntityDocument.swift */; };
A39B1B322C99A084003E5562 /* CurrencyAddView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A39B1B312C99A084003E5562 /* CurrencyAddView.swift */; };
A39B1B342C99A0A8003E5562 /* CurrencyDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A39B1B332C99A0A8003E5562 /* CurrencyDetailView.swift */; };
A39B1B362C99A0C2003E5562 /* CurrencyEditView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A39B1B352C99A0C2003E5562 /* CurrencyEditView.swift */; };
A39B1B382C99A0D8003E5562 /* CurrencyListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A39B1B372C99A0D8003E5562 /* CurrencyListView.swift */; };
A3B374C52C96A23E00943FDB /* SQLite in Frameworks */ = {isa = PBXBuildFile; productRef = A3B374C42C96A23E00943FDB /* SQLite */; };
A3B374C72C96A27800943FDB /* SQLite in Frameworks */ = {isa = PBXBuildFile; productRef = A3B374C62C96A27800943FDB /* SQLite */; };
A3C1422B2C89751500D3CEC0 /* MMEXApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3C1422A2C89751500D3CEC0 /* MMEXApp.swift */; };
Expand Down Expand Up @@ -60,6 +64,10 @@
A3462F632C9426F500F79145 /* InsightsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InsightsViewModel.swift; sourceTree = "<group>"; };
A3462F652C94854800F79145 /* ExportableEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportableEntity.swift; sourceTree = "<group>"; };
A3462F692C948CDB00F79145 /* ExportableEntityDocument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportableEntityDocument.swift; sourceTree = "<group>"; };
A39B1B312C99A084003E5562 /* CurrencyAddView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyAddView.swift; sourceTree = "<group>"; };
A39B1B332C99A0A8003E5562 /* CurrencyDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyDetailView.swift; sourceTree = "<group>"; };
A39B1B352C99A0C2003E5562 /* CurrencyEditView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyEditView.swift; sourceTree = "<group>"; };
A39B1B372C99A0D8003E5562 /* CurrencyListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyListView.swift; sourceTree = "<group>"; };
A3C142272C89751500D3CEC0 /* MMEX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MMEX.app; sourceTree = BUILT_PRODUCTS_DIR; };
A3C1422A2C89751500D3CEC0 /* MMEXApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMEXApp.swift; sourceTree = "<group>"; };
A3C1422C2C89751500D3CEC0 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -190,6 +198,10 @@
A3C142462C89CB4200D3CEC0 /* AccountDetailView.swift */,
A3C142622C8ED8C000D3CEC0 /* AccountAddView.swift */,
A3C142642C8ED8EA00D3CEC0 /* AccountEditView.swift */,
A39B1B312C99A084003E5562 /* CurrencyAddView.swift */,
A39B1B332C99A0A8003E5562 /* CurrencyDetailView.swift */,
A39B1B352C99A0C2003E5562 /* CurrencyEditView.swift */,
A39B1B372C99A0D8003E5562 /* CurrencyListView.swift */,
A3363EE82C9326A1004696C7 /* CategoryListView.swift */,
A3363EE22C9323A5004696C7 /* CategoryEditView.swift */,
A3363EE42C9323D2004696C7 /* CategoryDetailView.swift */,
Expand Down Expand Up @@ -293,14 +305,17 @@
files = (
A3C1424C2C8AB52600D3CEC0 /* Currency.swift in Sources */,
A3C142612C8E9DBF00D3CEC0 /* PayeeAddView.swift in Sources */,
A39B1B382C99A0D8003E5562 /* CurrencyListView.swift in Sources */,
A3C1423E2C89760600D3CEC0 /* AccountListView.swift in Sources */,
A3C1425F2C8DE70300D3CEC0 /* DatabaseManager.swift in Sources */,
A3C1424E2C8B335900D3CEC0 /* Payee.swift in Sources */,
A39B1B322C99A084003E5562 /* CurrencyAddView.swift in Sources */,
A3C142502C8B366400D3CEC0 /* PayeeListView.swift in Sources */,
A3C142672C8F2AF500D3CEC0 /* Transaction.swift in Sources */,
A3462F642C9426F500F79145 /* InsightsViewModel.swift in Sources */,
A3C142AC2C909C1C00D3CEC0 /* TransactionEditView.swift in Sources */,
A3C142962C8FE15E00D3CEC0 /* TransactionRepository.swift in Sources */,
A39B1B342C99A0A8003E5562 /* CurrencyDetailView.swift in Sources */,
A3C1422D2C89751500D3CEC0 /* ContentView.swift in Sources */,
A3C142AA2C90721800D3CEC0 /* TransactionListView2.swift in Sources */,
A3C142A62C90417700D3CEC0 /* TransactionAddView2.swift in Sources */,
Expand All @@ -310,6 +325,7 @@
A3C1429E2C8FFCF100D3CEC0 /* TransactionAddView.swift in Sources */,
A3C142922C8FE11700D3CEC0 /* AccountRepository.swift in Sources */,
A3C1422B2C89751500D3CEC0 /* MMEXApp.swift in Sources */,
A39B1B362C99A0C2003E5562 /* CurrencyEditView.swift in Sources */,
A3363EE72C93249D004696C7 /* CategoryAddView.swift in Sources */,
A3C142522C8B37E700D3CEC0 /* PayeeDetailView.swift in Sources */,
A3C142442C89C8FA00D3CEC0 /* Account.swift in Sources */,
Expand Down
7 changes: 3 additions & 4 deletions MMEX/Views/AccountListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import SwiftUI

struct AccountListView: View {
let databaseURL: URL
@State private var accounts: [Account] = []
@State private var currencies: [Currency] = []
@State private var accounts_by_type: [String:[Account]] = [:]
@State private var newAccount = Account.empty
Expand Down Expand Up @@ -108,8 +107,7 @@ struct AccountListView: View {
DispatchQueue.global(qos: .background).async {
let loadedAccounts = repository.loadAccountsWithCurrency()
DispatchQueue.main.async {
self.accounts = loadedAccounts
self.accounts_by_type = Dictionary(grouping: accounts) { account in
self.accounts_by_type = Dictionary(grouping: loadedAccounts) { account in
account.type
}
self.initializeExpandedSections() // Initialize expanded states
Expand All @@ -131,7 +129,8 @@ struct AccountListView: View {

func addAccount(account: inout Account) {
if repository.addAccount(account: &account) {
self.accounts.append(account)
// self.accounts.append(account)
self.loadAccounts()
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions MMEX/Views/CurrencyAddView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// CurrencyAddView.swift
// MMEX
//
// Created by Lisheng Guan on 2024/9/17.
//

import SwiftUI

struct CurrencyAddView: View {
@Binding var newCurrency: Currency
@Binding var isPresentingCurrencyAddView: Bool

var onSave: (inout Currency) -> Void

var body: some View {
NavigationStack {
CurrencyEditView(currency: $newCurrency)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Dismiss") {
isPresentingCurrencyAddView = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Add") {
isPresentingCurrencyAddView = false
onSave(&newCurrency)
}
}
}
}
}
}

#Preview {
CurrencyAddView(newCurrency: .constant(Currency.empty), isPresentingCurrencyAddView: .constant(true)) { newCurrency in
// Handle saving in preview
print("New currency: \(newCurrency.name)")
}
}
92 changes: 92 additions & 0 deletions MMEX/Views/CurrencyDetailView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// CurrencyDetailView.swift
// MMEX
//
// Created by Lisheng Guan on 2024/9/17.
//

import SwiftUI

struct CurrencyDetailView: View {
@State var currency: Currency
let databaseURL: URL
@State private var editingCurrency = Currency.empty
@State private var isPresentingEditView = false
@Environment(\.presentationMode) var presentationMode

var body: some View {
List {
Section(header: Text("Currency Name")) {
Text(currency.name)
}
Section(header: Text("Prefix Symbol")) {
Text(currency.prefixSymbol ?? "N/A")
}
Section(header: Text("Suffix Symbol")) {
Text(currency.suffixSymbol ?? "N/A")
}
Section(header: Text("Scale")) {
Text("\(currency.scale)")
}
Section(header: Text("Conversion Rate")) {
Text("\(currency.baseConversionRate ?? 0)")
}
Section(header: Text("Currency Type")) {
Text(currency.type)
}
Button("Delete Currency") {
deleteCurrency()
}
}
.textSelection(.enabled)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Edit") {
isPresentingEditView = true
editingCurrency = currency
}
}
}
.sheet(isPresented: $isPresentingEditView) {
NavigationStack {
CurrencyEditView(currency: $editingCurrency)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
isPresentingEditView = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
isPresentingEditView = false
currency = editingCurrency
saveChanges()
}
}
}
}
}
}

func saveChanges() {
let repository = DataManager(databaseURL: databaseURL).getCurrencyRepository()
if repository.updateCurrency(currency: currency) {
// Handle success
} else {
// Handle failure
}
}

func deleteCurrency() {
let repository = DataManager(databaseURL: databaseURL).getCurrencyRepository()
if repository.deleteCurrency(currency: currency) {
presentationMode.wrappedValue.dismiss()
} else {
// Handle deletion failure
}
}
}

#Preview {
CurrencyDetailView(currency: Currency.sampleData[0], databaseURL: URL(string: "path/to/database")!)
}
46 changes: 46 additions & 0 deletions MMEX/Views/CurrencyEditView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// CurrencyEditView.swift
// MMEX
//
// Created by Lisheng Guan on 2024/9/17.
//

import SwiftUI

struct CurrencyEditView: View {
@Binding var currency: Currency

var body: some View {
Form {
Section(header: Text("Currency Name")) {
TextField("Currency Name", text: $currency.name)
}
Section(header: Text("Prefix Symbol")) {
TextField("Prefix Symbol", text: Binding(
get: { currency.prefixSymbol ?? "" },
set: { currency.prefixSymbol = $0.isEmpty ? nil : $0 }
))
}
Section(header: Text("Suffix Symbol")) {
TextField("Suffix Symbol", text: Binding(
get: { currency.suffixSymbol ?? "" },
set: { currency.suffixSymbol = $0.isEmpty ? nil : $0 }
))
}
Section(header: Text("Scale")) {
TextField("Scale", value: $currency.scale, format: .number)
}
Section(header: Text("Conversion Rate")) {
TextField("Conversion Rate", value: $currency.baseConversionRate, format: .number)
}
Section(header: Text("Currency Type")) {
TextField("Currency Type", text: $currency.type)
}
}
.navigationTitle("Edit Currency")
}
}

#Preview {
CurrencyEditView(currency: .constant(Currency.sampleData[0]))
}
76 changes: 76 additions & 0 deletions MMEX/Views/CurrencyListView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// CurrencyListView.swift
// MMEX
//
// Created by Lisheng Guan on 2024/9/17.
//

import SwiftUI

struct CurrencyListView: View {
let databaseURL: URL
@State private var currencies: [Currency] = []
@State private var newCurrency = Currency.empty
@State private var isPresentingCurrencyAddView = false

private var repository: CurrencyRepository

init(databaseURL: URL) {
self.databaseURL = databaseURL
self.repository = DataManager(databaseURL: databaseURL).getCurrencyRepository()
}

var body: some View {
NavigationStack {
List(currencies) { currency in
NavigationLink(destination: CurrencyDetailView(currency: currency, databaseURL: databaseURL)) {
HStack {
Text(currency.name)
Spacer()
Text(currency.symbol)
}
}
}
.toolbar {
Button(action: {
isPresentingCurrencyAddView = true
}, label: {
Image(systemName: "plus")
})
}
}
.navigationTitle("Currencies")
.onAppear {
loadCurrencies()
}
.sheet(isPresented: $isPresentingCurrencyAddView) {
CurrencyAddView(newCurrency: $newCurrency, isPresentingCurrencyAddView: $isPresentingCurrencyAddView) { currency in
addCurrency(&currency)
}
}
}

func loadCurrencies() {
// Fetch accounts using repository and update the view
DispatchQueue.global(qos: .background).async {
let loadedCurrencies = repository.loadCurrencies()

// Update UI on the main thread
DispatchQueue.main.async {
self.currencies = loadedCurrencies
}
}
}

func addCurrency(_ currency: inout Currency) {
if repository.addCurrency(currency: &currency) {
self.loadCurrencies()
} else {
// TODO
}
}
}

#Preview {
CurrencyListView(databaseURL: URL(string: "path/to/database")!)
}
27 changes: 16 additions & 11 deletions MMEX/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ struct SettingsView: View {

var body: some View {
List {
NavigationLink(destination: AccountListView(databaseURL: databaseURL)) {
Text("Manage Accounts")
}
NavigationLink(destination: PayeeListView(databaseURL: databaseURL)) {
Text("Manage Payees")
}
NavigationLink(destination: CategoryListView(databaseURL: databaseURL)) {
Text("Manage Categories")
}
NavigationLink(destination: TransactionListView(databaseURL: databaseURL)) {
Text("Manage Transactions")
Section(header: Text("Manage Data")) {
NavigationLink(destination: AccountListView(databaseURL: databaseURL)) {
Text("Manage Accounts")
}
NavigationLink(destination: PayeeListView(databaseURL: databaseURL)) {
Text("Manage Payees")
}
NavigationLink(destination: CategoryListView(databaseURL: databaseURL)) {
Text("Manage Categories")
}
NavigationLink(destination: TransactionListView(databaseURL: databaseURL)) {
Text("Manage Transactions")
}
NavigationLink(destination: CurrencyListView(databaseURL: databaseURL)) {
Text("Manage Currencies")
}
}
}
}
Expand Down

0 comments on commit 2cb5050

Please sign in to comment.