Skip to content

Commit

Permalink
Updated to 1.0.0-beta07 version
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingermainbusiness committed Jun 16, 2022
1 parent f902d6c commit 52dc7a5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 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-beta06'
implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-beta07'
}
```

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-beta06"
versionName "1.0.0-beta07"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
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-beta06'
version = '1.0.0-beta07'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
* [CollapsingTopBar]s display is like a [TopAppBar] that can collapse and/or expand.
* [CollapsingTopBar] is like a [TopAppBar] that can collapse and/or expand.
*
* This [CollapsingTopBar] has slots for a title, subtitle, navigation icon, and actions.
*
Expand Down Expand Up @@ -46,7 +46,7 @@ fun CollapsingTopBar(
colors: CollapsingTopBarColors = CollapsingTopBarDefaults.colors(),
contentPadding: PaddingValues = CollapsingTopBarDefaults.ContentPadding,
scrollBehavior: CollapsingTopBarScrollBehavior,
elevation: Dp = 0.dp,
elevation: Dp = CollapsingTopBarDefaults.DefaultCollapsingTopBarElevation,
) = with(scrollBehavior) {

val columnWithTitleSubtitleAlpha by getTitleAndSubtitleColumnAlpha(
Expand Down Expand Up @@ -226,28 +226,44 @@ internal val actionsRow: @Composable (@Composable RowScope.() -> Unit) -> Unit =
}

/**
* We'll reference [collapsedTopBarHeight] as 56 and [expandedTopBarMaxHeight] as 200
* 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].
*
* We'll add a margin to make the Title Subtitle Column disappear before the CollapsingTopBar
* reaches the height of 56.dp which is a margin of 20.dp
* 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.
*
* 56 + 20 --------> 0f (alpha value meaning when the title subtitle section is fully invisible)
* 200 --------> 1f (alpha value meaning when the title subtitle section is fully visible)
* 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]
*
* The distance between 56+20 and 200 is 124. This distance represents the 100% distance from the
* collapsed state and expanded state of the [CollapsingTopBar]
* 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 -------------> x
*
* Whatever value x is, is considered the level of visibility the Title Subtitle Column should have
* 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 A distance added to start making the TitleAndSubtitleColumn's visible only when
* [currentTopBarHeight] reaches [collapsedTopBarHeight] + margin
* @param margin Making sure that the 'title subtitle column" become visible once the
* [currentTopBarHeight] reaches past [collapsedTopBarHeight] + [margin]
*/
@Composable
internal fun getTitleAndSubtitleColumnAlpha(
Expand All @@ -263,6 +279,7 @@ internal fun getTitleAndSubtitleColumnAlpha(
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.germainkevin.collapsingtopbar

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material.contentColorFor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/** Contains default values used for the [CollapsingTopBar] implementation. */
object CollapsingTopBarDefaults {

val DefaultCollapsingTopBarElevation = 0.dp

val ContentPadding =
PaddingValues(start = appBarHorizontalPadding, end = appBarHorizontalPadding)

Expand Down Expand Up @@ -53,7 +56,7 @@ object CollapsingTopBarDefaults {
@Composable
fun colors(
backgroundColor: Color = MaterialTheme.colorScheme.primary,
contentColor: Color = contentColorFor(backgroundColor)
contentColor: Color = contentColorFor(backgroundColor = backgroundColor)
): CollapsingTopBarColors = CollapsingTopBarColors(backgroundColor, contentColor)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ interface CollapsingTopBarScrollBehavior {
var isAlwaysCollapsed: Boolean

/**
* The height of the TopBar when it's collapsed in [Dp]
* The height of the CollapsingTopBar when it's collapsed in [Dp]
* */
var collapsedTopBarHeight: Dp

/**
* The height of the CollapsingTopBar when it's fully expanded
* The height of the CollapsingTopBar when it's fully expanded in [Dp]
* */
var expandedTopBarMaxHeight: Dp

/**
* The live height of the [CollapsingTopBar]
* The live height of the [CollapsingTopBar] in [Dp]
* */
var currentTopBarHeight: Dp

Expand Down Expand Up @@ -79,6 +79,9 @@ interface CollapsingTopBarScrollBehavior {
val nestedScrollConnection: NestedScrollConnection
}

/**
* Here lies the logic on how the [CollapsingTopBar] behaves at all times
* */
class DefaultBehaviorOnScroll(
override var isAlwaysCollapsed: Boolean,
override var isExpandedWhenFirstDisplayed: Boolean,
Expand Down Expand Up @@ -112,7 +115,7 @@ class DefaultBehaviorOnScroll(
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {

if (!isAlwaysCollapsed && !isExpandedWhenFirstDisplayed && trackOffSetIsZero >= 3) {
// Just making sure trackOffSetIsZero doesn't store high numbers, it's unnecessary
// Just making sure trackOffSetIsZero doesn't store high numbers, koz it's unnecessary
if (trackOffSetIsZero > 6) {
trackOffSetIsZero = 3
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

val appBarHorizontalPadding = 4.dp

val defaultMinimumTopBarHeight = 56.dp
Expand Down

0 comments on commit 52dc7a5

Please sign in to comment.