diff --git a/composeApp/src/commonMain/kotlin/dev/therealashik/github/App.kt b/composeApp/src/commonMain/kotlin/dev/therealashik/github/App.kt index feab061..a56625f 100755 --- a/composeApp/src/commonMain/kotlin/dev/therealashik/github/App.kt +++ b/composeApp/src/commonMain/kotlin/dev/therealashik/github/App.kt @@ -59,7 +59,12 @@ fun App() { onNavigateBack = { navController.popBackStack() }, onNavigateToNotificationOptions = { navController.navigate(Route.NotificationOptions) }, onNavigateToCodeOptions = { navController.navigate(Route.CodeOptions) }, - onNavigateToAddPat = { navController.navigate(Route.AddPat) } + onNavigateToAddPat = { navController.navigate(Route.AddPat) }, + onSignOut = { + navController.navigate(Route.AddPat) { + popUpTo(Route.Settings) { inclusive = true } + } + } ) } composable { diff --git a/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsScreen.kt b/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsScreen.kt index 04b374e..a1bed89 100644 --- a/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsScreen.kt +++ b/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsScreen.kt @@ -25,6 +25,7 @@ fun SettingsScreen( onNavigateToNotificationOptions: () -> Unit, onNavigateToCodeOptions: () -> Unit, onNavigateToAddPat: () -> Unit, + onSignOut: () -> Unit, viewModel: SettingsViewModel = viewModel { SettingsViewModel() } ) { val uiState by viewModel.uiState.collectAsState() @@ -37,6 +38,12 @@ fun SettingsScreen( if (lifecycleState == Lifecycle.State.RESUMED) viewModel.refresh() } + LaunchedEffect(Unit) { + viewModel.signOutEvent.collect { + onSignOut() + } + } + if (showAccountsSheet) { AccountsSheet( accounts = uiState.accounts, @@ -128,7 +135,7 @@ fun SettingsScreen( SettingsRow( title = stringResource(Res.string.settings_sign_out), titleColor = MaterialTheme.colorScheme.error, - onClick = { /* TODO */ } + onClick = { viewModel.signOut() } ) SectionDivider() } diff --git a/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsViewModel.kt b/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsViewModel.kt index a98015b..b4fb0e9 100644 --- a/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsViewModel.kt +++ b/composeApp/src/commonMain/kotlin/dev/therealashik/github/settings/SettingsViewModel.kt @@ -4,8 +4,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dev.therealashik.github.data.GitHubApiClient import dev.therealashik.github.data.createTokenStorage +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch @@ -16,6 +19,9 @@ class SettingsViewModel : ViewModel() { private val _uiState = MutableStateFlow(SettingsUiState()) val uiState: StateFlow = _uiState.asStateFlow() + private val _signOutEvent = MutableSharedFlow() + val signOutEvent: SharedFlow = _signOutEvent.asSharedFlow() + init { refresh() } @@ -37,6 +43,13 @@ class SettingsViewModel : ViewModel() { } } + fun signOut() { + viewModelScope.launch { + tokenStorage.clearToken() + _signOutEvent.emit(Unit) + } + } + override fun onCleared() { super.onCleared() apiClient.close()