-
Notifications
You must be signed in to change notification settings - Fork 239
Terrible Coroutines Homework #157
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 4 commits
2bba971
f4d439c
91a3c03
da3fe69
63dc6cf
39389b6
fea5e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package otus.homework.coroutines | ||
|
||
import retrofit2.Call | ||
import otus.homework.coroutines.domain.CatImage | ||
import otus.homework.coroutines.domain.Fact | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
|
||
interface CatsService { | ||
|
||
@GET("fact") | ||
fun getCatFact() : Call<Fact> | ||
suspend fun getCatFact() : Response<Fact> | ||
|
||
@GET("meow") | ||
suspend fun getCatImage() : Response<CatImage> | ||
} | ||
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. добавить перенос, gitHub, даже, подсвечивает |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,48 @@ package otus.homework.coroutines | |
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.widget.Button | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import com.squareup.picasso.Picasso | ||
import otus.homework.coroutines.domain.CatImage | ||
import otus.homework.coroutines.domain.Fact | ||
|
||
class CatsView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyleAttr: Int = 0 | ||
) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { | ||
|
||
var presenter :CatsPresenter? = 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. private? |
||
var callback:(()->Unit)? = null | ||
lateinit var button: Button | ||
lateinit var textView: TextView | ||
lateinit var imageView: ImageView | ||
|
||
override fun onFinishInflate() { | ||
super.onFinishInflate() | ||
findViewById<Button>(R.id.button).setOnClickListener { | ||
presenter?.onInitComplete() | ||
} | ||
button = findViewById(R.id.button) | ||
button.setOnClickListener{callback?.invoke()} | ||
textView = findViewById(R.id.fact_textView) | ||
imageView = findViewById(R.id.cat_image_view) | ||
} | ||
|
||
override fun populate(fact: Fact) { | ||
findViewById<TextView>(R.id.fact_textView).text = fact.text | ||
override fun populate(data: Any) { | ||
if(data is Fact) textView.text = data.fact | ||
else if(data is CatImage) { | ||
Picasso.get() | ||
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. хорошо |
||
.load(data.url) | ||
.error(R.drawable.baseline_image_not_supported_24) | ||
.centerCrop() | ||
.fit() | ||
.into(imageView) | ||
} | ||
|
||
} | ||
} | ||
|
||
interface ICatsView { | ||
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. 1 интерфейс/класс - 1 файл |
||
|
||
fun populate(fact: Fact) | ||
fun populate(data: Any) | ||
} | ||
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 |
---|---|---|
@@ -1,10 +1,30 @@ | ||
package otus.homework.coroutines | ||
|
||
import android.util.Log | ||
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. Log? проверить что не Timber )) |
||
import otus.homework.coroutines.domain.Error | ||
import otus.homework.coroutines.domain.ResultException | ||
|
||
object CrashMonitor { | ||
|
||
/** | ||
* Pretend this is Crashlytics/AppCenter | ||
*/ | ||
fun trackWarning() { | ||
|
||
fun trackWarning(result: Any): String { | ||
val e:String | ||
when(result) { | ||
is Error<*> -> { | ||
e = "Error: Error code: ${result.errorCode}(${result.errorMessage})" | ||
Log.d("CrashMonitor", e)} | ||
is ResultException<*> ->{ | ||
e = "Exception: ${result.exceptionMessage}" | ||
Log.d("CrashMonitor", e)} | ||
else -> e = UNKNOWN_EXCEPTION | ||
} | ||
|
||
return e | ||
|
||
} | ||
|
||
private const val UNKNOWN_EXCEPTION = "Unknown 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. перенос |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package otus.homework.coroutines | ||
|
||
|
||
enum class DataType{ | ||
FACT, | ||
CAT_IMAGE | ||
} | ||
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 |
---|---|---|
|
@@ -3,14 +3,21 @@ package otus.homework.coroutines | |
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
class DiContainer { | ||
class DiContainer(private val url:String) { | ||
|
||
private val retrofit by lazy { | ||
Retrofit.Builder() | ||
.baseUrl("https://catfact.ninja/") | ||
.baseUrl(url) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
val service by lazy { retrofit.create(CatsService::class.java) } | ||
val service by lazy { retrofit.create(CatsService::class.java) } | ||
|
||
companion object{ | ||
const val FACT_URL = "https://catfact.ninja/" | ||
//const val IMAGE_URL = "https://aws.random.cat/" | ||
const val IMAGE_URL = "https://testing-server-indol.vercel.app/" | ||
|
||
} | ||
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. перенос |
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,29 +2,65 @@ package otus.homework.coroutines | |
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.lifecycle.ViewModelProvider | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import otus.homework.coroutines.view_models.PresenterViewModel | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
lateinit var catsPresenter: CatsPresenter | ||
private lateinit var viewModel: PresenterViewModel | ||
private var _catsView: ICatsView? = null | ||
private val scope by lazy { CoroutineScope(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. форматирование |
||
|
||
|
||
private val diContainer = DiContainer() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
viewModel = ViewModelProvider(this)[PresenterViewModel::class.java] | ||
|
||
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView | ||
setContentView(view) | ||
attachView(view) | ||
view.callback = { | ||
viewModel.scope.cancel() | ||
downloadData() | ||
} | ||
downloadData() | ||
viewModel.data.observe(this){ | ||
_catsView?.populate(it) | ||
|
||
} | ||
|
||
|
||
|
||
catsPresenter = CatsPresenter(diContainer.service) | ||
view.presenter = catsPresenter | ||
catsPresenter.attachView(view) | ||
catsPresenter.onInitComplete() | ||
} | ||
private fun downloadData(){ | ||
scope.launch { | ||
viewModel.getDataFromNet(DataType.FACT) | ||
viewModel.getDataFromNet(DataType.CAT_IMAGE) | ||
} | ||
|
||
} | ||
private fun attachView(catsView: ICatsView) { | ||
_catsView = catsView | ||
} | ||
|
||
private fun detachView() { | ||
_catsView = null | ||
} | ||
|
||
override fun onStop() { | ||
if (isFinishing) { | ||
catsPresenter.detachView() | ||
detachView() | ||
} | ||
super.onStop() | ||
scope.cancel() | ||
Log.d("CatsCoroutine", "Coroutine canceled" ) | ||
|
||
} | ||
} | ||
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,59 @@ | ||
package otus.homework.coroutines.data | ||
|
||
|
||
import otus.homework.coroutines.CatsService | ||
import otus.homework.coroutines.DataType | ||
import otus.homework.coroutines.domain.CatsRepository | ||
import otus.homework.coroutines.domain.Error | ||
import otus.homework.coroutines.domain.Result | ||
import otus.homework.coroutines.domain.ResultException | ||
import otus.homework.coroutines.domain.Success | ||
import retrofit2.HttpException | ||
import java.io.IOException | ||
|
||
import java.net.SocketTimeoutException | ||
|
||
|
||
class CatsRepositoryImpl(private val service: CatsService):CatsRepository<Result<Any>> { | ||
|
||
|
||
override suspend fun getFact() = downloadData(DataType.FACT) | ||
|
||
|
||
override suspend fun getImage() = downloadData(DataType.CAT_IMAGE) | ||
|
||
|
||
private suspend fun downloadData(dataType: DataType):Result<Any>{ | ||
return try { | ||
val data = when(dataType){ | ||
DataType.FACT -> service.getCatFact() | ||
DataType.CAT_IMAGE -> service.getCatImage() | ||
} | ||
val body = data.body() | ||
|
||
if (data.isSuccessful && body != null) Success(body) | ||
else Error(data.code(), data.message()) | ||
} | ||
catch (e: SocketTimeoutException) { | ||
ResultException(SOCKET_TIMEOUT_EXCEPTION_MESSAGE) | ||
} | ||
|
||
catch (e: IOException){ | ||
ResultException(e.message) | ||
} | ||
|
||
catch (e: HttpException){ | ||
ResultException(e.message) | ||
} | ||
|
||
catch (e: Throwable){ | ||
ResultException(e.message) | ||
} | ||
|
||
} | ||
|
||
companion object{ | ||
const val SOCKET_TIMEOUT_EXCEPTION_MESSAGE = "Не удалось получить ответ от сервера" | ||
|
||
} | ||
} | ||
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,6 @@ | ||
package otus.homework.coroutines.domain | ||
|
||
data class CatImage( | ||
val id: Int, | ||
val url: String | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package otus.homework.coroutines.domain | ||
|
||
interface CatsRepository<T: Any> { | ||
|
||
suspend fun getFact(): T | ||
|
||
suspend fun getImage(): T | ||
|
||
|
||
} | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
отлично