From 980073b8bd9ad977c676fcebdd80778c50bbd65e Mon Sep 17 00:00:00 2001 From: NiraliSonani Date: Wed, 27 Nov 2024 18:15:12 +0530 Subject: [PATCH] Refactored code --- Data/Data/Model/Expense.swift | 4 +- Splito/Localization/Localizable.xcstrings | 3 + Splito/UI/Home/Expense/AddExpenseView.swift | 33 +++++------ .../UI/Home/Expense/AddExpenseViewModel.swift | 15 +++-- .../Expense Detail/ExpenseDetailsView.swift | 56 +++++++++---------- .../ExpenseDetailsViewModel.swift | 2 +- .../Expense/Notes/ExpenseAddNoteView.swift | 2 +- .../Notes/ExpenseAddNoteViewModel.swift | 1 - 8 files changed, 59 insertions(+), 57 deletions(-) diff --git a/Data/Data/Model/Expense.swift b/Data/Data/Model/Expense.swift index b74c73890..bd595d3ab 100644 --- a/Data/Data/Model/Expense.swift +++ b/Data/Data/Model/Expense.swift @@ -17,7 +17,7 @@ public struct Expense: Codable, Hashable, Identifiable { public var paidBy: [String: Double] public let addedBy: String public var updatedBy: String - public var note: String + public var note: String? public var imageUrl: String? public var splitTo: [String] // Reference to user ids involved in the split public var splitType: SplitType @@ -25,7 +25,7 @@ public struct Expense: Codable, Hashable, Identifiable { public var isActive: Bool public init(name: String, amount: Double, date: Timestamp, paidBy: [String: Double], addedBy: String, - updatedBy: String, note: String, imageUrl: String? = nil, splitTo: [String], + updatedBy: String, note: String? = nil, imageUrl: String? = nil, splitTo: [String], splitType: SplitType = .equally, splitData: [String: Double]? = [:], isActive: Bool = true) { self.name = name self.amount = amount diff --git a/Splito/Localization/Localizable.xcstrings b/Splito/Localization/Localizable.xcstrings index 17254077b..b86bea189 100644 --- a/Splito/Localization/Localizable.xcstrings +++ b/Splito/Localization/Localizable.xcstrings @@ -337,6 +337,9 @@ }, "Enter your last name" : { "extractionState" : "manual" + }, + "Enter your note here..." : { + }, "Enter your phone number" : { "extractionState" : "manual" diff --git a/Splito/UI/Home/Expense/AddExpenseView.swift b/Splito/UI/Home/Expense/AddExpenseView.swift index bca94b477..228eefdd1 100644 --- a/Splito/UI/Home/Expense/AddExpenseView.swift +++ b/Splito/UI/Home/Expense/AddExpenseView.swift @@ -36,7 +36,7 @@ struct AddExpenseView: View { AddExpenseFooterView(date: $viewModel.expenseDate, showImagePickerOptions: $viewModel.showImagePickerOptions, expenseImage: viewModel.expenseImage, expenseImageUrl: viewModel.expenseImageUrl, - handleNoteTap: viewModel.handleNoteAction, handleExpenseImageTap: viewModel.handleExpenseImageTap, + handleNoteBtnTap: viewModel.handleNoteBtnTap, handleExpenseImageTap: viewModel.handleExpenseImageTap, handleActionSelection: viewModel.handleActionSelection(_:)) } } @@ -212,7 +212,7 @@ private struct AddExpenseFooterView: View { let expenseImage: UIImage? let expenseImageUrl: String? - let handleNoteTap: (() -> Void) + let handleNoteBtnTap: (() -> Void) let handleExpenseImageTap: (() -> Void) let handleActionSelection: ((ActionsOfSheet) -> Void) @@ -228,7 +228,7 @@ private struct AddExpenseFooterView: View { ExpenseImagePickerView(image: expenseImage, imageUrl: expenseImageUrl, handleImageBtnTap: handleExpenseImageTap) - ExpenseNoteView(handleNoteTap: handleNoteTap) + NoteButtonView(handleNoteBtnTap: handleNoteBtnTap) } .padding(.vertical, 12) .padding(.horizontal, 16) @@ -273,21 +273,22 @@ private struct ExpenseImagePickerView: View { } } -struct ExpenseNoteView: View { +private struct NoteButtonView: View { - let handleNoteTap: (() -> Void) + let handleNoteBtnTap: (() -> Void) var body: some View { - Image(.noteIcon) - .resizable() - .scaledToFit() - .frame(width: 24, height: 24) - .padding(4) - .onTouchGesture { - UIApplication.shared.endEditing() - handleNoteTap() - } - .background(container2Color) - .cornerRadius(8) + Button { + UIApplication.shared.endEditing() + handleNoteBtnTap() + } label: { + Image(.noteIcon) + .resizable() + .scaledToFit() + .frame(width: 24, height: 24) + .padding(4) + .background(container2Color) + .cornerRadius(8) + } } } diff --git a/Splito/UI/Home/Expense/AddExpenseViewModel.swift b/Splito/UI/Home/Expense/AddExpenseViewModel.swift index c3ff725e4..63c02fe58 100644 --- a/Splito/UI/Home/Expense/AddExpenseViewModel.swift +++ b/Splito/UI/Home/Expense/AddExpenseViewModel.swift @@ -28,11 +28,11 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject { @Published var expenseAmount: Double = 0 @Published var showImagePicker = false + @Published var showAddNoteEditor = false @Published var showGroupSelection = false @Published var showPayerSelection = false @Published var showImagePickerOptions = false @Published var showSplitTypeSelection = false - @Published var showAddNoteEditor = false @Published private(set) var showLoader: Bool = false @Published private(set) var sourceTypeIsCamera = false @@ -53,7 +53,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject { } let expenseId: String? - let groupId: String? + private let groupId: String? private let router: Router init(router: Router, groupId: String? = nil, expenseId: String? = nil) { @@ -135,7 +135,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject { splitType = expense.splitType selectedPayers = expense.paidBy expenseImageUrl = expense.imageUrl - expenseNote = expense.note + expenseNote = expense.note ?? "" if let splitData = expense.splitData { self.splitData = splitData @@ -214,7 +214,7 @@ extension AddExpenseViewModel { showImagePickerOptions = true } - func handleNoteAction() { + func handleNoteBtnTap() { showAddNoteEditor = true } @@ -374,9 +374,8 @@ extension AddExpenseViewModel { private func handleAddExpenseAction(userId: String, group: Groups) async -> Bool { let expense = Expense(name: expenseName.trimming(spaces: .leadingAndTrailing), amount: expenseAmount, - date: Timestamp(date: expenseDate), paidBy: selectedPayers, addedBy: userId, - updatedBy: userId, note: expenseNote.trimming(spaces: .leadingAndTrailing), - splitTo: (splitType == .equally) ? selectedMembers : splitData.map({ $0.key }), + date: Timestamp(date: expenseDate), paidBy: selectedPayers, addedBy: userId, updatedBy: userId, + note: expenseNote, splitTo: (splitType == .equally) ? selectedMembers : splitData.map({ $0.key }), splitType: splitType, splitData: splitData) return await addExpense(group: group, expense: expense) @@ -416,7 +415,7 @@ extension AddExpenseViewModel { newExpense.amount = expenseAmount newExpense.date = Timestamp(date: expenseDate) newExpense.updatedBy = userId - newExpense.note = expenseNote.trimming(spaces: .leadingAndTrailing) + newExpense.note = expenseNote if selectedPayers.count == 1, let payerId = selectedPayers.keys.first { newExpense.paidBy = [payerId: expenseAmount] diff --git a/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsView.swift b/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsView.swift index 41ec8dbc8..5d93920e7 100644 --- a/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsView.swift +++ b/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsView.swift @@ -35,14 +35,14 @@ struct ExpenseDetailsView: View { .frame(maxWidth: .infinity, alignment: .leading) ExpenseImageView(showImageDisplayView: $showImageDisplayView, imageUrl: imageUrl) - .frame(height: 140, alignment: .leading) + .frame(height: 140) .frame(maxWidth: .infinity) .cornerRadius(12) } } if let note = viewModel.expense?.note, !note.isEmpty { - ExpenseDetailsNoteView(note: note, handleNoteTap: viewModel.handleNoteTap) + ExpenseNoteView(note: note, handleNoteTap: viewModel.handleNoteTap) } VSpacer(24) @@ -97,32 +97,6 @@ struct ExpenseDetailsView: View { } } -private struct ExpenseDetailsNoteView: View { - - let note: String - - let handleNoteTap: (() -> Void) - - var body: some View { - VStack(spacing: 8) { - Text("Note:") - .font(.subTitle3()) - .foregroundStyle(disableText) - .frame(maxWidth: .infinity, alignment: .leading) - - Text(note) - .font(.subTitle2()) - .foregroundStyle(primaryText) - .lineSpacing(3) - .padding(16) - .frame(maxWidth: .infinity, alignment: .leading) - .onTapGestureForced(perform: handleNoteTap) - .background(containerColor) - .cornerRadius(12) - } - } -} - @MainActor private struct ExpenseHeaderView: View { @@ -232,3 +206,29 @@ private struct ExpenseInfoView: View { .padding(.top, 8) } } + +private struct ExpenseNoteView: View { + + let note: String + + let handleNoteTap: (() -> Void) + + var body: some View { + VStack(spacing: 8) { + Text("Note:") + .font(.subTitle3()) + .foregroundStyle(disableText) + .frame(maxWidth: .infinity, alignment: .leading) + + Text(note) + .font(.subTitle2()) + .foregroundStyle(primaryText) + .lineSpacing(3) + .padding(16) + .frame(maxWidth: .infinity, alignment: .leading) + .background(containerColor) + .cornerRadius(12) + .onTapGestureForced(perform: handleNoteTap) + } + } +} diff --git a/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift b/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift index 5fd6be836..6e11ee780 100644 --- a/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift +++ b/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift @@ -93,7 +93,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject { } self.expense = expense - self.expenseNote = expense.note + self.expenseNote = expense.note ?? "" self.expenseUsersData = userData } diff --git a/Splito/UI/Home/Expense/Notes/ExpenseAddNoteView.swift b/Splito/UI/Home/Expense/Notes/ExpenseAddNoteView.swift index 8bfab728f..817f93aa3 100644 --- a/Splito/UI/Home/Expense/Notes/ExpenseAddNoteView.swift +++ b/Splito/UI/Home/Expense/Notes/ExpenseAddNoteView.swift @@ -18,7 +18,7 @@ struct ExpenseAddNoteView: View { var body: some View { VStack(alignment: .leading, spacing: 8) { - TextField("", text: $tempNote, prompt: Text(""), axis: .vertical) + TextField("", text: $tempNote, prompt: Text("Enter your note here..."), axis: .vertical) .font(.subTitle2()) .foregroundStyle(primaryText) .lineSpacing(3) diff --git a/Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift b/Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift index 1c3b65d43..894de17fd 100644 --- a/Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift +++ b/Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift @@ -34,7 +34,6 @@ class ExpenseAddNoteViewModel: BaseViewModel, ObservableObject { } func handleSaveNoteAction() async -> Bool { - if let handleSaveNoteTap { handleSaveNoteTap(expenseNote) return true