File tree 6 files changed +31
-12
lines changed
java/otus/homework/coroutines 6 files changed +31
-12
lines changed Original file line number Diff line number Diff line change @@ -6,8 +6,14 @@ import kotlinx.coroutines.Job
6
6
import kotlinx.coroutines.launch
7
7
import java.net.SocketTimeoutException
8
8
9
+ data class CatsUIState (
10
+ val fact : Fact ,
11
+ val image : Image
12
+ )
13
+
9
14
class CatsPresenter (
10
15
private val catsService : CatsService ,
16
+ private val imageService : ImageService ,
11
17
private val presenterScope : CoroutineScope
12
18
) {
13
19
@@ -17,12 +23,10 @@ class CatsPresenter(
17
23
fun onInitComplete () {
18
24
_job = presenterScope.launch {
19
25
try {
20
- // imageService.getCatImage().also {
21
- // println("image url ${it.url}")
22
- // }
23
- catsService.getCatFact().also { fact ->
24
- _catsView ?.populate(fact)
25
- }
26
+ val image = imageService.getCatImage().first()
27
+ val fact = catsService.getCatFact()
28
+
29
+ _catsView ?.populate(CatsUIState (fact, image))
26
30
} catch (e: CancellationException ) {
27
31
throw e
28
32
} catch (e: SocketTimeoutException ) {
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
private val 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 (state : CatsUIState ) {
28
+ findViewById<TextView >(R .id.fact_textView).text = state.fact.fact
29
+ Picasso .get().load(state.image.url).into(findViewById<ImageView >(R .id.catImage))
27
30
}
28
31
29
32
override fun showToast (message : String ) {
@@ -33,7 +36,7 @@ class CatsView @JvmOverloads constructor(
33
36
34
37
interface ICatsView {
35
38
36
- fun populate (fact : Fact )
39
+ fun populate (state : CatsUIState )
37
40
38
41
fun showToast (message : String )
39
42
}
Original file line number Diff line number Diff line change @@ -4,5 +4,5 @@ import retrofit2.http.GET
4
4
5
5
interface ImageService {
6
6
@GET(" search" )
7
- suspend fun getCatImage (): Image
7
+ suspend fun getCatImage (): List < Image >
8
8
}
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ class MainActivity : AppCompatActivity() {
17
17
18
18
catsPresenter = CatsPresenter (
19
19
diContainer.catsService,
20
+ diContainer.imageService,
20
21
diContainer.presenterScope
21
22
)
22
23
view.presenter = catsPresenter
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/catImage"
12
+ android : layout_width =" 300dp"
13
+ android : layout_height =" 300dp"
14
+ android : contentDescription =" @string/cat_image_description"
15
+ app : layout_constraintTop_toTopOf =" parent"
16
+ app : layout_constraintBottom_toTopOf =" @id/fact_textView"
17
+ app : layout_constraintStart_toStartOf =" parent"
18
+ app : layout_constraintEnd_toEndOf =" parent"
19
+ />
20
+
10
21
<TextView
11
22
android : id =" @+id/fact_textView"
12
23
android : textColor =" @color/black"
13
24
android : textSize =" 24sp"
14
25
android : layout_width =" wrap_content"
15
26
android : layout_height =" wrap_content"
16
- app : layout_constraintBottom_toBottomOf =" parent"
17
27
app : layout_constraintEnd_toEndOf =" parent"
18
28
app : layout_constraintStart_toStartOf =" parent"
19
- app : layout_constraintTop_toTopOf = " parent " />
29
+ app : layout_constraintTop_toBottomOf = " @id/catImage " />
20
30
21
31
<Button
22
32
android : id =" @+id/button"
Original file line number Diff line number Diff line change 1
1
<resources >
2
2
<string name =" app_name" >Cat Facts </string >
3
3
<string name =" more_facts" >More Facts</string >
4
+ <string name =" cat_image_description" >Random cat image</string >
4
5
</resources >
You can’t perform that action at this time.
0 commit comments