Skip to content

Commit 382356a

Browse files
committed
Kotlin DSL flow revamp continued
Emphasis of account & profile interactions
1 parent 53c97fe commit 382356a

File tree

24 files changed

+1063
-490
lines changed

24 files changed

+1063
-490
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.sap.cdc.bitsnbytes.extensions
22

3+
import com.sap.cdc.android.sdk.feature.auth.flow.RegistrationContext
34
import com.sap.cdc.android.sdk.feature.auth.flow.TwoFactorContext
45
import kotlinx.serialization.json.Json
56

67
fun TwoFactorContext.toJson(): String {
78
return Json.encodeToString(this)
9+
}
10+
11+
fun RegistrationContext.toJson(): String {
12+
return Json.encodeToString(this)
813
}

app/src/main/java/com/sap/cdc/bitsnbytes/feature/auth/AuthenticationFlowDelegate.kt

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import com.sap.cdc.android.sdk.feature.auth.model.Credentials
1111
import com.sap.cdc.android.sdk.feature.auth.session.Session
1212
import com.sap.cdc.android.sdk.feature.notifications.IFCMTokenRequest
1313
import com.sap.cdc.bitsnbytes.feature.auth.model.AccountEntity
14+
import kotlinx.coroutines.CoroutineScope
15+
import kotlinx.coroutines.Dispatchers
1416
import kotlinx.coroutines.flow.MutableStateFlow
1517
import kotlinx.coroutines.flow.StateFlow
1618
import kotlinx.coroutines.flow.asStateFlow
19+
import kotlinx.serialization.json.Json
1720

1821
/**
1922
* Complete authentication state solution for ViewModels.
@@ -61,6 +64,12 @@ class AuthenticationFlowDelegate(context: Context) {
6164
private val _userAccount = MutableStateFlow<AccountEntity?>(null)
6265
val userAccount: StateFlow<AccountEntity?> = _userAccount.asStateFlow()
6366

67+
// JSON serializer for parsing account data
68+
private val json = Json { ignoreUnknownKeys = true }
69+
70+
// Coroutine scope for async operations
71+
private val scope = CoroutineScope(Dispatchers.Main)
72+
6473
init {
6574
// Initialize state based on existing CDC session
6675
syncWithCDCSession()
@@ -111,6 +120,37 @@ class AuthenticationFlowDelegate(context: Context) {
111120
fun refreshAuthenticationState() {
112121
syncWithCDCSession()
113122
}
123+
124+
/**
125+
* Get account information with state management.
126+
* This method intercepts the response and updates the userAccount StateFlow.
127+
*/
128+
suspend fun getAccountInfo(
129+
parameters: MutableMap<String, String>? = mutableMapOf(),
130+
authCallbacks: AuthCallbacks.() -> Unit
131+
) {
132+
cdc.getAccountInfo(parameters) {
133+
// Register original callbacks first
134+
authCallbacks()
135+
136+
// Add state management side-effect
137+
doOnSuccess { authSuccess ->
138+
try {
139+
// Parse and update the state
140+
val accountData = json.decodeFromString<AccountEntity>(authSuccess.jsonData)
141+
_userAccount.value = accountData
142+
} catch (e: Exception) {
143+
// Handle parsing errors silently - don't break the callback chain
144+
// Could add logging here if needed
145+
}
146+
}
147+
148+
doOnError { error ->
149+
// Optionally clear account state on error
150+
// _userAccount.value = null
151+
}
152+
}
153+
}
114154
}
115155

