-
Notifications
You must be signed in to change notification settings - Fork 245
done #265
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?
done #265
Changes from 3 commits
c7b857f
b64b88d
a15b59f
22c4940
7c89320
3415b94
0d40a47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| data class CatImage(val url: String) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import retrofit2.Call | ||
| import retrofit2.http.GET | ||
|
|
||
| interface CatsService { | ||
|
|
||
| @GET("fact") | ||
| fun getCatFact() : Call<Fact> | ||
| suspend fun getCatFact() : Fact | ||
|
|
||
| } | ||
| interface CatsServicePics { | ||
| @GET("v1/images/search") | ||
| suspend fun getCatPicUrl() : List<CatImage> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import kotlinx.coroutines.CoroutineExceptionHandler | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.launch | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.cancel | ||
|
|
||
| class CatsViewModel( | ||
| private val catsService: CatsService, | ||
| private val catsServicePics: CatsServicePics | ||
| ) : ViewModel() { | ||
| private var _catsView: ICatsView? = null | ||
|
||
| private val _result = MutableLiveData<Result<Fact>>() | ||
| val result: LiveData<Result<Fact>> = _result | ||
|
|
||
| fun onInitComplete() { | ||
| val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
| CrashMonitor.trackWarning(throwable) | ||
| } | ||
| viewModelScope.launch(exceptionHandler) { | ||
| try { | ||
| val factDeferred = async { catsService.getCatFact() } | ||
| val imageDeferred = async { catsServicePics.getCatPicUrl() } | ||
|
|
||
| val fact = factDeferred.await() | ||
| val pic = imageDeferred.await() | ||
| val url = pic.firstOrNull()?.url | ||
| fact.url = url.toString() | ||
| _catsView?.populate(fact) | ||
| _result.value = Result.Success(fact) | ||
| } | ||
| catch (e: Exception) { | ||
| CrashMonitor.trackWarning(e) | ||
|
||
| _result.value = Result.Error(e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun attachView(catsView: ICatsView) { | ||
| _catsView = catsView | ||
| } | ||
|
|
||
| fun detachView() { | ||
| _catsView = null | ||
| } | ||
|
|
||
| override fun onCleared() { | ||
| super.onCleared() | ||
| detachView() | ||
| viewModelScope.cancel() | ||
|
||
| } | ||
|
|
||
| sealed class Result<out T> { | ||
| data class Success<T>(val data: T) : Result<T>() | ||
| data class Error(val exception: Throwable) : Result<Nothing>() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
|
|
||
| class CatsViewModelFactory( | ||
| private val catsService: CatsService, | ||
| private val catsServicePics: CatsServicePics | ||
| ) : ViewModelProvider.Factory { | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
| if (modelClass.isAssignableFrom(CatsViewModel::class.java)) { | ||
| @Suppress("UNCHECKED_CAST") | ||
| return CatsViewModel(catsService, catsServicePics) as T | ||
| } | ||
| throw IllegalArgumentException("Unknown ViewModel class") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import android.util.Log | ||
|
|
||
| object CrashMonitor { | ||
|
|
||
| /** | ||
| * Pretend this is Crashlytics/AppCenter | ||
| */ | ||
| fun trackWarning() { | ||
|
|
||
| } | ||
|
|
||
| fun trackWarning(throwable: Throwable) { | ||
| Log.d("viewModelError", throwable.toString()) | ||
| } | ||
| } |
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.
тут будет перехвачено
CancellationException, что сломает механизм отмены корутин, нужно прокидывать его дальшеis CancellationException -> throw e