Skip to content

Commit 9564bb7

Browse files
committed
Manage tap of expense
1 parent 9ffdaba commit 9564bb7

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

Data/Data/Store/ExpenseStore.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ public class ExpenseStore: ObservableObject {
7676

7777
let fetchedExpenses = expenseSnapshot.documents.compactMap { doc -> Expense? in
7878
do {
79-
return try doc.data(as: Expense.self)
79+
var expense = try doc.data(as: Expense.self)
80+
let groupId = doc.reference.parent.parent?.documentID ?? "" // Extract groupId
81+
expense.groupId = groupId
82+
return expense
8083
} catch {
8184
LogE("ExpenseStore: \(#function) Error decoding expense: \(error.localizedDescription)")
8285
return nil

Splito/UI/Home/ActivityLog/Search/SearchExpensesView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private struct ExpenseListView: View {
7171
GroupExpenseItemView(expenseWithUser: expense,
7272
isLastItem: expense.expense == (viewModel.groupExpenses[month] ?? []).last?.expense)
7373
.onTouchGesture {
74-
viewModel.handleExpenseItemTap(expenseId: expense.expense.id ?? "")
74+
viewModel.handleExpenseItemTap(expense: expense.expense)
7575
}
7676
.id(expense.expense.id)
7777
}

Splito/UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject {
4040
self.router = router
4141
super.init()
4242

43+
NotificationCenter.default.addObserver(self, selector: #selector(handleUpdateExpense(notification:)), name: .updateExpense, object: nil)
44+
NotificationCenter.default.addObserver(self, selector: #selector(handleDeleteExpense(notification:)), name: .deleteExpense, object: nil)
45+
4346
fetchInitialExpenses()
4447
}
4548

@@ -152,8 +155,42 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject {
152155
}
153156

154157
// MARK: - User Actions
155-
func handleExpenseItemTap(expenseId: String) {
156-
// router.push(.ExpenseDetailView(groupId: groupId, expenseId: expenseId))
158+
func handleExpenseItemTap(expense: Expense) {
159+
if let groupId = expense.groupId, let expenseId = expense.id {
160+
router.push(.ExpenseDetailView(groupId: groupId, expenseId: expenseId))
161+
}
162+
}
163+
164+
@objc func handleUpdateExpense(notification: Notification) {
165+
guard var updatedExpense = notification.object as? Expense else { return }
166+
167+
if let index = expenses.firstIndex(where: { $0.id == updatedExpense.id }) {
168+
expenses[index] = updatedExpense
169+
}
170+
171+
Task { [weak self] in
172+
if let user = await self?.fetchMemberData(for: updatedExpense.paidBy.keys.first ?? "") {
173+
if let index = self?.expensesWithUser.firstIndex(where: { $0.expense.id == updatedExpense.id }) {
174+
let updatedExpenseWithUser = ExpenseWithUser(expense: updatedExpense, user: user)
175+
withAnimation {
176+
self?.expensesWithUser[index] = updatedExpenseWithUser
177+
self?.updateGroupExpenses()
178+
}
179+
}
180+
}
181+
}
182+
}
183+
184+
@objc func handleDeleteExpense(notification: Notification) {
185+
guard let deletedExpense = notification.object as? Expense else { return }
186+
expenses.removeAll { $0.id == deletedExpense.id }
187+
if let index = expensesWithUser.firstIndex(where: { $0.expense.id == deletedExpense.id }) {
188+
withAnimation {
189+
expensesWithUser.remove(at: index)
190+
updateGroupExpenses()
191+
showToastFor(toast: .init(type: .success, title: "Success", message: "Expense deleted successfully."))
192+
}
193+
}
157194
}
158195

159196
// MARK: - Error Handling

firestore.rules

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ service cloud.firestore {
4242

4343
// For Groups Collection
4444
match /groups/{groupId} {
45-
allow read: if true;
45+
allow read: if isAuthorized() &&
46+
resource.data.members.hasAny([request.auth.uid]);
4647

4748
allow create: if isAuthorized() &&
4849
isCurrentUser(request.resource.data.created_by);
@@ -60,7 +61,7 @@ service cloud.firestore {
6061

6162
// For Expenses Collection
6263
match /groups/{groupId}/expenses/{expenseId} {
63-
allow read: if true;
64+
allow read: if isUserPartOfGroup(groupId);
6465

6566
allow create: if isCurrentUser(request.resource.data.added_by) &&
6667
isUserPartOfGroup(groupId);

0 commit comments

Comments
 (0)