-
Notifications
You must be signed in to change notification settings - Fork 237
Задача по Coroutines #160
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?
Задача по Coroutines #160
Changes from all commits
ffa0814
21557eb
56e9c98
4975aa0
1085f3d
5852c57
9b2f20c
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.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,54 @@ | ||
package otus.homework.coroutines | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Button | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
/** | ||
* Базовая `activity` | ||
*/ | ||
class MainActivity : AppCompatActivity() { | ||
|
||
lateinit var catsPresenter: CatsPresenter | ||
|
||
private val diContainer = DiContainer() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView | ||
setContentView(view) | ||
setContentView(R.layout.activity_main) | ||
|
||
catsPresenter = CatsPresenter(diContainer.service) | ||
view.presenter = catsPresenter | ||
catsPresenter.attachView(view) | ||
catsPresenter.onInitComplete() | ||
initView() | ||
} | ||
|
||
override fun onStop() { | ||
if (isFinishing) { | ||
catsPresenter.detachView() | ||
private fun initView() { | ||
findViewById<Button>(R.id.view_with_presenter_button).setOnClickListener { | ||
startActivity( | ||
Intent( | ||
this, otus.homework.coroutines.presentation.mvp.CatsActivity::class.java | ||
) | ||
) | ||
} | ||
|
||
findViewById<Button>(R.id.view_with_view_model_button).setOnClickListener { | ||
startActivity( | ||
Intent( | ||
this, otus.homework.coroutines.presentation.mvvm.parent.CatsActivity::class.java | ||
) | ||
) | ||
} | ||
|
||
findViewById<Button>(R.id.view_with_custom_owners_button).setOnClickListener { | ||
startActivity( | ||
Intent( | ||
this, otus.homework.coroutines.presentation.mvvm.owners.CatsActivity::class.java | ||
) | ||
) | ||
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. Activity ведет на другие три activity с тремя вариантами решения: с презентером, ViewModel и ViewModel с кастомными owаner-ами |
||
} | ||
|
||
findViewById<Button>(R.id.view_with_mvi_button).setOnClickListener { | ||
startActivity( | ||
Intent( | ||
this, otus.homework.coroutines.presentation.mvi.CatsActivity::class.java | ||
) | ||
) | ||
} | ||
super.onStop() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package otus.homework.coroutines.data | ||
|
||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import otus.homework.coroutines.data.converter.CatConverter | ||
import otus.homework.coroutines.data.models.Fact | ||
import otus.homework.coroutines.data.models.Image | ||
import otus.homework.coroutines.domain.CatRepository | ||
import otus.homework.coroutines.domain.models.Cat | ||
|
||
/** | ||
* Репозиторий информации о кошке [CatRepository] | ||
* | ||
* @param factService сервис получения случайного факта | ||
* @param imageService сервис получения случайных изображений | ||
* @param converter конвертер данных из [Fact] и списка [Image] в данные с информацией о кошке [Cat] | ||
*/ | ||
class CatRepositoryImpl( | ||
private val factService: FactService, | ||
private val imageService: ImagesService, | ||
private val converter: CatConverter, | ||
) : CatRepository { | ||
|
||
override suspend fun getCatInfo(): Cat = coroutineScope { | ||
val factDeferred = async { factService.getCatFact() } | ||
val imagesDeferred = async { imageService.getCatImages() } | ||
converter.convert(factDeferred.await(), imagesDeferred.await()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package otus.homework.coroutines.data | ||
|
||
import otus.homework.coroutines.data.models.Fact | ||
import retrofit2.http.GET | ||
|
||
/** | ||
* Сервис получения случайного факта | ||
*/ | ||
interface FactService { | ||
|
||
/** Получить случайных факт о кошке */ | ||
@GET("fact") | ||
suspend fun getCatFact(): Fact | ||
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. Какие ошибки может выбрасывать Retrofit здесь? 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. Зависит от статус кода который придет, ну точно может выбросить Таймаут, в целом все равно все статус коды которые не входят в 200-299 будут расценены колладаптером как исключение 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. Если нужно подробнее глянь реализацию колладаптеров 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. Спасибо! Пока кажется, что с ретрофитом мы не можем четко прописать бросаемые исключения, так как они внутри него самого прописаны и их могут менять |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package otus.homework.coroutines.data | ||
|
||
import otus.homework.coroutines.data.models.Image | ||
import retrofit2.http.GET | ||
|
||
/** | ||
* Сервис получения случайных изображений | ||
*/ | ||
interface ImagesService { | ||
|
||
/** Получить список случайных изображений */ | ||
@GET("search") | ||
suspend fun getCatImages(): List<Image> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package otus.homework.coroutines.data.converter | ||
|
||
import otus.homework.coroutines.data.models.Fact | ||
import otus.homework.coroutines.data.models.Image | ||
import otus.homework.coroutines.domain.models.Cat | ||
|
||
/** | ||
* Конвертер данных из [Fact] и списка [Image] в данные с информацией о кошке [Cat] | ||
*/ | ||
class CatConverter { | ||
|
||
/** Сконвертировать факт [Fact] и список изобрежений [Image] в информацию о кошке [Cat] */ | ||
fun convert(fact: Fact, images: List<Image>) = | ||
Cat(fact.fact, images.firstOrNull()?.url) | ||
} |
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.
Добавил реализацию на MVI, но скорее в качестве эксперимента, там coroutine толком нет