@@ -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,23 +55,24 @@ 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+ log(" No navigation within seed input" )
64+ return @LaunchedEffect
65+ }
66+ if (currentRoute is LoginGraph ) {
67+ log(" No navigation within account creation and onboarding" )
68+ } else {
69+ if (authenticated) {
70+ log(" Navigating to home" )
71+ onNavigate(listOf (HomeScreen ()), false )
72+ } else {
73+ log(" Navigating to login" )
74+ onNavigate(listOf (LoginScreen ()), false )
75+ }
7076 }
7177 } else {
7278 deeplinkRouted = false
@@ -81,23 +87,21 @@ fun AuthCheck(
8187 val scope = this
8288 deeplinkHandler.intent
8389 .filterNotNull()
84- .onEach {
85- deeplinkRouted = false
86- Timber .tag(AUTH_NAV ).d(" intent=${it.data} " )
87- }
90+ .distinctUntilChanged()
8891 .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
92+ .flatMapLatest { combine(flowOf(it), SessionManager .authState) { a, b -> a to b } }
93+ .filter { (data, authState) ->
94+ if (data .first is DeeplinkHandler .Type .Cash || data.first is DeeplinkHandler . Type . Sdk ) {
95+ return @filter authState .isAuthenticated == true
9396 }
9497 return @filter true
9598 }
96- .mapNotNull { (type, screens) ->
99+ .mapNotNull { (data, auth) ->
100+ val (type, screens) = data
97101 if (type is DeeplinkHandler .Type .Login ) {
98- if (SessionManager .isAuthenticated() == true ) {
102+ if (auth .isAuthenticated == true ) {
99103 val entropy = (screens.first() as ? LoginScreen )?.seed
100- Timber .d (" showing logout confirm" )
104+ log (" showing logout confirm" )
101105 if (entropy != null ) {
102106 deeplinkRouted = true
103107 context.getActivity()?.intent = null
@@ -107,7 +111,7 @@ fun AuthCheck(
107111 entropyB64 = entropy,
108112 onSwitchAccounts = {
109113 scope.launch {
110- delay(300 )
114+ delay(300 ) // wait for dismiss
111115 onSwitchAccounts(it)
112116 deeplinkRouted = false
113117 }
@@ -124,15 +128,17 @@ fun AuthCheck(
124128 }
125129 .onEach { screens ->
126130 deeplinkRouted = true
131+ log(" navigated" )
127132 onNavigate(screens, true )
128133 deeplinkHandler.debounceIntent = null
129134 context.getActivity()?.intent = null
130- deeplinkRouted = false
131135 }
132136 .launchIn(this )
133137 }
134138}
135139
140+ private fun log (message : String ) = Timber .tag(AUTH_NAV ).d(message)
141+
136142private fun showLogoutMessage (
137143 context : Context ,
138144 entropyB64 : String ,
@@ -142,7 +148,6 @@ private fun showLogoutMessage(
142148 BottomBarManager .showMessage(
143149 BottomBarManager .BottomBarMessage (
144150 title = context.getString(R .string.subtitle_logoutAndLoginConfirmation),
145- subtitle = " " ,
146151 positiveText = context.getString(R .string.action_logIn),
147152 negativeText = context.getString(R .string.action_cancel),
148153 isDismissible = false ,
0 commit comments