-
Notifications
You must be signed in to change notification settings - Fork 0
Add UI Component OnboardingContainer to core #126
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
Open
Boonapart228
wants to merge
7
commits into
dev
Choose a base branch
from
chore/onboarding-container
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
677bd27
Add OnboardingContainer to core
Boonapart228 13f5e06
Refactor onboarding container
mkhotych ac37605
Update api
mkhotych 6debc55
Use single lambda for page content
mkhotych ead9a4a
Add onboarding container overload with list of items
mkhotych 380fa84
Update api
mkhotych 1ba933d
Combine separate button lambdas into a single footer lambda
mkhotych File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...src/main/java/com/urlaunched/android/design/ui/onboardingcontainer/OnboardingContainer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| package com.urlaunched.android.design.ui.onboardingcontainer | ||
|
|
||
| import androidx.compose.animation.core.TweenSpec | ||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.layout.Arrangement | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.ColumnScope | ||
| import androidx.compose.foundation.layout.PaddingValues | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.foundation.layout.width | ||
| import androidx.compose.foundation.pager.HorizontalPager | ||
| import androidx.compose.foundation.pager.PagerState | ||
| import androidx.compose.foundation.pager.rememberPagerState | ||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||
| import androidx.compose.material3.Button | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.material3.TextButton | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.text.style.TextAlign | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
| import com.urlaunched.android.design.resources.dimens.Dimens | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.constants.OnboardingConstants | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.models.StepProgressBarColors | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.models.StepProgressBarStyle | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| private const val FIRST_PAGE_INDEX = 0 | ||
| private const val ONE_PAGE = 1 | ||
|
|
||
| @Composable | ||
| fun <T> OnboardingContainer( | ||
| modifier: Modifier = Modifier, | ||
| pages: List<T>, | ||
| initialPageIndex: Int = FIRST_PAGE_INDEX, | ||
| stepProgressBarColors: StepProgressBarColors = StepProgressBarColors(), | ||
| stepProgressBarStyle: StepProgressBarStyle = StepProgressBarStyle(), | ||
| stepProgressPadding: PaddingValues = PaddingValues(top = Dimens.spacingNormal), | ||
| contentArrangement: Arrangement.Vertical = Arrangement.Top, | ||
| contentAlignment: Alignment.Horizontal = Alignment.CenterHorizontally, | ||
| onPageChange: ((page: T) -> Unit)? = null, | ||
| footerContent: @Composable ColumnScope.(page: T, isLastPage: Boolean, nextPage: () -> Unit) -> Unit, | ||
| pageContent: @Composable ColumnScope.(page: T) -> Unit | ||
| ) { | ||
| val coroutineScope = rememberCoroutineScope() | ||
| val pagerState = rememberPagerState( | ||
| initialPage = initialPageIndex, | ||
| pageCount = { pages.size } | ||
| ) | ||
|
|
||
| onPageChange?.let { onChange -> | ||
| LaunchedEffect(pagerState.currentPage) { | ||
| onChange(pages[pagerState.currentPage]) | ||
| } | ||
| } | ||
|
|
||
| val goToNextPage: () -> Unit = { | ||
| coroutineScope.launch { | ||
| pagerState.animateScrollToPage( | ||
| page = pagerState.currentPage + ONE_PAGE, | ||
| animationSpec = TweenSpec(durationMillis = OnboardingConstants.PAGE_ANIMATION_DURATION_MILLIS) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| OnboardingContainer( | ||
| modifier = modifier, | ||
| pagerState = pagerState, | ||
| stepProgressBarColors = stepProgressBarColors, | ||
| stepProgressBarStyle = stepProgressBarStyle, | ||
| stepProgressPadding = stepProgressPadding, | ||
| contentArrangement = contentArrangement, | ||
| contentAlignment = contentAlignment, | ||
| pageContent = { pageIndex -> | ||
| pageContent(pages[pageIndex]) | ||
| }, | ||
| footerContent = { | ||
| footerContent(pages[pagerState.currentPage], pagerState.currentPage == pages.lastIndex, goToNextPage) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| fun OnboardingContainer( | ||
| modifier: Modifier = Modifier, | ||
| pagerState: PagerState, | ||
VMihalatiuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| stepProgressBarColors: StepProgressBarColors = StepProgressBarColors(), | ||
| stepProgressBarStyle: StepProgressBarStyle = StepProgressBarStyle(), | ||
| stepProgressPadding: PaddingValues = PaddingValues(top = Dimens.spacingNormal), | ||
| contentArrangement: Arrangement.Vertical = Arrangement.Top, | ||
| contentAlignment: Alignment.Horizontal = Alignment.CenterHorizontally, | ||
| footerContent: @Composable ColumnScope.() -> Unit, | ||
| pageContent: @Composable ColumnScope.(page: Int) -> Unit | ||
| ) { | ||
| Column( | ||
| modifier = modifier | ||
| ) { | ||
| HorizontalPager( | ||
| modifier = Modifier.weight(1f), | ||
| verticalAlignment = Alignment.Top, | ||
| state = pagerState | ||
| ) { page -> | ||
| Column( | ||
| modifier = Modifier.fillMaxSize(), | ||
| horizontalAlignment = contentAlignment, | ||
| verticalArrangement = contentArrangement | ||
| ) { | ||
| pageContent(page) | ||
| } | ||
| } | ||
|
|
||
| Column( | ||
| modifier = Modifier.fillMaxWidth(), | ||
| horizontalAlignment = Alignment.CenterHorizontally | ||
| ) { | ||
| StepProgressBar( | ||
| activeStepIndex = pagerState.currentPage, | ||
| stepsCount = pagerState.pageCount, | ||
| stepProgressBarColors = stepProgressBarColors, | ||
| stepProgressBarStyle = stepProgressBarStyle, | ||
| modifier = Modifier.padding(stepProgressPadding) | ||
| ) | ||
|
|
||
| footerContent() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Preview(showBackground = true) | ||
| @Composable | ||
| private fun OnboardingContainerPreview() { | ||
| val pagerState = rememberPagerState(pageCount = { 4 }) | ||
|
|
||
| OnboardingContainer( | ||
| pagerState = pagerState, | ||
| stepProgressBarColors = StepProgressBarColors( | ||
| selectedStepColor = MaterialTheme.colorScheme.primary, | ||
| unselectedStepColor = MaterialTheme.colorScheme.primaryContainer | ||
| ), | ||
| pageContent = { page -> | ||
| Box( | ||
| modifier = Modifier | ||
| .padding(top = 100.dp) | ||
| .size(270.dp) | ||
| .background(Color(0xFF4CAF50), RoundedCornerShape(24.dp)) | ||
| ) | ||
|
|
||
| Text( | ||
| text = "Welcome to the App", | ||
| style = MaterialTheme.typography.displaySmall, | ||
| modifier = Modifier.padding(top = 32.dp) | ||
| ) | ||
|
|
||
| Text( | ||
| text = "Your go-to application for everything.\nEmbark on an exciting journey with us.", | ||
| style = MaterialTheme.typography.labelLarge, | ||
| textAlign = TextAlign.Center, | ||
| color = Color(0xA3000000), | ||
| modifier = Modifier.padding(top = 8.dp) | ||
| ) | ||
| }, | ||
| footerContent = { | ||
| Button( | ||
| onClick = {}, | ||
| modifier = Modifier | ||
| .padding(top = Dimens.spacingBig) | ||
| .width(200.dp) | ||
| ) { | ||
| Text("Next") | ||
| } | ||
|
|
||
| TextButton( | ||
| onClick = {}, | ||
| modifier = Modifier.width(200.dp) | ||
| ) { | ||
| Text("Skip") | ||
| } | ||
|
|
||
| Text( | ||
| text = "Privacy Policy, Terms & Conditions", | ||
| modifier = Modifier.padding(vertical = Dimens.spacingNormal) | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
design/src/main/java/com/urlaunched/android/design/ui/onboardingcontainer/StepProgressBar.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.urlaunched.android.design.ui.onboardingcontainer | ||
|
|
||
| import androidx.compose.animation.animateColorAsState | ||
| import androidx.compose.animation.core.LinearEasing | ||
| import androidx.compose.animation.core.tween | ||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.layout.Arrangement | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.clip | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.constants.OnboardingConstants | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.models.StepProgressBarColors | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.models.StepProgressBarStyle | ||
|
|
||
| @Composable | ||
| fun StepProgressBar( | ||
| modifier: Modifier = Modifier, | ||
| activeStepIndex: Int, | ||
| stepsCount: Int, | ||
| stepProgressBarColors: StepProgressBarColors, | ||
| stepProgressBarStyle: StepProgressBarStyle | ||
| ) { | ||
| Row( | ||
| modifier = modifier, | ||
| horizontalArrangement = Arrangement.spacedBy(stepProgressBarStyle.stepsSpacing) | ||
| ) { | ||
| for (stepIndex in OnboardingConstants.PROGRESS_BAR_FIRST_STEP until stepsCount) { | ||
| val tabColor by animateColorAsState( | ||
| targetValue = if (stepIndex == activeStepIndex) { | ||
| stepProgressBarColors.selectedStepColor | ||
| } else { | ||
| stepProgressBarColors.unselectedStepColor | ||
| }, | ||
| animationSpec = tween(OnboardingConstants.PAGE_ANIMATION_DURATION_MILLIS, easing = LinearEasing) | ||
| ) | ||
|
|
||
| Box( | ||
| modifier = Modifier | ||
| .size(height = stepProgressBarStyle.stepHeight, width = stepProgressBarStyle.stepWidth) | ||
| .clip(stepProgressBarStyle.stepShape) | ||
| .background(tabColor) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Preview(showBackground = true) | ||
| @Composable | ||
| private fun StepProgressBarPreview() { | ||
| StepProgressBar( | ||
| modifier = Modifier.padding(16.dp), | ||
| activeStepIndex = 1, | ||
| stepsCount = 4, | ||
| stepProgressBarColors = StepProgressBarColors(), | ||
| stepProgressBarStyle = StepProgressBarStyle() | ||
| ) | ||
| } |
6 changes: 6 additions & 0 deletions
6
...ava/com/urlaunched/android/design/ui/onboardingcontainer/constants/OnboardingConstants.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.urlaunched.android.design.ui.onboardingcontainer.constants | ||
|
|
||
| internal object OnboardingConstants { | ||
| const val PROGRESS_BAR_FIRST_STEP = 0 | ||
| const val PAGE_ANIMATION_DURATION_MILLIS = 300 | ||
| } |
10 changes: 10 additions & 0 deletions
10
...a/com/urlaunched/android/design/ui/onboardingcontainer/constants/StepProgressBarDimens.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.urlaunched.android.design.ui.onboardingcontainer.constants | ||
|
|
||
| import androidx.compose.ui.unit.Dp | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| internal object StepProgressBarDimens { | ||
| val defaultStepHeight: Dp = 5.dp | ||
| val defaultStepWidth: Dp = 24.dp | ||
| val defaultStepsSpacing: Dp = 4.dp | ||
| } |
8 changes: 8 additions & 0 deletions
8
...java/com/urlaunched/android/design/ui/onboardingcontainer/models/StepProgressBarColors.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.urlaunched.android.design.ui.onboardingcontainer.models | ||
|
|
||
| import androidx.compose.ui.graphics.Color | ||
|
|
||
| data class StepProgressBarColors( | ||
| val selectedStepColor: Color = Color.Black, | ||
| val unselectedStepColor: Color = Color.LightGray | ||
| ) |
13 changes: 13 additions & 0 deletions
13
.../java/com/urlaunched/android/design/ui/onboardingcontainer/models/StepProgressBarStyle.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.urlaunched.android.design.ui.onboardingcontainer.models | ||
|
|
||
| import androidx.compose.foundation.shape.CircleShape | ||
| import androidx.compose.ui.graphics.Shape | ||
| import androidx.compose.ui.unit.Dp | ||
| import com.urlaunched.android.design.ui.onboardingcontainer.constants.StepProgressBarDimens | ||
|
|
||
| data class StepProgressBarStyle( | ||
| val stepHeight: Dp = StepProgressBarDimens.defaultStepHeight, | ||
| val stepWidth: Dp = StepProgressBarDimens.defaultStepWidth, | ||
| val stepsSpacing: Dp = StepProgressBarDimens.defaultStepsSpacing, | ||
| val stepShape: Shape = CircleShape | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А зачем этот вариант с pagerState?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Якщо потрібно буде контролювати цей стан, можливо якась інша імплементація, можна напевно додати до нього теж
Base