All Projects → enbandari → Kotlin Coroutines Android

enbandari / Kotlin Coroutines Android

Licence: mit
Useful extensions for coroutines. AutoDispose + MainScope

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Kotlin Coroutines Android

Paperplane
📚 PaperPlane - An Android reading app, including articles from Zhihu Daily, Guokr Handpick and Douban Moment.
Stars: ✭ 1,147 (+1389.61%)
Mutual labels:  coroutines, kotlin-coroutines
Uniflow Kt
Uniflow 🦄 - Simple Unidirectional Data Flow for Android & Kotlin, using Kotlin coroutines and open to functional programming
Stars: ✭ 414 (+437.66%)
Mutual labels:  coroutines, kotlin-coroutines
Korio
Korio: Kotlin cORoutines I/O : Virtual File System + Async/Sync Streams + Async TCP Client/Server + WebSockets for Multiplatform Kotlin 1.3
Stars: ✭ 282 (+266.23%)
Mutual labels:  coroutines, kotlin-coroutines
fluid-mongo
Kotlin coroutine support for MongoDB built on top of the official Reactive Streams Java Driver
Stars: ✭ 27 (-64.94%)
Mutual labels:  coroutines, kotlin-coroutines
Kotlin Coroutines Retrofit
Kotlin Coroutines await() extension for Retrofit Call
Stars: ✭ 812 (+954.55%)
Mutual labels:  coroutines, kotlin-coroutines
Coffeegram
Android app using Jetpack Compose together with StateFlow and MVI
Stars: ✭ 155 (+101.3%)
Mutual labels:  coroutines, kotlin-coroutines
Kroto Plus
gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Stars: ✭ 400 (+419.48%)
Mutual labels:  coroutines, kotlin-coroutines
BlueFlow
Android Bluetooth classic API wrapped in Coroutines Flow.
Stars: ✭ 64 (-16.88%)
Mutual labels:  coroutines, kotlin-coroutines
Korge
KorGE Game Engine. Multiplatform Kotlin Game Engine
Stars: ✭ 780 (+912.99%)
Mutual labels:  coroutines, kotlin-coroutines
Coil
Image loading for Android backed by Kotlin Coroutines.
Stars: ✭ 7,469 (+9600%)
Mutual labels:  coroutines, kotlin-coroutines
jda-ktx
Collection of useful Kotlin extensions for JDA
Stars: ✭ 49 (-36.36%)
Mutual labels:  coroutines, kotlin-coroutines
Ktx
LibKTX: Kotlin extensions for LibGDX games and applications
Stars: ✭ 913 (+1085.71%)
Mutual labels:  coroutines, kotlin-coroutines
Retrofit2-Flow-Call-Adapter
A Retrofit 2 adapter for Kotlin Flows.
Stars: ✭ 41 (-46.75%)
Mutual labels:  coroutines, kotlin-coroutines
Coroutineworker
Kotlin Coroutine-based workers for native
Stars: ✭ 282 (+266.23%)
Mutual labels:  coroutines, kotlin-coroutines
korte
Kotlin cORoutines Template Engine for Multiplatform Kotlin
Stars: ✭ 69 (-10.39%)
Mutual labels:  coroutines, kotlin-coroutines
Corbind
Kotlin Coroutines binding APIs for Android UI widgets from the platform and support libraries
Stars: ✭ 357 (+363.64%)
Mutual labels:  coroutines, kotlin-coroutines
android-clean-arc-coroutines
Clean Architecture(Coroutines,Dagger, MVVM, ROOM, retrofit, databinding)
Stars: ✭ 116 (+50.65%)
Mutual labels:  coroutines, kotlin-coroutines
WanAndroidJetpack
🔥 WanAndroid 客户端,Kotlin + MVVM + Jetpack + Retrofit + Glide。基于 MVVM 架构,用 Jetpack 实现,网络采用 Kotlin 的协程和 Retrofit 配合使用!精美的 UI,便捷突出的功能实现,欢迎下载体验!
Stars: ✭ 124 (+61.04%)
Mutual labels:  coroutines, kotlin-coroutines
Kotlin Coroutines Android Examples
Learn Kotlin Coroutines for Android by Examples. Learn how to use Kotlin Coroutines for Android App Development.
Stars: ✭ 572 (+642.86%)
Mutual labels:  coroutines, kotlin-coroutines
Myweatherkotlinflow
Android app that shows weather at your current location or any custom location you specify. Uses Kotlin Flow for data streaming and coroutines for asynchronous work. Also leverages Room, navigation component, Viewmodel and Livedata Jetpack components with MVVM presentation layer architecture. Dagger 2 with Dagger android for dependency injection
Stars: ✭ 23 (-70.13%)
Mutual labels:  coroutines, kotlin-coroutines

