File tree Expand file tree Collapse file tree 9 files changed +62
-9
lines changed
java/otus/homework/coroutines Expand file tree Collapse file tree 9 files changed +62
-9
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ import java.net.SocketTimeoutException
11
11
import kotlin.coroutines.CoroutineContext
12
12
13
13
class CatsPresenter (
14
- private val catsService : CatsService
14
+ private val catsService : CatsService ,
15
+ private val imageService : ImageService
15
16
): CoroutineScope {
16
17
private val job = Job ()
17
18
override val coroutineContext: CoroutineContext = CoroutineName (" CatsCoroutine" ) + job
@@ -22,8 +23,10 @@ class CatsPresenter(
22
23
launch {
23
24
try {
24
25
val fact = catsService.getCatFact()
26
+ val image = imageService.getRandomImage()
27
+ val model = FactPresentationModel (fact, image.first())
25
28
withContext(Dispatchers .Main ) {
26
- _catsView ?.populate(fact )
29
+ _catsView ?.populate(model )
27
30
}
28
31
} catch (e: Exception ) {
29
32
when (e) {
@@ -35,7 +38,6 @@ class CatsPresenter(
35
38
}
36
39
}
37
40
}
38
-
39
41
}
40
42
}
41
43
Original file line number Diff line number Diff line change @@ -3,9 +3,11 @@ package otus.homework.coroutines
3
3
import android.content.Context
4
4
import android.util.AttributeSet
5
5
import android.widget.Button
6
+ import android.widget.ImageView
6
7
import android.widget.TextView
7
8
import android.widget.Toast
8
9
import androidx.constraintlayout.widget.ConstraintLayout
10
+ import com.squareup.picasso.Picasso
9
11
10
12
class CatsView @JvmOverloads constructor(
11
13
context : Context ,
@@ -22,8 +24,9 @@ class CatsView @JvmOverloads constructor(
22
24
}
23
25
}
24
26
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))
27
30
}
28
31
29
32
override fun onError (textResId : Int ) {
@@ -32,7 +35,6 @@ class CatsView @JvmOverloads constructor(
32
35
}
33
36
34
37
interface ICatsView {
35
-
36
- fun populate (fact : Fact )
38
+ fun populate (factPresentationModel : FactPresentationModel )
37
39
fun onError (textResId : Int )
38
40
}
Original file line number Diff line number Diff line change @@ -12,5 +12,14 @@ class DiContainer {
12
12
.build()
13
13
}
14
14
15
+ private val imageServiceBuilder by lazy {
16
+ Retrofit .Builder ()
17
+ .baseUrl(" https://api.thecatapi.com/v1/" )
18
+ .addConverterFactory(GsonConverterFactory .create())
19
+ .build()
20
+ }
21
+
15
22
val service by lazy { retrofit.create(CatsService ::class .java) }
23
+
24
+ val imageService by lazy { imageServiceBuilder.create(ImageService ::class .java) }
16
25
}
Original file line number Diff line number Diff line change
1
+ package otus.homework.coroutines
2
+
3
+ data class FactPresentationModel (
4
+ val fact : Fact ,
5
+ val imageEntity : ImageEntity
6
+ )
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class MainActivity : AppCompatActivity() {
15
15
val view = layoutInflater.inflate(R .layout.activity_main, null ) as CatsView
16
16
setContentView(view)
17
17
18
- catsPresenter = CatsPresenter (diContainer.service)
18
+ catsPresenter = CatsPresenter (diContainer.service, diContainer.imageService )
19
19
view.presenter = catsPresenter
20
20
catsPresenter.attachView(view)
21
21
catsPresenter.onInitComplete()
Original file line number Diff line number Diff line change 7
7
android : layout_height =" match_parent"
8
8
tools : context =" .MainActivity" >
9
9
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
+
10
19
<TextView
11
20
android : id =" @+id/fact_textView"
12
21
android : textColor =" @color/black"
16
25
app : layout_constraintBottom_toBottomOf =" parent"
17
26
app : layout_constraintEnd_toEndOf =" parent"
18
27
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" />
20
30
21
31
<Button
22
32
android : id =" @+id/button"
Original file line number Diff line number Diff line change 2
2
<string name =" app_name" >Cat Facts </string >
3
3
<string name =" more_facts" >More Facts</string >
4
4
<string name =" connection_error_message" >Не удалось получить ответ от сервером</string >
5
+ <string name =" image_description" >image</string >
5
6
</resources >
You can’t perform that action at this time.
0 commit comments