Extension functions over Android's callback-based APIs which allows writing them in a sequential way within coroutines or observe multiple callbacks through kotlin flow.

Overview

callback-ktx

CI

A lightweight Android library that wraps Android's callback-based APIs into suspending extension functions which allow writing them in a sequential way within coroutines or observe multiple callbacks through Kotlin flow. This library is inspired by Chris Banes blog on suspending over view.

Currently covers following APIs

  • Animation
  • Location
  • RecyclerView
  • Sensor
  • View
  • Widget(TextView)

Including in your project

Callback extensions are divided across different modules based on the category they fall under. For example, all framework APIs would fall under the core module. Anything not related to the framework is in its separate module. So depending on your requirement you can depend on a specific module available on mavenCentral()

To include core extension add the following in your build.gradle

implementation("io.github.sagar-viradiya:callback-core-ktx:1.0.0")

Similarly, you can check individual module's README to know how to include those dependencies.

Examples

Below are a few examples of the extensions and it's usage in coroutine.

Await on animation start (Core extension)

viewLifecycleOwner.lifecycleScope.launch {
  animator.awaitStart()
  // Your code goes here after animation start
}

Await view's layout. If a view is already laid out it will resume coroutine immediately otherwise suspends till the next view layout. The extension takes care of removing the listener internally.

viewLifecycleOwner.lifecycleScope.launch {
  view.awaitDoOnLayout()
  // Do things after view laid out
}

Await on the last location.

viewLifecycleOwner.lifecycleScope.launch {
  val location = fusedLocationProviderClient.awaitLastLocation()    // Suspend coroutine
  // Use last location
}

Observe location changes. The extension takes care of registering and unregistering location update callback based on the state of the lifecycle owner internally.

viewLifecycleOwner.lifecycleScope.launch {
  fusedLocationProviderClient.locationFlow(locationRequest, lifecycleOwner).collect { location ->
    // Consume location
  }
}

Please check the individual module's README for more details.

Contributing

Found APIs that are not covered and want to contribute new extensions? Found an issue or have any suggestions for enhancements? Head over to Contribution guidelines to know more about contributing to this library.

License

Copyright 2021 callback-ktx contributors

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.
Comments
  • Maven publish setup

    Maven publish setup

    • Add gradle maven publish plugin
    • Add POM details in the gradle.properties
    • Add module specific POM details in the module's gradle.properties
    • Add job in github action workflow to upload snapshots
    • Add publish script
    opened by sagar-viradiya 0
  • View extensions

    View extensions

    • Add extension to await view attachment to window
    • Add extension to await view detachment to window
    • Add extension to await view pre draw
    • Add instrumentation test for all new extension
    opened by sagar-viradiya 0
  • Location extension

    Location extension

    • Add location extension for last location and location flow
    • Add instrumentation test for location extensions
    • Modify CI script to spin up devices with playstore
    • Add location permission in test activity
    opened by sagar-viradiya 0
  • Animation extensions

    Animation extensions

    • Add extension on animator callbacks
    • Add tests for extension on animator callbacks
    • Minor refactoring on view extensions
    • Add annotation dependency for annotating API restriction on extension
    opened by sagar-viradiya 0
  • View extensions

    View extensions

    • Add extension on doOnNextLayout and doOnLayout
    • Add test for global layout change callback extension
    • Add tests for doOnNextLayout and doOnLayout extensions
    opened by sagar-viradiya 0
  • Test setup

    Test setup

    • Add test module having test activity and other test utilities.
    • Add instrumentation test related dependencies.
    • Add instrumentation test for TextView extensions.
    • Add instrumentation test for View extensions.
    • Add shell script to run ktlint and all instrumentation tests.
    opened by sagar-viradiya 0
Releases(1.0.0)
Owner
Sagar Viradiya
Google Developer Expert for Android | Co-organizer of GDG MAD
Sagar Viradiya
WorkManager ,One time,Sequential Execution, Periodic time Execution

WokManagerSample WorkManager ,One time,Sequential Execution, Periodic time Execu

Chhote Lal Pal 0 Dec 21, 2021
Transform java callback to kotlin suspend method.

Callback2Coroutines Transform Java callback to coroutines suspend method. 将传统Java callback 方法转换为kotlin中的suspend方法。 接入指南 根目录下加入jatpack的repository: allp

