Skip to content

Commit

Permalink
Fix combine to always check current destination changes too
Browse files Browse the repository at this point in the history
This is important for scenarios where one may somehow end up in a
"logged in" destination without having their auth or demo status change
and there was nothing moving them away from the login screens.
  • Loading branch information
StylianosGakis committed Aug 5, 2024
1 parent abdbe20 commit d480e51
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hasRoute
import com.benasher44.uuid.Uuid
import com.google.firebase.messaging.RemoteMessage
import com.hedvig.android.app.notification.getMutablePendingIntentFlags
Expand All @@ -28,6 +27,7 @@ import com.hedvig.android.featureflags.FeatureManager
import com.hedvig.android.featureflags.flags.Feature
import com.hedvig.android.logger.LogPriority.ERROR
import com.hedvig.android.logger.logcat
import com.hedvig.android.navigation.compose.typedHasRoute
import com.hedvig.android.navigation.core.AppDestination
import com.hedvig.android.navigation.core.HedvigDeepLinkContainer
import com.hedvig.android.notification.core.NotificationSender
Expand Down Expand Up @@ -76,7 +76,7 @@ class ChatNotificationSender(
val currentDestination = CurrentDestinationInMemoryStorage.currentDestination
val currentlyOnDestinationWhichForbidsShowingChatNotification =
listOfDestinationsWhichShouldNotShowChatNotification.any { clazz ->
currentDestination?.hasRoute(clazz) == true
currentDestination?.typedHasRoute(clazz) == true
}
if (currentlyOnDestinationWhichForbidsShowingChatNotification && isAppForegrounded) {
logcat { "ChatNotificationSender ignoring notification since we are showing destination $currentDestination" }
Expand Down
12 changes: 5 additions & 7 deletions app/app/src/main/kotlin/com/hedvig/android/app/ui/HedvigApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavHostController
import coil.ImageLoader
Expand All @@ -41,6 +40,7 @@ import com.hedvig.android.navigation.core.HedvigDeepLinkContainer
import com.hedvig.android.notification.badge.data.tab.TabNotificationBadgeService
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
Expand Down Expand Up @@ -197,20 +197,18 @@ private fun LogoutOnInvalidCredentialsEffect(
combine(
authTokenService.authStatus.onEach(authStatusLog).filterNotNull().distinctUntilChanged(),
demoManager.isDemoMode().distinctUntilChanged(),
) { authStatus: AuthStatus, isDemoMode: Boolean ->
authStatus to isDemoMode
}.collect { (authStatus, isDemoMode) ->
val navBackStackEntry: NavBackStackEntry = hedvigAppState.navController.currentBackStackEntryFlow.first()
hedvigAppState.navController.currentBackStackEntryFlow,
) { authStatus: AuthStatus, isDemoMode: Boolean, navBackStackEntry ->
val isLoggedOut = navBackStackEntry.destination.hierarchy.any { navDestination ->
navDestination.typedHasRoute<LoginDestination>()
}
if (isLoggedOut) {
return@collect
return@combine
}
if (!isDemoMode && authStatus !is AuthStatus.LoggedIn) {
hedvigAppState.navigateToLoggedOut()
}
}
}.collect()
}
}
}

0 comments on commit d480e51

Please sign in to comment.