-
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 all 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> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,45 @@ 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 | ||
private lateinit var button: Button | ||
private lateinit var textView: TextView | ||
private 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 { | ||
|
||
fun populate(fact: Fact) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,37 @@ | ||
package otus.homework.coroutines | ||
|
||
import otus.homework.coroutines.domain.Error | ||
import otus.homework.coroutines.domain.ResultException | ||
import java.net.SocketTimeoutException | ||
|
||
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})" | ||
println(e) | ||
} | ||
|
||
is ResultException<*> -> { | ||
e = if (result.throwable is SocketTimeoutException) SOCKET_TIMEOUT_EXCEPTION_MESSAGE | ||
else "Exception: ${result.throwable.message}" | ||
println(e) | ||
} | ||
|
||
else -> e = UNKNOWN_EXCEPTION | ||
} | ||
|
||
return e | ||
|
||
} | ||
} | ||
|
||
private const val SOCKET_TIMEOUT_EXCEPTION_MESSAGE = "Не удалось получить ответ от сервера" | ||
|
||
private const val UNKNOWN_EXCEPTION = "Unknown Exception" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package otus.homework.coroutines | ||
|
||
|
||
enum class DataType { | ||
FACT, | ||
CAT_IMAGE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,22 @@ 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: CatsService 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package otus.homework.coroutines | ||
|
||
interface ICatsView { | ||
fun populate(data: Any) | ||
} |
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.
отлично