Skip to content

Commit

Permalink
fix: refactoring self user fetch [#WPB-15190]
Browse files Browse the repository at this point in the history
  • Loading branch information
sbakhtiarov committed Jan 15, 2025
1 parent b0aa25c commit ce12e33
Show file tree
Hide file tree
Showing 39 changed files with 100 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.wire.kalium.logic.feature.publicuser.GetKnownUserUseCase
import com.wire.kalium.logic.feature.publicuser.RefreshUsersWithoutMetadataUseCase
import com.wire.kalium.logic.feature.user.DeleteAccountUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import com.wire.kalium.logic.feature.user.GetUserInfoUseCase
import com.wire.kalium.logic.feature.user.IsPasswordRequiredUseCase
import com.wire.kalium.logic.feature.user.IsReadOnlyAccountUseCase
Expand Down Expand Up @@ -191,6 +192,11 @@ class UserModule {
fun provideGetSelfUseCase(userScope: UserScope): GetSelfUserUseCase =
userScope.getSelfUser

@ViewModelScoped
@Provides
fun provideObserveSelfUseCase(userScope: UserScope): ObserveSelfUserUseCase =
userScope.observeSelfUser

@ViewModelScoped
@Provides
fun provideGetAvatarAssetUseCase(userScope: UserScope): GetAvatarAssetUseCase =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class WireNotificationManager @Inject constructor(
} else {
userSessionScope.calls.getIncomingCalls()
}.map { calls ->
userSessionScope.users.getSelfUser().first()
userSessionScope.users.observeSelfUser().first()
.also { it.logIfEmptyUserName() }
.let { it.handle ?: it.name ?: "" } to calls
}
Expand All @@ -374,7 +374,7 @@ class WireNotificationManager @Inject constructor(
) {
val selfUserNameState = coreLogic.getSessionScope(userId)
.users
.getSelfUser()
.observeSelfUser()
.onEach { it.logIfEmptyUserName() }
.map { it.handle ?: it.name ?: "" }
.distinctUntilChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CallService : Service() {
) { outgoingCalls, establishedCalls ->
val calls = outgoingCalls + establishedCalls
calls.firstOrNull()?.let { call ->
val userName = userSessionScope.users.getSelfUser().first()
val userName = userSessionScope.users.observeSelfUser().first()
.also { it.logIfEmptyUserName() }
.let { it.handle ?: it.name ?: "" }
Either.Right(CallNotificationData(userId, call, userName))
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/kotlin/com/wire/android/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.wire.kalium.logic.feature.client.NeedsToRegisterClientUseCase
import com.wire.kalium.logic.feature.legalhold.LegalHoldStateForSelfUser
import com.wire.kalium.logic.feature.legalhold.ObserveLegalHoldStateForSelfUserUseCase
import com.wire.kalium.logic.feature.personaltoteamaccount.CanMigrateFromPersonalToTeamUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
Expand All @@ -50,7 +50,7 @@ class HomeViewModel @Inject constructor(
override val savedStateHandle: SavedStateHandle,
private val globalDataStore: GlobalDataStore,
private val dataStore: UserDataStore,
private val getSelf: GetSelfUserUseCase,
private val observeSelf: ObserveSelfUserUseCase,
private val needsToRegisterClient: NeedsToRegisterClientUseCase,
private val canMigrateFromPersonalToTeam: CanMigrateFromPersonalToTeamUseCase,
private val observeLegalHoldStatusForSelfUser: ObserveLegalHoldStateForSelfUserUseCase,
Expand Down Expand Up @@ -97,15 +97,15 @@ class HomeViewModel @Inject constructor(

fun checkRequirements(onRequirement: (HomeRequirement) -> Unit) {
viewModelScope.launch {
val userId = getSelf().first().id
val userId = observeSelf().first()
when {
shouldTriggerMigrationForUser(userId) ->
onRequirement(HomeRequirement.Migration(userId))
shouldTriggerMigrationForUser(userId.id) ->
onRequirement(HomeRequirement.Migration(userId.id))

needsToRegisterClient() -> // check if the client has been registered and open the proper screen if not
onRequirement(HomeRequirement.RegisterDevice)

getSelf().first().handle.isNullOrEmpty() -> // check if the user handle has been set and open the proper screen if not
userId.handle.isNullOrEmpty() -> // check if the user handle has been set and open the proper screen if not
onRequirement(HomeRequirement.CreateAccountUsername)

shouldDisplayWelcomeToARScreen() -> {
Expand All @@ -120,7 +120,7 @@ class HomeViewModel @Inject constructor(

private fun loadUserAvatar() {
viewModelScope.launch {
getSelf().collect { selfUser ->
observeSelf().collect { selfUser ->
homeState = homeState.copy(
userAvatarData = UserAvatarData(
asset = selfUser.previewPicture?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ForgotLockScreenViewModel @Inject constructor(
is IsPasswordRequiredUseCase.Result.Success -> {
state.copy(
dialogState = ForgotLockCodeDialogState.Visible(
username = getSelf().firstOrNull()?.name ?: "",
username = getSelf()?.name ?: "",
passwordRequired = isPasswordRequiredResult.value,
resetDeviceEnabled = !isPasswordRequiredResult.value,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import com.wire.kalium.logic.feature.call.usecase.ObserveOngoingCallsUseCase
import com.wire.kalium.logic.feature.conversation.ObserveConversationDetailsUseCase
import com.wire.kalium.logic.feature.conversation.ObserveDegradedConversationNotifiedUseCase
import com.wire.kalium.logic.feature.conversation.SetUserInformedAboutVerificationUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import com.wire.kalium.logic.sync.ObserveSyncStateUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
Expand All @@ -70,7 +70,7 @@ class ConversationListCallViewModel @Inject constructor(
private val setUserInformedAboutVerification: SetUserInformedAboutVerificationUseCase,
private val observeDegradedConversationNotified: ObserveDegradedConversationNotifiedUseCase,
private val observeConferenceCallingEnabled: ObserveConferenceCallingEnabledUseCase,
private val getSelf: GetSelfUserUseCase
private val observeSelf: ObserveSelfUserUseCase
) : SavedStateViewModel(savedStateHandle) {

private val conversationNavArgs: ConversationNavArgs = savedStateHandle.navArgs()
Expand Down Expand Up @@ -110,7 +110,7 @@ class ConversationListCallViewModel @Inject constructor(

private fun observeSelfTeamRole() {
viewModelScope.launch {
getSelf().collectLatest { self ->
observeSelf().collectLatest { self ->
selfTeamRole.value = self.userType
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import com.wire.kalium.logic.feature.team.DeleteTeamConversationUseCase
import com.wire.kalium.logic.feature.team.GetUpdatedSelfTeamUseCase
import com.wire.kalium.logic.feature.team.Result
import com.wire.kalium.logic.feature.user.GetDefaultProtocolUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import com.wire.kalium.logic.feature.user.IsMLSEnabledUseCase
import com.wire.kalium.logic.functional.getOrNull
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -91,7 +91,7 @@ class GroupConversationDetailsViewModel @Inject constructor(
private val observeConversationMembers: ObserveParticipantsForConversationUseCase,
private val updateConversationAccessRole: UpdateConversationAccessRoleUseCase,
private val getSelfTeam: GetUpdatedSelfTeamUseCase,
private val observerSelfUser: GetSelfUserUseCase,
private val observerSelfUser: ObserveSelfUserUseCase,
private val deleteTeamConversation: DeleteTeamConversationUseCase,
private val removeMemberFromConversation: RemoveMemberFromConversationUseCase,
private val updateConversationMutedStatus: UpdateConversationMutedStatusUseCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.wire.kalium.logic.data.user.SelfUser
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.conversation.ObserveConversationDetailsUseCase
import com.wire.kalium.logic.feature.conversation.ObserveConversationMembersUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterIsInstance
Expand All @@ -36,11 +36,11 @@ import javax.inject.Inject
class ObserveConversationRoleForUserUseCase @Inject constructor(
private val observeConversationMembers: ObserveConversationMembersUseCase,
private val observeConversationDetails: ObserveConversationDetailsUseCase,
private val getSelfUser: GetSelfUserUseCase
private val observeSelfUser: ObserveSelfUserUseCase
) {
suspend operator fun invoke(conversationId: ConversationId, userId: UserId): Flow<ConversationRoleData> =
combine(
getSelfUser(),
observeSelfUser(),
observeConversationDetails(conversationId)
.filterIsInstance<ObserveConversationDetailsUseCase.Result.Success>() // TODO handle StorageFailure
.map { it.conversationDetails },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.wire.kalium.logic.feature.conversation.folder.GetFavoriteFolderUseCas
import com.wire.kalium.logic.feature.conversation.folder.ObserveConversationsFromFolderUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
Expand All @@ -47,7 +46,7 @@ class GetConversationsFromSearchUseCase @Inject constructor(
private val observeConversationsFromFromFolder: ObserveConversationsFromFolderUseCase,
private val userTypeMapper: UserTypeMapper,
private val dispatchers: DispatcherProvider,
private val observeSelfUser: GetSelfUserUseCase
private val getSelfUser: GetSelfUserUseCase
) {
suspend operator fun invoke(
searchQuery: String = "",
Expand Down Expand Up @@ -96,7 +95,7 @@ class GetConversationsFromSearchUseCase @Inject constructor(
it.toConversationItem(
userTypeMapper = userTypeMapper,
searchQuery = searchQuery,
selfUserTeamId = observeSelfUser().firstOrNull()?.teamId
selfUserTeamId = getSelfUser()?.teamId
)
}
}.flowOn(dispatchers.io())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
Expand Down Expand Up @@ -150,7 +149,7 @@ class ConversationListViewModelImpl @AssistedInject constructor(
private val observeLegalHoldStateForSelfUser: ObserveLegalHoldStateForSelfUserUseCase,
@CurrentAccount val currentAccount: UserId,
private val userTypeMapper: UserTypeMapper,
private val observeSelfUser: GetSelfUserUseCase,
private val getSelfUser: GetSelfUserUseCase,
private val workManager: WorkManager
) : ConversationListViewModel, ViewModel() {

Expand Down Expand Up @@ -262,7 +261,7 @@ class ConversationListViewModelImpl @AssistedInject constructor(
conversationDetails.toConversationItem(
userTypeMapper = userTypeMapper,
searchQuery = searchQuery,
selfUserTeamId = observeSelfUser().firstOrNull()?.teamId
selfUserTeamId = getSelfUser()?.teamId
).hideIndicatorForSelfUserUnderLegalHold(isSelfUserUnderLegalHold)
} to searchQuery
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.type.UserType
import com.wire.kalium.logic.feature.conversation.CreateGroupConversationUseCase
import com.wire.kalium.logic.feature.user.GetDefaultProtocolUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -52,7 +52,7 @@ import javax.inject.Inject
@HiltViewModel
class NewConversationViewModel @Inject constructor(
private val createGroupConversation: CreateGroupConversationUseCase,
private val getSelfUser: GetSelfUserUseCase,
private val observeSelfUser: ObserveSelfUserUseCase,
getDefaultProtocol: GetDefaultProtocolUseCase
) : ViewModel() {

Expand All @@ -79,7 +79,7 @@ class NewConversationViewModel @Inject constructor(

init {
viewModelScope.launch {
val selfUser = getSelfUser().first()
val selfUser = observeSelfUser().first()
val isSelfTeamMember = selfUser.teamId != null
val isSelfExternalTeamMember = selfUser.userType == UserType.EXTERNAL
newGroupState = newGroupState.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import androidx.lifecycle.viewModelScope
import com.wire.android.datastore.GlobalDataStore
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.feature.featureConfig.ObserveIsAppLockEditableUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand All @@ -39,7 +39,7 @@ import javax.inject.Inject
class SettingsViewModel @Inject constructor(
private val globalDataStore: GlobalDataStore,
private val observeIsAppLockEditable: ObserveIsAppLockEditableUseCase,
private val getSelf: GetSelfUserUseCase,
private val getSelf: ObserveSelfUserUseCase,
private val dispatchers: DispatcherProvider,
) : ViewModel() {
var state by mutableStateOf(SettingsState())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.wire.android.appLogger
import com.wire.android.navigation.SavedStateViewModel
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.feature.team.GetUpdatedSelfTeamUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase
import com.wire.kalium.logic.feature.user.IsPasswordRequiredUseCase
import com.wire.kalium.logic.feature.user.IsReadOnlyAccountUseCase
Expand All @@ -50,7 +50,7 @@ import kotlin.properties.Delegates
@HiltViewModel
class MyAccountViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val getSelf: GetSelfUserUseCase,
private val getSelf: ObserveSelfUserUseCase,
private val getSelfTeam: GetUpdatedSelfTeamUseCase,
private val isSelfATeamMember: IsSelfATeamMemberUseCase,
private val serverConfig: SelfServerConfigUseCase,
Expand Down Expand Up @@ -123,7 +123,7 @@ class MyAccountViewModel @Inject constructor(
}
}

private suspend fun fetchSelfUser() {
private fun fetchSelfUser() {
viewModelScope.launch {
val self = getSelf().flowOn(dispatchers.io()).shareIn(this, SharingStarted.WhileSubscribed(1))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.UpdateDisplayNameUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -47,7 +46,7 @@ class ChangeDisplayNameViewModel @Inject constructor(

init {
viewModelScope.launch {
getSelf().firstOrNull()?.name.orEmpty().let { currentDisplayName ->
getSelf()?.name.orEmpty().let { currentDisplayName ->
textState.setTextAndPlaceCursorAtEnd(currentDisplayName)
textState.textAsFlow().collectLatest {
displayNameState = displayNameState.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.UpdateEmailUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -48,7 +47,7 @@ class ChangeEmailViewModel @Inject constructor(

init {
viewModelScope.launch {
getSelf().firstOrNull()?.email?.let { currentEmail ->
getSelf()?.email?.let { currentEmail ->
textState.setTextAndPlaceCursorAtEnd(currentEmail)
textState.textAsFlow().collectLatest {
val isValidEmail = Patterns.EMAIL_ADDRESS.matcher(it.trim()).matches()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.wire.kalium.logic.feature.user.SetUserHandleResult
import com.wire.kalium.logic.feature.user.SetUserHandleUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -52,7 +51,7 @@ class ChangeHandleViewModel @Inject constructor(

init {
viewModelScope.launch {
getSelf().firstOrNull()?.handle.orEmpty().let { currentHandle ->
getSelf()?.handle.orEmpty().let { currentHandle ->
textState.setTextAndPlaceCursorAtEnd(currentHandle)
textState.textAsFlow().collectLatest { newHandle ->
state = when (validateHandle(newHandle.toString())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.ui.navArgs
import com.wire.android.util.fileDateTime
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import com.wire.kalium.util.DateTimeUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.first
Expand All @@ -32,7 +32,7 @@ import javax.inject.Inject
@HiltViewModel
class E2eiCertificateDetailsViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val observerSelfUser: GetSelfUserUseCase,
private val observerSelfUser: ObserveSelfUserUseCase,
) : ViewModel() {
private val navArgs: E2eiCertificateDetailsScreenNavArgs =
savedStateHandle.navArgs()
Expand Down
Loading

0 comments on commit ce12e33

Please sign in to comment.