Skip to content

Commit d51eaa1

Browse files
committed
feat: enhance authentication polling and refine profile UI visibility
- Update `ThemeModeOption` and `ThemeColorCard` visibility to `private` within the profile appearance section. - Refactor `AuthenticationViewModel` to handle `PollNow` and `OnResumed` actions separately. - Implement `forcePollNow` to trigger an immediate token check and restart the background polling job if it is no longer active. - Add detailed debug logging to the device token polling flow to track manual requests, job status, and result states (success, pending, or failure).
1 parent 8064c44 commit d51eaa1

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

  • feature
    • auth/presentation/src/commonMain/kotlin/zed/rainxch/auth/presentation
    • profile/presentation/src/commonMain/kotlin/zed/rainxch/profile/presentation/components/sections

feature/auth/presentation/src/commonMain/kotlin/zed/rainxch/auth/presentation/AuthenticationViewModel.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ class AuthenticationViewModel(
109109
_events.trySend(AuthenticationEvents.OnNavigateToMain)
110110
}
111111

112-
AuthenticationAction.PollNow,
113-
AuthenticationAction.OnResumed,
114-
-> {
112+
AuthenticationAction.PollNow -> {
113+
forcePollNow()
114+
}
115+
116+
AuthenticationAction.OnResumed -> {
115117
tryPollIfReady()
116118
}
117119
}
@@ -158,6 +160,20 @@ class AuthenticationViewModel(
158160
}
159161
}
160162

163+
private fun forcePollNow() {
164+
val loginState = _state.value.loginState
165+
if (loginState is AuthLoginState.DevicePrompt) {
166+
val deviceCode = loginState.start.deviceCode
167+
logger.debug("Manual poll requested (isPolling=${_state.value.isPolling}, pollingJobActive=${pollingJob?.isActive})")
168+
// Restart background polling if it died
169+
if (pollingJob?.isActive != true) {
170+
logger.debug("Polling job was dead — restarting background polling")
171+
startPolling(deviceCode)
172+
}
173+
pollOnce(deviceCode)
174+
}
175+
}
176+
161177
private fun startLogin() {
162178
viewModelScope.launch {
163179
try {
@@ -245,6 +261,7 @@ class AuthenticationViewModel(
245261
private suspend fun doPoll(deviceCode: String) {
246262
_state.update { it.copy(isPolling = true) }
247263
try {
264+
logger.debug("Polling device token (code=${deviceCode.take(8)}...)")
248265
val result =
249266
withContext(Dispatchers.IO) {
250267
authenticationRepository.pollDeviceTokenOnce(deviceCode)
@@ -253,6 +270,7 @@ class AuthenticationViewModel(
253270
result
254271
.onSuccess { token ->
255272
if (token != null) {
273+
logger.debug("Poll success — token received, navigating")
256274
pollingJob?.cancel()
257275
countdownJob?.cancel()
258276
clearSavedState()
@@ -261,9 +279,11 @@ class AuthenticationViewModel(
261279
}
262280
_events.trySend(AuthenticationEvents.OnNavigateToMain)
263281
} else {
282+
logger.debug("Poll result: still pending")
264283
_state.update { it.copy(isPolling = false) }
265284
}
266285
}.onFailure { error ->
286+
logger.debug("Poll failed terminally: ${error.message}")
267287
pollingJob?.cancel()
268288
countdownJob?.cancel()
269289
clearSavedState()

feature/profile/presentation/src/commonMain/kotlin/zed/rainxch/profile/presentation/components/sections/Appearance.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private fun ThemeSelectionCard(
182182

183183
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
184184
@Composable
185-
fun ThemeModeOption(
185+
private fun ThemeModeOption(
186186
icon: ImageVector,
187187
label: String,
188188
isSelected: Boolean,
@@ -241,7 +241,7 @@ fun ThemeModeOption(
241241

242242
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
243243
@Composable
244-
fun ThemeColorCard(
244+
private fun ThemeColorCard(
245245
selectedThemeColor: AppTheme,
246246
onThemeColorSelected: (AppTheme) -> Unit,
247247
) {

0 commit comments

Comments
 (0)