Skip to content

Commit

Permalink
fix: run observers on workers thread to avoid ANRs (WPB-6051) (#2857)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine authored Apr 5, 2024
1 parent c29437c commit c9069ba
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions app/src/main/kotlin/com/wire/android/ui/WireActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
Expand Down Expand Up @@ -107,14 +106,15 @@ import com.wire.android.util.deeplink.DeepLinkResult
import com.wire.android.util.ui.updateScreenSettings
import dagger.Lazy
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@OptIn(ExperimentalComposeUiApi::class)
@AndroidEntryPoint
@Suppress("TooManyFunctions")
class WireActivity : AppCompatActivity() {
Expand Down Expand Up @@ -157,7 +157,7 @@ class WireActivity : AppCompatActivity() {
appLogger.i("$TAG proximity sensor")
proximitySensorManager.initialize()

lifecycleScope.launch {
lifecycleScope.launch(Dispatchers.Default) {

appLogger.i("$TAG persistent connection status")
viewModel.observePersistentConnectionStatus()
Expand All @@ -173,10 +173,12 @@ class WireActivity : AppCompatActivity() {
InitialAppState.LOGGED_IN -> HomeScreenDestination
}
appLogger.i("$TAG composable content")
setComposableContent(startDestination) {
appLogger.i("$TAG splash hide")
shouldKeepSplashOpen = false
handleDeepLink(intent, savedInstanceState)
withContext(Dispatchers.Main) {
setComposableContent(startDestination) {
appLogger.i("$TAG splash hide")
shouldKeepSplashOpen = false
handleDeepLink(intent, savedInstanceState)
}
}
}
}
Expand Down Expand Up @@ -464,15 +466,17 @@ class WireActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()

lifecycleScope.launch {
lifecycleScope.launch(Dispatchers.Default) {
lockCodeTimeManager.get().observeAppLock()
// Listen to one flow in a lifecycle-aware manner using flowWithLifecycle
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.first().let {
if (it) {
startActivity(
Intent(this@WireActivity, AppLockActivity::class.java)
)
withContext(Dispatchers.Main) {
startActivity(
Intent(this@WireActivity, AppLockActivity::class.java)
)
}
}
}
}
Expand Down

0 comments on commit c9069ba

Please sign in to comment.