-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e46417
commit 2cb5050
Showing
7 changed files
with
290 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")!) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¤cy) | ||
} | ||
} | ||
} | ||
|
||
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: ¤cy) { | ||
self.loadCurrencies() | ||
} else { | ||
// TODO | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
CurrencyListView(databaseURL: URL(string: "path/to/database")!) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters