Skip to content

Commit

Permalink
PM-10242 PM-10243 PM-10244 PM-10245 PM-10246: Welcome carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
shannon-livefront committed Jul 31, 2024
1 parent 260b3bf commit 74d261a
Show file tree
Hide file tree
Showing 18 changed files with 922 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ interface AuthRepository : AuthenticatorProvider, AuthRequestManager {
*/
val webAuthResultFlow: Flow<WebAuthResult>

/**
* Whether or not the user has logged in or created an account before.
*/
val hasUserLoggedInOrCreatedAccount: Boolean

/**
* The organization identifier currently associated with this user's SSO flow.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ class AuthRepositoryImpl(
private val webAuthResultChannel = Channel<WebAuthResult>(capacity = Int.MAX_VALUE)
override val webAuthResultFlow: Flow<WebAuthResult> = webAuthResultChannel.receiveAsFlow()

override val hasUserLoggedInOrCreatedAccount
get() = settingsRepository.hasUserLoggedInOrCreatedAccount

private val mutableSsoCallbackResultFlow = bufferedMutableSharedFlow<SsoCallbackResult>()
override val ssoCallbackResultFlow: Flow<SsoCallbackResult> =
mutableSsoCallbackResultFlow.asSharedFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.x8bit.bitwarden.ui.auth.feature.environment.environmentDestination
import com.x8bit.bitwarden.ui.auth.feature.environment.navigateToEnvironment
import com.x8bit.bitwarden.ui.auth.feature.landing.LANDING_ROUTE
import com.x8bit.bitwarden.ui.auth.feature.landing.landingDestination
import com.x8bit.bitwarden.ui.auth.feature.landing.navigateToLanding
import com.x8bit.bitwarden.ui.auth.feature.login.loginDestination
import com.x8bit.bitwarden.ui.auth.feature.login.navigateToLogin
import com.x8bit.bitwarden.ui.auth.feature.loginwithdevice.loginWithDeviceDestination
Expand All @@ -25,16 +26,20 @@ import com.x8bit.bitwarden.ui.auth.feature.setpassword.navigateToSetPassword
import com.x8bit.bitwarden.ui.auth.feature.setpassword.setPasswordDestination
import com.x8bit.bitwarden.ui.auth.feature.twofactorlogin.navigateToTwoFactorLogin
import com.x8bit.bitwarden.ui.auth.feature.twofactorlogin.twoFactorLoginDestination
import com.x8bit.bitwarden.ui.auth.feature.welcome.welcomeDestination

const val AUTH_GRAPH_ROUTE: String = "auth_graph"

/**
* Add auth destinations to the nav graph.
*/
@Suppress("LongMethod")
fun NavGraphBuilder.authGraph(navController: NavHostController) {
fun NavGraphBuilder.authGraph(
startDestination: String,
navController: NavHostController,
) {
navigation(
startDestination = LANDING_ROUTE,
startDestination = startDestination,
route = AUTH_GRAPH_ROUTE,
) {
createAccountDestination(
Expand Down Expand Up @@ -72,6 +77,10 @@ fun NavGraphBuilder.authGraph(navController: NavHostController) {
navController.navigateToEnvironment()
},
)
welcomeDestination(
onNavigateToCreateAccount = { navController.navigateToCreateAccount() },
onNavigateToLogin = { navController.navigateToLanding() },
)
loginDestination(
onNavigateBack = { navController.popBackStack() },
onNavigateToMasterPasswordHint = { emailAddress ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.x8bit.bitwarden.ui.auth.feature.welcome

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import com.x8bit.bitwarden.ui.platform.base.util.composableWithStayTransitions

const val WELCOME_ROUTE: String = "welcome"

/**
* Navigate to the welcome screen.
*/
fun NavController.navigateToWelcome(navOptions: NavOptions? = null) {
this.navigate(WELCOME_ROUTE, navOptions)
}

/**
* Add the Welcome screen to the nav graph.
*/
fun NavGraphBuilder.welcomeDestination(
onNavigateToCreateAccount: () -> Unit,
onNavigateToLogin: () -> Unit,
) {
composableWithStayTransitions(
route = WELCOME_ROUTE,
) {
WelcomeScreen(
onNavigateToCreateAccount = onNavigateToCreateAccount,
onNavigateToLogin = onNavigateToLogin,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package com.x8bit.bitwarden.ui.auth.feature.welcome

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.ui.platform.base.util.EventsEffect
import com.x8bit.bitwarden.ui.platform.components.button.BitwardenFilledButton
import com.x8bit.bitwarden.ui.platform.components.button.BitwardenTextButton
import com.x8bit.bitwarden.ui.platform.components.scaffold.BitwardenScaffold
import com.x8bit.bitwarden.ui.platform.components.util.rememberVectorPainter

/**
* Top level composable for the welcome screen.
*/
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun WelcomeScreen(
onNavigateToCreateAccount: () -> Unit,
onNavigateToLogin: () -> Unit,
viewModel: WelcomeViewModel = hiltViewModel(),
) {
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
val pagerState = rememberPagerState(pageCount = { state.pages.size })

EventsEffect(viewModel = viewModel) { event ->
when (event) {
is WelcomeEvent.UpdatePager -> {
pagerState.animateScrollToPage(event.index)
}

WelcomeEvent.NavigateToCreateAccount -> onNavigateToCreateAccount()
WelcomeEvent.NavigateToLogin -> onNavigateToLogin()
}
}

BitwardenScaffold(
modifier = Modifier.fillMaxSize(),
) { innerPadding ->
WelcomeScreenContent(
state = state,
pagerState = pagerState,
onPagerSwipe = remember(viewModel) {
{ viewModel.trySendAction(WelcomeAction.PagerSwipe(it)) }
},
onDotClick = remember(viewModel) {
{ viewModel.trySendAction(WelcomeAction.DotClick(it)) }
},
onCreateAccountClick = remember(viewModel) {
{ viewModel.trySendAction(WelcomeAction.CreateAccountClick) }
},
onLoginClick = remember(viewModel) {
{ viewModel.trySendAction(WelcomeAction.LoginClick) }
},
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
)
}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun WelcomeScreenContent(
state: WelcomeState,
pagerState: PagerState,
onPagerSwipe: (Int) -> Unit,
onDotClick: (Int) -> Unit,
onCreateAccountClick: () -> Unit,
onLoginClick: () -> Unit,
modifier: Modifier = Modifier,
) {
LaunchedEffect(pagerState) {
snapshotFlow { pagerState.currentPage }.collect { onPagerSwipe(it) }
}

Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier,
) {
Spacer(modifier = Modifier.weight(1f))

HorizontalPager(state = pagerState) { index ->
WelcomeCard(
state = state.pages[index],
modifier = Modifier.padding(horizontal = 16.dp),
)
}

Spacer(modifier = Modifier.weight(1f))

IndicatorDots(
selectedIndexProvider = { state.index },
totalCount = state.pages.size,
onDotClick = onDotClick,
modifier = Modifier
.padding(bottom = 32.dp)
.height(44.dp),
)

BitwardenFilledButton(
label = stringResource(id = R.string.create_account),
onClick = onCreateAccountClick,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth(),
)

BitwardenTextButton(
label = stringResource(id = R.string.log_in),
onClick = onLoginClick,
modifier = Modifier
.padding(horizontal = 16.dp)
.padding(bottom = 32.dp),
)
}
}

@Composable
private fun WelcomeCard(
state: WelcomeState.WelcomeCard,
modifier: Modifier = Modifier,
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier,
) {
Image(
painter = rememberVectorPainter(id = state.imageRes),
contentDescription = null,
modifier = Modifier
.size(200.dp)
.padding(bottom = 32.dp),
)

Text(
text = stringResource(id = state.titleRes),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier.padding(vertical = 16.dp),
)

Text(
text = stringResource(id = state.messageRes),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurface,
)
}
}

@Composable
private fun IndicatorDots(
selectedIndexProvider: () -> Int,
totalCount: Int,
onDotClick: (Int) -> Unit,
modifier: Modifier = Modifier,
) {
LazyRow(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = modifier,
) {
items(totalCount) { index ->
val color = animateColorAsState(
targetValue = if (index == selectedIndexProvider()) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f)
},
label = "dotColor",
)

Box(
modifier = Modifier
.size(8.dp)
.clip(CircleShape)
.background(color.value)
.clickable { onDotClick(index) },
)
}
}
}
Loading

0 comments on commit 74d261a

Please sign in to comment.