Advertisement
Guest User

Untitled

a guest
Feb 18th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.31 KB | None | 0 0
  1. @OptIn(ExperimentalPagerApi::class)
  2. @Composable
  3. fun HomeScreenToolbarContainer(
  4.   insets: Insets,
  5.   chanTheme: ChanTheme,
  6.   pagerState: PagerState,
  7.   childScreens: List<ComposeScreenWithToolbar>,
  8.   homeScreenViewModel: HomeScreenViewModel
  9. ) {
  10.   require(childScreens.isNotEmpty()) { "childScreens is empty!" }
  11.  
  12.   val currentPage = pagerState.currentPage.coerceIn(0, childScreens.lastIndex)
  13.   val targetPage = pagerState.targetPage.coerceIn(0, childScreens.lastIndex)
  14.   val animationProgress = pagerState.currentPageOffset
  15.  
  16.   val toolbarHeight = dimensionResource(id = R.dimen.toolbar_height)
  17.   val toolbarTranslationDistancePx = with(LocalDensity.current) { toolbarHeight.toPx() / 3f }
  18.   val toolbarTotalHeight = remember(key1 = insets.topDp) { insets.topDp + toolbarHeight }
  19.   val transitionIsProgress = currentPage != targetPage
  20.  
  21.   val postListScrollState = homeScreenViewModel.toolbarVisibilityInfo.postListScrollState.collectAsState()
  22.   val touchingTopOrBottomOfList by homeScreenViewModel.toolbarVisibilityInfo.postListTouchingTopOrBottomState.collectAsState()
  23.   val isDraggingPostList by homeScreenViewModel.toolbarVisibilityInfo.postListDragState.collectAsState()
  24.   val isDraggingFastScroller by homeScreenViewModel.toolbarVisibilityInfo.fastScrollerDragState.collectAsState()
  25.  
  26.   val toolbarAlpha by when {
  27.     isDraggingFastScroller -> animateFloatAsState(targetValue = 0f)
  28.     touchingTopOrBottomOfList -> animateFloatAsState(targetValue = 1f)
  29.     isDraggingPostList -> postListScrollState
  30.     else -> {
  31.       val targetValue = if (postListScrollState.value > 0.5f) 1f else 0f
  32.       animateFloatAsState(targetValue = targetValue)
  33.     }
  34.   }
  35.  
  36.   Column(
  37.     modifier = Modifier
  38.       .fillMaxWidth()
  39.       .height(toolbarTotalHeight)
  40.       .alpha(toolbarAlpha)
  41.       .background(chanTheme.primaryColorCompose)
  42.       .consumeClicks()
  43.   ) {
  44.     Spacer(modifier = Modifier.height(insets.topDp))
  45.  
  46.     Box(
  47.       modifier = Modifier
  48.         .fillMaxWidth()
  49.         .height(toolbarHeight)
  50.     ) {
  51.       val zOrders = remember(key1 = childScreens.size) { IntArray(childScreens.size) { 0 } }
  52.  
  53.       for ((pageIndex, _) in childScreens.withIndex()) {
  54.         when (pageIndex) {
  55.           // (Currently animated) Always behind the target and above everything else
  56.           currentPage -> zOrders[pageIndex] = 999
  57.           // (Currently animated) Always at the top
  58.           targetPage -> zOrders[pageIndex] = 1000
  59.           else -> zOrders[pageIndex] = pageIndex
  60.         }
  61.       }
  62.  
  63.       for ((pageIndex, currentScreen) in childScreens.withIndex()) {
  64.         val zOrder = zOrders[pageIndex]
  65.         val screenToolbarMovable = remember(currentScreen.screenKey) {
  66.           movableContentOf { currentScreen.Toolbar(this) }
  67.         }
  68.  
  69.         when (pageIndex) {
  70.           currentPage -> {
  71.             val currentToolbarAlpha = lerpFloat(1f, 0f, Math.abs(animationProgress))
  72.             val currentToolbarTranslation = if (animationProgress >= 0f) {
  73.               lerpFloat(0f, toolbarTranslationDistancePx, Math.abs(animationProgress))
  74.             } else {
  75.               lerpFloat(0f, -toolbarTranslationDistancePx, Math.abs(animationProgress))
  76.             }
  77.  
  78.             BuildChildToolbar(
  79.               composeScreenWithToolbar = currentScreen,
  80.               zOrder = zOrder,
  81.               targetToolbarAlpha = currentToolbarAlpha,
  82.               targetToolbarTranslation = currentToolbarTranslation,
  83.               transitionIsProgress = transitionIsProgress,
  84.               toolbarContent = { screenToolbarMovable() }
  85.             )
  86.           }
  87.           targetPage -> {
  88.             val targetToolbarAlpha = lerpFloat(0f, 1f, Math.abs(animationProgress))
  89.             val targetToolbarTranslation = if (animationProgress >= 0f) {
  90.               lerpFloat(-toolbarTranslationDistancePx, 0f, Math.abs(animationProgress))
  91.             } else {
  92.               lerpFloat(toolbarTranslationDistancePx, 0f, Math.abs(animationProgress))
  93.             }
  94.  
  95.             BuildChildToolbar(
  96.               composeScreenWithToolbar = currentScreen,
  97.               zOrder = zOrder,
  98.               targetToolbarAlpha = targetToolbarAlpha,
  99.               targetToolbarTranslation = targetToolbarTranslation,
  100.               transitionIsProgress = transitionIsProgress,
  101.               toolbarContent = { screenToolbarMovable() }
  102.             )
  103.           }
  104.           else -> {
  105.             BuildChildToolbar(
  106.               composeScreenWithToolbar = currentScreen,
  107.               zOrder = zOrder,
  108.               targetToolbarAlpha = 0f,
  109.               targetToolbarTranslation = 0f,
  110.               transitionIsProgress = transitionIsProgress,
  111.               toolbarContent = { screenToolbarMovable() }
  112.             )
  113.           }
  114.         }
  115.       }
  116.     }
  117.   }
  118. }
  119.  
  120. @Composable
  121. private fun BuildChildToolbar(
  122.   composeScreenWithToolbar: ComposeScreenWithToolbar,
  123.   zOrder: Int,
  124.   targetToolbarAlpha: Float,
  125.   targetToolbarTranslation: Float,
  126.   transitionIsProgress: Boolean,
  127.   toolbarContent: @Composable () -> Unit
  128. ) {
  129.   key(composeScreenWithToolbar.screenKey) {
  130.     Box(
  131.       modifier = Modifier
  132.         .zIndex(zOrder.toFloat())
  133.         .graphicsLayer {
  134.           alpha = targetToolbarAlpha
  135.           translationY = targetToolbarTranslation
  136.         }
  137.         .consumeClicks(consume = transitionIsProgress)
  138.     ) {
  139.       toolbarContent()
  140.     }
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement