Skip to content

Commit

Permalink
Second beta version
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingermainbusiness committed Jun 4, 2022
1 parent f946599 commit 813f239
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 45 deletions.
33 changes: 15 additions & 18 deletions 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-beta01'
implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-beta02'
}
```

Expand All @@ -48,14 +48,13 @@ the [sample app](https://github.com/germainkevinbusiness/CollapsingTopBarCompose
In order to use a ```CollapsingTopBar```, you first need to create a ```TopBarScrollBehavior```.

```kotlin
val scrollBehavior = remember {
CollapsingTopBarDefaults.scrollBehavior(
isAlwaysCollapsed = false,
isExpandedWhenFirstDisplayed = true,
collapsedTopBarHeight = 56.dp,
expandedTopBarMaxHeight = 156.dp,
)
}
val scrollBehavior = rememberCollapsingTopBarScrollBehavior(
isAlwaysCollapsed = false,
isExpandedWhenFirstDisplayed = true,
centeredTitleAndSubtitle = false,
collapsedTopBarHeight = 56.dp,
expandedTopBarMaxHeight = 256.dp,
)
```

To know when scrolling occurs inside your Layout so the ```CollapsingTopBar``` can collapse or
Expand All @@ -80,20 +79,18 @@ So when we put it all together we got:

```kotlin

val scrollBehavior = remember {
CollapsingTopBarDefaults.scrollBehavior(
isAlwaysCollapsed = false,
isExpandedWhenFirstDisplayed = true,
collapsedTopBarHeight = 56.dp,
expandedTopBarMaxHeight = 156.dp,
)
}
val scrollBehavior = rememberCollapsingTopBarScrollBehavior(
isAlwaysCollapsed = false,
isExpandedWhenFirstDisplayed = true,
centeredTitleAndSubtitle = false,
collapsedTopBarHeight = 56.dp,
expandedTopBarMaxHeight = 256.dp,
)
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
CollapsingTopBar(
scrollBehavior = scrollBehavior,
centeredTitleAndSubtitle = true, // set to false if you want the expanded title and subtitle to be at the left instead
title = { Text(text = "All contacts") },
subtitle = { Text(text = "17 contacts") },
)
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-beta01"
versionName "1.0.0-beta02"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Scaffold
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Face
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.rememberScaffoldState
import androidx.compose.material3.*
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
Expand All @@ -28,7 +24,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.germainkevin.collapsingtopbar.CollapsingTopBar
import com.germainkevin.collapsingtopbar.CollapsingTopBarDefaults
import com.germainkevin.collapsingtopbar.rememberCollapsingTopBarScrollBehavior
import com.germainkevin.collapsingtopbarcompose.ui.ContactListNames
import com.germainkevin.collapsingtopbarcompose.ui.LeftDrawer
import com.germainkevin.collapsingtopbarcompose.ui.MoreMenuIcons
Expand Down Expand Up @@ -64,21 +60,19 @@ class MainActivity : ComponentActivity() {
/**
* A scrollBehavior determines the behavior of the CollapsingTopBar when it is
* being scrolled and also to track the nestedScroll events*/
val scrollBehavior = remember {
CollapsingTopBarDefaults.scrollBehavior(
isAlwaysCollapsed = false,
isExpandedWhenFirstDisplayed = true,
expandedTopBarMaxHeight = 256.dp,
)
}
val scrollBehavior = rememberCollapsingTopBarScrollBehavior(
isAlwaysCollapsed = false,
isExpandedWhenFirstDisplayed = true,
centeredTitleAndSubtitle = false,
expandedTopBarMaxHeight = 256.dp,
)
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
scaffoldState = scaffoldState,
drawerContent = { LeftDrawer(closeLeftDrawer = closeLeftDrawer) },
topBar = {
CollapsingTopBar(
scrollBehavior = scrollBehavior,
centeredTitleAndSubtitle = false,
title = {
Text(
stringResource(id = R.string.all_contacts),
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-beta01'
version = '1.0.0-beta02'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import androidx.compose.ui.unit.dp
* This should typically be an [IconButton] or [IconToggleButton].
* @param actions the actions displayed at the end of the [CollapsingTopBar]. This should typically
* be [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally.
* @param centeredTitleAndSubtitle Whether the [title] and [subtitle] section should be centered or
* at the start of the [CollapsingTopBar]
* @param colors [CollapsingTopBarColors] that will be used to resolve the colors used for this
* [CollapsingTopBar] in different states. See [CollapsingTopBarDefaults.colors].
* @param contentPadding The padding of the content inside the [CollapsingTopBar]
Expand All @@ -45,7 +43,6 @@ fun CollapsingTopBar(
subtitle: @Composable (() -> Unit)? = null,
navigationIcon: @Composable (() -> Unit)? = null,
actions: @Composable RowScope.() -> Unit = {},
centeredTitleAndSubtitle: Boolean = true,
colors: CollapsingTopBarColors = CollapsingTopBarDefaults.colors(),
contentPadding: PaddingValues = CollapsingTopBarDefaults.ContentPadding,
elevation: Dp = 0.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.germainkevin.collapsingtopbar
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material.contentColorFor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
Expand All @@ -23,6 +23,8 @@ object CollapsingTopBarDefaults {
* @param isExpandedWhenFirstDisplayed When true, Sets the [CollapsingTopBar] to an expanded
* state when first displayed on the UI by setting the [CollapsingTopBar]'s height to
* [expandedTopBarMaxHeight]
* @param centeredTitleAndSubtitle Whether the title and subtitle should be centered when
* Expanded
* @param collapsedTopBarHeight The height of the [CollapsingTopBar] when it's collapsed, the
* default value is [defaultMinimumTopBarHeight]
* @param expandedTopBarMaxHeight The height of the [CollapsingTopBar] when it's expended,
Expand All @@ -31,13 +33,15 @@ object CollapsingTopBarDefaults {
fun scrollBehavior(
isAlwaysCollapsed: Boolean = false,
isExpandedWhenFirstDisplayed: Boolean = true,
centeredTitleAndSubtitle: Boolean = true,
collapsedTopBarHeight: Dp = defaultMinimumTopBarHeight,
expandedTopBarMaxHeight: Dp = defaultMaximumTopBarHeight,
): TopBarScrollBehavior = DefaultBehaviorOnScroll(
isAlwaysCollapsed,
isExpandedWhenFirstDisplayed,
collapsedTopBarHeight,
expandedTopBarMaxHeight
isAlwaysCollapsed = isAlwaysCollapsed,
isExpandedWhenFirstDisplayed = isExpandedWhenFirstDisplayed,
centeredTitleAndSubtitle = centeredTitleAndSubtitle,
collapsedTopBarHeight = collapsedTopBarHeight,
expandedTopBarMaxHeight = expandedTopBarMaxHeight
)

/**
Expand All @@ -50,4 +54,37 @@ object CollapsingTopBarDefaults {
backgroundColor: Color = MaterialTheme.colorScheme.primary,
contentColor: Color = contentColorFor(backgroundColor)
): CollapsingTopBarColors = DefaultCollapsingTopBarColors(backgroundColor, contentColor)
}


/**
* Specifies how the [CollapsingTopBar] should behave when a [Modifier.nestedScroll]
* is detected.
*
* @param isAlwaysCollapsed This will make this [CollapsingTopBar] stay collapsed and stay with
* the [collapsedTopBarHeight] height.
* @param isExpandedWhenFirstDisplayed When true, Sets the [CollapsingTopBar] to an expanded
* state when first displayed on the UI by setting the [CollapsingTopBar]'s height to
* [expandedTopBarMaxHeight]
* @param centeredTitleAndSubtitle Whether the title and subtitle should be centered when Expanded
* @param collapsedTopBarHeight The height of the [CollapsingTopBar] when it's collapsed, the
* default value is [defaultMinimumTopBarHeight]
* @param expandedTopBarMaxHeight The height of the [CollapsingTopBar] when it's expended, the
* default value is [defaultMaximumTopBarHeight]
* */
@Composable
fun rememberCollapsingTopBarScrollBehavior(
isAlwaysCollapsed: Boolean = false,
isExpandedWhenFirstDisplayed: Boolean = true,
centeredTitleAndSubtitle: Boolean = true,
collapsedTopBarHeight: Dp = defaultMinimumTopBarHeight,
expandedTopBarMaxHeight: Dp = defaultMaximumTopBarHeight,
): TopBarScrollBehavior = remember {
CollapsingTopBarDefaults.scrollBehavior(
isAlwaysCollapsed = isAlwaysCollapsed,
isExpandedWhenFirstDisplayed = isExpandedWhenFirstDisplayed,
centeredTitleAndSubtitle = centeredTitleAndSubtitle,
collapsedTopBarHeight = collapsedTopBarHeight,
expandedTopBarMaxHeight = expandedTopBarMaxHeight
)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.germainkevin.collapsingtopbar

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.unit.Dp


/**
* Defines how a [CollapsingTopBar] should behave during a [Modifier.nestedScroll] event.
* */
Expand Down Expand Up @@ -45,6 +42,11 @@ interface TopBarScrollBehavior {
* */
var isExpandedWhenFirstDisplayed: Boolean

/**
* Whether the title and subtitle should be centered when Expanded
* */
var centeredTitleAndSubtitle: Boolean

/**
* Tracks how many times [topBarOffset]'s value is 0.0f. Useful when
* [isExpandedWhenFirstDisplayed] is set to false, because we want the [CollapsingTopBar] to
Expand Down Expand Up @@ -76,6 +78,7 @@ interface TopBarScrollBehavior {
class DefaultBehaviorOnScroll(
override var isAlwaysCollapsed: Boolean,
override var isExpandedWhenFirstDisplayed: Boolean,
override var centeredTitleAndSubtitle: Boolean,
override var collapsedTopBarHeight: Dp,
override var expandedTopBarMaxHeight: Dp,
) : TopBarScrollBehavior {
Expand Down

0 comments on commit 813f239

Please sign in to comment.