Skip to content

Commit 6a61e84

Browse files
committed
Otus-Android#2 added ImageService
1 parent 8b06611 commit 6a61e84

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class CatsPresenter(
1717
fun onInitComplete() {
1818
_job = presenterScope.launch {
1919
try {
20+
// imageService.getCatImage().also {
21+
// println("image url ${it.url}")
22+
// }
2023
catsService.getCatFact().also { fact ->
2124
_catsView?.populate(fact)
2225
}

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

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

99
class DiContainer {
1010

11-
private val retrofit by lazy {
12-
Retrofit.Builder()
13-
.baseUrl("https://catfact.ninja/")
14-
.addConverterFactory(GsonConverterFactory.create())
15-
.build()
16-
}
11+
private fun buildRetrofit(baseUrl: String) = Retrofit.Builder()
12+
.baseUrl(baseUrl)
13+
.addConverterFactory(GsonConverterFactory.create())
14+
.build()
1715

18-
val service by lazy { retrofit.create(CatsService::class.java) }
16+
val catsService by lazy { buildRetrofit("https://catfact.ninja/").create(CatsService::class.java) }
17+
18+
val imageService by lazy { buildRetrofit("https://api.thecatapi.com/v1/images/").create(ImageService::class.java) }
1919

2020
val presenterScope get() = CoroutineScope(CoroutineName("CatsCoroutine") + Dispatchers.Main)
2121
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package otus.homework.coroutines
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class Image(
6+
@field:SerializedName("id")
7+
val id: String,
8+
@field:SerializedName("url")
9+
val url: String
10+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package otus.homework.coroutines
2+
3+
import retrofit2.http.GET
4+
5+
interface ImageService {
6+
@GET("search")
7+
suspend fun getCatImage(): Image
8+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ class MainActivity : AppCompatActivity() {
1515
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
1616
setContentView(view)
1717

18-
catsPresenter = CatsPresenter(diContainer.service, diContainer.presenterScope)
18+
catsPresenter = CatsPresenter(
19+
diContainer.catsService,
20+
diContainer.presenterScope
21+
)
1922
view.presenter = catsPresenter
2023
catsPresenter.attachView(view)
2124
catsPresenter.onInitComplete()

0 commit comments

Comments
 (0)