Skip to content

Commit

Permalink
refactor: Remove runBlocking from global data store and return `Flo…
Browse files Browse the repository at this point in the history
…w<Boolean>` instead of `Boolean`.
  • Loading branch information
kl3jvi committed Nov 18, 2024
1 parent 0411eae commit f426685
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -198,10 +197,10 @@ class GlobalDataStore @Inject constructor(@ApplicationContext private val contex
it.contains(APP_LOCK_PASSCODE)
}

fun isAppLockPasscodeSet(): Boolean = runBlocking {
context.dataStore.data.map {
fun isAppLockPasscodeSet(): Flow<Boolean> {
return context.dataStore.data.map {
it.contains(APP_LOCK_PASSCODE)
}.first()
}
}

suspend fun clearAppLockPasscode() {
Expand Down
26 changes: 14 additions & 12 deletions app/src/main/kotlin/com/wire/android/ui/WireActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -374,19 +376,19 @@ class WireActivity : AppCompatActivity() {
onConfirm = {
featureFlagNotificationViewModel.dismissTeamAppLockDialog()
if (isTeamAppLockEnabled) {
val isUserAppLockSet =
featureFlagNotificationViewModel.isUserAppLockSet()
// No need to setup another app lock if the user already has one
if (!isUserAppLockSet) {
Intent(this@WireActivity, AppLockActivity::class.java)
.apply {
putExtra(AppLockActivity.SET_TEAM_APP_LOCK, true)
}.also {
startActivity(it)
featureFlagNotificationViewModel.isUserAppLockSet()
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.onEach { isUserAppLockSet ->
// No need to setup another app lock if the user already has one
when (isUserAppLockSet) {
true -> featureFlagNotificationViewModel.markTeamAppLockStatusAsNot()
false -> startActivity(
Intent(this@WireActivity, AppLockActivity::class.java).apply {
putExtra(AppLockActivity.SET_TEAM_APP_LOCK, true)
}
)
}
} else {
featureFlagNotificationViewModel.markTeamAppLockStatusAsNot()
}
}.launchIn(lifecycleScope)
} else {
with(featureFlagNotificationViewModel) {
markTeamAppLockStatusAsNot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.touchlab.kermit.Logger
import com.wire.android.appLogger
import com.wire.android.datastore.GlobalDataStore
import com.wire.android.di.KaliumCoreLogic
Expand All @@ -48,10 +49,14 @@ import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.fold
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand Down Expand Up @@ -161,23 +166,20 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}
}

private suspend fun setTeamAppLockFeatureFlag(userId: UserId) {
coreLogic.getSessionScope(userId).appLockTeamFeatureConfigObserver()
.distinctUntilChanged()
.collectLatest { appLockConfig ->
appLockConfig?.isStatusChanged?.let { isStatusChanged ->
val shouldBlockApp = if (isStatusChanged) {
true
} else {
(!isUserAppLockSet() && appLockConfig.isEnforced)
}

featureFlagState = featureFlagState.copy(
isTeamAppLockEnabled = appLockConfig.isEnforced,
shouldShowTeamAppLockDialog = shouldBlockApp
)
}
private fun setTeamAppLockFeatureFlag(userId: UserId) {
combine(
coreLogic.getSessionScope(userId).appLockTeamFeatureConfigObserver().distinctUntilChanged(),
isUserAppLockSet()
) { appLockConfig, isUserAppLockSet ->
Logger.i(TAG) { "AppLockConfig: $appLockConfig, isUserAppLockSet: $isUserAppLockSet" }
appLockConfig?.isStatusChanged?.let { isStatusChanged ->
val shouldBlockApp = isStatusChanged || (!isUserAppLockSet && appLockConfig.isEnforced)
featureFlagState = featureFlagState.copy(
isTeamAppLockEnabled = appLockConfig.isEnforced,
shouldShowTeamAppLockDialog = shouldBlockApp
)
}
}.launchIn(viewModelScope)
}

private suspend fun observeTeamSettingsSelfDeletionStatus(userId: UserId) {
Expand Down Expand Up @@ -295,7 +297,7 @@ class FeatureFlagNotificationViewModel @Inject constructor(
}
}

fun isUserAppLockSet() = globalDataStore.isAppLockPasscodeSet()
fun isUserAppLockSet(): Flow<Boolean> = globalDataStore.isAppLockPasscodeSet().distinctUntilChanged()

fun enrollE2EICertificate() {
featureFlagState = featureFlagState.copy(isE2EILoading = true, startGettingE2EICertificate = true)
Expand Down

0 comments on commit f426685

Please sign in to comment.