Skip to content

Commit 1f059ec

Browse files
Otus-Android#2 Добавлен к запросу фактов запрос рандомных картинок с https://api.thecatapi.com/v1/images/search
1 parent d8bc68e commit 1f059ec

File tree

9 files changed

+62
-9
lines changed

9 files changed

+62
-9
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import java.net.SocketTimeoutException
1111
import kotlin.coroutines.CoroutineContext
1212

1313
class CatsPresenter(
14-
private val catsService: CatsService
14+
private val catsService: CatsService,
15+
private val imageService: ImageService
1516
): CoroutineScope {
1617
private val job = Job()
1718
override val coroutineContext: CoroutineContext = CoroutineName("CatsCoroutine") + job
@@ -22,8 +23,10 @@ class CatsPresenter(
2223
launch {
2324
try {
2425
val fact = catsService.getCatFact()
26+
val image = imageService.getRandomImage()
27+
val model = FactPresentationModel(fact, image.first())
2528
withContext(Dispatchers.Main) {
26-
_catsView?.populate(fact)
29+
_catsView?.populate(model)
2730
}
2831
} catch (e: Exception) {
2932
when (e) {
@@ -35,7 +38,6 @@ class CatsPresenter(
3538
}
3639
}
3740
}
38-
3941
}
4042
}
4143

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package otus.homework.coroutines
33
import android.content.Context
44
import android.util.AttributeSet
55
import android.widget.Button
6+
import android.widget.ImageView
67
import android.widget.TextView
78
import android.widget.Toast
89
import androidx.constraintlayout.widget.ConstraintLayout
10+
import com.squareup.picasso.Picasso
911

1012
class CatsView @JvmOverloads constructor(
1113
context: Context,
@@ -22,8 +24,9 @@ class CatsView @JvmOverloads constructor(
2224
}
2325
}
2426

25-
override fun populate(fact: Fact) {
26-
findViewById<TextView>(R.id.fact_textView).text = fact.fact
27+
override fun populate(factPresentationModel: FactPresentationModel) {
28+
findViewById<TextView>(R.id.fact_textView).text = factPresentationModel.fact.fact
29+
Picasso.get().load(factPresentationModel.imageEntity.url).into(findViewById<ImageView>(R.id.fact_imageView))
2730
}
2831

2932
override fun onError(textResId: Int) {
@@ -32,7 +35,6 @@ class CatsView @JvmOverloads constructor(
3235
}
3336

3437
interface ICatsView {
35-
36-
fun populate(fact: Fact)
38+
fun populate(factPresentationModel: FactPresentationModel)
3739
fun onError(textResId: Int)
3840
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ class DiContainer {
1212
.build()
1313
}
1414

15+
private val imageServiceBuilder by lazy {
16+
Retrofit.Builder()
17+
.baseUrl("https://api.thecatapi.com/v1/")
18+
.addConverterFactory(GsonConverterFactory.create())
19+
.build()
20+
}
21+
1522
val service by lazy { retrofit.create(CatsService::class.java) }
23+
24+
val imageService by lazy { imageServiceBuilder.create(ImageService::class.java) }
1625
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package otus.homework.coroutines
2+
3+
data class FactPresentationModel (
4+
val fact: Fact,
5+
val imageEntity: ImageEntity
6+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package otus.homework.coroutines
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class ImageEntity (
6+
@field:SerializedName("id")
7+
val id: String,
8+
@field:SerializedName("url")
9+
val url: String,
10+
@field:SerializedName("width")
11+
val width: Int,
12+
@field:SerializedName("height")
13+
val height: Int
14+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package otus.homework.coroutines
2+
3+
import retrofit2.http.GET
4+
5+
interface ImageService {
6+
7+
@GET("images/search")
8+
suspend fun getRandomImage(): List<ImageEntity>
9+
}

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

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

18-
catsPresenter = CatsPresenter(diContainer.service)
18+
catsPresenter = CatsPresenter(diContainer.service, diContainer.imageService)
1919
view.presenter = catsPresenter
2020
catsPresenter.attachView(view)
2121
catsPresenter.onInitComplete()

app/src/main/res/layout/activity_main.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
android:layout_height="match_parent"
88
tools:context=".MainActivity">
99

10+
<ImageView
11+
android:id="@+id/fact_imageView"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:contentDescription="@string/image_description"
15+
app:layout_constraintEnd_toEndOf="parent"
16+
app:layout_constraintStart_toStartOf="parent"
17+
app:layout_constraintBottom_toTopOf="@id/fact_textView"/>
18+
1019
<TextView
1120
android:id="@+id/fact_textView"
1221
android:textColor="@color/black"
@@ -16,7 +25,8 @@
1625
app:layout_constraintBottom_toBottomOf="parent"
1726
app:layout_constraintEnd_toEndOf="parent"
1827
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toTopOf="parent" />
28+
app:layout_constraintTop_toTopOf="parent"
29+
app:layout_constraintTop_toBottomOf="@+id/fact_textView"/>
2030

2131
<Button
2232
android:id="@+id/button"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<string name="app_name">Cat Facts </string>
33
<string name="more_facts">More Facts</string>
44
<string name="connection_error_message">Не удалось получить ответ от сервером</string>
5+
<string name="image_description">image</string>
56
</resources>

0 commit comments

Comments
 (0)