RainFool 14 May 25, 2021
A framework for writing composable parsers based on Kotlin Coroutines.

Parsus A framework for writing composable parsers based on Kotlin Coroutines. val booleanGrammar = object : Grammar<BooleanExpression>() { val ws

Aleksei Semin 28 Nov 1, 2022
A kotlin library of extension functions that add smalltalk style methods to objects.

KtTalk A kotlin library of extension functions that add smalltalk style methods to objects. Motivation Smalltalk is a pure OO language in which everyt

null 11 Oct 16, 2021
A library provides some useful kotlin extension functions

ktext ?? A library provides some useful kotlin extension functions. Including in your project Gradle Add below codes to your root build.gradle file (n

热心市民苏苏仔 76 Oct 26, 2022
A collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android.

requireKTX requireKTX is a collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android, using the sam

Márton Braun 82 Oct 1, 2022
Android Library to handle multiple Uri's(paths) received through Intents.

?? Handle Path Oz Android library written in Kotlin, but can be used in Java too. Built to handle a single or multiple Uri (paths) received through In

Murillo Comino 57 Dec 14, 2022
🪐 Modern Android development with Hilt, Coroutines, Flow, JetPack(ViewModel) based on MVVM architecture.

Ceres ?? Modern Android development with Hilt, Coroutines, Flow, JetPack(ViewModel) based on MVVM architecture. Download Gradle Add the dependency bel

Teodor G. 21 Jan 11, 2023
A kotlin implementation of commutative encryption based on ECC ElGamal encryption over Curve25519

komuta A commutative encryption implementation. This is a naive implementation of commutative encryption using the ElGamal scheme applied over Curve25

Mircea Nistor 7 Nov 17, 2022
Movie app that receives popular movies and allows the user to search for the specific movie through the Rest API with help of retrofit library &MVVM architecture.

MovieClue Millions of movies, TV shows and people to discover. Explore now Movie app that recieves popular movies and allow the user to search for spe

Shubham Tomar 6 Mar 31, 2022
An Android Library that provides social login for 15 platforms within by RxJava2, Kotlin and Firebase Authentication.

RxSocialLogin The license information for logo is located at the bottom of the document. These instructions are available in their respective language

WindSekirun (wind.seo) 124 Nov 21, 2022
🍭 GithubSearchKMM - Github Repos Search - Android - iOS - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Dependency Injection, shared KMP ViewModel, Clean Architecture

GithubSearchKMM Github Repos Search - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Depend

Petrus Nguyễn Thái Học 50 Jan 7, 2023
Workout Journal is a mobile app based on Multi-Module and Clean Architecture for those who want to track their progress over a workout and a calendar period.

Workout-Journal Workout Journal is a mobile app for those who want to track their progress over a workout and a calendar period. The app allows you to

Maxim Smolyakov 4 Oct 23, 2022
Muhammad Ariananda 7 Jul 17, 2022
Built with Jetpack compose, multi modules MVVM clean architecture, coroutines + flow, dependency injection, jetpack navigation and other jetpack components

RickAndMortyCompose - Work in progress A simple app using Jetpack compose, clean architecture, multi modules, coroutines + flows, dependency injection

Daniel Waiguru 9 Jul 13, 2022
Includes jetpack compose, navigation, paging, hilt, retrofit, coil, coroutines, flow..

Nextflix-Composable A Clean Architecture App to show use of multi-module-architecture in a Jetpack Compose. The modules are as follow: app: Presentati

Talha Fakıoğlu 198 Jan 1, 2023
CryptoMovies is a small app that show modern Android developement: with Hilt, Coroutines, Flow, Jetpack and Material Design 3

CryptoMovies is a small app that show modern Android developement: with Hilt, Coroutines, Flow, Jetpack and Material Design 3.

Leonardo Pirro 7 Sep 2, 2022
Educational App made with Retrofit, Coroutines, Navigation Component, Room, Dagger Hilt, Flow & Material Motion Animations.

TechHub TechHub is a sample educational app that provides courses for people who want to learn new skills in mostly tech-related areas. The goal of th

Jon Areas 32 Dec 20, 2022
A declarative, Kotlin-idiomatic API for writing dynamic command line applications.

A declarative, Kotlin-idiomatic API for writing dynamic command line applications.

Varabyte 349 Jan 9, 2023