-
Notifications
You must be signed in to change notification settings - Fork 237
HW_1_Kuzmin #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
HW_1_Kuzmin #174
Changes from 5 commits
88b6f35
f5b5ebd
0c067f4
d56cef2
e58123e
02fe218
89dbfa4
ab402ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
package otus.homework.coroutines | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import android.widget.Toast | ||
import java.net.SocketTimeoutException | ||
|
||
object CrashMonitor { | ||
|
||
/** | ||
* Pretend this is Crashlytics/AppCenter | ||
*/ | ||
fun trackWarning() { | ||
private const val SOCKET_TIME_EXCEPTION_ANSWER = "Не удалось получить ответ от сервера." | ||
|
||
fun trackWarning(context: Context, e: Throwable) { | ||
when(e) { | ||
is SocketTimeoutException -> { | ||
Toast.makeText(context, SOCKET_TIME_EXCEPTION_ANSWER, Toast.LENGTH_SHORT).show() | ||
} | ||
else -> { | ||
Log.d(this.toString(), e.toString()) | ||
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package otus.homework.coroutines.data | ||
|
||
import otus.homework.coroutines.domain.CatImage | ||
import otus.homework.coroutines.domain.Fact | ||
import retrofit2.http.GET | ||
|
||
interface CatsService { | ||
@GET("fact") | ||
suspend fun getCatFact(): Fact | ||
|
||
@GET("https://api.thecatapi.com/v1/images/search") | ||
suspend fun getCatImage(): List<CatImage> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package otus.homework.coroutines.domain | ||
|
||
import com.google.gson.annotations.Expose | ||
import com.google.gson.annotations.SerializedName | ||
|
||
data class CatImage( | ||
@SerializedName("url") | ||
@Expose | ||
val imageUrl: String? | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package otus.homework.coroutines.domain | ||
|
||
data class CatModel( | ||
val fact: Fact, | ||
val image: CatImage? | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package otus.homework.coroutines.domain | ||
|
||
sealed class Result { | ||
class Error(val exception: Throwable): Result() | ||
class Success(val catModel: CatModel): Result() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package otus.homework.coroutines.presentation | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.launch | ||
import otus.homework.coroutines.data.DiContainer | ||
import otus.homework.coroutines.domain.CatModel | ||
import otus.homework.coroutines.domain.Result | ||
|
||
class CatViewModel: ViewModel(){ | ||
|
||
private val catsService = DiContainer().service | ||
|
||
private val _result = MutableLiveData<Result>() | ||
val result: LiveData<Result> get() = _result | ||
|
||
private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
_result.value = Result.Error(throwable) | ||
} | ||
|
||
fun onInitComplete() { | ||
val deferredFact = viewModelScope.async { catsService.getCatFact() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему ты их вынес за пределы launch? |
||
|
||
val deferredImage = viewModelScope.async { catsService.getCatImage() } | ||
|
||
viewModelScope.launch(exceptionHandler) { | ||
val catModel = CatModel(deferredFact.await(), deferredImage.await().first()) | ||
_result.postValue(Result.Success(catModel)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ты тут уже на главном потоке, можно setValue вызвать |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package otus.homework.coroutines.presentation | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.ViewModelProvider | ||
import otus.homework.coroutines.CatsView | ||
import otus.homework.coroutines.CrashMonitor | ||
import otus.homework.coroutines.R | ||
import otus.homework.coroutines.domain.Result | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
private val catViewModel by lazy { | ||
ViewModelProvider(this)[CatViewModel::class.java] | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView | ||
setContentView(view) | ||
|
||
view.catViewModel = catViewModel | ||
|
||
catViewModel.result.observe(this) { | ||
when(it) { | ||
is Result.Success -> view.populate(it.catModel) | ||
is Result.Error -> CrashMonitor.trackWarning(this, it.exception) | ||
} | ||
} | ||
|
||
catViewModel.onInitComplete() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Предлагаю оставить CoroutineExceptionHandler длдя необработанных исключений, а socketexction обработать через try/catch или runCatching