Skip to content

Commit

Permalink
Focused expense name instead amount
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Dec 17, 2024
1 parent 9b25974 commit 9906a8e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
13 changes: 6 additions & 7 deletions Splito/UI/Home/Expense/AddExpenseView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ struct AddExpenseView: View {
.navigationBarTitleDisplayMode(.inline)
.toastView(toast: $viewModel.toast)
.alertView.alert(isPresented: $viewModel.showAlert, alertStruct: viewModel.alert)
.onAppear {
focusedField = .expenseName
}
.sheet(isPresented: $viewModel.showGroupSelection) {
NavigationStack {
SelectGroupView(viewModel: SelectGroupViewModel(selectedGroup: viewModel.selectedGroup,
Expand Down Expand Up @@ -119,6 +122,8 @@ private struct ExpenseInfoView: View {

var focusedField: FocusState<AddExpenseViewModel.AddExpenseField?>.Binding

@FocusState var isAmountFocused: Bool

var body: some View {
VStack(spacing: 16) {
ExpenseDetailRow(name: $viewModel.expenseName, focusedField: focusedField, subtitle: "With you and:",
Expand All @@ -128,10 +133,7 @@ private struct ExpenseInfoView: View {
ExpenseDetailRow(name: $viewModel.expenseName, focusedField: focusedField,
subtitle: "Description", field: .expenseName)

AmountRowView(amount: $viewModel.expenseAmount, subtitle: "Amount")
.onTapGesture {
focusedField.wrappedValue = .amount
}
AmountRowView(amount: $viewModel.expenseAmount, isAmountFocused: $isAmountFocused, subtitle: "Amount")
.focused(focusedField, equals: .amount)

HStack(alignment: .top, spacing: 16) {
Expand Down Expand Up @@ -171,9 +173,6 @@ private struct ExpenseDetailRow: View {
.font(.subTitle2())
.foregroundStyle(primaryText)
.keyboardType(.default)
.onTapGesture {
focusedField.wrappedValue = .expenseName
}
.tint(primaryColor)
.focused(focusedField, equals: field)
.textInputAutocapitalization(.sentences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct GroupPaymentView: View {

@StateObject var viewModel: GroupPaymentViewModel

@FocusState var isAmountFocused: Bool

var body: some View {
GeometryReader { geometry in
VStack(alignment: .center, spacing: 0) {
Expand Down Expand Up @@ -59,7 +61,7 @@ struct GroupPaymentView: View {

VSpacer(16)

AmountRowView(amount: $viewModel.amount, subtitle: "Enter amount")
AmountRowView(amount: $viewModel.amount, isAmountFocused: $isAmountFocused, subtitle: "Enter amount")

Spacer(minLength: 40)
}
Expand All @@ -84,8 +86,11 @@ struct GroupPaymentView: View {
.background(surfaceColor)
.toastView(toast: $viewModel.toast)
.alertView.alert(isPresented: $viewModel.showAlert, alertStruct: viewModel.alert)
.onAppear {
isAmountFocused = true
}
.onTapGesture {
UIApplication.shared.endEditing()
isAmountFocused = false
}
.toolbarRole(.editor)
.toolbar {
Expand Down Expand Up @@ -123,10 +128,10 @@ struct GroupPaymentView: View {
struct AmountRowView: View {

@Binding var amount: Double
var isAmountFocused: FocusState<Bool>.Binding

let subtitle: String

@FocusState var isAmountFocused: Bool
@State private var amountString: String = ""

var body: some View {
Expand All @@ -141,15 +146,14 @@ struct AmountRowView: View {
.font(.Header1())
.tint(primaryColor)
.foregroundStyle(amountString.isEmpty ? outlineColor : primaryText)
.focused($isAmountFocused)
.focused(isAmountFocused)
.multilineTextAlignment(.center)
.autocorrectionDisabled()
.onChange(of: amountString) { newValue in
formatAmount(newValue: newValue)
}
.onAppear {
amountString = amount == 0 ? "" : String(format: "₹ %.2f", amount)
isAmountFocused = true
}
}
.padding(16)
Expand Down

0 comments on commit 9906a8e

Please sign in to comment.