Android Coroutine UI Extensions
This is Android Coroutine UI Extensions libaray. available for kotlin.
Library version
AndroidX package
// use kotlin and coroutine
ext.kotlin_version = '1.3.21'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
ext.coroutines_version = '1.1.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
ext.supportLibraryVersion = '1.0.1'
implementation "androidx.appcompat:appcompat:$supportLibraryVersion"
ext.lifecycleVersion = '2.0.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
implementation 'tech.thdev.coroutines:coroutines-extensions:$last_version'
// And RX Java 2
implementation "tech.thdev.coroutines:coroutines-extensions-rx2:$last_version"
Legacy package
ext.kotlin_version = '1.3.21'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
ext.coroutines_version = '1.1.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
ext.supportLibraryVersion = '27.1.1'
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
ext.lifecycleVersion = '1.1.1'
implementation "android.arch.lifecycle:extensions:$lifecycleVersion"
implementation "tech.thdev.coroutines:coroutines-extensions-legacy:$last_version_legacy"
// And RX Java 2
implementation "tech.thdev.coroutines:coroutines-extensions-rx2-legacy:$last_version_legacy"
Use api - Activity/Fragment/viewModel/LifecycleObservable
Activity
class MainActivity : CoroutineScopeActivity() {
// ...
launch {
// UI Thread
// Thread change
launch(Dispatchers.Default) {
}
}
// ...
}
Fragment
class HomeFragment : CoroutineScopeFragment() {
// ...
launch {
// UI Thread
}
// ...
}
ViewModel
class MainViewModel : CoroutineScopeViewModel() /* or CoroutineScopeAndroidViewModel() */ {
// ...
launch {
// UI Thread
}
// ...
}
LifecycleObservable
class MainLifecycleObservable : CoroutineScopeLifecycleObserver() {
// ...
launch {
// UI Thread
}
// ...
}
Use api - onClick
In a Inheritance from BaseCoroutineScope.
class MainViewModel : CoroutineScopeViewModel() /* or CoroutineScopeAndroidViewModel() */ {
// ...
fab.onClick(this) {
gitHubService.contributors(tv_owner.text.toString(), tv_repo.text.toString())
.onErrorReturn {
mutableListOf()
}
.await()
}.runUi {
if (it.isNotEmpty()) {
for ((name, contributions) in it) {
tv_message.text = "$name as $contributions contributions!"
}
} else {
tv_message.text = "Not found"
}
}
// or
onClick(fab) {
// background thread
}.runUi {
ui thread
}
// ...
}
License
Copyright 2018 Tae-hwan
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.