@@ -23,9 +23,14 @@ import com.getcode.navigation.screens.LoginScreen
2323import com.getcode.util.DeeplinkHandler
2424import com.getcode.util.getActivity
2525import kotlinx.coroutines.delay
26+ import kotlinx.coroutines.flow.combine
27+ import kotlinx.coroutines.flow.distinctUntilChanged
2628import kotlinx.coroutines.flow.filter
2729import kotlinx.coroutines.flow.filterNotNull
30+ import kotlinx.coroutines.flow.flatMapLatest
31+ import kotlinx.coroutines.flow.flowOf
2832import kotlinx.coroutines.flow.launchIn
33+ import kotlinx.coroutines.flow.map
2934import kotlinx.coroutines.flow.mapNotNull
3035import kotlinx.coroutines.flow.onEach
3136import kotlinx.coroutines.launch
@@ -50,26 +55,25 @@ fun AuthCheck(
5055 }
5156
5257 LaunchedEffect (isAuthenticated) {
53- Timber .tag(AUTH_NAV ).d(" authenticated=$isAuthenticated " )
5458 isAuthenticated?.let { authenticated ->
55- // Allow the seed input screen to complete and avoid
56- // premature navigation
57- if (currentRoute is AccessKeyLoginScreen ) {
58- Timber .tag(AUTH_NAV ).d(" No navigation within seed input" )
59- return @LaunchedEffect
60- }
61- if (currentRoute is LoginGraph ) {
62- Timber .tag(AUTH_NAV ).d(" No navigation within account creation and onboarding" )
63- } else if (! deeplinkRouted) {
64- if (authenticated) {
65- Timber .tag(AUTH_NAV ).d(" Navigating to home" )
66- onNavigate(listOf (HomeScreen ()), false )
67- } else {
68- Timber .tag(AUTH_NAV ).d(" Navigating to login" )
69- onNavigate(listOf (LoginScreen ()), false )
59+ if (! deeplinkRouted) {
60+ // Allow the seed input screen to complete and avoid
61+ // premature navigation
62+ if (currentRoute is AccessKeyLoginScreen ) {
63+ Timber .tag(AUTH_NAV ).d(" No navigation within seed input" )
64+ return @LaunchedEffect
65+ }
66+ if (currentRoute is LoginGraph ) {
67+ Timber .tag(AUTH_NAV ).d(" No navigation within account creation and onboarding" )
68+ } else {
69+ if (authenticated) {
70+ Timber .tag(AUTH_NAV ).d(" Navigating to home" )
71+ onNavigate(listOf (HomeScreen ()), false )
72+ } else {
73+ Timber .tag(AUTH_NAV ).d(" Navigating to login" )
74+ onNavigate(listOf (LoginScreen ()), false )
75+ }
7076 }
71- } else {
72- deeplinkRouted = false
7377 }
7478 }
7579 }
@@ -81,23 +85,21 @@ fun AuthCheck(
8185 val scope = this
8286 deeplinkHandler.intent
8387 .filterNotNull()
84- .onEach {
85- deeplinkRouted = false
86- Timber .tag(AUTH_NAV ).d(" intent=${it.data} " )
87- }
88+ .distinctUntilChanged()
8889 .mapNotNull { deeplinkHandler.handle() }
89- .onEach { Timber .d( " ${it.first} " ) }
90- .filter {
91- if (it .first is DeeplinkHandler .Type .Cash ) {
92- return @filter SessionManager .isAuthenticated() == true
90+ .flatMapLatest { combine(flowOf(it), SessionManager .authState) { a, b -> a to b } }
91+ .filter { (data, authState) ->
92+ if (data .first is DeeplinkHandler .Type .Cash || data.first is DeeplinkHandler . Type . Sdk ) {
93+ return @filter authState .isAuthenticated == true
9394 }
9495 return @filter true
9596 }
96- .mapNotNull { (type, screens) ->
97+ .mapNotNull { (data, auth) ->
98+ val (type, screens) = data
9799 if (type is DeeplinkHandler .Type .Login ) {
98- if (SessionManager .isAuthenticated() == true ) {
100+ if (auth .isAuthenticated == true ) {
99101 val entropy = (screens.first() as ? LoginScreen )?.seed
100- Timber .d(" showing logout confirm" )
102+ Timber .tag( AUTH_NAV ). d(" showing logout confirm" )
101103 if (entropy != null ) {
102104 deeplinkRouted = true
103105 context.getActivity()?.intent = null
@@ -124,10 +126,10 @@ fun AuthCheck(
124126 }
125127 .onEach { screens ->
126128 deeplinkRouted = true
129+ Timber .tag(AUTH_NAV ).d(" navigated" )
127130 onNavigate(screens, true )
128131 deeplinkHandler.debounceIntent = null
129132 context.getActivity()?.intent = null
130- deeplinkRouted = false
131133 }
132134 .launchIn(this )
133135 }
0 commit comments