-
Notifications
You must be signed in to change notification settings - Fork 237
HW2 #214
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
KPAHAPK
wants to merge
4
commits into
Otus-Android:development
Choose a base branch
from
KPAHAPK:development
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HW2 #214
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ceb8a8d
Перейти с коллбеков на саспенд функции и корутины
c45aebc
Добавить к запросу фактов запрос рандомных картинок с https://api.the…
807b89f
Добавить к запросу фактов запрос рандомных картинок с https://api.the…
7ae08af
Реализовать решение ViewModel
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package otus.homework.coroutines | ||
|
||
sealed class ApiResult<out T: Any> { | ||
data class Success<out T: Any>(val data: T) : ApiResult<T>() | ||
data class Error(val error: Throwable) : ApiResult<Nothing>() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package otus.homework.coroutines | ||
|
||
class CatapiImage : ArrayList<CatapiImageItem>() |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/otus/homework/coroutines/CatapiImageItem.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package otus.homework.coroutines | ||
|
||
data class CatapiImageItem( | ||
val height: Int, | ||
val id: String, | ||
val url: String, | ||
val width: Int | ||
) |
35 changes: 0 additions & 35 deletions
35
app/src/main/java/otus/homework/coroutines/CatsPresenter.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package otus.homework.coroutines | ||
|
||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Url | ||
|
||
interface CatsService { | ||
|
||
@GET("fact") | ||
fun getCatFact() : Call<Fact> | ||
suspend fun getCatFact() : Fact | ||
|
||
@GET | ||
suspend fun getImg(@Url url:String = "https://api.thecatapi.com/v1/images/search") : CatapiImage | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/src/main/java/otus/homework/coroutines/CatsViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package otus.homework.coroutines | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.launch | ||
import java.net.SocketTimeoutException | ||
|
||
class CatsViewModel( | ||
private val catsService: CatsService | ||
) : ViewModel() { | ||
|
||
private val _liveData = MutableLiveData<ApiResult<Content>>() | ||
val liveData: LiveData<ApiResult<Content>> = _liveData | ||
|
||
val context = CoroutineExceptionHandler { _, throwable -> | ||
CrashMonitor.trackWarning(throwable.message.toString()) | ||
} + SupervisorJob() | ||
|
||
init { | ||
populate() | ||
} | ||
|
||
fun populate() { | ||
viewModelScope.launch(context) { | ||
val resultFact = async { catsService.getCatFact() } | ||
val resultImg = async { catsService.getImg() } | ||
try { | ||
val content = Content(resultImg.await(), resultFact.await()) | ||
_liveData.value = ApiResult.Success(content) | ||
} catch (_: CancellationException) { | ||
} catch (e: SocketTimeoutException) { | ||
_liveData.value = | ||
ApiResult.Error(Throwable("Не удалось получить ответ от сервером")) | ||
} catch (e: Exception) { | ||
_liveData.value = ApiResult.Error(Throwable(e.message)) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package otus.homework.coroutines | ||
|
||
data class Content( | ||
val catapiImage: CatapiImage, | ||
val fact: Fact, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package otus.homework.coroutines | ||
|
||
import android.util.Log | ||
|
||
object CrashMonitor { | ||
|
||
/** | ||
* Pretend this is Crashlytics/AppCenter | ||
*/ | ||
fun trackWarning() { | ||
fun trackWarning(msg: String) { | ||
Log.e("CrashMonitor", msg) | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
app/src/main/java/otus/homework/coroutines/MainActivity.kt
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
app/src/main/java/otus/homework/coroutines/MainActivity2.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package otus.homework.coroutines | ||
|
||
import android.os.Bundle | ||
import android.widget.Button | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.squareup.picasso.Picasso | ||
|
||
class MainActivity2 : AppCompatActivity() { | ||
|
||
|
||
private val diContainer = DiContainer() | ||
val viewModel by lazy(LazyThreadSafetyMode.NONE) { | ||
ViewModelProvider(this, | ||
object : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return CatsViewModel(diContainer.service) as T | ||
} | ||
} | ||
)[CatsViewModel::class.java] | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val view = layoutInflater.inflate(R.layout.activity_main, null) | ||
setContentView(view) | ||
|
||
findViewById<Button>(R.id.button).setOnClickListener { | ||
viewModel.populate() | ||
} | ||
|
||
viewModel.liveData.observe(this) { result -> | ||
when (result){ | ||
is ApiResult.Error -> | ||
Toast.makeText(this, result.error.message, Toast.LENGTH_LONG).show() | ||
is ApiResult.Success -> { | ||
Picasso.get().load(result.data.catapiImage[0].url) | ||
.into(findViewById<ImageView>(R.id.fact_imgView)) | ||
findViewById<TextView>(R.id.fact_textView).text = result.data.fact.fact | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
app/src/main/java/otus/homework/coroutines/PresentationMopdel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package otus.homework.coroutines | ||
|
||
class PresentationMopdel { | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/otus/homework/coroutines/PresenterScope.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package otus.homework.coroutines | ||
|
||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class PresenterScope(override val coroutineContext: CoroutineContext = CoroutineName("CatsCoroutine") + Dispatchers.Main) : CoroutineScope { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<resources> | ||
<string name="app_name">Cat Facts </string> | ||
<string name="more_facts">More Facts</string> | ||
<string name="socket_timeout_exception">Не удалось получить ответ от сервером</string> | ||
</resources> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Тут пробросить нужно дальше