Skip to content

Commit

Permalink
ui fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Feb 11, 2025
1 parent c500328 commit 9316ce6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,36 @@ final class SendStableCoinPresenter: ObservableObject, SceneViewModel {
accountAddress: recipient
)

// 1) Convert the "amount" string to a Decimal
guard let decimalAmount = Decimal(string: amount) else {
// 1) Normalize the decimal separator and try to convert to Decimal
let normalizedAmount = amount.replacingOccurrences(of: ",", with: ".")
guard let decimalAmount = Decimal(string: normalizedAmount) else {
throw NSError(domain: "SendStableCoinPresenter", code: 0, userInfo: [
NSLocalizedDescriptionKey: "Invalid numeric input: \(amount)"
])
}

// USDC/USDT => 6 decimals
let baseUnitsDecimal = decimalAmount * Decimal(1_000_000)
let baseUnitsString = NSDecimalNumber(decimal: baseUnitsDecimal).stringValue

// Use a number formatter to ensure consistent string conversion
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0 // No decimal places for base units
formatter.groupingSeparator = "" // No thousand separators
formatter.decimalSeparator = "." // Force decimal point

guard let baseUnitsString = formatter.string(from: NSDecimalNumber(decimal: baseUnitsDecimal)) else {
throw NSError(domain: "SendStableCoinPresenter", code: 0, userInfo: [
NSLocalizedDescriptionKey: "Failed to convert amount to string"
])
}

// 2) Determine which contract address to use
let tokenAddress: String
switch stableCoinChoice {
case .usdc:
tokenAddress = selectedNetwork.usdcContractAddress
case .usdt:
// already checked if .Base => throw error => must not get here if base
tokenAddress = selectedNetwork.usdtContractAddress
}

Expand All @@ -247,7 +259,6 @@ final class SendStableCoinPresenter: ObservableObject, SceneViewModel {
)
return call
}

/// Persists the latest recipient in UserDefaults
private func saveRecipientToUserDefaults() {
userDefaults.set(recipient, forKey: recipientKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ extension View {

struct SendStableCoinView: View {
@ObservedObject var presenter: SendStableCoinPresenter

/// Controls whether the network selection dialog is shown
@State private var showNetworkPicker = false

/// Tracks whether the amount TextField is currently focused
@FocusState private var amountFieldIsFocused: Bool

var body: some View {
VStack(spacing: 24) {
// 1) "My Address" section
Expand Down Expand Up @@ -136,9 +141,21 @@ struct SendStableCoinView: View {
.foregroundColor(.gray)

HStack {
// The amount TextField, with decimal keyboard
TextField("0.00", text: $presenter.amount)
.keyboardType(.decimalPad)
.textFieldStyle(.roundedBorder)
.focused($amountFieldIsFocused)
// Add a toolbar with "Done" to dismiss the keyboard
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("Done") {
// Unfocus the text field => dismiss keyboard
amountFieldIsFocused = false
}
}
}

Button {
showNetworkPicker = true
Expand Down Expand Up @@ -207,7 +224,7 @@ struct SendStableCoinView: View {
.padding()
}
.padding()
.hideKeyboardOnTap()
.hideKeyboardOnTap() // Tap anywhere to dismiss keyboard
// Present success screen when transaction completes
.sheet(isPresented: $presenter.transactionCompleted) {
SendStableCoinCompletedView(presenter: presenter)
Expand Down

0 comments on commit 9316ce6

Please sign in to comment.