Create a presenter and a state for OnBoarding screen.

This commit is contained in:
Benoit Marty 2023-06-01 17:53:58 +02:00
parent f05dea79c4
commit 43df6cfbb8
5 changed files with 111 additions and 24 deletions

View file

@ -32,6 +32,7 @@ import io.element.android.libraries.di.AppScope
class OnBoardingNode @AssistedInject constructor( class OnBoardingNode @AssistedInject constructor(
@Assisted buildContext: BuildContext, @Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>, @Assisted plugins: List<Plugin>,
private val presenter: OnBoardingPresenter,
) : Node( ) : Node(
buildContext = buildContext, buildContext = buildContext,
plugins = plugins plugins = plugins
@ -47,11 +48,11 @@ class OnBoardingNode @AssistedInject constructor(
@Composable @Composable
override fun View(modifier: Modifier) { override fun View(modifier: Modifier) {
OnBoardingScreen( val state = presenter.present()
OnBoardingView(
state = state,
modifier = modifier, modifier = modifier,
canLoginWithQrCode = OnBoardingConfig.canLoginWithQrCode, onSignIn = ::onSignIn,
canCreateAccount = OnBoardingConfig.canCreateAccount,
onSignIn = this::onSignIn,
) )
} }
} }

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.onboarding.impl
import androidx.compose.runtime.Composable
import io.element.android.libraries.architecture.Presenter
import javax.inject.Inject
class OnBoardingPresenter @Inject constructor(
) : Presenter<OnBoardingState> {
@Composable
override fun present(): OnBoardingState {
return OnBoardingState(
canLoginWithQrCode = OnBoardingConfig.canLoginWithQrCode,
canCreateAccount = OnBoardingConfig.canCreateAccount,
)
}
}

View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.onboarding.impl
data class OnBoardingState(
val canLoginWithQrCode: Boolean,
val canCreateAccount: Boolean,
)

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.onboarding.impl
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
open class OnBoardingStateProvider : PreviewParameterProvider<OnBoardingState> {
override val values: Sequence<OnBoardingState>
get() = sequenceOf(
anOnBoardingState(),
anOnBoardingState(canLoginWithQrCode = true),
anOnBoardingState(canCreateAccount = true),
anOnBoardingState(canLoginWithQrCode = true, canCreateAccount = true),
)
}
fun anOnBoardingState(
canLoginWithQrCode: Boolean = false,
canCreateAccount: Boolean = false
) = OnBoardingState(
canLoginWithQrCode = canLoginWithQrCode,
canCreateAccount = canCreateAccount
)

View file

@ -37,6 +37,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import io.element.android.libraries.designsystem.atomic.pages.OnBoardingPage import io.element.android.libraries.designsystem.atomic.pages.OnBoardingPage
@ -51,9 +52,8 @@ import io.element.android.libraries.testtags.testTag
// Ref: https://www.figma.com/file/o9p34zmiuEpZRyvZXJZAYL/FTUE?type=design&node-id=133-5427&t=5SHVppfYzjvkEywR-0 // Ref: https://www.figma.com/file/o9p34zmiuEpZRyvZXJZAYL/FTUE?type=design&node-id=133-5427&t=5SHVppfYzjvkEywR-0
@Composable @Composable
fun OnBoardingScreen( fun OnBoardingView(
canLoginWithQrCode: Boolean, state: OnBoardingState,
canCreateAccount: Boolean,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onSignInWithQrCode: () -> Unit = {}, onSignInWithQrCode: () -> Unit = {},
onSignIn: () -> Unit = {}, onSignIn: () -> Unit = {},
@ -63,20 +63,19 @@ fun OnBoardingScreen(
modifier = modifier, modifier = modifier,
footer = { footer = {
OnBoardingButtons( OnBoardingButtons(
canLoginWithQrCode = canLoginWithQrCode, state = state,
canCreateAccount = canCreateAccount,
onSignInWithQrCode = onSignInWithQrCode, onSignInWithQrCode = onSignInWithQrCode,
onSignIn = onSignIn, onSignIn = onSignIn,
onCreateAccount = onCreateAccount, onCreateAccount = onCreateAccount,
) )
} }
) { ) {
OnBoardingHeader() OnBoardingContent()
} }
} }
@Composable @Composable
private fun OnBoardingHeader(modifier: Modifier = Modifier) { private fun OnBoardingContent(modifier: Modifier = Modifier) {
Box( Box(
modifier = modifier.fillMaxSize(), modifier = modifier.fillMaxSize(),
contentAlignment = BiasAlignment( contentAlignment = BiasAlignment(
@ -112,8 +111,7 @@ private fun OnBoardingHeader(modifier: Modifier = Modifier) {
@Composable @Composable
private fun OnBoardingButtons( private fun OnBoardingButtons(
canLoginWithQrCode: Boolean, state: OnBoardingState,
canCreateAccount: Boolean,
onSignInWithQrCode: () -> Unit, onSignInWithQrCode: () -> Unit,
onSignIn: () -> Unit, onSignIn: () -> Unit,
onCreateAccount: () -> Unit, onCreateAccount: () -> Unit,
@ -125,7 +123,7 @@ private fun OnBoardingButtons(
horizontalAlignment = CenterHorizontally, horizontalAlignment = CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp) verticalArrangement = Arrangement.spacedBy(16.dp)
) { ) {
if (canLoginWithQrCode) { if (state.canLoginWithQrCode) {
Button( Button(
onClick = { onClick = {
onSignInWithQrCode() onSignInWithQrCode()
@ -153,7 +151,7 @@ private fun OnBoardingButtons(
) { ) {
Text(text = stringResource(id = R.string.screen_onboarding_sign_in_manually)) Text(text = stringResource(id = R.string.screen_onboarding_sign_in_manually))
} }
if (canCreateAccount) { if (state.canCreateAccount) {
OutlinedButton( OutlinedButton(
onClick = { onClick = {
onCreateAccount() onCreateAccount()
@ -170,18 +168,15 @@ private fun OnBoardingButtons(
@Preview @Preview
@Composable @Composable
internal fun OnBoardingScreenLightPreview() = internal fun OnBoardingScreenLightPreview(@PreviewParameter(OnBoardingStateProvider::class) state: OnBoardingState) =
ElementPreviewLight { ContentToPreview() } ElementPreviewLight { ContentToPreview(state) }
@Preview @Preview
@Composable @Composable
internal fun OnBoardingScreenDarkPreview() = internal fun OnBoardingScreenDarkPreview(@PreviewParameter(OnBoardingStateProvider::class) state: OnBoardingState) =
ElementPreviewDark { ContentToPreview() } ElementPreviewDark { ContentToPreview(state) }
@Composable @Composable
private fun ContentToPreview() { private fun ContentToPreview(state: OnBoardingState) {
OnBoardingScreen( OnBoardingView(state)
canLoginWithQrCode = true,
canCreateAccount = true,
)
} }