Skip to content

Commit

Permalink
Создан модуль corbind-material. Добавлены ивенты AppBarLayoutOffsetCh…
Browse files Browse the repository at this point in the history
…anges
  • Loading branch information
LDRAlighieri committed Oct 25, 2018
1 parent 39805de commit efb1a0f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
1 change: 1 addition & 0 deletions corbind-material/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
40 changes: 40 additions & 0 deletions corbind-material/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions corbind-material/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions corbind-material/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="ru.ldralighieri.corbind.material" />
Original file line number Diff line number Diff line change
@@ -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<Int>(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<Int>(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<Int> = scope.produce(Dispatchers.Main, Channel.CONFLATED) {

val listener = listener(::offer)
addOnOffsetChangedListener(listener)
invokeOnClose { removeOnOffsetChangedListener(listener) }
}

suspend fun AppBarLayout.offsetChanges(): ReceiveChannel<Int> = coroutineScope {

produce<Int>(Dispatchers.Main, Channel.CONFLATED) {
val listener = listener(::offer)
addOnOffsetChangedListener(listener)
invokeOnClose { removeOnOffsetChangedListener(listener) }
}
}


// -----------------------------------------------------------------------------------------------


private fun listener(
emitter: (Int) -> Boolean
) = AppBarLayout.OnOffsetChangedListener { _, verticalOffset -> emitter(verticalOffset) }
7 changes: 4 additions & 3 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit efb1a0f

Please sign in to comment.