Skip to content

Commit

Permalink
fix: Remove direct usage of Lazy<CoreLogic> (WPB-10304) (#3311)
Browse files Browse the repository at this point in the history
Signed-off-by: alexandreferris <[email protected]>
(cherry picked from commit 443db9d)
  • Loading branch information
alexandreferris authored and MohamadJaara committed Aug 13, 2024
1 parent f499d1c commit 9a95bea
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 45 deletions.
16 changes: 8 additions & 8 deletions app/src/main/kotlin/com/wire/android/ui/WireActivityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import javax.inject.Inject
@OptIn(ExperimentalCoroutinesApi::class)
@HiltViewModel
class WireActivityViewModel @Inject constructor(
@KaliumCoreLogic private val coreLogic: Lazy<CoreLogic>,
@KaliumCoreLogic private val coreLogic: CoreLogic,
private val dispatchers: DispatcherProvider,
private val currentSessionFlow: Lazy<CurrentSessionFlowUseCase>,
private val doesValidSessionExist: Lazy<DoesValidSessionExistUseCase>,
Expand Down Expand Up @@ -309,11 +309,11 @@ class WireActivityViewModel @Inject constructor(

@VisibleForTesting
internal suspend fun canLoginThroughDeepLinks() = viewModelScope.async {
coreLogic.get().getGlobalScope().session.currentSession().takeIf {
coreLogic.getGlobalScope().session.currentSession().takeIf {
it is CurrentSessionResult.Success
}?.let {
val currentUserId = (it as CurrentSessionResult.Success).accountInfo.userId
coreLogic.get().getSessionScope(currentUserId).calls.establishedCall().first().isEmpty()
coreLogic.getSessionScope(currentUserId).calls.establishedCall().first().isEmpty()
} ?: true
}

Expand Down Expand Up @@ -395,11 +395,11 @@ class WireActivityViewModel @Inject constructor(
switchAccountActions: SwitchAccountActions
) {
viewModelScope.launch {
coreLogic.get().getGlobalScope().session.currentSession().takeIf {
coreLogic.getGlobalScope().session.currentSession().takeIf {
it is CurrentSessionResult.Success
}?.let {
val currentUserId = (it as CurrentSessionResult.Success).accountInfo.userId
coreLogic.get().getSessionScope(currentUserId).logout(LogoutReason.SELF_HARD_LOGOUT)
coreLogic.getSessionScope(currentUserId).logout(LogoutReason.SELF_HARD_LOGOUT)
clearUserData(currentUserId)
}
accountSwitch.get().invoke(SwitchAccountParam.TryToSwitchToNextAccount).also {
Expand Down Expand Up @@ -475,11 +475,11 @@ class WireActivityViewModel @Inject constructor(
key: String,
domain: String?,
onSuccess: (ConversationId) -> Unit
) = when (val currentSession = coreLogic.get().getGlobalScope().session.currentSession()) {
) = when (val currentSession = coreLogic.getGlobalScope().session.currentSession()) {
is CurrentSessionResult.Failure.Generic -> null
CurrentSessionResult.Failure.SessionNotFound -> null
is CurrentSessionResult.Success -> {
coreLogic.get().sessionScope(currentSession.accountInfo.userId) {
coreLogic.sessionScope(currentSession.accountInfo.userId) {
when (val result = conversations.checkIConversationInviteCode(code, key, domain)) {
is CheckConversationInviteCodeUseCase.Result.Success -> {
if (result.isSelfMember) {
Expand Down Expand Up @@ -533,7 +533,7 @@ class WireActivityViewModel @Inject constructor(

fun observePersistentConnectionStatus() {
viewModelScope.launch {
coreLogic.get().getGlobalScope().observePersistentWebSocketConnectionStatus()
coreLogic.getGlobalScope().observePersistentWebSocketConnectionStatus()
.let { result ->
when (result) {
is ObservePersistentWebSocketConnectionStatusUseCase.Result.Failure -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.data.sync.SyncState
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import dagger.Lazy
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
Expand All @@ -49,7 +48,7 @@ import javax.inject.Inject
@HiltViewModel
class CommonTopAppBarViewModel @Inject constructor(
private val currentScreenManager: CurrentScreenManager,
@KaliumCoreLogic private val coreLogic: Lazy<CoreLogic>,
@KaliumCoreLogic private val coreLogic: CoreLogic
) : ViewModel() {

var state by mutableStateOf(CommonTopAppBarState())
Expand All @@ -59,7 +58,7 @@ class CommonTopAppBarViewModel @Inject constructor(
currentScreenManager.observeCurrentScreen(viewModelScope)

private fun connectivityFlow(userId: UserId): Flow<Connectivity> =
coreLogic.get().sessionScope(userId) {
coreLogic.sessionScope(userId) {
observeSyncState().map {
when (it) {
is SyncState.Failed, SyncState.Waiting -> Connectivity.WAITING_CONNECTION
Expand All @@ -71,7 +70,7 @@ class CommonTopAppBarViewModel @Inject constructor(

@VisibleForTesting
internal suspend fun activeCallFlow(userId: UserId): Flow<Call?> =
coreLogic.get().sessionScope(userId) {
coreLogic.sessionScope(userId) {
combine(
calls.establishedCall(),
calls.getIncomingCalls(),
Expand All @@ -85,7 +84,7 @@ class CommonTopAppBarViewModel @Inject constructor(

init {
viewModelScope.launch {
coreLogic.get().globalScope {
coreLogic.globalScope {
session.currentSessionFlow().flatMapLatest {
when (it) {
is CurrentSessionResult.Failure.Generic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.user.E2EIRequiredResult
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.fold
import dagger.Lazy
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -55,7 +54,7 @@ import javax.inject.Inject
@Suppress("TooManyFunctions")
@HiltViewModel
class FeatureFlagNotificationViewModel @Inject constructor(
@KaliumCoreLogic private val coreLogic: Lazy<CoreLogic>,
@KaliumCoreLogic private val coreLogic: CoreLogic,
private val currentSessionFlow: CurrentSessionFlowUseCase,
private val globalDataStore: GlobalDataStore,
private val disableAppLockUseCase: DisableAppLockUseCase,
Expand Down Expand Up @@ -103,7 +102,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
featureFlagState = FeatureFlagState() // new session, clear feature flag state to default and wait until synced
currentSessionResult.accountInfo.userId.let { userId ->
currentUserId = userId
coreLogic.get().getSessionScope(userId).observeSyncState()
coreLogic.getSessionScope(userId).observeSyncState()
.firstOrNull { it == SyncState.Live }?.let {
observeStatesAfterInitialSync(userId)
}
Expand All @@ -126,13 +125,13 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}

private suspend fun observeShouldNotifyForRevokedCertificate(userId: UserId) {
coreLogic.get().getSessionScope(userId).observeShouldNotifyForRevokedCertificate().collect {
coreLogic.getSessionScope(userId).observeShouldNotifyForRevokedCertificate().collect {
featureFlagState = featureFlagState.copy(shouldShowE2eiCertificateRevokedDialog = it)
}
}

private suspend fun setFileSharingState(userId: UserId) {
coreLogic.get().getSessionScope(userId).observeFileSharingStatus().collect { fileSharingStatus ->
coreLogic.getSessionScope(userId).observeFileSharingStatus().collect { fileSharingStatus ->
val state: FeatureFlagState.FileSharingState = fileSharingStatus.state.toFeatureFlagState()
featureFlagState = featureFlagState.copy(
isFileSharingState = state,
Expand All @@ -142,7 +141,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}

private suspend fun setGuestRoomLinkFeatureFlag(userId: UserId) {
coreLogic.get().getSessionScope(userId).observeGuestRoomLinkFeatureFlag()
coreLogic.getSessionScope(userId).observeGuestRoomLinkFeatureFlag()
.collect { guestRoomLinkStatus ->
guestRoomLinkStatus.isGuestRoomLinkEnabled?.let {
featureFlagState = featureFlagState.copy(isGuestRoomLinkEnabled = it)
Expand All @@ -154,7 +153,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}

private suspend fun setTeamAppLockFeatureFlag(userId: UserId) {
coreLogic.get().getSessionScope(userId).appLockTeamFeatureConfigObserver()
coreLogic.getSessionScope(userId).appLockTeamFeatureConfigObserver()
.distinctUntilChanged()
.collectLatest { appLockConfig ->
appLockConfig?.isStatusChanged?.let { isStatusChanged ->
Expand All @@ -173,7 +172,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}

private suspend fun observeTeamSettingsSelfDeletionStatus(userId: UserId) {
coreLogic.get().getSessionScope(userId).observeTeamSettingsSelfDeletionStatus()
coreLogic.getSessionScope(userId).observeTeamSettingsSelfDeletionStatus()
.collect { teamSettingsSelfDeletingStatus ->
val areSelfDeletedMessagesEnabled =
teamSettingsSelfDeletingStatus.enforcedSelfDeletionTimer !is TeamSelfDeleteTimer.Disabled
Expand All @@ -197,7 +196,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}

private suspend fun setE2EIRequiredState(userId: UserId) {
coreLogic.get().getSessionScope(userId).observeE2EIRequired().collect { result ->
coreLogic.getSessionScope(userId).observeE2EIRequired().collect { result ->
val state = when (result) {
E2EIRequiredResult.NoGracePeriod.Create -> FeatureFlagState.E2EIRequired.NoGracePeriod.Create
E2EIRequiredResult.NoGracePeriod.Renew -> FeatureFlagState.E2EIRequired.NoGracePeriod.Renew
Expand All @@ -216,15 +215,15 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}

private suspend fun observeCallEndedBecauseOfConversationDegraded(userId: UserId) =
coreLogic.get().getSessionScope(userId).calls.observeEndCallDialog().collect {
coreLogic.getSessionScope(userId).calls.observeEndCallDialog().collect {
featureFlagState = featureFlagState.copy(showCallEndedBecauseOfConversationDegraded = true)
}

fun dismissSelfDeletingMessagesDialog() {
featureFlagState = featureFlagState.copy(shouldShowSelfDeletingMessagesDialog = false)
viewModelScope.launch {
currentUserId?.let {
coreLogic.get().getSessionScope(it).markSelfDeletingMessagesAsNotified()
coreLogic.getSessionScope(it).markSelfDeletingMessagesAsNotified()
}
}
}
Expand All @@ -233,22 +232,22 @@ class FeatureFlagNotificationViewModel @Inject constructor(
featureFlagState = featureFlagState.copy(shouldShowE2eiCertificateRevokedDialog = false)
currentUserId?.let {
viewModelScope.launch {
coreLogic.get().getSessionScope(it).markNotifyForRevokedCertificateAsNotified()
coreLogic.getSessionScope(it).markNotifyForRevokedCertificateAsNotified()
}
}
}

fun dismissFileSharingDialog() {
featureFlagState = featureFlagState.copy(showFileSharingDialog = false)
viewModelScope.launch {
currentUserId?.let { coreLogic.get().getSessionScope(it).markFileSharingStatusAsNotified() }
currentUserId?.let { coreLogic.getSessionScope(it).markFileSharingStatusAsNotified() }
}
}

fun dismissGuestRoomLinkDialog() {
viewModelScope.launch {
currentUserId?.let {
coreLogic.get().getSessionScope(it).markGuestLinkFeatureFlagAsNotChanged()
coreLogic.getSessionScope(it).markGuestLinkFeatureFlagAsNotChanged()
}
}
featureFlagState = featureFlagState.copy(shouldShowGuestRoomLinkDialog = false)
Expand All @@ -261,7 +260,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
fun markTeamAppLockStatusAsNot() {
viewModelScope.launch {
currentUserId?.let {
coreLogic.get().getSessionScope(it).markTeamAppLockStatusAsNotified()
coreLogic.getSessionScope(it).markTeamAppLockStatusAsNotified()
}
}
}
Expand Down Expand Up @@ -318,7 +317,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
)
currentUserId?.let { userId ->
viewModelScope.launch {
coreLogic.get().getSessionScope(userId).markE2EIRequiredAsNotified(result.timeLeft)
coreLogic.getSessionScope(userId).markE2EIRequiredAsNotified(result.timeLeft)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.wire.kalium.logic.feature.legalhold.LegalHoldState
import com.wire.kalium.logic.feature.legalhold.MarkLegalHoldChangeAsNotifiedForSelfUseCase
import com.wire.kalium.logic.feature.legalhold.ObserveLegalHoldChangeNotifiedForSelfUseCase
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import dagger.Lazy
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -43,14 +42,14 @@ import javax.inject.Inject

@HiltViewModel
class LegalHoldDeactivatedViewModel @Inject constructor(
@KaliumCoreLogic private val coreLogic: Lazy<CoreLogic>,
@KaliumCoreLogic private val coreLogic: CoreLogic
) : ViewModel() {

var state: LegalHoldDeactivatedState by mutableStateOf(LegalHoldDeactivatedState.Hidden)
private set

private fun <T> currentSessionFlow(noSession: T, session: suspend UserSessionScope.(UserId) -> Flow<T>): Flow<T> =
coreLogic.get().getGlobalScope().session.currentSessionFlow()
coreLogic.getGlobalScope().session.currentSessionFlow()
.flatMapLatest { currentSessionResult ->
when (currentSessionResult) {
is CurrentSessionResult.Failure.Generic -> {
Expand All @@ -60,7 +59,7 @@ class LegalHoldDeactivatedViewModel @Inject constructor(

CurrentSessionResult.Failure.SessionNotFound -> flowOf(noSession)
is CurrentSessionResult.Success ->
currentSessionResult.accountInfo.userId.let { coreLogic.get().getSessionScope(it).session(it) }
currentSessionResult.accountInfo.userId.let { coreLogic.getSessionScope(it).session(it) }
}
}

Expand All @@ -79,7 +78,7 @@ class LegalHoldDeactivatedViewModel @Inject constructor(
when (it.legalHoldState) {
is LegalHoldState.Disabled -> LegalHoldDeactivatedState.Visible(userId)
is LegalHoldState.Enabled -> { // for enabled we don't show the dialog, just mark as already notified
coreLogic.get().getSessionScope(userId).markLegalHoldChangeAsNotifiedForSelf()
coreLogic.getSessionScope(userId).markLegalHoldChangeAsNotifiedForSelf()
LegalHoldDeactivatedState.Hidden
}
}
Expand All @@ -92,7 +91,7 @@ class LegalHoldDeactivatedViewModel @Inject constructor(
fun dismiss() {
viewModelScope.launch {
(state as? LegalHoldDeactivatedState.Visible)?.let {
coreLogic.get().getSessionScope(it.userId).markLegalHoldChangeAsNotifiedForSelf().let {
coreLogic.getSessionScope(it.userId).markLegalHoldChangeAsNotifiedForSelf().let {
if (it is MarkLegalHoldChangeAsNotifiedForSelfUseCase.Result.Success) {
state = LegalHoldDeactivatedState.Hidden
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.wire.kalium.logic.feature.legalhold.ApproveLegalHoldRequestUseCase
import com.wire.kalium.logic.feature.legalhold.ObserveLegalHoldRequestUseCase
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.user.IsPasswordRequiredUseCase
import dagger.Lazy
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -51,7 +50,7 @@ import javax.inject.Inject
@HiltViewModel
class LegalHoldRequestedViewModel @Inject constructor(
private val validatePassword: ValidatePasswordUseCase,
@KaliumCoreLogic private val coreLogic: Lazy<CoreLogic>,
@KaliumCoreLogic private val coreLogic: CoreLogic
) : ViewModel() {

val passwordTextState: TextFieldState = TextFieldState()
Expand Down Expand Up @@ -92,7 +91,7 @@ class LegalHoldRequestedViewModel @Inject constructor(
}.stateIn(viewModelScope, SharingStarted.Eagerly, LegalHoldRequestData.None)

private fun <T> currentSessionFlow(noSession: T, session: UserSessionScope.(UserId) -> Flow<T>): Flow<T> =
coreLogic.get().getGlobalScope().session.currentSessionFlow()
coreLogic.getGlobalScope().session.currentSessionFlow()
.flatMapLatest { currentSessionResult ->
when (currentSessionResult) {
is CurrentSessionResult.Failure.Generic -> {
Expand All @@ -102,7 +101,7 @@ class LegalHoldRequestedViewModel @Inject constructor(

CurrentSessionResult.Failure.SessionNotFound -> flowOf(noSession)
is CurrentSessionResult.Success ->
currentSessionResult.accountInfo.userId.let { coreLogic.get().getSessionScope(it).session(it) }
currentSessionResult.accountInfo.userId.let { coreLogic.getSessionScope(it).session(it) }
}
}

Expand Down Expand Up @@ -155,7 +154,7 @@ class LegalHoldRequestedViewModel @Inject constructor(
} else {
val password = if (it.requiresPassword) passwordTextState.text.toString() else null
viewModelScope.launch {
coreLogic.get().sessionScope(it.userId) {
coreLogic.sessionScope(it.userId) {
approveLegalHoldRequest(password).let { approveLegalHoldResult ->
state = when (approveLegalHoldResult) {
is ApproveLegalHoldRequestUseCase.Result.Success ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class WireActivityViewModelTest {

private val viewModel by lazy {
WireActivityViewModel(
coreLogic = { coreLogic },
coreLogic = coreLogic,
dispatchers = TestDispatcherProvider(),
currentSessionFlow = { currentSessionFlow },
doesValidSessionExist = { doesValidSessionExist },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class CommonTopAppBarViewModelTest {
private val commonTopAppBarViewModel by lazy {
CommonTopAppBarViewModel(
currentScreenManager = currentScreenManager,
coreLogic = { coreLogic }
coreLogic = coreLogic
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class FeatureFlagNotificationViewModelTest {

val viewModel: FeatureFlagNotificationViewModel by lazy {
FeatureFlagNotificationViewModel(
coreLogic = { coreLogic },
coreLogic = coreLogic,
currentSessionFlow = currentSessionFlow,
globalDataStore = globalDataStore,
disableAppLockUseCase = disableAppLockUseCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LegalHoldDeactivatedViewModelTest {

@MockK
lateinit var coreLogic: CoreLogic
val viewModel by lazy { LegalHoldDeactivatedViewModel(coreLogic = { coreLogic }) }
val viewModel by lazy { LegalHoldDeactivatedViewModel(coreLogic = coreLogic) }

init { MockKAnnotations.init(this) }
fun withNotCurrentSession() = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class LegalHoldRequestedViewModelTest {
val viewModel by lazy {
LegalHoldRequestedViewModel(
validatePassword = validatePassword,
coreLogic = { coreLogic }
coreLogic = coreLogic
)
}

Expand Down

0 comments on commit 9a95bea

Please sign in to comment.