File tree 7 files changed +24
-9
lines changed
java/otus/homework/coroutines
7 files changed +24
-9
lines changed Original file line number Diff line number Diff line change 1
1
package otus.homework.coroutines
2
2
3
+ import android.content.res.Resources
3
4
import androidx.lifecycle.MutableLiveData
4
5
import androidx.lifecycle.ViewModel
5
- import androidx.lifecycle.ViewModelProvider
6
6
import androidx.lifecycle.viewModelScope
7
7
import kotlinx.coroutines.CancellationException
8
8
import kotlinx.coroutines.CoroutineExceptionHandler
@@ -26,7 +26,7 @@ class CatViewModel(
26
26
private val handler = CoroutineExceptionHandler { _, throwable ->
27
27
when (throwable) {
28
28
is SocketTimeoutException -> {
29
- catModel.value = Result .Error (Throwable (" Не удалось получить ответ от сервера " ))
29
+ catModel.value = Result .Error (Throwable (Resources .getSystem().getString( R .string.error_connection) ))
30
30
}
31
31
is CancellationException -> {
32
32
throw throwable
Original file line number Diff line number Diff line change @@ -8,4 +8,8 @@ interface CatsFactService {
8
8
9
9
@GET(" fact" )
10
10
suspend fun getCatFact () : Response <Fact >
11
+
12
+ companion object {
13
+ const val BASE_URL = " https://catfact.ninja/"
14
+ }
11
15
}
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ class CatsPresenter(
32
32
} catch (e: Exception ) {
33
33
when (e) {
34
34
is SocketTimeoutException -> {
35
- _catsView ?.showToast(" Не удалось получить ответ от сервера " )
35
+ _catsView ?.showToast(R .string.error_connection )
36
36
}
37
37
is CancellationException -> {
38
38
throw e
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import android.widget.Button
6
6
import android.widget.ImageView
7
7
import android.widget.TextView
8
8
import android.widget.Toast
9
+ import androidx.annotation.StringRes
9
10
import androidx.constraintlayout.widget.ConstraintLayout
10
11
import com.squareup.picasso.Picasso
11
12
import otus.homework.coroutines.model.CatModel
@@ -33,13 +34,18 @@ class CatsView @JvmOverloads constructor(
33
34
Picasso .get().load(catModel.imageUrl).into(imageView)
34
35
}
35
36
36
- override fun showToast (message : String ) {
37
- Toast .makeText(context, message, Toast .LENGTH_SHORT ).show()
37
+ override fun showToast (messageRes : Int ) {
38
+ Toast .makeText(context, messageRes, Toast .LENGTH_SHORT ).show()
39
+ }
40
+
41
+ override fun showToast (messageRes : String ) {
42
+ Toast .makeText(context, messageRes, Toast .LENGTH_SHORT ).show()
38
43
}
39
44
}
40
45
41
46
interface ICatsView {
42
47
43
48
fun populate (catModel : CatModel )
44
- fun showToast (message : String )
49
+ fun showToast (@StringRes messageRes : Int )
50
+ fun showToast (messageRes : String )
45
51
}
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ class DiContainer {
29
29
private val retrofitCatFact by lazy {
30
30
Retrofit .Builder ()
31
31
.client(okHttpClient)
32
- .baseUrl(" https://catfact.ninja/ " )
32
+ .baseUrl(CatsFactService . BASE_URL )
33
33
.addConverterFactory(GsonConverterFactory .create())
34
34
.build()
35
35
}
Original file line number Diff line number Diff line change @@ -9,5 +9,9 @@ import kotlin.coroutines.CoroutineContext
9
9
class PresenterScope : CoroutineScope {
10
10
11
11
override val coroutineContext: CoroutineContext
12
- get() = Job () + Dispatchers .Main + CoroutineName (" CatsCoroutine" )
12
+ get() = Job () + Dispatchers .Main + CoroutineName (CATS_COROUTINE )
13
+
14
+ companion object {
15
+ const val CATS_COROUTINE = " CatsCoroutine"
16
+ }
13
17
}
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
- </resources >
4
+ <string name =" error_connection" >"Не удалось получить ответ от сервера"</string >
5
+ </resources >
You can’t perform that action at this time.
0 commit comments