Skip to content

Commit

Permalink
Passed object instead id
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Nov 27, 2024
1 parent b1e5cce commit 9e5b4cc
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 139 deletions.
2 changes: 1 addition & 1 deletion Splito.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
D85E86F02BB41CBA002EDF76 /* Detail Selection */,
D856C7302BCFD2080008A341 /* Expense Detail */,
D8CD952A2BD65E6400407B47 /* Expense Split Option */,
210CC0392CF6DA7F0035682E /* Notes */,
);
path = Expense;
sourceTree = "<group>";
Expand All @@ -518,7 +519,6 @@
D85E86EC2BB41B87002EDF76 /* SelectGroupView.swift */,
D85E86EE2BB41B9F002EDF76 /* SelectGroupViewModel.swift */,
21BA6D4B2C3BB64B0020ED04 /* Payer */,
210CC0392CF6DA7F0035682E /* Notes */,
);
path = "Detail Selection";
sourceTree = "<group>";
Expand Down
3 changes: 3 additions & 0 deletions Splito/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@
"Expense deleted successfully" : {
"extractionState" : "manual"
},
"Failed to save note." : {
"extractionState" : "manual"
},
"First Name" : {
"extractionState" : "manual"
},
Expand Down
13 changes: 4 additions & 9 deletions Splito/UI/Home/Expense/AddExpenseView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ struct AddExpenseView: View {
}
.sheet(isPresented: $viewModel.showAddNoteEditor) {
NavigationStack {
if let groupId = viewModel.groupId, let expenseId = viewModel.expenseId {
ExpenseAddNoteView(viewModel: ExpenseAddNoteViewModel(
groupId: groupId, expenseId: expenseId, expenseNote: viewModel.expenseNote, handleSaveNoteTap: viewModel.handleNoteSaveBtnTap(note:)
))
}
ExpenseAddNoteView(viewModel: ExpenseAddNoteViewModel(group: viewModel.selectedGroup, expense: viewModel.expense,
expenseNote: viewModel.expenseNote,
handleSaveNoteTap: viewModel.handleNoteSaveBtnTap(note:)))
}
}
.sheet(isPresented: $viewModel.showImagePicker) {
Expand All @@ -90,10 +88,7 @@ struct AddExpenseView: View {
}
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button("Cancel") {
dismiss()
}
.foregroundStyle(.blue)
CancelButton()
}
ToolbarItem(placement: .topBarTrailing) {
CheckmarkButton(showLoader: viewModel.showLoader) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ struct ExpenseDetailsView: View {
}
.fullScreenCover(isPresented: $viewModel.showAddNoteEditor) {
NavigationStack {
ExpenseAddNoteView(viewModel: ExpenseAddNoteViewModel(
groupId: viewModel.groupId, expenseId: viewModel.expenseId, expenseNote: viewModel.expenseNote
))
ExpenseAddNoteView(viewModel: ExpenseAddNoteViewModel(group: viewModel.group, expense: viewModel.expense,
expenseNote: viewModel.expenseNote))
}
}
.toolbarRole(.editor)
Expand Down Expand Up @@ -114,6 +113,7 @@ private struct ExpenseDetailsNoteView: View {
Text(note)
.font(.subTitle2())
.foregroundStyle(primaryText)
.lineSpacing(3)
.padding(16)
.frame(maxWidth: .infinity, alignment: .leading)
.onTapGestureForced(perform: handleNoteTap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
}

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

Expand All @@ -116,7 +116,8 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
}

func handleNoteTap() {
showAddNoteEditor = true
guard let expense else { return }
showAddNoteEditor = expense.isActive
}

func handleEditBtnAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ struct ExpenseAddNoteView: View {
var body: some View {
VStack(alignment: .leading, spacing: 8) {
TextEditor(text: $tempNote)
.font(.subTitle3())
.foregroundColor(primaryText)
.lineSpacing(5)
.multilineTextAlignment(.leading)
.scrollContentBackground(.hidden)
.font(.subTitle2())
.foregroundStyle(primaryText)
.lineSpacing(3)
.padding(16)
.frame(maxWidth: .infinity, minHeight: 50, maxHeight: .infinity)
.focused($isFocused)
.overlay {
RoundedRectangle(cornerRadius: 12)
.stroke(outlineColor, lineWidth: 1)
}
}
.padding(.horizontal, 16)
.padding(16)
.frame(maxWidth: isIpad ? 600 : nil, alignment: .center)
.frame(maxWidth: .infinity, alignment: .center)
.background(surfaceColor)
Expand All @@ -44,16 +40,13 @@ struct ExpenseAddNoteView: View {
}
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button("Cancel") {
dismiss()
}
.foregroundStyle(.blue)
CancelButton()
}
ToolbarItem(placement: .topBarTrailing) {
CheckmarkButton(showLoader: viewModel.showLoader) {
viewModel.expenseNote = tempNote
viewModel.expenseNote = tempNote.trimming(spaces: .leadingAndTrailing)
Task {
let isActionSucceed = await viewModel.handleSaveAction()
let isActionSucceed = await viewModel.handleSaveNoteAction()
if isActionSucceed {
dismiss()
} else {
Expand All @@ -65,3 +58,14 @@ struct ExpenseAddNoteView: View {
}
}
}

struct CancelButton: View {
@Environment(\.dismiss) var dismiss

var body: some View {
Button("Cancel") {
dismiss()
}
.foregroundStyle(.blue)
}
}
67 changes: 67 additions & 0 deletions Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// ExpenseAddNoteViewModel.swift
// Splito
//
// Created by Nirali Sonani on 27/11/24.
//

import Data
import BaseStyle
import Foundation

class ExpenseAddNoteViewModel: BaseViewModel, ObservableObject {

@Inject private var expenseRepository: ExpenseRepository

@Published var expenseNote: String
@Published private(set) var showLoader: Bool = false

private let group: Groups?
private let expense: Expense?
private let handleSaveNoteTap: ((String) -> Void)?

init(group: Groups?, expense: Expense?, expenseNote: String, handleSaveNoteTap: ((String) -> Void)? = nil) {
self.group = group
self.expense = expense
self.expenseNote = expenseNote
self.handleSaveNoteTap = handleSaveNoteTap
super.init()
}

// MARK: - User Actions
func showSaveFailedError() {
self.showToastFor(toast: ToastPrompt(type: .error, title: "Oops", message: "Failed to save note."))
}

func handleSaveNoteAction() async -> Bool {

if let handleSaveNoteTap {
handleSaveNoteTap(expenseNote)
return true
}

guard let expense, expense.note != expenseNote else { return true }
return await updateExpenseNote()
}

private func updateExpenseNote() async -> Bool {
guard let group, let expense else { return false }

do {
showLoader = true
var updatedExpense = expense
updatedExpense.note = expenseNote

try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated)
NotificationCenter.default.post(name: .updateExpense, object: updatedExpense)

showLoader = false
LogD("ExpenseAddNoteViewModel: \(#function) Expense note updated successfully.")
return true
} catch {
LogE("ExpenseAddNoteViewModel: \(#function) Failed to update expense note: \(error).")
showToastForError()
return false
}
}
}

0 comments on commit 9e5b4cc

Please sign in to comment.