Skip to content

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

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'org.jetbrains.kotlin.android'
}

android {
compileSdkVersion 30
compileSdkVersion 33
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "otus.homework.coroutines"
minSdkVersion 23
targetSdkVersion 30
targetSdkVersion 33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

отлично

versionCode 1
versionName "1.0"

Expand All @@ -30,16 +30,19 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
namespace 'otus.homework.coroutines'
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.squareup.picasso:picasso:2.71828'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'junit:junit:4.12'
}
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="otus.homework.coroutines">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<application
Expand Down
35 changes: 0 additions & 35 deletions app/src/main/java/otus/homework/coroutines/CatsPresenter.kt

This file was deleted.

11 changes: 8 additions & 3 deletions app/src/main/java/otus/homework/coroutines/CatsService.kt
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>
}
35 changes: 25 additions & 10 deletions app/src/main/java/otus/homework/coroutines/CatsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link

Choose a reason for hiding this comment

The 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()
Copy link

Choose a reason for hiding this comment

The 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)
}
31 changes: 29 additions & 2 deletions app/src/main/java/otus/homework/coroutines/CrashMonitor.kt
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"
}
7 changes: 7 additions & 0 deletions app/src/main/java/otus/homework/coroutines/DataType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package otus.homework.coroutines


enum class DataType {
FACT,
CAT_IMAGE
}
16 changes: 12 additions & 4 deletions app/src/main/java/otus/homework/coroutines/DiContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

перенос

}
24 changes: 0 additions & 24 deletions app/src/main/java/otus/homework/coroutines/Fact.kt

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/java/otus/homework/coroutines/ICatsView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package otus.homework.coroutines

interface ICatsView {
fun populate(data: Any)
}
69 changes: 59 additions & 10 deletions app/src/main/java/otus/homework/coroutines/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,78 @@ package otus.homework.coroutines

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
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 lateinit var view: CatsView
private var scope: CoroutineScope? = null

private val diContainer = DiContainer()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
viewModel = ViewModelProvider(this)[PresenterViewModel::class.java]
CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine"))
view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
scope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine"))
setContentView(view)
attachView(view)
downloadData()
viewModel.data.observe(this) {
_catsView?.populate(it)

}


}

private fun moreFactsButtonOnClick() {
view.callback = {
viewModel.scope.cancel()
downloadData()
}
}

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

catsPresenter = CatsPresenter(diContainer.service)
view.presenter = catsPresenter
catsPresenter.attachView(view)
catsPresenter.onInitComplete()
}

override fun onStop() {
if (isFinishing) {
catsPresenter.detachView()
detachView()
}
super.onStop()
scope?.cancel()


}

override fun onResume() {
scope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine"))
super.onResume()
moreFactsButtonOnClick()


}
}
}
Loading