1
1
package otus.homework.coroutines
2
2
3
3
import kotlinx.coroutines.Dispatchers
4
+ import kotlinx.coroutines.async
4
5
import kotlinx.coroutines.cancel
5
6
import kotlinx.coroutines.launch
6
7
import kotlinx.coroutines.runBlocking
8
+ import otus.homework.coroutines.model.CatModel
7
9
import java.lang.Exception
8
10
import java.net.SocketTimeoutException
9
11
10
12
class CatsPresenter (
11
- private val catsService : CatsService
13
+ private val factService : CatsFactService ,
14
+ private val imageService : CatsImageService
12
15
) {
13
16
14
17
private var _catsView : ICatsView ? = null
@@ -18,13 +21,17 @@ class CatsPresenter(
18
21
runBlocking(Dispatchers .IO ) {
19
22
catsScope.launch {
20
23
try {
21
- val response = catsService.getCatFact()
22
- if (response.isSuccessful && response.body() != null ) {
23
- _catsView ?.populate(response.body()!! )
24
- }
24
+ val catFactJob = async { factService.getCatFact() }
25
+ val catImageJob = async { imageService.getCatImage() }
26
+
27
+ val catModel = CatModel (
28
+ catFactJob.await().body()?.fact,
29
+ CatsImageService .BASE_URL + catImageJob.await().body()?.url)
30
+
31
+ _catsView ?.populate(catModel)
25
32
} catch (e: Exception ) {
26
33
if (e is SocketTimeoutException ) {
27
- _catsView ?.showToast(" Не удалось получить ответ от сервером " )
34
+ _catsView ?.showToast(" Не удалось получить ответ от сервера " )
28
35
} else {
29
36
CrashMonitor .trackWarning(e.message.toString())
30
37
_catsView ?.showToast(e.message.toString())
0 commit comments