-
Notifications
You must be signed in to change notification settings - Fork 251
HomeWork2 #264
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?
HomeWork2 #264
Changes from 3 commits
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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import otus.homework.coroutines.model.Fact | ||
| import retrofit2.Call | ||
| import retrofit2.http.GET | ||
|
|
||
| interface CatsService { | ||
|
|
||
| @GET("fact") | ||
| fun getCatFact() : Call<Fact> | ||
| suspend fun getCatFact(): Fact | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
|
|
||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.CoroutineExceptionHandler | ||
|
|
||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import otus.homework.coroutines.CrashMonitor.trackWarning | ||
| import otus.homework.coroutines.model.Fact | ||
| import otus.homework.coroutines.model.Model | ||
| import java.net.SocketTimeoutException | ||
|
|
||
| class CatsViewModel( | ||
| private val catsService: CatsService, | ||
| private val imagesService: ImagesService, | ||
| private val onShowToast: (String?) -> Unit | ||
| ) : ViewModel() { | ||
|
|
||
| private val _state = MutableStateFlow<Result>(Result.Error("Не получилось получить факт")) | ||
| val state: StateFlow<Result> = _state.asStateFlow() | ||
|
|
||
| val handler = CoroutineExceptionHandler { _, exception -> | ||
| if(exception is SocketTimeoutException){ | ||
| onShowToast("Не удалось получить ответ от сервера") | ||
| }else { | ||
| trackWarning("CoroutineExceptionHandler got $exception") | ||
| onShowToast(exception.message) | ||
| } | ||
| _state.value = Result.Error(exception.message) | ||
| } | ||
|
|
||
| fun onInitComplete() { | ||
| viewModelScope.launch (handler){ | ||
| val fact = async { catsService.getCatFact() } | ||
| val image = async { imagesService.getImage().firstOrNull() } | ||
| val model = Model(fact.await(), image.await()?.url) | ||
| _state.value = Result.Success(model) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sealed class Result { | ||
| data class Success<T>(val data: T) : Result() | ||
| data class Error(val msg: String?) : Result() | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import otus.homework.coroutines.model.Image | ||
| import retrofit2.http.GET | ||
|
|
||
| interface ImagesService { | ||
|
|
||
| @GET("v1/images/search") | ||
| suspend fun getImage(): List<Image> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,29 +2,72 @@ package otus.homework.coroutines | |
|
|
||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import android.widget.Toast | ||
| import androidx.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.flow.observeOn | ||
| import kotlinx.coroutines.launch | ||
| import otus.homework.coroutines.model.Model | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| lateinit var catsPresenter: CatsPresenter | ||
|
|
||
| private val diContainer = DiContainer() | ||
| private var isPresenretMode = true | ||
|
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. nit: опечатка isPresenterMode
Author
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. Fixed |
||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView | ||
| setContentView(view) | ||
| if (isPresenretMode) { | ||
| catsPresenter = CatsPresenter( | ||
| diContainer.service, | ||
| diContainer.imagesService, | ||
| ::onShowToast | ||
| ) | ||
|
|
||
| view.presenter = catsPresenter | ||
| catsPresenter.attachView(view) | ||
| catsPresenter.onInitComplete() | ||
| } else { | ||
| val viewModel = CatsViewModel( | ||
| diContainer.service, | ||
| diContainer.imagesService, | ||
| ::onShowToast | ||
| ) | ||
| viewModel.onInitComplete() | ||
| view.viewModel = viewModel | ||
|
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. не стоит во вью подкладывать ссылку на вьюмодель, она там вообще не нужна
Author
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. Fixed |
||
| lifecycleScope.launch { | ||
| viewModel.state.collect { state -> | ||
| when (state) { | ||
| is Result.Success<*> -> { | ||
| if (state.data is Model) { | ||
| view.populate(Model(state.data.fact, state.data.imageUrl)) | ||
|
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. лишнее пересоздание модели, можно скастить к Model |
||
| } | ||
| } | ||
|
|
||
| is Result.Error -> { | ||
| onShowToast(state.msg) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| catsPresenter = CatsPresenter(diContainer.service) | ||
| view.presenter = catsPresenter | ||
| catsPresenter.attachView(view) | ||
| catsPresenter.onInitComplete() | ||
| private fun onShowToast(message: String?) { | ||
| Toast.makeText(this, message, Toast.LENGTH_SHORT).show() | ||
| } | ||
|
|
||
| override fun onStop() { | ||
| if (isFinishing) { | ||
| catsPresenter.detachView() | ||
| catsPresenter.onStop() | ||
|
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. нет проверки на инициализацию catsPresenter, в режиме
Author
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. Fixed |
||
| } | ||
| super.onStop() | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package otus.homework.coroutines.model | ||
|
|
||
| import com.google.gson.annotations.SerializedName | ||
|
|
||
| data class Fact( | ||
| @field:SerializedName("fact") | ||
| val fact: String, | ||
| @field:SerializedName("length") | ||
| val length: Int | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package otus.homework.coroutines.model | ||
|
|
||
| import com.google.gson.annotations.SerializedName | ||
|
|
||
| data class Image( | ||
| @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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package otus.homework.coroutines.model | ||
|
|
||
| data class Model( | ||
| val fact: Fact, | ||
| val imageUrl: String? | ||
| ) |
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.
можно сделать приватным, снаружи не нужен
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.
Fixed