Skip to content

Commit

Permalink
Updated to 1.0.0-beta09 version
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingermainbusiness committed Jun 25, 2022
1 parent 970c8c5 commit ac8ff2e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ repositories {

```groovy
dependencies {
implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-beta08'
implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-beta09'
}
```

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0.0-beta08"
versionName "1.0.0-beta09"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ class MainActivity : ComponentActivity() {
}
}

/**
* @param scrollBehavior Defines how a [CollapsingTopBar] should behave during a
* [Modifier.nestedScroll][androidx.compose.ui.input.nestedscroll.nestedScroll] event.
* @param contactNames Just the list of contact names so we can count how much names there are to
* display that number inside our subtitle [Text]
* @param openLeftDrawer Triggers the Left Drawer to be displayed
* */
@Composable
fun OurCollapsingTopBar(
scrollBehavior: CollapsingTopBarScrollBehavior,
Expand Down
2 changes: 1 addition & 1 deletion collapsingtopbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ afterEvaluate {
// You can then customize attributes of the publication as shown below.
groupId = 'com.germainkevin.collapsingtopbarcompose'
artifactId = 'collapsingtopbarcompose'
version = '1.0.0-beta08'
version = '1.0.0-beta09'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.germainkevin.collapsingtopbar

import androidx.compose.animation.*
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
Expand Down Expand Up @@ -48,20 +45,6 @@ fun CollapsingTopBar(
scrollBehavior: CollapsingTopBarScrollBehavior,
elevation: Dp = CollapsingTopBarDefaults.DefaultCollapsingTopBarElevation,
) = with(scrollBehavior) {

val columnWithTitleSubtitleAlpha by getTitleAndSubtitleColumnAlpha(
currentTopBarHeight = currentTopBarHeight,
collapsedTopBarHeight = collapsedTopBarHeight,
expandedTopBarMaxHeight = expandedTopBarMaxHeight,
margin = 20.dp
)

val collapsedTitleAlpha by getCollapsedTitleAlpha(
currentTopBarHeight = currentTopBarHeight,
visibleValue = collapsedTopBarHeight,
invisibleValue = collapsedTopBarHeight + 6.dp
)

CollapsingTopBarLayout(
modifier = modifier,
title = title,
Expand All @@ -71,8 +54,8 @@ fun CollapsingTopBar(
centeredTitleAndSubtitle = centeredTitleAndSubtitle,
contentPadding = contentPadding,
colors = colors,
columnWithTitleSubtitleAlpha = columnWithTitleSubtitleAlpha,
collapsedTitleAlpha = collapsedTitleAlpha,
expandedColumnAlphaValue = expandedColumnAlphaValue.invoke().value,
collapsedTitleAlpha = collapsedTitleAlpha.invoke().value,
currentTopBarHeight = currentTopBarHeight,
elevation = elevation
)
Expand Down Expand Up @@ -103,7 +86,7 @@ private fun CollapsingTopBarLayout(
centeredTitleAndSubtitle: Boolean,
contentPadding: PaddingValues,
colors: CollapsingTopBarColors,
columnWithTitleSubtitleAlpha: Float,
expandedColumnAlphaValue: Float,
collapsedTitleAlpha: Float,
currentTopBarHeight: Dp,
elevation: Dp,
Expand Down Expand Up @@ -134,7 +117,7 @@ private fun CollapsingTopBarLayout(
end = appBarHorizontalPadding
)
)
.alpha(columnWithTitleSubtitleAlpha),
.alpha(expandedColumnAlphaValue),
horizontalAlignment =
if (centeredTitleAndSubtitle) Alignment.CenterHorizontally else Alignment.Start,
verticalArrangement = Arrangement.Center,
Expand Down Expand Up @@ -223,77 +206,4 @@ private val actionsRow: @Composable (@Composable RowScope.() -> Unit) -> Unit =
verticalAlignment = Alignment.Bottom,
content = it
)
}

/**
* In order to know when the "title subtitle column" should have a 1f alpha visibility or 0f alpha
* visibility or a alpha value between 0f and 1f, we need to base that alpha value on the
* [currentTopBarHeight] of the [CollapsingTopBar].
*
* So in this sense when the [currentTopBarHeight] of the [CollapsingTopBar]
* is [collapsedTopBarHeight] + [margin] then the "title subtitle column" should be invisible
* or alpha = 0f, and when the [CollapsingTopBar]'s [currentTopBarHeight] is
* [expandedTopBarMaxHeight] then the "title subtitle column" should be visible or alpha = 1f.
*
* But we also want the "title subtitle column"'s alpha value to be between 0f and 1f when the
* [CollapsingTopBar]'s [currentTopBarHeight] is between
* [collapsedTopBarHeight] + [margin] and [expandedTopBarMaxHeight]
*
* We'll reference [collapsedTopBarHeight] as 56 (Dp) and [expandedTopBarMaxHeight] as 200 (Dp), and
* the [margin] as 20 (Dp)
*
* 56 + 20 --------> 0f (alpha value meaning when the title subtitle column is fully invisible)
*
* 200 --------> 1f (alpha value meaning when the title subtitle column is fully visible)
*
* The distance between [expandedTopBarMaxHeight] - ([collapsedTopBarHeight] + [margin])
* << A distance which we will label as 124 (Dp) because (200 - (56+20) = 124) >>,
* is going to be the 100% distance from making the "title subtitle column" fully visible (100%) or
* alpha =1f and fully invisible (0%) or alpha = 0f, or in between (0%..100%) 0.0f to 1.0f.
*
* So what we do is:
* 124 ----------> 100%
*
* currentTopBarHeight's actual value -------------> alphaValue
*
* Whatever value alphaValue is, is considered the level of visibility the "title subtitle column"
* should have
*
* @param currentTopBarHeight The current height of the [CollapsingTopBar] in [Dp]
* @param collapsedTopBarHeight The height of the [CollapsingTopBar] when it is collapsed
* @param expandedTopBarMaxHeight The height of the [CollapsingTopBar] when it is expanded
* @param margin Making sure that the 'title subtitle column" become visible once the
* [currentTopBarHeight] reaches past [collapsedTopBarHeight] + [margin]
*/
@Composable
private fun getTitleAndSubtitleColumnAlpha(
currentTopBarHeight: Dp,
collapsedTopBarHeight: Dp,
expandedTopBarMaxHeight: Dp,
margin: Dp
): State<Float> {
return animateFloatAsState(
(currentTopBarHeight - (collapsedTopBarHeight + margin)) /
(expandedTopBarMaxHeight - (collapsedTopBarHeight + margin))
)
}

/**
* Sets the alpha value of the collapsed title section
* @param currentTopBarHeight The current height of the [CollapsingTopBar] in [Dp]
* @param visibleValue A value in [Dp] that if [currentTopBarHeight] reaches it, the
* Collapsed Title should become visible
* @param invisibleValue A value in [Dp] that if [currentTopBarHeight] reaches it, the
* Collapsed Title section should become invisible
* */
@Composable
private fun getCollapsedTitleAlpha(
currentTopBarHeight: Dp,
visibleValue: Dp,
invisibleValue: Dp
): State<Float> {
return animateFloatAsState(
if (currentTopBarHeight == visibleValue) 1f
else (visibleValue - currentTopBarHeight) / (invisibleValue - visibleValue)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ interface CollapsingTopBarScrollBehavior {
* keep track of the scroll events.
*/
val nestedScrollConnection: NestedScrollConnection

/**
* The visibility alpha value of the Column which holds as children the Title and Subtitle
* */
val expandedColumnAlphaValue: @Composable () -> State<Float>

/**
* The visibility alpha value of the Title displayed and visible when the [CollapsingTopBar] is
* collapsed
* */
val collapsedTitleAlpha: @Composable () -> State<Float>
}

class DefaultBehaviorOnScroll(
Expand All @@ -88,7 +99,8 @@ class DefaultBehaviorOnScroll(

init {
require(expandedTopBarMaxHeight > collapsedTopBarHeight) {
"expandedTopBarMaxHeight must be greater than collapsedTopBarHeight"
"expandedTopBarMaxHeight ($expandedTopBarMaxHeight) must be greater " +
"than collapsedTopBarHeight ($collapsedTopBarHeight)"
}
}

Expand Down Expand Up @@ -133,4 +145,75 @@ class DefaultBehaviorOnScroll(
return Offset.Zero
}
}

override val expandedColumnAlphaValue: @Composable () -> State<Float> = {
getExpandedColumnAlpha()
}

override val collapsedTitleAlpha: @Composable () -> State<Float> = {
getCollapsedTitleAlpha()
}

/**
* In order to know when the "title subtitle column" should have a 1f alpha visibility or 0f alpha
* visibility or a alpha value between 0f and 1f, we need to base that alpha value on the
* [currentTopBarHeight] of the [CollapsingTopBar].
*
* So in this sense when the [currentTopBarHeight] of the [CollapsingTopBar]
* is [collapsedTopBarHeight] + [margin] then the "title subtitle column" should be invisible
* or alpha = 0f, and when the [CollapsingTopBar]'s [currentTopBarHeight] is
* [expandedTopBarMaxHeight] then the "title subtitle column" should be visible or alpha = 1f.
*
* But we also want the "title subtitle column"'s alpha value to be between 0f and 1f when the
* [CollapsingTopBar]'s [currentTopBarHeight] is between
* [collapsedTopBarHeight] + [margin] and [expandedTopBarMaxHeight]
*
* We'll reference [collapsedTopBarHeight] as 56 (Dp) and [expandedTopBarMaxHeight] as 200 (Dp), and
* the [margin] as 20 (Dp)
*
* 56 + 20 --------> 0f (alpha value meaning when the title subtitle column is fully invisible)
*
* 200 --------> 1f (alpha value meaning when the title subtitle column is fully visible)
*
* The distance between [expandedTopBarMaxHeight] - ([collapsedTopBarHeight] + [margin])
* << A distance which we will label as 124 (Dp) because (200 - (56+20) = 124) >>,
* is going to be the 100% distance from making the "title subtitle column" fully visible (100%) or
* alpha =1f and fully invisible (0%) or alpha = 0f, or in between (0%..100%) 0.0f to 1.0f.
*
* So what we do is:
* 124 ----------> 100%
*
* currentTopBarHeight's actual value -------------> alphaValue
*
* Whatever value alphaValue is, is considered the level of visibility the "title subtitle column"
* should have
*
* @param margin Making sure that the 'title subtitle column" become visible once the
* [currentTopBarHeight] reaches past [collapsedTopBarHeight] + [margin]
*/
@Composable
fun getExpandedColumnAlpha(margin: Dp = 20.dp): State<Float> {
return animateFloatAsState(
(currentTopBarHeight - (collapsedTopBarHeight + margin)) /
(expandedTopBarMaxHeight - (collapsedTopBarHeight + margin))
)
}

/**
* Sets the alpha value of the collapsed title section
* @param visibleValue A value in [Dp] that if [currentTopBarHeight] reaches it, the
* Collapsed Title should become visible
* @param invisibleValue A value in [Dp] that if [currentTopBarHeight] reaches it, the
* Collapsed Title section should become invisible
* */
@Composable
fun getCollapsedTitleAlpha(
visibleValue: Dp = collapsedTopBarHeight,
invisibleValue: Dp = collapsedTopBarHeight + 6.dp
): State<Float> {
return animateFloatAsState(
if (currentTopBarHeight == visibleValue) 1f
else (visibleValue - currentTopBarHeight) / (invisibleValue - visibleValue)
)
}
}

0 comments on commit ac8ff2e

Please sign in to comment.