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 4 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
10 changes: 5 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 @@ -34,11 +34,11 @@ android {

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'
Expand Down
35 changes: 0 additions & 35 deletions app/src/main/java/otus/homework/coroutines/CatsPresenter.kt

This file was deleted.

9 changes: 7 additions & 2 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>
}
Copy link

Choose a reason for hiding this comment

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

добавить перенос, gitHub, даже, подсвечивает

32 changes: 25 additions & 7 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,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

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

Choose a reason for hiding this comment

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

1 интерфейс/класс - 1 файл


fun populate(fact: Fact)
fun populate(data: Any)
}
Copy link

Choose a reason for hiding this comment

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

перенос

22 changes: 21 additions & 1 deletion app/src/main/java/otus/homework/coroutines/CrashMonitor.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
package otus.homework.coroutines

import android.util.Log
Copy link

Choose a reason for hiding this comment

The 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"
}
Copy link

Choose a reason for hiding this comment

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

перенос

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
}
Copy link

Choose a reason for hiding this comment

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

перенос

13 changes: 10 additions & 3 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,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/"

}
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.

50 changes: 43 additions & 7 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,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")) }
Copy link

Choose a reason for hiding this comment

The 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" )

}
}
Copy link

Choose a reason for hiding this comment

The 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 = "Не удалось получить ответ от сервера"

}
}
Copy link

Choose a reason for hiding this comment

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

перенос

6 changes: 6 additions & 0 deletions app/src/main/java/otus/homework/coroutines/domain/CatImage.kt
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


}
Copy link

Choose a reason for hiding this comment

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

форматирование и перенос

Loading