From efb1a0f415803ba9f7950a742a628032bf28c560 Mon Sep 17 00:00:00 2001 From: Raupov Vladimir Date: Thu, 25 Oct 2018 16:42:09 +1000 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20corbind-material.=20=D0=94?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B8=D0=B2?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D1=8B=20AppBarLayoutOffsetChanges?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- corbind-material/.gitignore | 1 + corbind-material/build.gradle | 40 +++++++++++ corbind-material/proguard-rules.pro | 21 ++++++ corbind-material/src/main/AndroidManifest.xml | 1 + .../material/AppBarLayoutOffsetChanges.kt | 68 +++++++++++++++++++ settings.gradle | 7 +- 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 corbind-material/.gitignore create mode 100644 corbind-material/build.gradle create mode 100644 corbind-material/proguard-rules.pro create mode 100644 corbind-material/src/main/AndroidManifest.xml create mode 100644 corbind-material/src/main/kotlin/ru/ldralighieri/corbind/material/AppBarLayoutOffsetChanges.kt diff --git a/corbind-material/.gitignore b/corbind-material/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/corbind-material/.gitignore @@ -0,0 +1 @@ +/build diff --git a/corbind-material/build.gradle b/corbind-material/build.gradle new file mode 100644 index 00000000..e0cdc7c1 --- /dev/null +++ b/corbind-material/build.gradle @@ -0,0 +1,40 @@ +apply plugin: 'com.android.library' +apply plugin: 'org.jetbrains.kotlin.android' + +kotlin { + experimental { + coroutines "enable" + } +} + +android { + + compileSdkVersion buildConfig.compileSdk + defaultConfig { + minSdkVersion buildConfig.minSdk + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + sourceSets { + main.java.srcDirs += 'src/main/kotlin' + } + +} + +dependencies { + api project(':corbind') + + api deps.googleMaterial +} diff --git a/corbind-material/proguard-rules.pro b/corbind-material/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/corbind-material/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/corbind-material/src/main/AndroidManifest.xml b/corbind-material/src/main/AndroidManifest.xml new file mode 100644 index 00000000..ce5616a7 --- /dev/null +++ b/corbind-material/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/corbind-material/src/main/kotlin/ru/ldralighieri/corbind/material/AppBarLayoutOffsetChanges.kt b/corbind-material/src/main/kotlin/ru/ldralighieri/corbind/material/AppBarLayoutOffsetChanges.kt new file mode 100644 index 00000000..290ab559 --- /dev/null +++ b/corbind-material/src/main/kotlin/ru/ldralighieri/corbind/material/AppBarLayoutOffsetChanges.kt @@ -0,0 +1,68 @@ +package ru.ldralighieri.corbind.material + +import com.google.android.material.appbar.AppBarLayout +import kotlinx.coroutines.experimental.CoroutineScope +import kotlinx.coroutines.experimental.Dispatchers +import kotlinx.coroutines.experimental.channels.Channel +import kotlinx.coroutines.experimental.channels.ReceiveChannel +import kotlinx.coroutines.experimental.channels.actor +import kotlinx.coroutines.experimental.channels.produce +import kotlinx.coroutines.experimental.coroutineScope + +// ----------------------------------------------------------------------------------------------- + + +fun AppBarLayout.offsetChanges( + scope: CoroutineScope, + action: suspend (Int) -> Unit +) { + val events = scope.actor(Dispatchers.Main, Channel.CONFLATED) { + for (offset in channel) action(offset) + } + + val listener = listener(events::offer) + addOnOffsetChangedListener(listener) + events.invokeOnClose { removeOnOffsetChangedListener(listener) } +} + +suspend fun AppBarLayout.offsetChanges( + action: suspend (Int) -> Unit +) = coroutineScope { + val events = actor(Dispatchers.Main, Channel.CONFLATED) { + for (offset in channel) action(offset) + } + + val listener = listener(events::offer) + addOnOffsetChangedListener(listener) + events.invokeOnClose { removeOnOffsetChangedListener(listener) } +} + + +// ----------------------------------------------------------------------------------------------- + + +fun AppBarLayout.offsetChanges( + scope: CoroutineScope +): ReceiveChannel = scope.produce(Dispatchers.Main, Channel.CONFLATED) { + + val listener = listener(::offer) + addOnOffsetChangedListener(listener) + invokeOnClose { removeOnOffsetChangedListener(listener) } +} + +suspend fun AppBarLayout.offsetChanges(): ReceiveChannel = coroutineScope { + + produce(Dispatchers.Main, Channel.CONFLATED) { + val listener = listener(::offer) + addOnOffsetChangedListener(listener) + invokeOnClose { removeOnOffsetChangedListener(listener) } + } +} + + +// ----------------------------------------------------------------------------------------------- + + +private fun listener( + emitter: (Int) -> Boolean +) = AppBarLayout.OnOffsetChangedListener { _, verticalOffset -> emitter(verticalOffset) } \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index ee855d7c..093c1a82 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,9 +1,10 @@ rootProject.name = 'Corbind' include ':corbind' -include ':corbind-core' include ':corbind-appcompat' -include ':corbind-viewpager' -include ':corbind-swiperefreshlayout' +include ':corbind-core' include ':corbind-drawerlayout' include ':corbind-leanback' +include ':corbind-material' +include ':corbind-swiperefreshlayout' +include ':corbind-viewpager'