Skip to content

Commit

Permalink
Release 1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingermainbusiness committed Sep 30, 2022
1 parent 561ee02 commit b8ec981
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ repositories {

```groovy
dependencies {
implementation "com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.4"
implementation "com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.5"
}
```

Expand Down
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
}
apply plugin: 'kotlin-android'

android {
compileSdk 33
Expand Down Expand Up @@ -37,13 +37,14 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
kotlinCompilerExtensionVersion '1.3.1'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
namespace 'com.germainkevin.collapsingtopbarcompose'
}

dependencies {
Expand All @@ -52,7 +53,7 @@ dependencies {

implementation "androidx.core:core-ktx:$coreKtx"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.5.1'
implementation 'androidx.activity:activity-compose:1.6.0'


// Compose dependencies
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.germainkevin.collapsingtopbarcompose">
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".MyApp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ import com.germainkevin.collapsingtopbar.*
import com.germainkevin.collapsingtopbarcompose.ui.*
import com.germainkevin.collapsingtopbarcompose.ui.theme.CollapsingTopBarComposeTheme
import kotlinx.coroutines.launch
import timber.log.Timber

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CollapsingTopBarComposeTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
Expand Down Expand Up @@ -88,22 +86,16 @@ private fun HomeScreen(
topBar = {
CollapsingTopBar(
scrollBehavior = scrollBehavior,
// With the addition of the onBackgroundColorChange() callback method
// you can color your app's status bar the same as whatever color your
// CollapsingTopBar background is
colors = CollapsingTopBarDefaults
.colors(
// This will be the color of the CollapsingTopBar when your
// CollapsingTopBar is either collapsing or expanding,
// By default, its value is the same as backgroundColor :
// " CollapsingTopBarDefaults.colors(backgroundColor) "
backgroundColorWhenCollapsingOrExpanding =
MaterialTheme.colorScheme.onPrimaryContainer,
// Emits any current backgroundColor of the CollapsingTopBar
onBackgroundColorChange = {
window.statusBarColor = it.toArgb()
},
),
colors = CollapsingTopBarDefaults.colors(
// This will be the color of the CollapsingTopBar when its state
// is CollapsingTopBarState.IN_BETWEEN
backgroundColorWhenCollapsingOrExpanding = MaterialTheme.colorScheme.onPrimaryContainer,
onBackgroundColorChange = {
// Changes the status bar color to the current background color of the
// CollapsingTopBar
window.statusBarColor = it.toArgb()
},
),
title = TitleText,
expandedTitle = ExpandedTitleText,
subtitle = { SubtitleText(contactNames) },
Expand All @@ -112,23 +104,25 @@ private fun HomeScreen(
)
},
) { contentPadding ->
val context = LocalContext.current
var topBarState = true
LazyColumn(
modifier = Modifier.background(MaterialTheme.colorScheme.background),
contentPadding = contentPadding,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
item {
Spacer(modifier = Modifier.height(6.dp))
Timber.d("CollapsingTopBarState currentTopBarHeight: ${scrollBehavior.currentTopBarHeight}")
Timber.d("CollapsingTopBarState: ${scrollBehavior.currentState.value}")
}
items(count = contactNames.size) {
ContactNameItem(contactNames[it]) { name ->
createToast(context, name)
if (topBarState) scrollBehavior.collapse() else scrollBehavior.expand()
topBarState = !topBarState
val isCollapsed = scrollBehavior.currentState == CollapsingTopBarState.COLLAPSED
val isExpanded = scrollBehavior.currentState == CollapsingTopBarState.EXPANDED
// val context = LocalContext.current
ContactNameItem(contactNames[it]) { _ ->
// createToast(context, name)
if (isExpanded) {
scrollBehavior.collapse(delay = 10, steps = 5.dp)
} else if (isCollapsed) {
scrollBehavior.expand(delay = 10, steps = 5.dp)
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
buildscript {
ext {
compose_version = '1.2.1'
coreKtx = '1.8.0'
libraryVersion ='1.0.4'
coreKtx = '1.9.0'
libraryVersion ='1.0.5'
kotlin_version = '1.7.0'
material3Version ='1.0.0-beta01'
material3Version ='1.0.0-beta03'
timberVersion ='5.0.1'
}
repositories {
Expand All @@ -15,9 +15,9 @@ 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.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

task clean(type: Delete) {
Expand Down
11 changes: 6 additions & 5 deletions collapsingtopbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'maven-publish'
id 'kotlin-android'
}
apply plugin: 'kotlin-android'

android {
publishing {
Expand Down Expand Up @@ -32,11 +32,11 @@ afterEvaluate {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 32
targetSdk 33
consumerProguardFiles "consumer-rules.pro"
}

Expand All @@ -57,19 +57,20 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
kotlinCompilerExtensionVersion '1.3.1'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
namespace 'com.germainkevin.collapsingtopbar'
}

dependencies {

implementation "androidx.core:core-ktx:$coreKtx"
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation "com.jakewharton.timber:timber:$timberVersion"

implementation "androidx.compose.ui:ui:$compose_version"
Expand Down
2 changes: 1 addition & 1 deletion collapsingtopbar/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.germainkevin.collapsingtopbar" />
<manifest />
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import timber.log.Timber
import kotlin.math.roundToInt

/**
* Defines how a [CollapsingTopBar] should behave, mainly during a
Expand Down Expand Up @@ -36,7 +38,7 @@ interface CollapsingTopBarScrollBehavior {
/**
* Notifies about the current [CollapsingTopBarState]
* */
val currentState: State<CollapsingTopBarState>
var currentState: CollapsingTopBarState

/**
* The offset that is added to the height of the [CollapsingTopBar] based on detected
Expand Down Expand Up @@ -118,6 +120,7 @@ class DefaultBehaviorOnScroll(
}
}


override var topBarOffset: Float by mutableStateOf(0f)

override var trackOffSetIsZero: Int by mutableStateOf(0)
Expand All @@ -130,19 +133,27 @@ class DefaultBehaviorOnScroll(
else expandedTopBarMaxHeight
)

override var currentState: CollapsingTopBarState by mutableStateOf(
when (currentTopBarHeight) {
collapsedTopBarHeight -> CollapsingTopBarState.COLLAPSED
expandedTopBarMaxHeight -> CollapsingTopBarState.EXPANDED
else -> CollapsingTopBarState.IN_BETWEEN
}
)

override var offsetLimit: Float = (expandedTopBarMaxHeight - collapsedTopBarHeight).value

override val nestedScrollConnection = object : NestedScrollConnection {

override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {

if (!isAlwaysCollapsed && !isExpandedWhenFirstDisplayed && trackOffSetIsZero >= 3) {
// Just making sure trackOffSetIsZero doesn't store high numbers,
// koz it's unnecessary
if (trackOffSetIsZero > 6) trackOffSetIsZero = 3
currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.dp
// Just keeping trackOffSetIsZero from storing high numbers that are above 3
if (trackOffSetIsZero > 6) {
trackOffSetIsZero = 3
}
currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.roundToInt().dp
} else if (isExpandedWhenFirstDisplayed && !isAlwaysCollapsed) {
currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.dp
currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.roundToInt().dp
}

val newOffset = (topBarOffset + available.y)
Expand All @@ -152,22 +163,16 @@ class DefaultBehaviorOnScroll(
if (topBarOffset == 0.0f) {
trackOffSetIsZero += 1
}

defineCurrentState()

// Consume only the scroll on the Y axis.
available.copy(x = 0f)

return Offset.Zero
}
}

override val currentState: State<CollapsingTopBarState>
get() = mutableStateOf(
when (currentTopBarHeight) {
collapsedTopBarHeight -> CollapsingTopBarState.COLLAPSED
expandedTopBarMaxHeight -> CollapsingTopBarState.EXPANDED
else -> CollapsingTopBarState.IN_BETWEEN
}
)

override val expandedColumnAlphaValue: @Composable () -> State<Float> = {
getExpandedColumnAlpha()
}
Expand Down Expand Up @@ -224,17 +229,16 @@ class DefaultBehaviorOnScroll(
/**
* 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 should become visible.
* Collapsed Title section should become invisible
* */
@Composable
private fun getCollapsedTitleAlpha(
visibleValue: Dp = collapsedTopBarHeight,
visibleValue: Dp = collapsedTopBarHeight.value.toInt().dp,
invisibleValue: Dp = collapsedTopBarHeight + 6.dp
): State<Float> {
return animateFloatAsState(
if (currentTopBarHeight == visibleValue) 1f
if (currentTopBarHeight.toIntDp() == visibleValue) 1f
else (visibleValue - currentTopBarHeight) / (invisibleValue - visibleValue)
)
}
Expand Down
Loading

0 comments on commit b8ec981

Please sign in to comment.