From 6c1f633ab43c6c77388ede525d8e08d5ba654b3f Mon Sep 17 00:00:00 2001 From: Nguyen Quang Minh Date: Fri, 29 Nov 2024 23:01:08 +0700 Subject: [PATCH] =?UTF-8?q?fix(state):=20s=E1=BB=ADa=20l=E1=BB=97i=20?= =?UTF-8?q?=C4=91=C4=83ng=20xu=E1=BA=A5t=20=C4=91=C4=83ng=20nh=E1=BA=ADp?= =?UTF-8?q?=20l=E1=BA=A1i=20th=C3=AC=20state=20v=E1=BA=ABn=20l=C3=A0=20t?= =?UTF-8?q?=C3=A0i=20kho=E1=BA=A3n=20c=C5=A9,=20ko=20ph=E1=BA=A3i=20l?= =?UTF-8?q?=C3=A0=20t=C3=A0i=20kho=E1=BA=A3n=20m=E1=BB=9Bi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quickmem/core/datastore/AppManager.kt | 4 +- .../add_folder/AddFolderToClassViewModel.kt | 4 +- .../AddStudySetToClassViewModel.kt | 4 +- .../app/explore/ExploreViewModel.kt | 57 +++++++------ .../AddStudySetToFolderViewModel.kt | 4 +- .../presentation/app/home/HomeUiAction.kt | 1 - .../presentation/app/home/HomeViewModel.kt | 83 ++++++++++--------- .../presentation/app/library/LibraryScreen.kt | 2 +- .../app/library/LibraryUiState.kt | 5 +- .../app/library/LibraryViewModel.kt | 82 +++++++++++------- .../app/profile/ProfileViewModel.kt | 44 ++++++---- .../choose_picture/ChoosePictureViewModel.kt | 2 +- .../app/settings/SettingsViewModel.kt | 9 +- .../AddStudySetToClassesViewModel.kt | 4 +- .../AddStudySetToFoldersViewModel.kt | 4 +- .../onboarding/OnboardingScreen.kt | 2 +- 16 files changed, 175 insertions(+), 136 deletions(-) diff --git a/app/src/main/java/com/pwhs/quickmem/core/datastore/AppManager.kt b/app/src/main/java/com/pwhs/quickmem/core/datastore/AppManager.kt index 1e094bf9..161a7e99 100644 --- a/app/src/main/java/com/pwhs/quickmem/core/datastore/AppManager.kt +++ b/app/src/main/java/com/pwhs/quickmem/core/datastore/AppManager.kt @@ -41,11 +41,11 @@ class AppManager(private val context: Context) { .map { preferences -> preferences[USER_FULL_NAME] ?: "" } - val userName: Flow = context.dataStore.data + val username: Flow = context.dataStore.data .map { preferences -> preferences[USER_NAME] ?: "" } - val userAvatar: Flow = context.dataStore.data + val userAvatarUrl: Flow = context.dataStore.data .map { preferences -> preferences[USER_AVATAR] ?: "" } diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_folder/AddFolderToClassViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_folder/AddFolderToClassViewModel.kt index 9c46c5cc..ca291af0 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_folder/AddFolderToClassViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_folder/AddFolderToClassViewModel.kt @@ -45,8 +45,8 @@ class AddFolderToClassViewModel @Inject constructor( viewModelScope.launch { val token = tokenManager.accessToken.firstOrNull() ?: return@launch val ownerId = appManager.userId.firstOrNull() ?: return@launch - val userAvatar = appManager.userAvatar.firstOrNull() ?: return@launch - val username = appManager.userName.firstOrNull() ?: return@launch + val userAvatar = appManager.userAvatarUrl.firstOrNull() ?: return@launch + val username = appManager.username.firstOrNull() ?: return@launch _uiState.update { it.copy( token = token, diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_study_set/AddStudySetToClassViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_study_set/AddStudySetToClassViewModel.kt index 857c05d6..582a0b65 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_study_set/AddStudySetToClassViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/classes/add_study_set/AddStudySetToClassViewModel.kt @@ -38,8 +38,8 @@ class AddStudySetToClassViewModel @Inject constructor( viewModelScope.launch { val token = tokenManager.accessToken.firstOrNull() ?: return@launch val ownerId = appManager.userId.firstOrNull() ?: return@launch - val userAvatar = appManager.userAvatar.firstOrNull() ?: return@launch - val username = appManager.userName.firstOrNull() ?: return@launch + val userAvatar = appManager.userAvatarUrl.firstOrNull() ?: return@launch + val username = appManager.username.firstOrNull() ?: return@launch _uiState.update { it.copy( token = token, diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreViewModel.kt index 3745902d..df4d55f5 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/explore/ExploreViewModel.kt @@ -16,6 +16,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update @@ -98,35 +99,39 @@ class ExploreViewModel @Inject constructor( private fun getTopStreaks() { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - streakRepository.getTopStreaks(token, 10).collect { resource -> - when (resource) { - is Resources.Loading -> { - _uiState.update { it.copy(isLoading = true) } - } - - is Resources.Success -> { - val topStreaks = resource.data ?: emptyList() - val streakOwner = topStreaks.find { it.userId == uiState.value.ownerId } - val rankOwner = - topStreaks.indexOfFirst { it.userId == uiState.value.ownerId } - .takeIf { it != -1 }?.plus(1) - _uiState.update { - it.copy( - isLoading = false, - topStreaks = topStreaks, - streakOwner = streakOwner, - rankOwner = rankOwner - ) + tokenManager.accessToken.collect { token -> + streakRepository.getTopStreaks(token = token ?: "", limit = 10) + .collect { resource -> + when (resource) { + is Resources.Loading -> { + _uiState.update { it.copy(isLoading = true) } + } + + is Resources.Success -> { + val topStreaks = resource.data ?: emptyList() + val streakOwner = + topStreaks.find { it.userId == uiState.value.ownerId } + val rankOwner = + topStreaks.indexOfFirst { it.userId == uiState.value.ownerId } + .takeIf { it != -1 }?.plus(1) + _uiState.update { + it.copy( + isLoading = false, + topStreaks = topStreaks, + streakOwner = streakOwner, + rankOwner = rankOwner + ) + } + } + + is Resources.Error -> { + _uiState.update { it.copy(isLoading = false) } + _uiEvent.send(ExploreUiEvent.Error(resource.message ?: "")) + } } } - - is Resources.Error -> { - _uiState.update { it.copy(isLoading = false) } - _uiEvent.send(ExploreUiEvent.Error(resource.message ?: "")) - } - } } + } } diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/folder/add_study_set/AddStudySetToFolderViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/folder/add_study_set/AddStudySetToFolderViewModel.kt index 810bcd7f..8e0488c2 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/folder/add_study_set/AddStudySetToFolderViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/folder/add_study_set/AddStudySetToFolderViewModel.kt @@ -40,8 +40,8 @@ class AddStudySetToFolderViewModel @Inject constructor( viewModelScope.launch { val token = tokenManager.accessToken.firstOrNull() ?: return@launch val ownerId = appManager.userId.firstOrNull() ?: return@launch - val userAvatar = appManager.userAvatar.firstOrNull() ?: return@launch - val username = appManager.userName.firstOrNull() ?: return@launch + val userAvatar = appManager.userAvatarUrl.firstOrNull() ?: return@launch + val username = appManager.username.firstOrNull() ?: return@launch _uiState.update { it.copy( token = token, diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeUiAction.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeUiAction.kt index e4048193..adbcf82a 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeUiAction.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeUiAction.kt @@ -9,6 +9,5 @@ sealed class HomeUiAction { data class OnChangeCustomerInfo(val customerInfo: CustomerInfo) : HomeUiAction() data class LoadNotifications(val userId: String) : HomeUiAction() data class MarkAsRead(val notificationId: String) : HomeUiAction() - data object RefreshNotifications : HomeUiAction() data object RefreshHome : HomeUiAction() } \ No newline at end of file diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeViewModel.kt index 3bef30e5..aec81993 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/home/HomeViewModel.kt @@ -23,6 +23,7 @@ import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update @@ -55,28 +56,41 @@ class HomeViewModel @Inject constructor( val userId = appManager.userId.firstOrNull() ?: "" _uiState.value = HomeUiState(userId = userId) initData() - updateStreak() } } private fun initData() { job?.cancel() job = viewModelScope.launch { - val studySetsDeferred = async { getRecentAccessStudySets() } - val foldersDeferred = async { getRecentAccessFolders() } - val classesDeferred = async { getRecentAccessClasses() } - val top5SubjectsDeferred = async { getTop5Subjects() } - val streaksDeferred = async { getStreaksByUserId() } - val customerInfoDeferred = async { getCustomerInfo() } - val notificationsDeferred = async { loadNotifications() } - - studySetsDeferred.await() - foldersDeferred.await() - classesDeferred.await() - top5SubjectsDeferred.await() - streaksDeferred.await() - customerInfoDeferred.await() - notificationsDeferred.await() + combine(tokenManager.accessToken, appManager.userId) { token, userId -> + token to userId + }.collect { (token, userId) -> + if (token?.isNotEmpty() == true && userId.isNotEmpty()) { + val studySetsDeferred = + async { getRecentAccessStudySets(token = token, userId = userId) } + val foldersDeferred = + async { getRecentAccessFolders(token = token, userId = userId) } + val classesDeferred = + async { getRecentAccessClasses(token = token, userId = userId) } + val top5SubjectsDeferred = async { getTop5Subjects(token = token) } + val streaksDeferred = + async { getStreaksByUserId(token = token, userId = userId) } + val customerInfoDeferred = async { getCustomerInfo() } + val notificationsDeferred = + async { loadNotifications(token = token, userId = userId) } + val updateStreakDeferred = + async { updateStreak(token = token, userId = userId) } + + studySetsDeferred.await() + foldersDeferred.await() + classesDeferred.await() + top5SubjectsDeferred.await() + streaksDeferred.await() + customerInfoDeferred.await() + notificationsDeferred.await() + updateStreakDeferred.await() + } + } } } @@ -97,17 +111,17 @@ class HomeViewModel @Inject constructor( } is HomeUiAction.LoadNotifications -> { - loadNotifications() + viewModelScope.launch { + val token = tokenManager.accessToken.firstOrNull() ?: "" + val userId = appManager.userId.firstOrNull() ?: "" + loadNotifications(token = token, userId = userId) + } } is HomeUiAction.MarkAsRead -> { markNotificationAsRead(event.notificationId) } - is HomeUiAction.RefreshNotifications -> { - loadNotifications() - } - HomeUiAction.RefreshHome -> { initData() } @@ -130,10 +144,8 @@ class HomeViewModel @Inject constructor( }) } - private fun getStreaksByUserId() { + private fun getStreaksByUserId(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" streakRepository.getStreaksByUserId(token, userId).collect { resource -> when (resource) { is Resources.Loading -> { @@ -169,10 +181,8 @@ class HomeViewModel @Inject constructor( }.distinct() } - private fun updateStreak() { + private fun updateStreak(token: String, userId: String) { viewModelScope.launch { - val userId = appManager.userId.firstOrNull() ?: "" - val token = tokenManager.accessToken.firstOrNull() ?: "" streakRepository.updateStreak(token, userId).collect { resource -> when (resource) { is Resources.Loading -> { @@ -200,10 +210,8 @@ class HomeViewModel @Inject constructor( } } - private fun loadNotifications() { + private fun loadNotifications(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" notificationRepository.loadNotifications(userId, token).collect { result -> when (result) { is Resources.Loading -> { @@ -259,9 +267,8 @@ class HomeViewModel @Inject constructor( } } - private fun getTop5Subjects() { + private fun getTop5Subjects(token: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" studySetRepository.getTop5Subject(token).collect { resource -> when (resource) { is Resources.Loading -> { @@ -297,10 +304,8 @@ class HomeViewModel @Inject constructor( } } - private fun getRecentAccessStudySets() { + private fun getRecentAccessStudySets(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" studySetRepository.getRecentAccessStudySet(token, userId).collect { resource -> when (resource) { is Resources.Loading -> { @@ -322,10 +327,8 @@ class HomeViewModel @Inject constructor( } } - private fun getRecentAccessFolders() { + private fun getRecentAccessFolders(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" folderRepository.getRecentAccessFolders(token, userId).collect { resource -> when (resource) { is Resources.Loading -> { @@ -347,10 +350,8 @@ class HomeViewModel @Inject constructor( } } - private fun getRecentAccessClasses() { + private fun getRecentAccessClasses(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" classRepository.getRecentAccessClass(token, userId).collect { resource -> when (resource) { is Resources.Loading -> { diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryScreen.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryScreen.kt index 22e3830e..6317715e 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryScreen.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryScreen.kt @@ -124,7 +124,7 @@ fun LibraryScreen( studySets = uiState.studySets, classes = uiState.classes, folders = uiState.folders, - avatarUrl = uiState.userAvatar, + avatarUrl = uiState.userAvatarUrl, username = uiState.username, onStudySetRefresh = { viewModel.onEvent(LibraryUiAction.RefreshStudySets) diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryUiState.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryUiState.kt index 5ec3f2ef..ce2baf24 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryUiState.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryUiState.kt @@ -1,17 +1,14 @@ package com.pwhs.quickmem.presentation.app.library import com.pwhs.quickmem.domain.model.classes.GetClassByOwnerResponseModel -import com.pwhs.quickmem.domain.model.classes.GetClassDetailResponseModel import com.pwhs.quickmem.domain.model.folder.GetFolderResponseModel import com.pwhs.quickmem.domain.model.study_set.GetStudySetResponseModel data class LibraryUiState( val title: String = "", val isLoading: Boolean = false, - val userAvatar: String = "", + val userAvatarUrl: String = "", val username: String = "", - val token: String = "", - val userId: String = "", val studySets: List = emptyList(), val classes: List = emptyList(), val folders: List = emptyList(), diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryViewModel.kt index 7920d598..fea9ea81 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/library/LibraryViewModel.kt @@ -9,11 +9,13 @@ import com.pwhs.quickmem.domain.repository.ClassRepository import com.pwhs.quickmem.domain.repository.FolderRepository import com.pwhs.quickmem.domain.repository.StudySetRepository import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update @@ -34,61 +36,81 @@ class LibraryViewModel @Inject constructor( private val _uiEvent = Channel() val uiEvent = _uiEvent.receiveAsFlow() + private var job: Job? = null + init { + initData() + } + + private fun initData() { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: return@launch - val ownerId = appManager.userId.firstOrNull() ?: return@launch - val userAvatar = appManager.userAvatar.firstOrNull() ?: return@launch - val username = appManager.userName.firstOrNull() ?: return@launch - _uiState.update { - it.copy( - token = token, - userId = ownerId, - userAvatar = userAvatar, - username = username - ) + combine(tokenManager.accessToken, appManager.userId) { token, userId -> + token to userId + }.collectLatest { (token, userId) -> + if (token?.isNotEmpty() == true && userId.isNotEmpty()) { + getUserInfo() + getStudySets(token = token, userId = userId) + getClasses(token = token, userId = userId) + getFolders(token = token, userId = userId) + } } - getStudySets() - getClasses() - getFolders() } - } fun onEvent(event: LibraryUiAction) { when (event) { is LibraryUiAction.Refresh -> { - getStudySets() - getClasses() - getFolders() + initData() } LibraryUiAction.RefreshStudySets -> { - viewModelScope.launch { + job?.cancel() + job = viewModelScope.launch { delay(500) - getStudySets() + val token = tokenManager.accessToken.firstOrNull() ?: "" + val userId = appManager.userId.firstOrNull() ?: "" + getStudySets(token = token, userId = userId) } } LibraryUiAction.RefreshClasses -> { - viewModelScope.launch { + job?.cancel() + job = viewModelScope.launch { delay(500) - getClasses() + val token = tokenManager.accessToken.firstOrNull() ?: "" + val userId = appManager.userId.firstOrNull() ?: "" + getClasses(token = token, userId = userId) } } LibraryUiAction.RefreshFolders -> { - viewModelScope.launch { + job?.cancel() + job = viewModelScope.launch { delay(500) - getFolders() + val token = tokenManager.accessToken.firstOrNull() ?: "" + val userId = appManager.userId.firstOrNull() ?: "" + getFolders(token = token, userId = userId) } } } } - private fun getStudySets() { + private fun getUserInfo() { + viewModelScope.launch { + val username = appManager.username.firstOrNull() ?: "" + val userAvatarUrl = appManager.userAvatarUrl.firstOrNull() ?: "" + _uiState.update { + it.copy( + username = username, + userAvatarUrl = userAvatarUrl + ) + } + } + } + + private fun getStudySets(token: String, userId: String) { viewModelScope.launch { - studySetRepository.getStudySetsByOwnerId(_uiState.value.token, _uiState.value.userId, null, null) + studySetRepository.getStudySetsByOwnerId(token, userId, null, null) .collectLatest { resources -> when (resources) { is Resources.Success -> { @@ -121,9 +143,9 @@ class LibraryViewModel @Inject constructor( } } - private fun getClasses() { + private fun getClasses(token: String, userId: String) { viewModelScope.launch { - classRepository.getClassByOwnerId(_uiState.value.token, _uiState.value.userId, null, null) + classRepository.getClassByOwnerId(token, userId, null, null) .collectLatest { resource -> when (resource) { is Resources.Error -> { @@ -156,9 +178,9 @@ class LibraryViewModel @Inject constructor( } } - private fun getFolders() { + private fun getFolders(token: String, userId: String) { viewModelScope.launch { - folderRepository.getFoldersByUserId(_uiState.value.token, _uiState.value.userId, null, null) + folderRepository.getFoldersByUserId(token, userId, null, null) .collectLatest { resources -> when (resources) { is Resources.Success -> { diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/ProfileViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/ProfileViewModel.kt index e13c6d4e..14ac1fdb 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/ProfileViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/ProfileViewModel.kt @@ -12,14 +12,15 @@ import com.revenuecat.purchases.Purchases import com.revenuecat.purchases.PurchasesError import com.revenuecat.purchases.interfaces.ReceiveCustomerInfoCallback import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -39,11 +40,25 @@ class ProfileViewModel @Inject constructor( private val _uiEvent = Channel() val uiEvent = _uiEvent.receiveAsFlow() + private var job: Job? = null + init { - loadProfile() - getUserProfile() - getCustomerInfo() - getStudyTime() + initData() + } + + private fun initData() { + viewModelScope.launch { + combine(tokenManager.accessToken, appManager.userId) { token, userId -> + token to userId + }.collectLatest { (token, userId) -> + if (token?.isNotEmpty() == true && userId.isNotEmpty()) { + loadProfile() + getUserProfile(token = token, userId = userId) + getCustomerInfo() + getStudyTime(token = token, userId = userId) + } + } + } } fun onEvent(event: ProfileUiAction) { @@ -57,10 +72,11 @@ class ProfileViewModel @Inject constructor( } ProfileUiAction.Refresh -> { - getUserProfile() - getUserProfile() - getCustomerInfo() - getStudyTime() + job?.cancel() + job = viewModelScope.launch { + delay(500) + initData() + } } } } @@ -81,10 +97,8 @@ class ProfileViewModel @Inject constructor( }) } - private fun getUserProfile() { + private fun getUserProfile(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" authRepository.getUserProfile(token, userId).collectLatest { resource -> when (resource) { @@ -123,7 +137,7 @@ class ProfileViewModel @Inject constructor( } viewModelScope.launch { try { - appManager.userName.combine(appManager.userAvatar) { username, avatar -> + appManager.username.combine(appManager.userAvatarUrl) { username, avatar -> _uiState.update { it.copy( isLoading = false, @@ -145,10 +159,8 @@ class ProfileViewModel @Inject constructor( } } - private fun getStudyTime() { + private fun getStudyTime(token: String, userId: String) { viewModelScope.launch { - val token = tokenManager.accessToken.firstOrNull() ?: "" - val userId = appManager.userId.firstOrNull() ?: "" studyTimeRepository.getStudyTimeByUser(token, userId).collectLatest { resource -> when (resource) { diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/choose_picture/ChoosePictureViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/choose_picture/ChoosePictureViewModel.kt index 30378cb9..96c6a9e5 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/choose_picture/ChoosePictureViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/profile/choose_picture/ChoosePictureViewModel.kt @@ -39,7 +39,7 @@ class ChoosePictureViewModel @Inject constructor( private fun getCurrentAvatar() { viewModelScope.launch { - val currentAvatarUrl = appManager.userAvatar.firstOrNull() + val currentAvatarUrl = appManager.userAvatarUrl.firstOrNull() if (currentAvatarUrl != null) { _uiState.update { it.copy( diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/settings/SettingsViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/settings/SettingsViewModel.kt index 698b70f3..360022ef 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/settings/SettingsViewModel.kt @@ -1,5 +1,7 @@ package com.pwhs.quickmem.presentation.app.settings +import android.app.Application +import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.pwhs.quickmem.core.datastore.AppManager @@ -28,8 +30,9 @@ class SettingsViewModel @Inject constructor( private val tokenManager: TokenManager, private val appManager: AppManager, private val authRepository: AuthRepository, - private val searchQueryRepository: SearchQueryRepository -) : ViewModel() { + private val searchQueryRepository: SearchQueryRepository, + application: Application +) : AndroidViewModel(application) { private val _uiState = MutableStateFlow(SettingUiState()) val uiState = _uiState.asStateFlow() @@ -98,7 +101,7 @@ class SettingsViewModel @Inject constructor( try { val userId = appManager.userId.firstOrNull() ?: "" val fullName = appManager.userFullName.firstOrNull() ?: "" - val username = appManager.userName.firstOrNull() ?: "" + val username = appManager.username.firstOrNull() ?: "" val role = appManager.userRole.firstOrNull() ?: "" val email = appManager.userEmail.firstOrNull() ?: "" val isPushNotificationsEnabled = appManager.pushNotifications.firstOrNull() ?: false diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_class/AddStudySetToClassesViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_class/AddStudySetToClassesViewModel.kt index 391ae5b1..19e6fca7 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_class/AddStudySetToClassesViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_class/AddStudySetToClassesViewModel.kt @@ -40,8 +40,8 @@ class AddStudySetToClassesViewModel @Inject constructor( viewModelScope.launch { val token = tokenManager.accessToken.firstOrNull() ?: return@launch val ownerId = appManager.userId.firstOrNull() ?: return@launch - val userAvatar = appManager.userAvatar.firstOrNull() ?: return@launch - val username = appManager.userName.firstOrNull() ?: return@launch + val userAvatar = appManager.userAvatarUrl.firstOrNull() ?: return@launch + val username = appManager.username.firstOrNull() ?: return@launch _uiState.update { it.copy( token = token, diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_folder/AddStudySetToFoldersViewModel.kt b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_folder/AddStudySetToFoldersViewModel.kt index d1d9545b..024b7bbd 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_folder/AddStudySetToFoldersViewModel.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/app/study_set/add_to_folder/AddStudySetToFoldersViewModel.kt @@ -40,8 +40,8 @@ class AddStudySetToFoldersViewModel @Inject constructor( viewModelScope.launch { val token = tokenManager.accessToken.firstOrNull() ?: return@launch val ownerId = appManager.userId.firstOrNull() ?: return@launch - val userAvatar = appManager.userAvatar.firstOrNull() ?: return@launch - val username = appManager.userName.firstOrNull() ?: return@launch + val userAvatar = appManager.userAvatarUrl.firstOrNull() ?: return@launch + val username = appManager.username.firstOrNull() ?: return@launch _uiState.update { it.copy( token = token, diff --git a/app/src/main/java/com/pwhs/quickmem/presentation/onboarding/OnboardingScreen.kt b/app/src/main/java/com/pwhs/quickmem/presentation/onboarding/OnboardingScreen.kt index f3bc46eb..8e695ad2 100644 --- a/app/src/main/java/com/pwhs/quickmem/presentation/onboarding/OnboardingScreen.kt +++ b/app/src/main/java/com/pwhs/quickmem/presentation/onboarding/OnboardingScreen.kt @@ -61,7 +61,7 @@ fun OnboardingScreen( }, onGetStartedClick = { viewModel.saveIsFirstRun(false) - navigator.navigate(WelcomeScreenDestination) { + navigator.navigate(WelcomeScreenDestination()) { popUpTo(NavGraphs.root) { saveState = false }