Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor reset password into new auth flow #90

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {

android {
defaultConfig {
val buildVersion = 147
val buildVersion = 149
applicationId = "com.crisiscleanup"
versionCode = buildVersion
versionName = "0.7.${buildVersion - 140}"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<data android:scheme="https" />

<data android:host="${APP_LINK_TLD}" />

<data android:pathPrefix="/o/callback" />
<data android:pathPrefix="/password/reset" />
</intent-filter>
</activity>
</application>
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/crisiscleanup/ExternalIntentProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class ExternalIntentProcessor @Inject constructor(
private val authEventBus: AuthEventBus,
@Logger(CrisisCleanupLoggers.App) private val logger: AppLogger,
) {
fun processMainIntent(intent: Intent) {
fun processMainIntent(intent: Intent): Boolean {
when (val action = intent.action) {
Intent.ACTION_VIEW -> {
intent.data?.let { intentUri ->
intentUri.path?.let { urlPath ->
processMainIntent(intentUri, urlPath)
return processMainIntent(intentUri, urlPath)
}
}
}
Expand All @@ -26,9 +26,11 @@ class ExternalIntentProcessor @Inject constructor(
logger.logDebug("Main intent action not handled $action")
}
}

return false
}

private fun processMainIntent(url: Uri, urlPath: String) {
private fun processMainIntent(url: Uri, urlPath: String): Boolean {
if (urlPath.startsWith("/o/callback")) {
url.getQueryParameter("code")?.let { code ->
authEventBus.onEmailLoginLink(code)
Expand All @@ -38,6 +40,9 @@ class ExternalIntentProcessor @Inject constructor(
if (code.isNotBlank()) {
authEventBus.onResetPassword(code)
}
} else {
return false
}
return true
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/com/crisiscleanup/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ class MainActivity : ComponentActivity() {
}

intent?.let {
intentProcessor.processMainIntent(it)
if (!intentProcessor.processMainIntent(it)) {
it.data?.let { dataUri ->
// TODO Open to browser or WebView. Do no loop back here.
logger.logDebug("App link not processed $dataUri")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class AuthenticationViewModel @Inject constructor(
started = SharingStarted.WhileSubscribed(),
)

val showResetPassword = authEventBus.showResetPassword

val isAuthenticateSuccessful = MutableStateFlow(false)

val loginInputData = LoginInputData()
Expand Down Expand Up @@ -215,10 +213,6 @@ class AuthenticationViewModel @Inject constructor(
}
authEventBus.onLogout()
}

fun clearResetPassword() {
authEventBus.onResetPassword("")
}
}

sealed interface AuthenticateScreenUiState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package com.crisiscleanup.feature.authentication

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.crisiscleanup.core.common.event.AuthEventBus
import com.crisiscleanup.core.common.log.AppLogger
import com.crisiscleanup.core.common.log.CrisisCleanupLoggers
import com.crisiscleanup.core.common.log.Logger
import com.crisiscleanup.core.common.network.CrisisCleanupDispatchers
import com.crisiscleanup.core.common.network.Dispatcher
import com.crisiscleanup.core.data.repository.AccountDataRepository
import com.crisiscleanup.core.model.data.AccountData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
Expand All @@ -19,7 +17,7 @@ import javax.inject.Inject
@HiltViewModel
class RootAuthViewModel @Inject constructor(
accountDataRepository: AccountDataRepository,
@Dispatcher(CrisisCleanupDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
private val authEventBus: AuthEventBus,
@Logger(CrisisCleanupLoggers.Auth) private val logger: AppLogger,
) : ViewModel() {
val authState = accountDataRepository.accountData
Expand All @@ -35,6 +33,12 @@ class RootAuthViewModel @Inject constructor(
initialValue = AuthState.Loading,
started = SharingStarted.WhileSubscribed(),
)

val showResetPassword = authEventBus.showResetPassword

fun clearResetPassword() {
authEventBus.onResetPassword("")
}
}

sealed interface AuthState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,60 +52,51 @@ fun LoginWithEmailRoute(
openEmailMagicLink: () -> Unit = {},
viewModel: AuthenticationViewModel = hiltViewModel(),
) {
// TODO Push route rather than toggling state
val showResetPassword by viewModel.showResetPassword.collectAsStateWithLifecycle(false)
if (showResetPassword) {
PasswordRecoverRoute(
onBack = viewModel::clearResetPassword,
showResetPassword = true,
)
} else {
val onCloseScreen = remember(viewModel, closeAuthentication) {
{
viewModel.onCloseScreen()
closeAuthentication()
}
val onCloseScreen = remember(viewModel, closeAuthentication) {
{
viewModel.onCloseScreen()
closeAuthentication()
}
}

val isAuthenticateSuccessful by viewModel.isAuthenticateSuccessful.collectAsStateWithLifecycle()
if (isAuthenticateSuccessful) {
onCloseScreen()
}
val isAuthenticateSuccessful by viewModel.isAuthenticateSuccessful.collectAsStateWithLifecycle()
if (isAuthenticateSuccessful) {
onCloseScreen()
}

val uiState by viewModel.uiState.collectAsStateWithLifecycle()
when (uiState) {
is AuthenticateScreenUiState.Loading -> {
Box(Modifier.fillMaxSize()) {
CircularProgressIndicator(Modifier.align(Alignment.Center))
}
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
when (uiState) {
is AuthenticateScreenUiState.Loading -> {
Box(Modifier.fillMaxSize()) {
CircularProgressIndicator(Modifier.align(Alignment.Center))
}
}

is AuthenticateScreenUiState.Ready -> {
val isKeyboardOpen = rememberIsKeyboardOpen()
val closeKeyboard = rememberCloseKeyboard(viewModel)

val readyState = uiState as AuthenticateScreenUiState.Ready
val authState = readyState.authenticationState
Box(modifier) {
// TODO Scroll when content is longer than screen height with keyboard open
Column(
Modifier
.scrollFlingListener(closeKeyboard)
.fillMaxSize()
.verticalScroll(rememberScrollState()),
) {
AnimatedVisibility(visible = !isKeyboardOpen) {
CrisisCleanupLogoRow()
}
LoginWithEmailScreen(
authState,
onBack = onBack,
openForgotPassword = openForgotPassword,
openEmailMagicLink = openEmailMagicLink,
closeAuthentication = closeAuthentication,
)
Spacer(modifier = Modifier.weight(1f))
is AuthenticateScreenUiState.Ready -> {
val isKeyboardOpen = rememberIsKeyboardOpen()
val closeKeyboard = rememberCloseKeyboard(viewModel)

val readyState = uiState as AuthenticateScreenUiState.Ready
val authState = readyState.authenticationState
Box(modifier) {
// TODO Scroll when content is longer than screen height with keyboard open
Column(
Modifier
.scrollFlingListener(closeKeyboard)
.fillMaxSize()
.verticalScroll(rememberScrollState()),
) {
AnimatedVisibility(visible = !isKeyboardOpen) {
CrisisCleanupLogoRow()
}
LoginWithEmailScreen(
authState,
onBack = onBack,
openForgotPassword = openForgotPassword,
openEmailMagicLink = openEmailMagicLink,
closeAuthentication = closeAuthentication,
)
Spacer(modifier = Modifier.weight(1f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,25 @@ fun RootAuthRoute(
enableBackHandler: Boolean = false,
openLoginWithEmail: () -> Unit = {},
closeAuthentication: () -> Unit = {},
viewModel: RootAuthViewModel = hiltViewModel(),
) {
BackHandler(enableBackHandler) {
closeAuthentication()
}

RootAuthScreen(
openLoginWithEmail = openLoginWithEmail,
closeAuthentication = closeAuthentication,
)
// TODO Push route rather than toggling state
val showResetPassword by viewModel.showResetPassword.collectAsStateWithLifecycle(false)
if (showResetPassword) {
PasswordRecoverRoute(
onBack = viewModel::clearResetPassword,
showResetPassword = true,
)
} else {
RootAuthScreen(
openLoginWithEmail = openLoginWithEmail,
closeAuthentication = closeAuthentication,
)
}
}

@Composable
Expand Down
Loading