Skip to content

Instantly share code, notes, and snippets.

@javlonrahimov
Last active July 19, 2022 14:30
Show Gist options
  • Save javlonrahimov/931d590425ddb208c1af9812ff5d6f2e to your computer and use it in GitHub Desktop.
Save javlonrahimov/931d590425ddb208c1af9812ff5d6f2e to your computer and use it in GitHub Desktop.
@AndroidEntryPoint
class SplashFragment : BaseFragment() {
private lateinit var circle1: Circle
private lateinit var circle2: Circle
private lateinit var logo: ImageView
private lateinit var view: FrameLayout
private lateinit var animatorSet: AnimatorSet
override val viewModel: AuthViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
view = FrameLayout(ctx)
initViews(ctx)
view.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
view.addView(circle1)
view.addView(circle2)
view.addView(logo)
return view
}
override fun onStart() {
super.onStart()
toggler.hideFooter(animate = false)
viewModel.getToken()
viewModel.getBranch()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
animateToShrink(ctx)
setStatusBarColor(uz.unical.reduz.teacher.R.color.background_color, true)
lifecycleScope.launch {
delay(5000)
if (viewModel.token.value != null) {
if (viewModel.branch.value != null) {
navController.navigate(Direction.Main)
toggler.showFooter(true)
} else {
viewModel.navigate(NavigationCommand(SplashFragmentDirections.splashToAuth()))
}
} else {
viewModel.navigate(NavigationCommand(SplashFragmentDirections.splashToAuth()))
}
}
}
private fun initViews(context: Context) {
circle1 = Circle(context).apply {
val params = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
color = ContextCompat.getColor(context, R.color.ultramarine_blue)
radius = 3000
params.gravity = Gravity.CENTER
layoutParams = params
isClickable = true
}
circle2 = Circle(context).apply {
val params = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
color = ContextCompat.getColor(context, R.color.ultramarine_blue)
radius = 3000
params.gravity = Gravity.CENTER
layoutParams = params
isClickable = true
}
logo = ImageView(context).apply {
val params = FrameLayout.LayoutParams(
(screenWidth * .5.dpi),
(screenWidth * .5.dpi)
)
alpha = 0f
setImageResource(R.drawable.reduz_logo)
params.gravity = Gravity.CENTER
layoutParams = params
}
}
private fun animateToShrink(context: Context) {
ValueAnimator.ofInt(
3000,
(screenWidth * .7).toInt()
).apply {
duration = 3000
start()
addUpdateListener {
circle1.radius = it.animatedValue as Int
circle2.radius = it.animatedValue as Int
}
doOnEnd {
animateToSplit(context)
}
}
}
private fun animateToSplit(context: Context) {
animatorSet = AnimatorSet()
animatorSet.playTogether(
ObjectAnimator.ofFloat(
circle1,
"translationX",
screenWidth.toFloat()
),
ObjectAnimator.ofFloat(
circle1,
"translationY",
screenHeight.toFloat()
),
ObjectAnimator.ofFloat(
circle2,
"translationX",
-screenWidth.toFloat()
),
ObjectAnimator.ofFloat(
circle2,
"translationY",
-screenHeight.toFloat()
),
ValueAnimator.ofFloat(0f, 1f).apply {
duration = 2500
start()
addUpdateListener {
logo.alpha = it.animatedValue as Float
logo.scaleX = it.animatedValue as Float
logo.scaleY = it.animatedValue as Float
}
},
ValueAnimator.ofInt(
(screenWidth * .7).toInt(),
screenWidth
).apply {
duration = 3000
start()
addUpdateListener {
circle1.radius = it.animatedValue as Int
circle2.radius = it.animatedValue as Int
}
}
)
animatorSet.duration = 2000
animatorSet.start()
animatorSet.doOnEnd {
if (viewModel.token.value != null) {
if (viewModel.branch.value != null) {
navController.navigate(Direction.Main)
toggler.showFooter(true)
} else {
viewModel.navigate(NavigationCommand(SplashFragmentDirections.splashToAuth()))
}
} else {
viewModel.navigate(NavigationCommand(SplashFragmentDirections.splashToAuth()))
}
}
}
override fun onStop() {
super.onStop()
if (this::animatorSet.isInitialized)
animatorSet.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment