diff --git a/README.md b/README.md index 99d53e2..ffeaa9d 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ repositories { ```groovy dependencies { - implementation "com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.1" + implementation "com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.2" } ``` diff --git a/app/build.gradle b/app/build.gradle index def70f0..a27c064 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,14 +2,15 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } +apply plugin: 'kotlin-android' android { - compileSdk 32 + compileSdk 33 defaultConfig { applicationId "com.germainkevin.collapsingtopbarcompose" minSdk 21 - targetSdk 32 + targetSdk 33 versionCode 1 versionName libraryVersion @@ -50,8 +51,8 @@ dependencies { implementation project(path: ':collapsingtopbar') implementation "androidx.core:core-ktx:$coreKtx" - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0' - implementation 'androidx.activity:activity-compose:1.5.0' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' + implementation 'androidx.activity:activity-compose:1.5.1' // Compose dependencies diff --git a/app/src/main/java/com/germainkevin/collapsingtopbarcompose/MainActivity.kt b/app/src/main/java/com/germainkevin/collapsingtopbarcompose/MainActivity.kt index aedefc7..2949d65 100644 --- a/app/src/main/java/com/germainkevin/collapsingtopbarcompose/MainActivity.kt +++ b/app/src/main/java/com/germainkevin/collapsingtopbarcompose/MainActivity.kt @@ -23,12 +23,11 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringArrayResource import androidx.compose.ui.unit.dp -import com.germainkevin.collapsingtopbar.CollapsingTopBar -import com.germainkevin.collapsingtopbar.CollapsingTopBarDefaults -import com.germainkevin.collapsingtopbar.rememberCollapsingTopBarScrollBehavior +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?) { @@ -112,6 +111,8 @@ private fun HomeScreen( ) }, ) { contentPadding -> + val context = LocalContext.current + var topBarState = true LazyColumn( modifier = Modifier.background(MaterialTheme.colorScheme.background), contentPadding = contentPadding, @@ -119,9 +120,15 @@ private fun HomeScreen( ) { item { Spacer(modifier = Modifier.height(6.dp)) + Timber.d("CollapsingTopBarState currentTopBarHeight: ${scrollBehavior.currentTopBarHeight}") + Timber.d("CollapsingTopBarState: ${scrollBehavior.currentState.value}") } items(count = contactNames.size) { - ContactNameItem(LocalContext.current, contactNames[it]) + ContactNameItem(contactNames[it]) { name -> + createToast(context, name) + if (topBarState) scrollBehavior.collapse() else scrollBehavior.expand() + topBarState = !topBarState + } } } } diff --git a/app/src/main/java/com/germainkevin/collapsingtopbarcompose/ui/Components.kt b/app/src/main/java/com/germainkevin/collapsingtopbarcompose/ui/Components.kt index 548b3da..df44e56 100644 --- a/app/src/main/java/com/germainkevin/collapsingtopbarcompose/ui/Components.kt +++ b/app/src/main/java/com/germainkevin/collapsingtopbarcompose/ui/Components.kt @@ -122,12 +122,12 @@ fun LeftDrawer( } @Composable -fun ContactNameItem(context: Context, contactName: String) { +fun ContactNameItem(contactName: String, onClick: (String) -> Unit) { Row( modifier = Modifier .fillMaxWidth() .background(MaterialTheme.colorScheme.background) - .clickable { createToast(context, contactName) }, + .clickable { onClick(contactName) }, verticalAlignment = Alignment.CenterVertically, content = { Text( diff --git a/build.gradle b/build.gradle index b8254ed..998f743 100644 --- a/build.gradle +++ b/build.gradle @@ -1,15 +1,22 @@ buildscript { ext { - compose_version = '1.2.0' + compose_version = '1.2.1' coreKtx = '1.8.0' - libraryVersion ='1.0.1' - material3Version ='1.0.0-alpha15' + libraryVersion ='1.0.3' + kotlin_version = '1.7.0' + material3Version ='1.0.0-beta01' timberVersion ='5.0.1' } + repositories { + mavenCentral() + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '7.2.1' apply false - id 'com.android.library' version '7.2.1' apply false + 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 } diff --git a/collapsingtopbar/build.gradle b/collapsingtopbar/build.gradle index 4bb3176..7047dfa 100644 --- a/collapsingtopbar/build.gradle +++ b/collapsingtopbar/build.gradle @@ -3,6 +3,7 @@ plugins { id 'org.jetbrains.kotlin.android' id 'maven-publish' } +apply plugin: 'kotlin-android' android { publishing { @@ -68,11 +69,14 @@ android { dependencies { implementation "androidx.core:core-ktx:$coreKtx" - implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'androidx.appcompat:appcompat:1.5.0' implementation "com.jakewharton.timber:timber:$timberVersion" implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.material3:material3:$material3Version" +} +repositories { + mavenCentral() } \ No newline at end of file diff --git a/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarScrollBehavior.kt b/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarScrollBehavior.kt index dddb16d..6fb7fd5 100644 --- a/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarScrollBehavior.kt +++ b/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarScrollBehavior.kt @@ -33,6 +33,11 @@ interface CollapsingTopBarScrollBehavior { * */ var currentTopBarHeight: Dp + /** + * Notifies about the current [CollapsingTopBarState] + * */ + val currentState: State + /** * The offset that is added to the height of the [CollapsingTopBar] based on detected * scroll events from the @@ -132,10 +137,9 @@ class DefaultBehaviorOnScroll( 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 - } + // Just making sure trackOffSetIsZero doesn't store high numbers, + // koz it's unnecessary + if (trackOffSetIsZero > 6) trackOffSetIsZero = 3 currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.dp } else if (isExpandedWhenFirstDisplayed && !isAlwaysCollapsed) { currentTopBarHeight = expandedTopBarMaxHeight + topBarOffset.dp @@ -155,6 +159,15 @@ class DefaultBehaviorOnScroll( } } + override val currentState: State + get() = mutableStateOf( + when (currentTopBarHeight) { + collapsedTopBarHeight -> CollapsingTopBarState.COLLAPSED + expandedTopBarMaxHeight -> CollapsingTopBarState.EXPANDED + else -> CollapsingTopBarState.IN_BETWEEN + } + ) + override val expandedColumnAlphaValue: @Composable () -> State = { getExpandedColumnAlpha() } diff --git a/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarState.kt b/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarState.kt new file mode 100644 index 0000000..a7fc296 --- /dev/null +++ b/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/CollapsingTopBarState.kt @@ -0,0 +1,7 @@ +package com.germainkevin.collapsingtopbar + +enum class CollapsingTopBarState { + COLLAPSED, // When the CollapsingTopBar is collapsed + IN_BETWEEN, // When the CollapsingTopBar is neither collapsed neither expanded + EXPANDED // When the CollapsingTopBar is expanded +} \ No newline at end of file diff --git a/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/Constants.kt b/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/Constants.kt index 26aaaeb..21b034a 100644 --- a/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/Constants.kt +++ b/collapsingtopbar/src/main/java/com/germainkevin/collapsingtopbar/Constants.kt @@ -101,4 +101,22 @@ internal fun CollapsingTopBarScrollBehavior.currentBackgroundColor( colors.onBackgroundColorChange(colors.backgroundColorWhenCollapsingOrExpanding) colors.backgroundColorWhenCollapsingOrExpanding } -) \ No newline at end of file +) + +/** + * Collapses the [CollapsingTopBar] + * */ +fun CollapsingTopBarScrollBehavior.collapse() { + if (currentTopBarHeight != collapsedTopBarHeight) { + currentTopBarHeight = collapsedTopBarHeight + } +} + +/** + * Expands the [CollapsingTopBar] + * */ +fun CollapsingTopBarScrollBehavior.expand() { + if (currentTopBarHeight != expandedTopBarMaxHeight) { + currentTopBarHeight = expandedTopBarMaxHeight + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 9b453ae..4dd2708 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,5 @@ +import org.gradle.api.initialization.resolve.RepositoriesMode + pluginManagement { repositories { gradlePluginPortal() @@ -6,10 +8,11 @@ pluginManagement { } } dependencyResolutionManagement { - repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() + maven { url 'https://jitpack.io' } } } rootProject.name = "CollapsingTopBarCompose"