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 Aug 6, 2024
1 parent f110687 commit c3161a1
Show file tree
Hide file tree
Showing 18 changed files with 1,010 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,272 @@
package com.x8bit.bitwarden.ui.auth.feature.welcome

import android.content.res.Configuration
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.Row
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.navigationBarsPadding
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.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalConfiguration
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(),

Check warning on line 58 in app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt#L58

Added line #L58 was not covered by tests
) {
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,

Check warning on line 108 in app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt#L108

Added line #L108 was not covered by tests
) {
val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
val horizontalPadding = if (isLandscape) 128.dp else 16.dp

LaunchedEffect(pagerState.currentPage) {
onPagerSwipe(pagerState.currentPage)
}

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

HorizontalPager(state = pagerState) { index ->
if (isLandscape) {
WelcomeCardLandscape(
state = state.pages[index],
modifier = Modifier.padding(horizontal = horizontalPadding),
)
} else {
WelcomeCardPortrait(
state = state.pages[index],
modifier = Modifier.padding(horizontal = horizontalPadding),
)
}
}

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 = horizontalPadding)
.fillMaxWidth(),
)

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

Spacer(modifier = Modifier.navigationBarsPadding())
}
}

@Composable
private fun WelcomeCardLandscape(
state: WelcomeState.WelcomeCard,
modifier: Modifier = Modifier,

Check warning on line 171 in app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt#L171

Added line #L171 was not covered by tests
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = modifier,
) {
Image(
painter = rememberVectorPainter(id = state.imageRes),
contentDescription = null,
modifier = Modifier.size(132.dp),
)

Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(start = 40.dp),
) {
Text(
text = stringResource(id = state.titleRes),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier.padding(bottom = 16.dp),
)

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

@Composable
private fun WelcomeCardPortrait(
state: WelcomeState.WelcomeCard,
modifier: Modifier = Modifier,

Check warning on line 208 in app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt#L208

Added line #L208 was not covered by tests
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier,
) {
Image(
painter = rememberVectorPainter(id = state.imageRes),
contentDescription = null,
modifier = Modifier.size(200.dp),
)

Text(
text = stringResource(id = state.titleRes),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier
.padding(
top = 48.dp,
bottom = 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,

Check warning on line 246 in app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/welcome/WelcomeScreen.kt#L246

Added line #L246 was not covered by tests
) {
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 c3161a1

Please sign in to comment.