116156
class AuthenticationFlowCDCDelegate(val service: AuthenticationService) {
@@ -126,11 +166,36 @@ class AuthenticationFlowCDCDelegate(val service: AuthenticationService) {
126166
parameters: MutableMap<String, String> = mutableMapOf()
127167
) {
128168
service.authenticate().register().credentials(
129-
credentials = credentials,
130-
configure = authCallbacks,
169+
credentials = credentials, configure = authCallbacks,
131170
parameters = parameters
132171
)
133172
}
173+
174+
suspend fun logOut(authCallbacks: AuthCallbacks.() -> Unit) {
175+
service.authenticate().logout(authCallbacks = authCallbacks)
176+
}
177+
178+
suspend fun getAccountInfo(
179+
parameters: MutableMap<String, String>? = mutableMapOf(), authCallbacks: AuthCallbacks.() -> Unit
180+
) {
181+
service.account().get(
182+
parameters = parameters!!,
183+
configure = authCallbacks
184+
)
185+
}
186+
187+
suspend fun resolvePendingRegistration(
188+
missingFieldsSerialized: MutableMap<String, String>,
189+
regToken: String,
190+
authCallbacks: AuthCallbacks.() -> Unit
191+
) {
192+
service.authenticate().register().resolve().pendingRegistrationWith(
193+
missingFields = missingFieldsSerialized,
194+
regToken = regToken,
195+
configure = authCallbacks
196+
)
197+
}
198+
134199
}
135200

136201
/**
@@ -139,4 +204,3 @@ class AuthenticationFlowCDCDelegate(val service: AuthenticationService) {
139204
fun AuthenticationFlowDelegate.isUserLoggedIn(): Boolean = isAuthenticated.value
140205

141206
fun AuthenticationFlowDelegate.getCurrentUser(): AccountEntity? = userAccount.value
142-

app/src/main/java/com/sap/cdc/bitsnbytes/feature/auth/IdentityServiceRepository.kt

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,6 @@ class IdentityServiceRepository private constructor(context: Context) {
158158

159159
//region AUTHENTICATION FLOWS
160160

161-
/**
162-
* Request cdc SDK latest account information.
163-
*/
164-
suspend fun getAccountInfo(parameters: MutableMap<String, String>? = mutableMapOf()): IAuthResponse {
165-
return authenticationService.account().get(parameters!!)
166-
}
167-
168-
/**
169-
* Update cdc SDK account information.
170-
*/
171-
suspend fun setAccountInfo(parameters: MutableMap<String, String>): IAuthResponse {
172-
return authenticationService.account().set(parameters)
173-
}
174-
175-
/**
176-
* Logout from current CDC session.
177-
*/
178-
suspend fun logout(): IAuthResponse {
179-
return authenticationService.authenticate().logout()
180-
}
181-
182161
/**
183162
* Initiate cdc SDK native social provider login flow.
184163
*/
@@ -273,23 +252,6 @@ class IdentityServiceRepository private constructor(context: Context) {
273252

274253
//region RESOLVE INTERRUPTIONS
275254

276-
/**
277-
* Attempt to resolve "Account Pending Registration" interruption by providing the necessary
278-
* missing fields.
279-
* Note: regToken is required for authenticating the request.
280-
*/
281-
suspend fun resolvePendingRegistrationWithMissingFields(
282-
key: String,
283-
serializedJsonValue: String,
284-
regToken: String,
285-
): IAuthResponse {
286-
val params = mutableMapOf(key to serializedJsonValue)
287-
return authenticationService
288-
.authenticate()
289-
.register()
290-
.resolve()
291-
.pendingRegistrationWith(regToken, params)
292-
}
293255

294256
/**
295257
* Attempt to resolve account linking interruption to an existing site account.

app/src/main/java/com/sap/cdc/bitsnbytes/navigation/AppStateManager.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class AppStateManager : ViewModel() {
2828
private val _canNavigateBack = MutableStateFlow(false)
2929
val canNavigateBack: StateFlow<Boolean> = _canNavigateBack.asStateFlow()
3030

31+
private val _hasProfileBackStack = MutableStateFlow(false)
32+
val hasProfileBackStack: StateFlow<Boolean> = _hasProfileBackStack.asStateFlow()
33+
3134
// NAVIGATION CONTROLLER MANAGEMENT
3235
private var currentNavController: NavController? = null
3336

@@ -47,6 +50,10 @@ class AppStateManager : ViewModel() {
4750
_canNavigateBack.value = canGoBack
4851
}
4952

53+
fun setHasProfileBackStack(hasBackStack: Boolean) {
54+
_hasProfileBackStack.value = hasBackStack
55+
}
56+
5057
// NAVIGATION CONTROLLER METHODS
5158
fun setNavController(navController: NavController) {
5259
currentNavController = navController

0 commit comments

Comments
 (0)