Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Nov 27, 2024
1 parent 9ef3a9b commit 980073b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Data/Data/Model/Expense.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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
public var splitData: [String: Double]? // Use this to store percentage or share data
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
Expand Down
3 changes: 3 additions & 0 deletions Splito/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@
},
"Enter your last name" : {
"extractionState" : "manual"
},
"Enter your note here..." : {

},
"Enter your phone number" : {
"extractionState" : "manual"
Expand Down
33 changes: 17 additions & 16 deletions Splito/UI/Home/Expense/AddExpenseView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(_:))
}
}
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
15 changes: 7 additions & 8 deletions Splito/UI/Home/Expense/AddExpenseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -53,7 +53,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject {
}

let expenseId: String?
let groupId: String?
private let groupId: String?
private let router: Router<AppRoute>

init(router: Router<AppRoute>, groupId: String? = nil, expenseId: String? = nil) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -214,7 +214,7 @@ extension AddExpenseViewModel {
showImagePickerOptions = true
}

func handleNoteAction() {
func handleNoteBtnTap() {
showAddNoteEditor = true
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
56 changes: 28 additions & 28 deletions Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
}

self.expense = expense
self.expenseNote = expense.note
self.expenseNote = expense.note ?? ""
self.expenseUsersData = userData
}

Expand Down
2 changes: 1 addition & 1 deletion Splito/UI/Home/Expense/Notes/ExpenseAddNoteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ExpenseAddNoteViewModel: BaseViewModel, ObservableObject {
}

func handleSaveNoteAction() async -> Bool {

if let handleSaveNoteTap {
handleSaveNoteTap(expenseNote)
return true
Expand Down

0 comments on commit 980073b

Please sign in to comment.