From d7e8c0dd358c8e0e983737f72679198c77dc19b6 Mon Sep 17 00:00:00 2001 From: Danil Nikolaev Date: Wed, 4 Dec 2024 18:57:14 +0300 Subject: [PATCH] fix online friends & pagination --- .../fast/data/api/friends/FriendsRepositoryImpl.kt | 2 +- .../main/kotlin/dev/meloda/fast/model/FriendsInfo.kt | 2 +- .../dev/meloda/fast/friends/FriendsViewModel.kt | 12 ++++++------ .../fast/friends/presentation/FriendsScreen.kt | 8 ++++++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/core/data/src/main/kotlin/dev/meloda/fast/data/api/friends/FriendsRepositoryImpl.kt b/core/data/src/main/kotlin/dev/meloda/fast/data/api/friends/FriendsRepositoryImpl.kt index 0abb9519..3723e688 100644 --- a/core/data/src/main/kotlin/dev/meloda/fast/data/api/friends/FriendsRepositoryImpl.kt +++ b/core/data/src/main/kotlin/dev/meloda/fast/data/api/friends/FriendsRepositoryImpl.kt @@ -36,7 +36,7 @@ class FriendsRepositoryImpl( val onlineFriends = async { getOnlineFriends(count, offset) }.await() .successOrElse { failure -> return@withContext failure - }.mapNotNull { userId -> friends.find { it.id == userId } } + } ApiResult.success(FriendsInfo(friends, onlineFriends)) } diff --git a/core/model/src/main/kotlin/dev/meloda/fast/model/FriendsInfo.kt b/core/model/src/main/kotlin/dev/meloda/fast/model/FriendsInfo.kt index 393ac4bd..2364476b 100644 --- a/core/model/src/main/kotlin/dev/meloda/fast/model/FriendsInfo.kt +++ b/core/model/src/main/kotlin/dev/meloda/fast/model/FriendsInfo.kt @@ -4,5 +4,5 @@ import dev.meloda.fast.model.api.domain.VkUser data class FriendsInfo( val friends: List, - val onlineFriends: List + val onlineFriendsIds: List ) diff --git a/feature/friends/src/main/kotlin/dev/meloda/fast/friends/FriendsViewModel.kt b/feature/friends/src/main/kotlin/dev/meloda/fast/friends/FriendsViewModel.kt index 2dcbeb75..b3f51389 100644 --- a/feature/friends/src/main/kotlin/dev/meloda/fast/friends/FriendsViewModel.kt +++ b/feature/friends/src/main/kotlin/dev/meloda/fast/friends/FriendsViewModel.kt @@ -67,7 +67,7 @@ class FriendsViewModelImpl( } private fun loadFriends(offset: Int = currentOffset.value) { - friendsUseCase.getAllFriends(count = LOAD_COUNT, offset = offset) + friendsUseCase.getFriends(count = LOAD_COUNT, offset = offset) .listenValue(viewModelScope) { state -> state.processState( error = { error -> @@ -81,8 +81,7 @@ class FriendsViewModelImpl( } } }, - success = { info -> - val response = info.friends + success = { response -> val itemsCountSufficient = response.size == LOAD_COUNT canPaginate.setValue { itemsCountSufficient } @@ -98,8 +97,9 @@ class FriendsViewModelImpl( val loadedFriends = response.map { it.asPresentation(userSettings.useContactNames.value) } - val loadedOnlineFriends = info.onlineFriends.map { - it.asPresentation(userSettings.useContactNames.value) + + val loadedOnlineFriends = loadedFriends.filter { + it.onlineStatus.isOnline() } val newState = screenState.value.copy( @@ -156,6 +156,6 @@ class FriendsViewModelImpl( } companion object { - const val LOAD_COUNT = 60 + const val LOAD_COUNT = 15 } } diff --git a/feature/friends/src/main/kotlin/dev/meloda/fast/friends/presentation/FriendsScreen.kt b/feature/friends/src/main/kotlin/dev/meloda/fast/friends/presentation/FriendsScreen.kt index 86f3b055..d2a8b9ba 100644 --- a/feature/friends/src/main/kotlin/dev/meloda/fast/friends/presentation/FriendsScreen.kt +++ b/feature/friends/src/main/kotlin/dev/meloda/fast/friends/presentation/FriendsScreen.kt @@ -135,7 +135,7 @@ fun FriendsScreen( val listState = rememberLazyListState() - val paginationConditionMet by remember { + val paginationConditionMet by remember(canPaginate, listState) { derivedStateOf { canPaginate && (listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index @@ -300,7 +300,11 @@ fun FriendsScreen( ) } ) { - val friendsToDisplay = screenState.friends + val friendsToDisplay = if (index == 0) { + screenState.friends + } else { + screenState.onlineFriends + } FriendsList( modifier = if (currentTheme.enableBlur) {