All Projects → bennyhuo → kotlin-coroutines-android

bennyhuo / kotlin-coroutines-android

Licence: MIT license
Useful extensions for coroutines. AutoDispose + MainScope

Programming Languages

kotlin
9241 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to kotlin-coroutines-android

Eyepetizer
🔥基于 Kotlin 语言仿写「开眼 Eyepetizer」的一个短视频 Android 客户端项目,采用 Jetpack + 协程实现的 MVVM 架构。
Stars: ✭ 988 (+1076.19%)
Mutual labels:  coroutines, kotlin-coroutines
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (-58.33%)
Mutual labels:  coroutines, kotlin-coroutines
Paperplane
📚 PaperPlane - An Android reading app, including articles from Zhihu Daily, Guokr Handpick and Douban Moment.
Stars: ✭ 1,147 (+1265.48%)
Mutual labels:  coroutines, kotlin-coroutines
Kotlin Coroutines Retrofit
Kotlin Coroutines await() extension for Retrofit Call
Stars: ✭ 812 (+866.67%)
Mutual labels:  coroutines, kotlin-coroutines
StarWarsSearch-MVI
Star wars sample android project showcasing the use of View components for rendering UI in Fragments and Activities. Uses Android Jetpack, clean architecture with MVI (Uni-directional data flow), dagger hilt, and kotlin coroutines with StateFlow
Stars: ✭ 189 (+125%)
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 (-72.62%)
Mutual labels:  coroutines, kotlin-coroutines
Androidcoroutinesplayground
Android Coroutines Playground
Stars: ✭ 119 (+41.67%)
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 (+392.86%)
Mutual labels:  coroutines, kotlin-coroutines
Android Clean Arch Coroutines Koin
Implemented by Clean Architecture, MVVM, Koin, Coroutines, Moshi, Mockk, LiveData & DataBinding
Stars: ✭ 173 (+105.95%)
Mutual labels:  coroutines, kotlin-coroutines
Splitties
A collection of hand-crafted extensions for your Kotlin projects.
Stars: ✭ 1,945 (+2215.48%)
Mutual labels:  coroutines, kotlin-coroutines
Korge
KorGE Game Engine. Multiplatform Kotlin Game Engine
Stars: ✭ 780 (+828.57%)
Mutual labels:  coroutines, kotlin-coroutines
Delish
Delish, a Food Recipes App in Jetpack Compose and Hilt based on modern Android tech-stacks and MVI clean architecture.
Stars: ✭ 356 (+323.81%)
Mutual labels:  coroutines, kotlin-coroutines
Coil
Image loading for Android backed by Kotlin Coroutines.
Stars: ✭ 7,469 (+8791.67%)
Mutual labels:  coroutines, kotlin-coroutines
Ktx
LibKTX: Kotlin extensions for LibGDX games and applications
Stars: ✭ 913 (+986.9%)
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 (+580.95%)
Mutual labels:  coroutines, kotlin-coroutines
Kotlin Coroutines Android
Useful extensions for coroutines. AutoDispose + MainScope
Stars: ✭ 77 (-8.33%)
Mutual labels:  coroutines, kotlin-coroutines
Corbind
Kotlin Coroutines binding APIs for Android UI widgets from the platform and support libraries
Stars: ✭ 357 (+325%)
Mutual labels:  coroutines, kotlin-coroutines
Kroto Plus
gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Stars: ✭ 400 (+376.19%)
Mutual labels:  coroutines, kotlin-coroutines
Modular App Core
Core implementations for a modular Android App
Stars: ✭ 127 (+51.19%)
Mutual labels:  coroutines, kotlin-coroutines
the-white-rabbit
The White Rabbit is an asynchronous RabbitMQ (AMQP) client based on Kotlin coroutines
Stars: ✭ 90 (+7.14%)
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].