kotlin-coroutines-android

Useful extensions for coroutines.

Provide easier MainScope integration and auto disposable Job according to the view attached state.

AutoDisposable

Inspired by AutoDispose, auto cancel the job according to the attach state of corresponding view.

Usage

Add dependency in gradle:

api "com.bennyhuo.kotlin:coroutines-android-autodisposable:1.0"

Use asAutoDisposable to convert a Job to an AutoDisposableJob. Then the job will be cancelled when the view is removed.

In the sample below, delay(1000) will not be executed.

val anotherButton = Button(this)
parentView.addView(anotherButton, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)

anotherButton.setOnClickListener {
    GlobalScope.launch(Dispatchers.Main) {
        log(1)
        parentView.removeView(anotherButton)
        delay(1000)
        log(2)
    }.asAutoDisposable(it)
}

MainScope

Versions see ChangeLog.

Supplement for kotlinx.coroutines, providing useful extensions for android Views and easier way to integrate.

An instance of MainScope which use Dispatchers.Main as its dispatcher will be bound to the lifecycle of the corresponding Activity or Fragment. In other words, an instance of MainScope will be created when an activity is created and cancelled when the activity is destroyed or the view of the fragment is destroyed.

Usage

Add dependency in gradle:

api 'com.bennyhuo.kotlin:coroutines-android-mainscope:1.0'

Initialize this library in your customized Application:

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        MainScope.setUp(this)
    }
}

Implement BasicScoped to include View extensions for android sdk, RecyclerViewScoped for RecyclerView, DesignScoped for android support design library, AppCompatScoped for android support appcompat library. If more than one interfaces are used, just implement whatever you need.

class MainActivity : AppCompatActivity(), AppCompatScoped, RecyclerViewScoped {
    ...
}

To access the MainScope, just use the property mainScope. Be careful that it is not thread safe to use outsize the main thread.

class MainActivity : AppCompatActivity(), BasicScoped {
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        mainScope.launch {
            log("Hey!")
        }
        ...
    }
}

It is also easy to launch multi-coroutines with the withMainScope method:

...
withMainScope {
    launch {
        ...
    }

    async(Dispatchers.IO) {
        ...
    }
}
...

Most of the listeners borrowed from Anko are equipped with MainScope instead of the GlobalScope so that coroutines launched in these listeners will be cancelled when the corresponding activity is destroyed.

...
    button.onClick {
        log(1)
        delay(1000)
        log(2)
        textView.text = "Hello Coroutine!"
    }
...

onClick receives a suspend lambda as the body of the job. Once the button clicked, the lambda block will be started immediately on the main thread, and suspended at delay(1000). If you leave the activity by pressing the back key, the suspend lambda won't be dispatched since the job is cancelled by the MainScope installed in the activity.

About Fragment

Since android.app.Fragment is deprecated, we choose to support android.support.v4.app.Fragment only. If you include the library into the classpath, Fragment support will be automatically enabled for subtypes of FragmentActivity. Otherwise if you never use Fragment, don't worry, nothing will happen.

FragmentLifecycleCallbacks was added from v25.1.0 so older versions will not be supported.

Issue

Please feel free to issue and pull request.

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].