Skip to content

Commit

Permalink
Added groupCollection query &refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 1, 2025
1 parent 8b5bd72 commit e58c9bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Data/Data/Repository/ExpenseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public class ExpenseRepository: ObservableObject {
return try await store.fetchExpenseBy(groupId: groupId, expenseId: expenseId)
}

public func fetchExpensesForUser(userId: String, limit: Int = 10, lastDocument: DocumentSnapshot? = nil) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) {
return try await store.fetchExpensesForUser(userId: userId, limit: limit, lastDocument: lastDocument)
public func fetchExpensesOfAllGroups(limit: Int = 10, lastDocument: DocumentSnapshot? = nil) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) {
return try await store.fetchExpensesOfAllGroups(limit: limit, lastDocument: lastDocument)
}
}
27 changes: 26 additions & 1 deletion Data/Data/Store/ExpenseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ExpenseStore: ObservableObject {
return (expenses, snapshot.documents.last)
}

func fetchExpensesForUser(userId: String, limit: Int, lastDocument: DocumentSnapshot?) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) {
func fetchExpensesForUser2(userId: String, limit: Int, lastDocument: DocumentSnapshot?) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) {
var userExpenses: [Expense] = []

// Step 1: Fetch groups where the user is a member
Expand Down Expand Up @@ -109,4 +109,29 @@ public class ExpenseStore: ObservableObject {

return (userExpenses, lastFetchedDocument)
}

func fetchExpensesOfAllGroups(limit: Int, lastDocument: DocumentSnapshot?) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) {
// Query to fetch expenses from all groups using collectionGroup
var query = database.collectionGroup("expenses")
.whereField("is_active", isEqualTo: true)
.order(by: "date", descending: true)
.limit(to: limit)

if let lastDocument {
query = query.start(afterDocument: lastDocument)
}

let expenseSnapshot = try await query.getDocuments()

let fetchedExpenses = expenseSnapshot.documents.compactMap { doc -> Expense? in
do {
return try doc.data(as: Expense.self)
} catch {
LogE("ExpenseStore: \(#function) Error decoding expense: \(error.localizedDescription)")
return nil
}
}

return (fetchedExpenses, expenseSnapshot.documents.last)
}
}
7 changes: 4 additions & 3 deletions Splito/UI/Home/ActivityLog/Search/SearchExpensesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ struct SearchExpensesView: View {
if case .noExpense = viewModel.viewState {
EmptyStateView(geometry: geometry)
} else if case .hasExpense = viewModel.viewState {
ExpenseListView(viewModel: viewModel, geometry: geometry)
ExpenseListView(viewModel: viewModel, geometry: geometry, isFocused: $isFocused)
}
}
}
}
.background(surfaceColor)
.toastView(toast: $viewModel.toast)
.toolbarRole(.editor)
.toastView(toast: $viewModel.toast)
.alertView.alert(isPresented: $viewModel.showAlert, alertStruct: viewModel.alert)
}
}
Expand All @@ -59,6 +59,7 @@ private struct ExpenseListView: View {
@ObservedObject var viewModel: SearchExpensesViewModel

let geometry: GeometryProxy
var isFocused: FocusState<Bool>.Binding

var body: some View {
List {
Expand Down Expand Up @@ -104,7 +105,7 @@ private struct ExpenseListView: View {
Spacer()
}
.onTapGestureForced {
UIApplication.shared.endEditing()
isFocused.wrappedValue = false
}
}
}
10 changes: 6 additions & 4 deletions Splito/UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject {
func fetchInitialExpenses() {
lastDocument = nil
Task {
await fetchAllUserExpenses()
await fetchExpensesOfAllGroups()
}
}

// MARK: - Data Loading
private func fetchAllUserExpenses() async {
guard let userId = preference.user?.id, hasMoreExpenses else {
private func fetchExpensesOfAllGroups() async {
guard hasMoreExpenses else {
viewState = .noExpense
return
}
Expand All @@ -62,13 +62,15 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject {
}

do {
let result = try await expenseRepository.fetchExpensesForUser(userId: userId, limit: EXPENSES_LIMIT, lastDocument: lastDocument)
let result = try await expenseRepository.fetchExpensesOfAllGroups(limit: EXPENSES_LIMIT, lastDocument: lastDocument)
self.expenses = lastDocument == nil ? result.expenses.uniqued() : (expenses + result.expenses.uniqued())
lastDocument = result.lastDocument

await combineMemberWithExpense(expenses: result.expenses.uniqued())
hasMoreExpenses = !(result.expenses.count < self.EXPENSES_LIMIT)
LogD("SearchExpensesViewModel: \(#function) Expenses fetched successfully.")

print("xxx \(expenses.count)")
} catch {
LogE("SearchExpensesViewModel: \(#function) Failed to fetch expenses: \(error).")
handleServiceError()
Expand Down

0 comments on commit e58c9bb

Please sign in to comment.