Advertisement
EnGold

Untitled

Feb 14th, 2024 (edited)
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.69 KB | None | 0 0
  1. @Composable
  2. fun ContentList(modifier: Modifier) {
  3.     //List of Users
  4.     val list = listOf(
  5.         ProfileData("Person", "", null, null, null, null),
  6.         ProfileData("Person 2", "", null, null, null, null),
  7.         ProfileData("Person 3", "", null, null, null, null),
  8.         ProfileData("Person 4", "", null, null, null, null),
  9.         ProfileData("Person 5", "", null, null, null, null),
  10.     )
  11.     //current user
  12.     var current by remember {
  13.         mutableIntStateOf(0)
  14.     }
  15.     //animate state
  16.     var animState by remember { mutableStateOf(ProfileAnim.Enter) }
  17.  
  18.     //animate
  19.     val transition = updateTransition(animState, label = "offset")
  20.     val offset by transition.animateIntOffset(transitionSpec = {
  21.         when {
  22.             ProfileAnim.Down isTransitioningTo ProfileAnim.Default ->
  23.                 tween(durationMillis = 200)
  24.  
  25.             ProfileAnim.Up isTransitioningTo ProfileAnim.Default ->
  26.                 tween(durationMillis = 200)
  27.             else ->
  28.                 tween(durationMillis = 500)
  29.         }
  30.     }, label = "offset") { state ->
  31.         when (state) {
  32.             ProfileAnim.Default -> IntOffset(-1500, 0)
  33.             ProfileAnim.Enter -> IntOffset(0, 0)
  34.             ProfileAnim.Down -> IntOffset(0, 2000)
  35.             ProfileAnim.Up -> IntOffset(0, -2000)
  36.         }
  37.     }
  38.     //is animation running
  39.     val isAnimationEnded = transition.isRunning
  40.     Log.d("LOGG", animState.name)
  41.  
  42.     LaunchedEffect(isAnimationEnded) {
  43.         if ((animState == ProfileAnim.Up || animState == ProfileAnim.Down ) && !isAnimationEnded) {
  44.             animState = ProfileAnim.Default
  45.         }
  46.         if(animState == ProfileAnim.Default && !isAnimationEnded){
  47.             current++
  48.             animState = ProfileAnim.Enter
  49.         }
  50.     }
  51.     //checking direction of gesture
  52.     var dragDirection by remember { mutableFloatStateOf(0f) }
  53.     Box(modifier = modifier
  54.         .background(color = Color.Gray)
  55.         .pointerInput(Unit) {
  56.             detectVerticalDragGestures(onVerticalDrag = { _, dragAmount ->
  57.                 dragDirection = dragAmount
  58.             }, onDragEnd = {
  59.                 if (dragDirection < 0) {
  60.                     animState = ProfileAnim.Up
  61.                 } else if (dragDirection > 0) {
  62.                     animState = ProfileAnim.Down
  63.                 }
  64.             })
  65.         }
  66.         .background(Color.Blue)
  67.     ) {
  68.         Box(modifier = Modifier
  69.             .fillMaxSize()
  70.             .offset {
  71.                 offset
  72.             }, contentAlignment = Alignment.Center) {
  73.             UserProfile(profileData = list[current])
  74.         }
  75.     }
  76. }
  77.  
  78. enum class ProfileAnim {
  79.     Default,
  80.     Enter,
  81.     Up,
  82.     Down
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement