Skip to content

Commit

Permalink
1.0.0-alpha06 release version
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingermainbusiness committed May 31, 2022
1 parent c546569 commit 7ebd66e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 172 deletions.
74 changes: 41 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![](https://jitpack.io/v/germainkevinbusiness/CollapsingTopBarCompose.svg)](https://jitpack.io/#germainkevinbusiness/CollapsingTopBarCompose)

# CollapsingTopBarCompose

A Jetpack Compose Collapsing Top Bar, that expands or collapses based on the scrolling of a content

<table>
Expand Down Expand Up @@ -33,75 +35,81 @@ repositories {

```groovy
dependencies {
implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-alpha05'
implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-alpha06'
}
```

# Usage
Basic usage is shown below, there's a more elaborate example in
the [sample app](https://github.com/germainkevinbusiness/CollapsingTopBarCompose/blob/master/app/src/main/java/com/germainkevin/collapsingtopbarcompose/MainActivity.kt).

Basic usage is shown below, there's a more elaborate example in
the [sample app](https://github.com/germainkevinbusiness/CollapsingTopBarCompose/blob/master/app/src/main/java/com/germainkevin/collapsingtopbarcompose/MainActivity.kt)
.

In order to use a ```CollapsingTopBar```, you first need to create a ```TopBarScrollBehavior```.

```kotlin
val scrollBehavior = remember {
CollapsingTopBarDefaults.collapsingTopBarScrollBehavior(
val scrollBehavior = remember {
CollapsingTopBarDefaults.behaviorOnScroll(
isAlwaysCollapsed = false,
isInitiallyCollapsed = true,
collapsedTopBarHeight = 56.dp,
expandedTopBarMaxHeight = 156.dp,
)
}
)
}
```
To know when scrolling occurs inside your Layout so the ```CollapsingTopBar``` can collapse or expand, add the ```scrollBehavior.nestedScrollConnection``` inside your Layout's ```Modifier.nestedScroll``` :

To know when scrolling occurs inside your Layout so the ```CollapsingTopBar``` can collapse or
expand, add the ```scrollBehavior.nestedScrollConnection``` inside your
Layout's ```Modifier.nestedScroll``` :

```kotlin
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
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") },
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") },
)
},
){}
},
) {}
```

So when we put it all together we got:

```kotlin

val scrollBehavior = remember {
CollapsingTopBarDefaults.collapsingTopBarScrollBehavior(
val scrollBehavior = remember {
CollapsingTopBarDefaults.behaviorOnScroll(
isAlwaysCollapsed = false,
isInitiallyCollapsed = true,
collapsedTopBarHeight = 56.dp,
expandedTopBarMaxHeight = 156.dp,
)
}
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
)
}
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") },
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") },
)
},
) {
},
) {
LazyColumn(
contentPadding = innerPadding,
verticalArrangement = Arrangement.spacedBy(8.dp)
contentPadding = innerPadding,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
val context = LocalContext.current
val contactNames = context.resources.getStringArray(R.array.contactNames)
items(count = contactNames.size) {
ContactListNames(context, contactNames[it])
ContactListNames(context, contactNames[it])
}
}
}
}
```

**That's it!**
Expand Down
20 changes: 6 additions & 14 deletions 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-alpha05"
versionName "1.0.0-alpha06"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -50,25 +50,17 @@ dependencies {
implementation project(path: ':collapsingtopbar')

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha12'
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.4.0'

implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha12"

// Compose dependencies
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.0-rc01"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.material:material-icons-extended:$compose_version"

// Accompanist
//Jetpack Navigation Compose Material
implementation 'com.google.accompanist:accompanist-navigation-material:0.24.9-beta'
//Insets for Jetpack Compose
implementation 'com.google.accompanist:accompanist-insets:0.24.9-beta'
//System UI Controller
implementation 'com.google.accompanist:accompanist-systemuicontroller:0.24.9-beta'
implementation 'androidx.compose.material3:material3:1.0.0-alpha12'
implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha12"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.0-rc01"

// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,6 @@ import androidx.compose.ui.unit.sp
import com.germainkevin.collapsingtopbar.CollapsingTopBar
import com.germainkevin.collapsingtopbar.TopBarScrollBehavior

@Composable
fun CollapsingTopBarExample(scrollBehavior: TopBarScrollBehavior, contactNames: Array<String>) {
CollapsingTopBar(
scrollBehavior = scrollBehavior,
centeredTitleAndSubtitle = true,
title = {
Text(
stringResource(id = R.string.all_contacts),
style = LocalTextStyle.current.copy(
fontSize = 24.sp,
fontWeight = FontWeight.Normal,
color = MaterialTheme.colorScheme.primary
)
)
},
subtitle = {
Text(
stringResource(
id = R.string.contactNamesCount,
contactNames.size.toString()
),
style = LocalTextStyle.current.copy(
fontWeight = FontWeight.Normal,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
)
},
navigationIcon = {
IconButton(onClick = { }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = stringResource(id = R.string.hamburger_menu),
tint = MaterialTheme.colorScheme.primary
)
}
},
actions = { MoreMenuIcons() },
)
}

@Composable
fun ContactListNames(context: Context, contactName: String) {
Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.*
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
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.collapsingtopbarcompose.ui.theme.CollapsingTopBarComposeTheme

Expand All @@ -34,18 +37,49 @@ 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
.collapsingTopBarScrollBehavior(
isAlwaysCollapsed = false,
expandedTopBarMaxHeight = 256.dp,
)
CollapsingTopBarDefaults.behaviorOnScroll(
isAlwaysCollapsed = false,
expandedTopBarMaxHeight = 256.dp,
)
}
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
CollapsingTopBarExample(
CollapsingTopBar(
scrollBehavior = scrollBehavior,
contactNames = contactNames
centeredTitleAndSubtitle = true,
title = {
Text(
stringResource(id = R.string.all_contacts),
style = LocalTextStyle.current.copy(
fontSize = 24.sp,
fontWeight = FontWeight.Normal,
color = MaterialTheme.colorScheme.primary
)
)
},
subtitle = {
Text(
stringResource(
id = R.string.contactNamesCount,
contactNames.size.toString()
),
style = LocalTextStyle.current.copy(
fontWeight = FontWeight.Normal,
color = MaterialTheme.colorScheme.onPrimaryContainer
)
)
},
navigationIcon = {
IconButton(onClick = { }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = stringResource(id = R.string.hamburger_menu),
tint = MaterialTheme.colorScheme.primary
)
}
},
actions = { MoreMenuIcons() },
)
},
content = { innerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package com.germainkevin.collapsingtopbarcompose.ui.theme
import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat

private val DarkColorScheme = darkColorScheme(
primary = Purple80,
Expand Down Expand Up @@ -55,8 +51,11 @@ fun CollapsingTopBarComposeTheme(
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
(view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
(view.context as Activity).apply {
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view)?.isAppearanceLightStatusBars =
darkTheme
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}

Expand Down
15 changes: 12 additions & 3 deletions collapsingtopbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ plugins {
id 'maven-publish'
}

android {
publishing {
singleVariant("release") {
withSourcesJar()
withJavadocJar()
}
}
}

afterEvaluate {
publishing {
publications {
Expand All @@ -15,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-alpha05'
version = '1.0.0-alpha06'
}
}
}
Expand Down Expand Up @@ -60,10 +69,10 @@ dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'com.jakewharton.timber:timber:5.0.1'

implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha12'
}
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 information and actions at the top of a screen.
* [CollapsingTopBar]s display 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 @@ -52,11 +52,11 @@ fun CollapsingTopBar(
scrollBehavior: TopBarScrollBehavior
) = with(scrollBehavior) {

if (!isAlwaysCollapsed && isInitiallyCollapsed && trackOffSetIsZero >= 3) {
if (!isAlwaysCollapsed && !isExpandedWhenFirstDisplayed && trackOffSetIsZero >= 3) {
// Make sure the trackOffSetIsZero variable does not exceed the number 10
if (trackOffSetIsZero > 10) trackOffSetIsZero = 3
currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.dp
} else if (!isInitiallyCollapsed) {
} else if (isExpandedWhenFirstDisplayed) {
currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.dp
}

Expand Down
Loading

0 comments on commit 7ebd66e

Please sign in to comment.