• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

bennyhuo/kotlin-coroutines-android: Useful extensions for coroutines. AutoDispos ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

bennyhuo/kotlin-coroutines-android

开源软件地址(OpenSource Url):

https://github.com/bennyhuo/kotlin-coroutines-android

开源编程语言(OpenSource Language):

Kotlin 96.0%

开源软件介绍(OpenSource Introduction):

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cy6erGn0m/kotlinx-sockets: Kotlinx coroutines sockets发布时间:2022-07-10
下一篇:
reime005/splintersweets: A LibGDX and Kotlin Mobile Game Example发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap