Skip to content

Commit

Permalink
Merge pull request #17 from LDRAlighieri/release/1.9.0
Browse files Browse the repository at this point in the history
Release 1.9.0. New back events, update kotlin, coroutines, material and other dependencies
  • Loading branch information
LDRAlighieri authored Oct 8, 2023
2 parents 67f5d59 + 9360f91 commit e1f449f
Show file tree
Hide file tree
Showing 42 changed files with 548 additions and 123 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# ChangeLog


## Version 1.9.0

* New: Bindings list:
* AndroidX library bindings:
* `corbind-activity`:
* `OnBackPressedDispatcher`:
* `backProgressed`
* `backEvents`
* Update: Kotlin to v1.9.10.
* Update: Kotlin coroutines to v1.7.3.
* Update: Material components to v1.10.0.
* Update: Minor update of other libraries.


## Version 1.8.0

* New: Bindings list:
Expand Down
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![Corbind](logo.svg)](https://ldralighieri.github.io/Corbind)

[![Maven Central](https://img.shields.io/maven-central/v/ru.ldralighieri.corbind/corbind.svg)](https://search.maven.org/search?q=g:ru.ldralighieri.corbind)
[![Kotlin Version](https://img.shields.io/badge/Kotlin-v1.8.21-blue.svg?logo=kotlin)](https://kotlinlang.org)
[![Kotlin Coroutines Version](https://img.shields.io/badge/Coroutines-v1.7.0-blue.svg)](https://kotlinlang.org/docs/reference/coroutines-overview.html)
[![Kotlin Version](https://img.shields.io/badge/Kotlin-v1.9.10-blue.svg?logo=kotlin)](https://kotlinlang.org)
[![Kotlin Coroutines Version](https://img.shields.io/badge/Coroutines-v1.7.3-blue.svg)](https://kotlinlang.org/docs/reference/coroutines-overview.html)
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a1c9a1b1d1ce4ca7a201ab93492bf6e0)](https://app.codacy.com/gh/LDRAlighieri/Corbind)
Expand All @@ -28,33 +28,33 @@ Please consider giving this repository a star ⭐ if you like the project.
Platform bindings:
```kotlin
dependencies {
implementation("ru.ldralighieri.corbind:corbind:1.8.0")
implementation("ru.ldralighieri.corbind:corbind:1.9.0")
}
```

AndroidX library bindings:
```kotlin
dependencies {
implementation("ru.ldralighieri.corbind:corbind-activity:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-appcompat:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-core:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-drawerlayout:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-fragment:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-leanback:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-lifecycle:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-navigation:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-recyclerview:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-slidingpanelayout:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-swiperefreshlayout:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-viewpager:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-viewpager2:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-activity:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-appcompat:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-core:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-drawerlayout:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-fragment:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-leanback:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-lifecycle:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-navigation:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-recyclerview:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-slidingpanelayout:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-swiperefreshlayout:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-viewpager:1.9.0")
implementation("ru.ldralighieri.corbind:corbind-viewpager2:1.9.0")
}
```

Google 'material' library bindings:
```kotlin
dependencies {
implementation("ru.ldralighieri.corbind:corbind-material:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-material:1.9.0")
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ru.ldralighieri.corbind

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.kotlin.dsl.configure
Expand All @@ -28,7 +27,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions

internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *>,
commonExtension: CommonExtension<*, *, *, *, *>,
) {
val compileSdk: String by project
val minSdk: String by project
Expand All @@ -44,13 +43,6 @@ internal fun Project.configureKotlinAndroid(
jvmToolchain(17)
}

// Remove this after https://issuetracker.google.com/issues/260059413 is fixed
// https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
allWarningsAsErrors = true
freeCompilerArgs = freeCompilerArgs + listOf(
Expand All @@ -63,6 +55,6 @@ internal fun Project.configureKotlinAndroid(
private fun Project.kotlin(action: KotlinAndroidProjectExtension.() -> Unit) =
extensions.configure(action)

private fun CommonExtension<*, *, *, *>.kotlinOptions(block: KotlinJvmOptions.() -> Unit) {
private fun CommonExtension<*, *, *, *, *>.kotlinOptions(block: KotlinJvmOptions.() -> Unit) {
(this as ExtensionAware).extensions.configure("kotlinOptions", block)
}
23 changes: 17 additions & 6 deletions corbind-activity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@ To add androidx activity bindings, import `corbind-activity` module:

```kotlin
dependencies {
implementation("ru.ldralighieri.corbind:corbind-activity:1.8.0")
implementation("ru.ldralighieri.corbind:corbind-activity:1.9.0")
}
```

## List of extensions

Component | Extension | Description
--|---|--
**OnBackPressedDispatcher** | [`backPresses`][OnBackPressedDispatcher_backPresses] | Called when OnBackPressedDispatcher.onBackPressed triggered.
**OnBackPressedDispatcher** | [`backPresses`][OnBackPressedDispatcher_backPresses] | Called when OnBackPressedDispatcher.onBackPressed triggered. OnBackPressed events only
**OnBackPressedDispatcher** | [`backProgressed`][OnBackPressedDispatcher_backProgressed] | Called when OnBackPressedDispatcher.dispatchOnBackProgressed triggered. OnBackProgressed event only
**OnBackPressedDispatcher** | [`backEvents`][OnBackPressedDispatcher_backEvents] | Called when any callback event triggered. All events


## Simple examples

```kotlin
onBackPressedDispatcher.backPresses(lifecycleOwner = this) // Flow<Unit>
.onEach { /* handle onBackPressed event */ }
.flowWithLifecycle(lifecycle)
.launchIn(lifecycleScope) // lifecycle-runtime-ktx
onBackPressedDispatcher.backEvents(lifecycleOwner = this)
.onEach { event ->
when (event) {
is OnBackPressed -> { /* handle back pressed event */ }
is OnBackCanceled -> { /* handle back cancel event */ }
is OnBackStarted -> { /* handle back started event */ }
is OnBackProgressed -> { /* handle back progressed event */ }
}
}
.flowWithLifecycle(lifecycle)
.launchIn(lifecycleScope) // lifecycle-runtime-ktx
```

More examples in source code

[OnBackPressedDispatcher_backPresses]: https://github.com/LDRAlighieri/Corbind/blob/master/corbind-activity/src/main/kotlin/ru/ldralighieri/corbind/activity/OnBackPressedDispatcherBackPresses.kt
[OnBackPressedDispatcher_backProgressed]: https://github.com/LDRAlighieri/Corbind/blob/master/corbind-activity/src/main/kotlin/ru/ldralighieri/corbind/activity/OnBackPressedDispatcherBackProgressed.kt
[OnBackPressedDispatcher_backEvents]: https://github.com/LDRAlighieri/Corbind/blob/master/corbind-activity/src/main/kotlin/ru/ldralighieri/corbind/activity/OnBackPressedDispatcherOnBackEvents.kt
59 changes: 59 additions & 0 deletions corbind-activity/api/corbind-activity.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
public final class ru/ldralighieri/corbind/activity/OnBackCanceled : ru/ldralighieri/corbind/activity/OnBackEvent {
public static final field INSTANCE Lru/ldralighieri/corbind/activity/OnBackCanceled;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public abstract interface class ru/ldralighieri/corbind/activity/OnBackEvent {
}

public final class ru/ldralighieri/corbind/activity/OnBackPressed : ru/ldralighieri/corbind/activity/OnBackEvent {
public static final field INSTANCE Lru/ldralighieri/corbind/activity/OnBackPressed;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class ru/ldralighieri/corbind/activity/OnBackPressedDispatcherBackPressesKt {
public static final fun backPresses (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;)Lkotlinx/coroutines/flow/Flow;
public static final fun backPresses (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand All @@ -8,3 +25,45 @@ public final class ru/ldralighieri/corbind/activity/OnBackPressedDispatcherBackP
public static synthetic fun backPresses$default (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class ru/ldralighieri/corbind/activity/OnBackPressedDispatcherBackProgressedKt {
public static final fun backProgressed (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;)Lkotlinx/coroutines/flow/Flow;
public static final fun backProgressed (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun backProgressed (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;I)Lkotlinx/coroutines/channels/ReceiveChannel;
public static final fun backProgressed (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;)V
public static synthetic fun backProgressed$default (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun backProgressed$default (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;IILjava/lang/Object;)Lkotlinx/coroutines/channels/ReceiveChannel;
public static synthetic fun backProgressed$default (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
}

public final class ru/ldralighieri/corbind/activity/OnBackPressedDispatcherOnBackEventsKt {
public static final fun backEvents (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;)Lkotlinx/coroutines/flow/Flow;
public static final fun backEvents (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun backEvents (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;I)Lkotlinx/coroutines/channels/ReceiveChannel;
public static final fun backEvents (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;)V
public static synthetic fun backEvents$default (Landroidx/activity/OnBackPressedDispatcher;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun backEvents$default (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;IILjava/lang/Object;)Lkotlinx/coroutines/channels/ReceiveChannel;
public static synthetic fun backEvents$default (Landroidx/activity/OnBackPressedDispatcher;Lkotlinx/coroutines/CoroutineScope;Landroidx/lifecycle/LifecycleOwner;ILkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
}

public final class ru/ldralighieri/corbind/activity/OnBackProgressed : ru/ldralighieri/corbind/activity/OnBackEvent {
public fun <init> (Landroidx/activity/BackEventCompat;)V
public final fun component1 ()Landroidx/activity/BackEventCompat;
public final fun copy (Landroidx/activity/BackEventCompat;)Lru/ldralighieri/corbind/activity/OnBackProgressed;
public static synthetic fun copy$default (Lru/ldralighieri/corbind/activity/OnBackProgressed;Landroidx/activity/BackEventCompat;ILjava/lang/Object;)Lru/ldralighieri/corbind/activity/OnBackProgressed;
public fun equals (Ljava/lang/Object;)Z
public final fun getBackEvent ()Landroidx/activity/BackEventCompat;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class ru/ldralighieri/corbind/activity/OnBackStarted : ru/ldralighieri/corbind/activity/OnBackEvent {
public fun <init> (Landroidx/activity/BackEventCompat;)V
public final fun component1 ()Landroidx/activity/BackEventCompat;
public final fun copy (Landroidx/activity/BackEventCompat;)Lru/ldralighieri/corbind/activity/OnBackStarted;
public static synthetic fun copy$default (Lru/ldralighieri/corbind/activity/OnBackStarted;Landroidx/activity/BackEventCompat;ILjava/lang/Object;)Lru/ldralighieri/corbind/activity/OnBackStarted;
public fun equals (Ljava/lang/Object;)Z
public final fun getBackEvent ()Landroidx/activity/BackEventCompat;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ suspend fun OnBackPressedDispatcher.backPresses(
*
* ```
* launch {
* onBackPressedDispatcher.backPresses(lifecycleOwner = this)
* .consumeEach { /* handle onBackPressed event */ }
* onBackPressedDispatcher.backPresses(scope, lifecycleOwner = this)
* .consumeEach { /* handle back pressed event */ }
* }
* ```
*
Expand All @@ -103,7 +103,7 @@ fun OnBackPressedDispatcher.backPresses(
*
* ```
* onBackPressedDispatcher.backPresses(lifecycleOwner = this)
* .onEach { /* handle onBackPressed event */ }
* .onEach { /* handle back pressed event */ }
* .flowWithLifecycle(lifecycle)
* .launchIn(lifecycleScope) // lifecycle-runtime-ktx
* ```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2023 Vladimir Raupov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ru.ldralighieri.corbind.activity

import androidx.activity.BackEventCompat
import androidx.activity.OnBackPressedCallback
import androidx.activity.OnBackPressedDispatcher
import androidx.annotation.CheckResult
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.isActive
import ru.ldralighieri.corbind.internal.corbindReceiveChannel

/**
* Perform an action on [OnBackPressedDispatcher.dispatchOnBackProgressed] call.
*
* @param scope Root coroutine scope
* @param lifecycleOwner The LifecycleOwner which controls when the callback should be invoked
* @param capacity Capacity of the channel's buffer (no buffer by default)
* @param action An action to perform
*/
fun OnBackPressedDispatcher.backProgressed(
scope: CoroutineScope,
lifecycleOwner: LifecycleOwner,
capacity: Int = Channel.RENDEZVOUS,
action: suspend (Float) -> Unit
) {
val events = scope.actor<Float>(Dispatchers.Main.immediate, capacity) {
for (progress in channel) action(progress)
}

val callback = callback(scope, events::trySend)
addCallback(lifecycleOwner, callback)
events.invokeOnClose { callback.remove() }
}

/**
* Perform an action on [OnBackPressedDispatcher.dispatchOnBackProgressed] call, inside new
* [CoroutineScope].
*
* @param lifecycleOwner The LifecycleOwner which controls when the callback should be invoked
* @param capacity Capacity of the channel's buffer (no buffer by default)
* @param action An action to perform
*/
suspend fun OnBackPressedDispatcher.backProgressed(
lifecycleOwner: LifecycleOwner,
capacity: Int = Channel.RENDEZVOUS,
action: suspend (Float) -> Unit
) = coroutineScope {
backProgressed(this, lifecycleOwner, capacity, action)
}

/**
* Create a channel which emits back progress on [OnBackPressedDispatcher.dispatchOnBackProgressed]
* call.
*
* Example:
*
* ```
* launch {
* onBackPressedDispatcher.backProgressed(scope, lifecycleOwner = this)
* .consumeEach { /* handle back progressed event */ }
* }
* ```
*
* @param scope Root coroutine scope
* @param lifecycleOwner The LifecycleOwner which controls when the callback should be invoked
* @param capacity Capacity of the channel's buffer (no buffer by default)
*/
fun OnBackPressedDispatcher.backProgressed(
scope: CoroutineScope,
lifecycleOwner: LifecycleOwner,
capacity: Int = Channel.RENDEZVOUS
): ReceiveChannel<Float> = corbindReceiveChannel(capacity) {
val callback = callback(scope, ::trySend)
addCallback(lifecycleOwner, callback)
invokeOnClose { callback.remove() }
}

/**
* Create a flow which emits back progress on [OnBackPressedDispatcher.dispatchOnBackProgressed]
* call.
*
* Example:
*
* ```
* onBackPressedDispatcher.backProgressed(lifecycleOwner = this)
* .onEach { /* handle back progressed event */ }
* .flowWithLifecycle(lifecycle)
* .launchIn(lifecycleScope) // lifecycle-runtime-ktx
* ```
*
* @param lifecycleOwner The LifecycleOwner which controls when the callback should be invoked
*/
fun OnBackPressedDispatcher.backProgressed(lifecycleOwner: LifecycleOwner): Flow<Float> =
channelFlow {
val callback = callback(this, ::trySend)
addCallback(lifecycleOwner, callback)
awaitClose { callback.remove() }
}

@CheckResult
private fun callback(
scope: CoroutineScope,
emitter: (Float) -> Unit
) = object : OnBackPressedCallback(true) {

override fun handleOnBackProgressed(backEvent: BackEventCompat) {
if (scope.isActive) { emitter(backEvent.progress) }
}

override fun handleOnBackPressed() = Unit
}
Loading

0 comments on commit e1f449f

Please sign in to comment.