-
Notifications
You must be signed in to change notification settings - Fork 242
Aleksei Vinogradov - Coroutines #215
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
aykme
wants to merge
8
commits into
Otus-Android:development
Choose a base branch
from
aykme: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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
566de51
Домашняя работа
aykme 63c00e4
Дописал комментарии
aykme da38a2a
Убрал nullable ответы с сервера
aykme d687665
Убрал локальные коллбэки
aykme 36881ec
Убрал проверку активности job и Map'ы c job
aykme 4356121
Убрал withTimeout
aykme 357c3d3
Вынес переменную scopeContext в функцию-расширение и переиспользовал …
aykme e827ea0
Убрал методы очистки корутин
aykme 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package otus.homework.coroutines | ||
|
||
data class Cat( | ||
val fact: String, | ||
val imageUrl: String | ||
) |
5 changes: 2 additions & 3 deletions
5
...a/otus/homework/coroutines/CatsService.kt → ...tus/homework/coroutines/CatFactService.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,9 @@ | ||
package otus.homework.coroutines | ||
|
||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface CatsService { | ||
interface CatFactService { | ||
|
||
@GET("fact") | ||
fun getCatFact() : Call<Fact> | ||
suspend fun getCatFact(): FactResponse | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/otus/homework/coroutines/CatImageService.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,9 @@ | ||
package otus.homework.coroutines | ||
|
||
import retrofit2.http.GET | ||
|
||
interface CatImageService { | ||
|
||
@GET("images/search") | ||
suspend fun getCatImage(): List<ImageResponse> | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/otus/homework/coroutines/CatsCoroutineContext.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,19 @@ | ||
package otus.homework.coroutines | ||
|
||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* Вынесено отдельно, так как используется, как в презентере, так и во ViewModel | ||
*/ | ||
|
||
fun getCoroutineContext(): CoroutineContext = getCatsExceptionHandler() + getCatsCoroutineName() | ||
|
||
fun getCatsExceptionHandler() = | ||
CoroutineExceptionHandler { _, e -> | ||
CrashMonitor.trackWarning(e) | ||
} | ||
|
||
fun getCatsCoroutineName() = | ||
CoroutineName("CatsCoroutine") |
63 changes: 50 additions & 13 deletions
63
app/src/main/java/otus/homework/coroutines/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
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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
package otus.homework.coroutines | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.launch | ||
import java.net.SocketTimeoutException | ||
|
||
class CatsViewModel( | ||
private val catFactService: CatFactService, | ||
private val catImageService: CatImageService, | ||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO, | ||
private val isDataLoadedCallbaсk: (Boolean) -> Unit | ||
) : ViewModel() { | ||
|
||
private var _catsView: ICatsView? = null | ||
private var isLogsEnabled = true | ||
|
||
fun onInitComplete() { | ||
viewModelScope.launch(getCoroutineContext()) { | ||
val factResponseDeffered = | ||
viewModelScope.async(ioDispatcher) { | ||
aykme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
catFactService.getCatFact() | ||
} | ||
val imageResponseDeffered = | ||
viewModelScope.async(ioDispatcher) { | ||
catImageService.getCatImage() | ||
} | ||
|
||
val result = try { | ||
/** Тестовое пробрасывание SocketTimeoutException для проверки */ | ||
// throw SocketTimeoutException() | ||
|
||
val factResponse = factResponseDeffered.await() | ||
val imageResponse = imageResponseDeffered.await() | ||
val imageResponseFirstElement = imageResponse.firstOrNull() | ||
?: return@launch | ||
|
||
val cat = mapServerResponseToCat( | ||
factResponse = factResponse, | ||
imageResponse = imageResponseFirstElement | ||
) | ||
|
||
Result.Success<Cat>(cat) | ||
} catch (e: CancellationException) { | ||
throw e | ||
} catch (e: SocketTimeoutException) { | ||
Result.Error.SocketError | ||
} catch (e: Throwable) { | ||
CrashMonitor.trackWarning(e) | ||
Result.Error.OtherError(e) | ||
} | ||
|
||
/** | ||
* Логика показа Тоста находится внутри View, | ||
* работает в зависимости от результата | ||
*/ | ||
_catsView?.populate(result) | ||
isDataLoadedCallbaсk(true) | ||
} | ||
} | ||
|
||
fun attachView(catsView: ICatsView) { | ||
_catsView = catsView | ||
} | ||
|
||
fun detachView() { | ||
_catsView = null | ||
} | ||
} | ||
|
||
class CatsViewModelFactory( | ||
private val catFactService: CatFactService, | ||
private val catImageService: CatImageService, | ||
private val isDataLoadedCallbaсk: (Boolean) -> Unit | ||
) : | ||
ViewModelProvider.NewInstanceFactory() { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T = CatsViewModel( | ||
catFactService = catFactService, | ||
catImageService = catImageService, | ||
isDataLoadedCallbaсk = isDataLoadedCallbaсk, | ||
) as T | ||
} |
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,15 @@ | ||
package otus.homework.coroutines | ||
|
||
import android.util.Log | ||
|
||
object CrashMonitor { | ||
|
||
private const val CRASH_MONITOR_TAG = "CoroutinesHomework: CrashMonitor" | ||
|
||
/** | ||
* Pretend this is Crashlytics/AppCenter | ||
*/ | ||
fun trackWarning() { | ||
fun trackWarning(e: Throwable) { | ||
Log.d(CRASH_MONITOR_TAG, "trackWarning(): $e") | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
app/src/main/java/otus/homework/coroutines/FactResponse.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 com.google.gson.annotations.SerializedName | ||
|
||
data class FactResponse( | ||
@field:SerializedName("fact") | ||
val fact: String, | ||
@field:SerializedName("length") | ||
val length: Int | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.