Skip to content

Commit

Permalink
Silently update group list on appear
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-amisha-i committed Jan 6, 2025
1 parent b1dc7a6 commit 3d27639
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions Splito/UI/Home/Groups/GroupListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct GroupListView: View {
}
.background(surfaceColor)
.onDisappear { isFocused = false }
.onAppear { viewModel.refreshLoadedGroupsDetails() }
.alertView.alert(isPresented: $viewModel.showAlert, alertStruct: viewModel.alert)
.navigationTitle("")
.navigationBarTitleDisplayMode(.inline)
Expand Down
45 changes: 38 additions & 7 deletions Splito/UI/Home/Groups/GroupListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
@Published private(set) var showScrollToTopBtn = false

let router: Router<AppRoute>
var hasMoreGroups: Bool = true
private var users: [AppUser] = []
private var lastDocument: DocumentSnapshot?

var hasMoreGroups: Bool = true
private var isListLoadedInitially = false
private var task: Task<Void, Never>? // Reference to the current asynchronous task that fetches users

var filteredGroups: [GroupInformation] {
Expand Down Expand Up @@ -130,8 +132,33 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
}

// MARK: - Data Loading
func refreshLoadedGroupsDetails() {
guard isListLoadedInitially else {
isListLoadedInitially = true
return
}

Task { [weak self] in
guard let self, let userId = preference.user?.id else { return }
do {
let result = try await groupRepository.fetchGroupsBy(userId: userId, limit: combinedGroups.count)
let freshGroups = try await self.processNewGroups(newGroups: result.data)

if freshGroups.count != self.combinedGroups.count {
self.groupListState = freshGroups.isEmpty ? .noGroup : .hasGroup
}

self.combinedGroups = freshGroups
LogD("GroupListViewModel: \(#function) Groups reloaded successfully.")
} catch {
LogE("GroupListViewModel: \(#function) Failed to fetch groups: \(error).")
handleErrorState()
}
}
}

private func fetchGroups(needToReload: Bool = false) async {
guard let userId = preference.user?.id, hasMoreGroups || needToReload else {
guard let userId = preference.user?.id else {
currentViewState = .initial
return
}
Expand All @@ -140,11 +167,14 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
let result = try await groupRepository.fetchGroupsBy(userId: userId, limit: GROUPS_LIMIT, lastDocument: lastDocument)
let sortedGroups = try await self.processNewGroups(newGroups: result.data)
combinedGroups = lastDocument == nil ? sortedGroups : (combinedGroups + sortedGroups)

if lastDocument == nil {
currentViewState = .initial
groupListState = combinedGroups.isEmpty ? .noGroup : .hasGroup
}

lastDocument = result.lastDocument
hasMoreGroups = !(result.data.count < self.GROUPS_LIMIT)

currentViewState = .initial
groupListState = combinedGroups.isEmpty ? .noGroup : .hasGroup
LogD("GroupListViewModel: \(#function) Groups fetched successfully.")
} catch {
LogE("GroupListViewModel: \(#function) Failed to fetch groups: \(error).")
Expand All @@ -153,8 +183,9 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
}

func loadMoreGroups() {
Task {
await fetchGroups()
guard hasMoreGroups else { return }
Task { [weak self] in
await self?.fetchGroups()
}
}

Expand Down

0 comments on commit 3d27639

Please sign in to comment.