Skip to content

Commit 68f873a

Browse files
author
Vera Sokolova
committed
string to res
1 parent fcdfbfb commit 68f873a

File tree

7 files changed

+23
-8
lines changed

7 files changed

+23
-8
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package otus.homework.coroutines
22

3+
import android.content.res.Resources
34
import androidx.lifecycle.MutableLiveData
45
import androidx.lifecycle.ViewModel
5-
import androidx.lifecycle.ViewModelProvider
66
import androidx.lifecycle.viewModelScope
77
import kotlinx.coroutines.CancellationException
88
import kotlinx.coroutines.CoroutineExceptionHandler
@@ -26,7 +26,7 @@ class CatViewModel(
2626
private val handler = CoroutineExceptionHandler { _, throwable ->
2727
when (throwable) {
2828
is SocketTimeoutException -> {
29-
catModel.value = Result.Error(Throwable("Не удалось получить ответ от сервера"))
29+
catModel.value = Result.Error(Throwable(Resources.getSystem().getString(R.string.error_connection)))
3030
}
3131
is CancellationException -> {
3232
throw throwable

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

+4
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ interface CatsFactService {
88

99
@GET("fact")
1010
suspend fun getCatFact() : Response<Fact>
11+
12+
companion object {
13+
const val BASE_URL = "https://catfact.ninja/"
14+
}
1115
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CatsPresenter(
3232
} catch (e: Exception) {
3333
when (e) {
3434
is SocketTimeoutException -> {
35-
_catsView?.showToast("Не удалось получить ответ от сервера")
35+
_catsView?.showToast(R.string.error_connection)
3636
}
3737
is CancellationException -> {
3838
throw e

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.widget.Button
66
import android.widget.ImageView
77
import android.widget.TextView
88
import android.widget.Toast
9+
import androidx.annotation.StringRes
910
import androidx.constraintlayout.widget.ConstraintLayout
1011
import com.squareup.picasso.Picasso
1112
import otus.homework.coroutines.model.CatModel
@@ -33,13 +34,18 @@ class CatsView @JvmOverloads constructor(
3334
Picasso.get().load(catModel.imageUrl).into(imageView)
3435
}
3536

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()
3843
}
3944
}
4045

4146
interface ICatsView {
4247

4348
fun populate(catModel: CatModel)
44-
fun showToast(message: String)
49+
fun showToast(@StringRes messageRes: Int)
50+
fun showToast(messageRes: String)
4551
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DiContainer {
2929
private val retrofitCatFact by lazy {
3030
Retrofit.Builder()
3131
.client(okHttpClient)
32-
.baseUrl("https://catfact.ninja/")
32+
.baseUrl(CatsFactService.BASE_URL)
3333
.addConverterFactory(GsonConverterFactory.create())
3434
.build()
3535
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ import kotlin.coroutines.CoroutineContext
99
class PresenterScope : CoroutineScope {
1010

1111
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+
}
1317
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">Cat Facts </string>
33
<string name="more_facts">More Facts</string>
4+
<string name="error_connection">"Не удалось получить ответ от сервера"</string>
45
</resources>

0 commit comments

Comments
 (0)