Skip to content

Commit 92b8f43

Browse files
committed
Otus-Android#2 parallel api calls
1 parent f52279b commit 92b8f43

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

app/src/main/java/otus/homework/coroutines/CatsPresenter.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package otus.homework.coroutines
22

33
import kotlinx.coroutines.CancellationException
44
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Deferred
56
import kotlinx.coroutines.Job
7+
import kotlinx.coroutines.async
68
import kotlinx.coroutines.launch
79
import java.net.SocketTimeoutException
810

@@ -18,15 +20,18 @@ class CatsPresenter(
1820
) {
1921

2022
private var _catsView: ICatsView? = null
21-
private var _job: Job? = null
23+
private var job: Job? = null
2224

2325
fun onInitComplete() {
24-
_job = presenterScope.launch {
25-
try {
26-
val image = imageService.getCatImage().first()
27-
val fact = catsService.getCatFact()
26+
job = presenterScope.async {
27+
val image = async { imageService.getCatImage().first() }
28+
val fact = async { catsService.getCatFact() }
2829

29-
_catsView?.populate(CatsUIState(fact, image))
30+
_catsView?.populate(CatsUIState(fact.await(), image.await()))
31+
}
32+
presenterScope.launch {
33+
try {
34+
(job as Deferred<*>).await()
3035
} catch (e: CancellationException) {
3136
throw e
3237
} catch (e: SocketTimeoutException) {
@@ -51,7 +56,7 @@ class CatsPresenter(
5156
}
5257

5358
fun cancelCoroutine() {
54-
_job?.run {
59+
job?.run {
5560
if (isActive) {
5661
cancel()
5762
}

app/src/main/java/otus/homework/coroutines/DiContainer.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ import retrofit2.converter.gson.GsonConverterFactory
88

99
class DiContainer {
1010

11-
private fun buildRetrofit(baseUrl: String) = Retrofit.Builder()
12-
.baseUrl(baseUrl)
13-
.addConverterFactory(GsonConverterFactory.create())
14-
.build()
11+
private val converter by lazy { GsonConverterFactory.create() }
1512

16-
val catsService by lazy { buildRetrofit("https://catfact.ninja/").create(CatsService::class.java) }
13+
private fun buildRetrofit(baseUrl: String) =
14+
Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(converter)
15+
.build()
1716

18-
val imageService by lazy { buildRetrofit("https://api.thecatapi.com/v1/images/").create(ImageService::class.java) }
17+
val catsService by lazy {
18+
buildRetrofit("https://catfact.ninja/").create(
19+
CatsService::class.java
20+
)
21+
}
22+
23+
val imageService by lazy {
24+
buildRetrofit("https://api.thecatapi.com/v1/images/").create(
25+
ImageService::class.java
26+
)
27+
}
1928

2029
val presenterScope get() = CoroutineScope(CoroutineName("CatsCoroutine") + Dispatchers.Main)
2130
}

app/src/main/java/otus/homework/coroutines/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class MainActivity : AppCompatActivity() {
2929
if (isFinishing) {
3030
catsPresenter.apply {
3131
detachView()
32-
cancelCoroutine()
3332
}
3433
}
34+
catsPresenter.cancelCoroutine()
3535
super.onStop()
3636
}
3737
}

0 commit comments

Comments
 (0)