Skip to content

Commit

Permalink
fix(state): sửa lỗi đăng xuất đăng nhập lại thì state vẫn là tài khoả…
Browse files Browse the repository at this point in the history
…n cũ, ko phải là tài khoản mới
  • Loading branch information
nqmgaming committed Nov 29, 2024
1 parent d055e92 commit 6c1f633
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class AppManager(private val context: Context) {
.map { preferences ->
preferences[USER_FULL_NAME] ?: ""
}
val userName: Flow<String> = context.dataStore.data
val username: Flow<String> = context.dataStore.data
.map { preferences ->
preferences[USER_NAME] ?: ""
}
val userAvatar: Flow<String> = context.dataStore.data
val userAvatarUrl: Flow<String> = context.dataStore.data
.map { preferences ->
preferences[USER_AVATAR] ?: ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ?: ""))
}
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}
}

Expand All @@ -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()
}
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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 -> {
Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetStudySetResponseModel> = emptyList(),
val classes: List<GetClassByOwnerResponseModel> = emptyList(),
val folders: List<GetFolderResponseModel> = emptyList(),
Expand Down
Loading

0 comments on commit 6c1f633

Please sign in to comment.