-
Notifications
You must be signed in to change notification settings - Fork 251
ДЗ #262
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?
ДЗ #262
Changes from 8 commits
95461e8
a6e09a0
3592031
0659a05
c12acef
3cb556a
23f6767
5b16433
be0dfff
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,6 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| data class Cat( | ||
| val fact: String, | ||
| val imageUrl: String | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import com.google.gson.annotations.SerializedName | ||
|
|
||
| data class CatImage( | ||
| @field:SerializedName("id") | ||
| val id: String, | ||
| @field:SerializedName("url") | ||
| val url: String, | ||
| @field:SerializedName("width") | ||
| val width: Int, | ||
| @field:SerializedName("height") | ||
| val height: Int | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,51 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import retrofit2.Call | ||
| import retrofit2.Callback | ||
| import retrofit2.Response | ||
|
|
||
| class CatsPresenter( | ||
| private val catsService: CatsService | ||
| ) { | ||
|
|
||
| private var _catsView: ICatsView? = null | ||
|
|
||
| fun onInitComplete() { | ||
| catsService.getCatFact().enqueue(object : Callback<Fact> { | ||
|
|
||
| override fun onResponse(call: Call<Fact>, response: Response<Fact>) { | ||
| if (response.isSuccessful && response.body() != null) { | ||
| _catsView?.populate(response.body()!!) | ||
| } | ||
| } | ||
|
|
||
| override fun onFailure(call: Call<Fact>, t: Throwable) { | ||
| CrashMonitor.trackWarning() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fun attachView(catsView: ICatsView) { | ||
| _catsView = catsView | ||
| } | ||
|
|
||
| fun detachView() { | ||
| _catsView = null | ||
| } | ||
| } | ||
| //package otus.homework.coroutines | ||
| // | ||
| //import kotlinx.coroutines.CoroutineName | ||
| //import kotlinx.coroutines.CoroutineScope | ||
| //import kotlinx.coroutines.Dispatchers | ||
| //import kotlinx.coroutines.Job | ||
| //import kotlinx.coroutines.async | ||
| //import kotlinx.coroutines.launch | ||
| // | ||
| // | ||
| // | ||
| //class CatsPresenter( | ||
| // private val catsService: CatsService, | ||
| // private val catsImagesService: CatsImageService, | ||
| // private val presenterScope: CoroutineScope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine")) | ||
| //) { | ||
| // | ||
| // private var _catsView: ICatsView? = null | ||
| // private var job: Job? = null | ||
| // | ||
| // fun onInitComplete() { | ||
| // job = presenterScope.launch { | ||
| // try { | ||
| // val fact = async { catsService.getCatFact() }.await() | ||
| // val catImages:List<CatImage> = async { catsImagesService.getCatImage() }.await() | ||
| // _catsView?.populate(fact, catImages.first()) | ||
| // | ||
| // } catch (ex: Exception) { | ||
| // if (ex is java.net.SocketTimeoutException) { | ||
| // _catsView?.onError("Unable to get response from server") | ||
| // } | ||
| // if (ex is CancellationException) throw ex | ||
| // | ||
| // CrashMonitor.trackWarning("Error") | ||
|
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. по условиям задачи в
|
||
| // } | ||
| // } | ||
| // } | ||
| // | ||
| // fun onStop() { | ||
| // job?.cancel() | ||
| // } | ||
| // | ||
| // | ||
| // fun attachView(catsView: ICatsView) { | ||
| // _catsView = catsView | ||
| // } | ||
| // | ||
| // fun detachView() { | ||
| // _catsView = null | ||
| // } | ||
| //} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import retrofit2.Call | ||
|
|
||
| import retrofit2.http.GET | ||
|
|
||
| interface CatsService { | ||
|
|
||
| @GET("fact") | ||
| fun getCatFact() : Call<Fact> | ||
| } | ||
| suspend fun getCatFact() : Fact | ||
| } | ||
|
|
||
| interface CatsImageService { | ||
|
|
||
| @GET("v1/images/search") | ||
| suspend fun getCatImage() : List<CatImage> | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.viewModelScope | ||
| import androidx.lifecycle.viewmodel.initializer | ||
| import androidx.lifecycle.viewmodel.viewModelFactory | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.CoroutineExceptionHandler | ||
|
|
||
| class CatsViewModel( | ||
| private val catsService: CatsService, | ||
| private val catsImageService: CatsImageService | ||
| ) : ViewModel() { | ||
|
|
||
|
|
||
| private val _uiState = MutableLiveData<Result>() | ||
|
|
||
| private val exceptionHandler = CoroutineExceptionHandler { _, e -> | ||
| CrashMonitor.trackWarning("${e.message}") | ||
| _uiState.value = Result.Error( | ||
| Throwable() | ||
|
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. тут потеряется исходная ошибка, можно использовать |
||
| ) | ||
| } | ||
|
|
||
| val uiState: LiveData<Result> | ||
| get() = _uiState | ||
|
|
||
| fun populate () { | ||
| viewModelScope.launch(exceptionHandler) { | ||
| val fact = async { catsService.getCatFact() } | ||
| val image = async { catsImageService.getCatImage()[0] } | ||
| val cat = Cat(fact.await().fact, image.await().url) | ||
|
|
||
| _uiState.value = Result.Success(cat) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| fun provideFactory( | ||
| catsService: CatsService, | ||
| catsImageService: CatsImageService | ||
| ): ViewModelProvider.Factory = viewModelFactory { | ||
| initializer { | ||
| CatsViewModel(catsService, catsImageService) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import android.util.Log | ||
|
|
||
| object CrashMonitor { | ||
|
|
||
| /** | ||
| * Pretend this is Crashlytics/AppCenter | ||
| */ | ||
| fun trackWarning() { | ||
| fun trackWarning(warningMessage: String) { | ||
| Log.d("TAG", "Crash logged: $warningMessage") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
|
|
||
| sealed class Result { | ||
| data class Success(val catData: Cat) : Result() | ||
|
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.
|
||
| data class Error(val throwable: Throwable) : Result() | ||
| } | ||
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.
не стыкуются типы, во вью
populateпринимаетCat