Skip to content

Instantly share code, notes, and snippets.

@ernestkamara
Last active January 4, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ernestkamara/60111d6de8c2b514227136a163d7f449 to your computer and use it in GitHub Desktop.
Save ernestkamara/60111d6de8c2b514227136a163d7f449 to your computer and use it in GitHub Desktop.
The Onboarding Fragment
class OnboardingFragment : DialogFragment() {
private val indicators = mutableListOf<View>()
private var currentPosition = 0
private fun attachNextButtonListener() {
next.setOnClickListener {
when (currentPosition) {
0 -> next.navigate(R.id.firstTransition, R.id.secondTransition)
1 -> next.navigate(R.id.secondTransition, R.id.thirdTransition)
else -> dismiss()
}
}
}
private fun attachPreviousButtonListener() {
previous.setOnClickListener {
when (currentPosition) {
2 -> previous.navigate(R.id.thirdTransition, R.id.secondTransition)
1 -> previous.navigate(R.id.secondTransition, R.id.firstTransition)
}
}
}
private fun Button.navigate(startId: Int, endId: Int) {
currentPosition = if (id == R.id.next) {
currentPosition.inc()
} else {
currentPosition.dec()
}
updateNavState()
if (endId == R.id.thirdTransition) {
navContainer.setBackgroundColor(resources.getColor(R.color.color_onboarding_page3))
next.setText(R.string.button_text_complete)
} else {
next.setText(R.string.button_text_next)
navContainer.setBackgroundColor(resources.getColor(R.color.onboarding_background))
}
motionLayout.setTransition(startId, endId)
motionLayout.transitionToEnd()
updateIndicators()
}
private fun updateIndicators() {
indicators.forEachIndexed { index, view ->
val background = when (index) {
currentPosition -> R.drawable.selected_dot
else -> R.drawable.un_selected_dot
}
view.setBackgroundDrawable(context?.getDrawable(background))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment