-
Notifications
You must be signed in to change notification settings - Fork 237
HW_1_Kuzmin #174
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
AlexanderYKuzmin
wants to merge
8
commits into
Otus-Android:development
Choose a base branch
from
AlexanderYKuzmin:home_work_1
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
HW_1_Kuzmin #174
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88b6f35
test_commit
AlexanderYKuzmin f5b5ebd
hw_1_coroutines_part_1
AlexanderYKuzmin 0c067f4
hw_1_coroutines_part_1_job_removed
AlexanderYKuzmin d56cef2
hw_1_coroutines_part_2
AlexanderYKuzmin e58123e
hw_1_coroutines_part_3_view_model
AlexanderYKuzmin 02fe218
hw_1_coroutines|
AlexanderYKuzmin 89dbfa4
hw_1_coroutines|
AlexanderYKuzmin ab402ec
async_execution_provided
AlexanderYKuzmin 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
25 changes: 21 additions & 4 deletions
25
app/src/main/java/otus/homework/coroutines/CrashMonitor.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 |
---|---|---|
@@ -1,10 +1,27 @@ | ||
package otus.homework.coroutines | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import android.widget.Toast | ||
import java.net.SocketTimeoutException | ||
|
||
object CrashMonitor { | ||
|
||
/** | ||
* Pretend this is Crashlytics/AppCenter | ||
*/ | ||
fun trackWarning() { | ||
private const val SOCKET_TIME_EXCEPTION_ANSWER = "Не удалось получить ответ от сервера." | ||
|
||
fun trackWarning(e: Throwable) { | ||
Log.d(this.toString(), e.toString()) | ||
} | ||
|
||
fun trackWarning(context: Context, e: Throwable) { | ||
when(e) { | ||
is SocketTimeoutException -> { | ||
Toast.makeText(context, SOCKET_TIME_EXCEPTION_ANSWER, Toast.LENGTH_SHORT).show() | ||
} | ||
else -> { | ||
Log.d(this.toString(), e.toString()) | ||
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
app/src/main/java/otus/homework/coroutines/MainActivity.kt
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
app/src/main/java/otus/homework/coroutines/data/CatsService.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,13 @@ | ||
package otus.homework.coroutines.data | ||
|
||
import otus.homework.coroutines.domain.CatImage | ||
import otus.homework.coroutines.domain.Fact | ||
import retrofit2.http.GET | ||
|
||
interface CatsService { | ||
@GET("fact") | ||
suspend fun getCatFact(): Fact | ||
|
||
@GET("https://api.thecatapi.com/v1/images/search") | ||
suspend fun getCatImage(): List<CatImage> | ||
} |
2 changes: 1 addition & 1 deletion
2
...a/otus/homework/coroutines/DiContainer.kt → ...s/homework/coroutines/data/DiContainer.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
10 changes: 10 additions & 0 deletions
10
app/src/main/java/otus/homework/coroutines/domain/CatImage.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.domain | ||
|
||
import com.google.gson.annotations.Expose | ||
import com.google.gson.annotations.SerializedName | ||
|
||
data class CatImage( | ||
@SerializedName("url") | ||
@Expose | ||
val imageUrl: String? | ||
) |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/otus/homework/coroutines/domain/CatModel.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,6 @@ | ||
package otus.homework.coroutines.domain | ||
|
||
data class CatModel( | ||
val fact: Fact, | ||
val image: CatImage? | ||
) |
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.domain | ||
|
||
sealed class Result { | ||
class Error(val exception: Throwable): Result() | ||
class Success(val catModel: CatModel): Result() | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/main/java/otus/homework/coroutines/presentation/CatViewModel.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,58 @@ | ||
package otus.homework.coroutines.presentation | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import otus.homework.coroutines.data.DiContainer | ||
import otus.homework.coroutines.domain.CatImage | ||
import otus.homework.coroutines.domain.CatModel | ||
import otus.homework.coroutines.domain.Fact | ||
import otus.homework.coroutines.domain.Result | ||
import java.net.SocketTimeoutException | ||
|
||
class CatViewModel: ViewModel(){ | ||
|
||
private val catsService = DiContainer().service | ||
|
||
private val _result = MutableLiveData<Result>() | ||
val result: LiveData<Result> get() = _result | ||
|
||
private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
_result.value = Result.Error(throwable) | ||
} | ||
|
||
fun onInitComplete() { | ||
viewModelScope.launch(exceptionHandler) { | ||
|
||
val deferredFact = withContext(Dispatchers.IO) { | ||
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. Так, а тут же не поправил, у тебя последовательные запросы |
||
try { | ||
catsService.getCatFact() | ||
} catch (e: SocketTimeoutException) { | ||
_result.value = Result.Error(e) | ||
} | ||
} | ||
|
||
val deferredImage = withContext(Dispatchers.IO) { | ||
try { | ||
catsService.getCatImage() | ||
} catch (e: SocketTimeoutException) { | ||
_result.value = Result.Error(e) | ||
} | ||
} | ||
|
||
if (deferredFact != Unit && deferredImage != Unit) { | ||
val catModel = | ||
CatModel( | ||
deferredFact as Fact, | ||
(deferredImage as List<CatImage>).first() | ||
) | ||
_result.value = Result.Success(catModel) | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/otus/homework/coroutines/presentation/CatsPresenter.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,68 @@ | ||
package otus.homework.coroutines.presentation | ||
|
||
import kotlinx.coroutines.* | ||
import otus.homework.coroutines.CrashMonitor | ||
import otus.homework.coroutines.ICatsView | ||
import otus.homework.coroutines.data.CatsService | ||
import otus.homework.coroutines.domain.CatImage | ||
import otus.homework.coroutines.domain.CatModel | ||
import otus.homework.coroutines.domain.Fact | ||
import java.net.SocketTimeoutException | ||
|
||
class CatsPresenter( | ||
private val catsService: CatsService | ||
) { | ||
|
||
private var _catsView: ICatsView? = null | ||
private val presenterScope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine")) | ||
|
||
fun onInitComplete() { | ||
presenterScope.launch { | ||
|
||
val deferredFact = withContext(Dispatchers.IO) { | ||
try { | ||
catsService.getCatFact() | ||
} catch (e: SocketTimeoutException) { | ||
_catsView?.showExceptionMessage("Не удалось получить ответ от сервера.") | ||
showExceptionMessage("Не удалось получить ответ от сервера.") | ||
} catch (e: Exception) { | ||
CrashMonitor.trackWarning(e) | ||
showExceptionMessage(e.message ?: "") | ||
} | ||
} | ||
|
||
val deferredImage = withContext(Dispatchers.IO) { | ||
try { | ||
catsService.getCatImage() | ||
} catch (e: SocketTimeoutException) { | ||
showExceptionMessage("Не удалось получить ответ от сервера.") | ||
} catch (e: Exception) { | ||
CrashMonitor.trackWarning(e) | ||
showExceptionMessage(e.message ?: "") | ||
} | ||
} | ||
|
||
try { | ||
val fact = deferredFact as Fact | ||
val catImage = (deferredImage as List<CatImage>).first() | ||
_catsView?.populate(CatModel(fact, catImage)) | ||
} catch (e: ClassCastException) { | ||
CrashMonitor.trackWarning(e) | ||
} | ||
} | ||
} | ||
|
||
private fun showExceptionMessage(text: String) { | ||
_catsView?.showExceptionMessage(text) | ||
} | ||
|
||
fun attachView(catsView: ICatsView) { | ||
_catsView = catsView | ||
} | ||
fun detachView() { | ||
_catsView = null | ||
} | ||
fun cancelAllJobs() { | ||
presenterScope.cancel() | ||
} | ||
} |
Oops, something went wrong.
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.
Предлагаю оставить CoroutineExceptionHandler длдя необработанных исключений, а socketexction обработать через try/catch или runCatching