Skip to content

Commit cfd3a11

Browse files
committed
Convert from Java Thread to Coroutines
1 parent 960a01d commit cfd3a11

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ buildscript {
1313
dagger_version = '2.24'
1414
enhanced_adapter_version = '1.0.0'
1515
kotlin_version = '1.3.50'
16+
coroutines_version = '1.3.2'
1617
retrofit_version = '2.6.1'
1718

1819
// SDKs

dagger-2/src/main/java/com/github/mrbean355/android/dagger2/ui/ViewHeroesViewModel.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import androidx.lifecycle.LiveData
44
import androidx.lifecycle.MutableLiveData
55
import com.github.mrbean355.android.dagger2.data.Hero
66
import com.github.mrbean355.android.dagger2.data.HeroRepository
7+
import kotlinx.coroutines.Dispatchers.IO
8+
import kotlinx.coroutines.GlobalScope
9+
import kotlinx.coroutines.launch
710
import javax.inject.Inject
811

912
class ViewHeroesViewModel @Inject constructor(private val repository: HeroRepository) {
1013

1114
fun getHeroes(): LiveData<List<Hero>> {
1215
val liveData = MutableLiveData<List<Hero>>()
13-
Thread {
16+
GlobalScope.launch(context = IO) {
1417
liveData.postValue(repository.getHeroes())
15-
}.start()
18+
}
1619
return liveData
1720
}
1821
}

framework/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ android {
1717

1818
dependencies {
1919
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
20+
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
21+
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
2022
api "androidx.appcompat:appcompat:$appcompat_version"
2123
api "androidx.core:core-ktx:$core_version"
2224
api "androidx.recyclerview:recyclerview:$recyclerview_version"

framework/src/main/java/com/github/mrbean355/android/framework/PokemonRepository.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.github.mrbean355.android.framework
22

33
import androidx.annotation.MainThread
4-
import androidx.annotation.WorkerThread
54
import androidx.lifecycle.LiveData
65
import androidx.lifecycle.MutableLiveData
6+
import kotlinx.coroutines.Dispatchers.IO
7+
import kotlinx.coroutines.GlobalScope
8+
import kotlinx.coroutines.delay
9+
import kotlinx.coroutines.launch
710
import retrofit2.Retrofit
811
import retrofit2.converter.gson.GsonConverterFactory
912

@@ -15,9 +18,8 @@ class PokemonRepository {
1518
.create(PokemonService::class.java)
1619

1720
/** Make a blocking service call to fetch all Pokemon. */
18-
@WorkerThread
19-
fun getAll(): List<Pokemon> {
20-
Thread.sleep(1000) // emulate a long service call.
21+
suspend fun getAll(): List<Pokemon> {
22+
delay(1000) // emulate a long service call.
2123
val response = service.getAll().execute()
2224
val body = response.body()
2325
if (response.isSuccessful && body != null) {
@@ -30,9 +32,9 @@ class PokemonRepository {
3032
@MainThread
3133
fun getAllAsLiveData(): LiveData<List<Pokemon>> {
3234
val liveData = MutableLiveData<List<Pokemon>>()
33-
Thread {
35+
GlobalScope.launch(context = IO) {
3436
liveData.postValue(getAll())
35-
}.start()
37+
}
3638
return liveData
3739
}
3840
}

room/src/main/java/com/github/mrbean355/android/room/ViewPokemonActivity.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import com.github.mrbean355.android.framework.PokemonRepository
99
import com.github.mrbean355.android.room.db.PokemonDatabase
1010
import com.github.mrbean355.android.room.db.PokemonEntity
1111
import kotlinx.android.synthetic.main.activity_view_pokemon.*
12+
import kotlinx.coroutines.Dispatchers.IO
13+
import kotlinx.coroutines.Dispatchers.Main
14+
import kotlinx.coroutines.GlobalScope
15+
import kotlinx.coroutines.launch
16+
import kotlinx.coroutines.withContext
1217

1318
/**
1419
* NOTE: All this logic should be moved out of the activity!
@@ -27,17 +32,17 @@ class ViewPokemonActivity : AppCompatActivity() {
2732
}
2833

2934
private fun loadPokemon() {
30-
Thread {
35+
GlobalScope.launch(context = IO) {
3136
var items = loadFromDatabase()
3237
if (items.isEmpty()) {
3338
items = loadFromService()
3439
saveToDatabase(items)
3540
}
36-
runOnUiThread {
41+
withContext(Main) {
3742
adapter.setItems(items)
3843
progress_bar.visibility = View.GONE
3944
}
40-
}.start()
45+
}
4146
}
4247

4348
private fun loadFromDatabase(): List<Pokemon> {
@@ -52,7 +57,7 @@ class ViewPokemonActivity : AppCompatActivity() {
5257
dao.insertAll(items.map { PokemonEntity(0, it.name, it.url) })
5358
}
5459

55-
private fun loadFromService(): List<Pokemon> {
60+
private suspend fun loadFromService(): List<Pokemon> {
5661
return PokemonRepository().getAll()
5762
}
5863
}

0 commit comments

Comments
 (0)