-
Notifications
You must be signed in to change notification settings - Fork 239
ДЗ Coroutines #3
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?
Changes from 1 commit
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,28 +1,48 @@ | ||
package otus.homework.coroutines | ||
|
||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import android.util.Log | ||
import kotlinx.coroutines.* | ||
import retrofit2.Response | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class CatsPresenter( | ||
private val catsService: CatsService | ||
private val catsServiceFact: CatsService, | ||
private val catsServiceImage: CatsService | ||
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. для того чтобы вызывать https://aws.random.cat/ который на другом ретрофит клиенте private val retrofitImage by lazy { |
||
) { | ||
|
||
private var _catsView: ICatsView? = null | ||
private val presenterScope = | ||
PresenterScope(Job(), Dispatchers.Main, CoroutineName("CatsCoroutine")) | ||
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 onInitComplete() { | ||
catsService.getCatFact().enqueue(object : Callback<Fact> { | ||
presenterScope.launch { | ||
try { | ||
val factResponse = getCatFactResponse() | ||
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. Добавил withContext(Dispatcher.IO) в CatsViewModel, в предудущем комите тольков CatsPresenter добавлял. Это решает проблему |
||
val imageResponse = getCatImageResponse() | ||
if (factResponse != null | ||
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. А разве у тебя |
||
&& imageResponse != null ) { | ||
val factImage = FactImage(factResponse, imageResponse) | ||
_catsView?.populate(factImage) | ||
} else | ||
CrashMonitor.trackWarning() | ||
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. Это лучше перенести в последний |
||
|
||
override fun onResponse(call: Call<Fact>, response: Response<Fact>) { | ||
if (response.isSuccessful && response.body() != null) { | ||
_catsView?.populate(response.body()!!) | ||
} | ||
} catch (e: java.net.SocketTimeoutException) { | ||
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. Можно не полный импорт указывать |
||
_catsView?.showToast("Не удалось получить ответ от сервером") | ||
} catch (e: Exception) { | ||
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. Можно сделать один |
||
_catsView?.showToast(e.message.toString()) | ||
e.printStackTrace() | ||
} | ||
|
||
override fun onFailure(call: Call<Fact>, t: Throwable) { | ||
CrashMonitor.trackWarning() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
private suspend fun getCatFactResponse(): 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. Эти функции лишние. Лучше их удалить. |
||
return catsServiceFact.getCatFact() | ||
} | ||
|
||
|
||
private suspend fun getCatImageResponse(): Image { | ||
return catsServiceImage.getCatImage() | ||
} | ||
|
||
fun attachView(catsView: ICatsView) { | ||
|
@@ -31,5 +51,17 @@ class CatsPresenter( | |
|
||
fun detachView() { | ||
_catsView = null | ||
presenterScope.cancel() | ||
} | ||
} | ||
|
||
class PresenterScope( | ||
private val job: Job, | ||
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. Смотри тут получается что у тебя не |
||
private val dispatchers: CoroutineDispatcher, | ||
private val coroutineName: CoroutineName | ||
) : CoroutineScope { | ||
override val coroutineContext: CoroutineContext | ||
get() = job + dispatchers + coroutineName | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package otus.homework.coroutines | ||
|
||
import kotlinx.coroutines.Deferred | ||
import retrofit2.Call | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
|
||
interface CatsService { | ||
|
||
@GET("random?animal_type=cat") | ||
fun getCatFact() : Call<Fact> | ||
suspend fun getCatFact() : Fact | ||
|
||
@GET("meow") | ||
suspend fun getCatImage() : Image | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package otus.homework.coroutines | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
|
||
class CatsViewModel( | ||
private val catsServiceFact: CatsService, | ||
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. Опять лишний класс-зависимость |
||
private val catsServiceImage: CatsService | ||
): ViewModel() { | ||
private var _catsResponse = MutableLiveData<Result<FactImage>>() | ||
val catsResponse: LiveData<Result<FactImage>> | ||
get() = _catsResponse | ||
|
||
fun getCatFactImage(){ | ||
viewModelScope.launch(SupervisorJob() + CoroutineExceptionHandler{ | ||
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. У |
||
coroutineContext, throwable -> CrashMonitor.trackWarning() | ||
}) { | ||
try { | ||
val factResponse = getCatFactResponse() | ||
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. Вопрос: изначально я сделал без async чтобы они выполнялись полседовательно |
||
val imageResponse = getCatImageResponse() | ||
if (factResponse != null && imageResponse != null) { | ||
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 factImage = FactImage(factResponse, imageResponse) | ||
_catsResponse.value = Result.Success(factImage) | ||
} else{ | ||
_catsResponse.value = Result.Error("Что то пошло не так", null) | ||
} | ||
|
||
} catch (e: java.net.SocketTimeoutException) { | ||
_catsResponse.value = Result.Error("Не удалось получить ответ от сервером", e) | ||
} catch (e: Exception) { | ||
_catsResponse.value = Result.Error(e.message.toString(), e) | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
|
||
private suspend fun getCatFactResponse(): 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. Ненужные функции |
||
return catsServiceFact.getCatFact() | ||
} | ||
|
||
|
||
private suspend fun getCatImageResponse(): Image { | ||
return catsServiceImage.getCatImage() | ||
} | ||
} | ||
|
||
sealed class Result<out T>{ | ||
data class Success<out R>(val value: R): Result<R>() | ||
data class Error( | ||
val message: String, | ||
val throwable: Throwable? | ||
): Result<Nothing>() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package otus.homework.coroutines | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class FactImage( | ||
val fact: Fact, | ||
val image: Image | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package otus.homework.coroutines | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class FactImageResponse( | ||
val fact: Fact? = null, | ||
val image: Image? = null, | ||
var isSuccessful: Boolean? = true, | ||
var errorMessage: String? = null | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package otus.homework.coroutines | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class Image( | ||
@field:SerializedName("file") | ||
val file: String | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,62 @@ package otus.homework.coroutines | |
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
import androidx.lifecycle.Observer | ||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout | ||
import com.squareup.picasso.Picasso | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
lateinit var catsPresenter: CatsPresenter | ||
|
||
private val diContainer = DiContainer() | ||
private lateinit var viewModel: CatsViewModel | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView | ||
setContentView(view) | ||
|
||
catsPresenter = CatsPresenter(diContainer.service) | ||
view.presenter = catsPresenter | ||
catsPresenter.attachView(view) | ||
catsPresenter.onInitComplete() | ||
/* | ||
### Реализовать решение ViewModel | ||
*/ | ||
viewModel = CatsViewModel(diContainer.serviceFact, diContainer.serviceImage) | ||
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. Тут неправильное использование public class CatsViewModel extends ViewModelProvider.NewInstanceFactory {
private String name;
public CatsViewModel(String name) {
super();
this.name = name;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass == CatsViewModel.class) {
return (T) new CatsViewModel(name);
}
return null;
}
} |
||
|
||
viewModel.getCatFactImage() | ||
|
||
val refreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipe) | ||
refreshLayout?.setOnRefreshListener { | ||
viewModel.getCatFactImage() | ||
} | ||
|
||
viewModel.catsResponse.observe(this, Observer { factImage -> | ||
refreshLayout.isRefreshing = false | ||
when (factImage) { | ||
is Result.Success -> { | ||
findViewById<TextView>(R.id.fact_textView).text = factImage.value.fact?.text | ||
Picasso.get().load(factImage.value.image?.file) | ||
.into(findViewById<ImageView>(R.id.iv_image)) | ||
} | ||
is Result.Error -> { | ||
Toast.makeText(this, factImage.message, Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
}) | ||
|
||
//++++++++++++++++++++++++++++ | ||
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. Комментарии лучше убрать) |
||
/* | ||
### Перейти с коллбеков на саспенд функции и корутины | ||
### Добавить к запросу фактов запрос рандомных картинок с [https://aws.random.cat/meow](https://aws.random.cat/meow) | ||
*/ | ||
// catsPresenter = CatsPresenter(diContainer.serviceFact, diContainer.serviceImage) | ||
// view.presenter = catsPresenter | ||
// catsPresenter.attachView(view) | ||
// catsPresenter.onInitComplete() | ||
|
||
} | ||
|
||
override fun onStop() { | ||
|
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.
Несколько последних версий ретрофита поддерживают саспенд функции, так что этот адаптер уже не нужен