-
Notifications
You must be signed in to change notification settings - Fork 207
done #211
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
dmilkov97
wants to merge
8
commits into
Otus-Android:master
Choose a base branch
from
dmilkov97:master
base: master
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
done #211
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9c7f550
1st point
dmilkov97 050d2e2
2nd point
dmilkov97 56687e5
3rd point + fixes
dmilkov97 62986aa
done
dmilkov97 47d4c38
1st issue fixes
dmilkov97 059c8fc
2nd issue fixes
dmilkov97 bf5d101
3rd issue fixes
dmilkov97 a47086c
2nd issue fixes #2
dmilkov97 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 |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| package otus.homework.reactivecats | ||
|
|
||
| import io.reactivex.Single | ||
| import retrofit2.Call | ||
| import retrofit2.http.GET | ||
|
|
||
| interface CatsService { | ||
|
|
||
| //@GET("random?animal_type=cat") | ||
| @GET("fact") | ||
| fun getCatFact(): Call<Fact> | ||
| fun getCatFact(): Single<Fact> | ||
| } |
62 changes: 40 additions & 22 deletions
62
app/src/main/java/otus/homework/reactivecats/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 |
|---|---|---|
| @@ -1,55 +1,73 @@ | ||
| package otus.homework.reactivecats | ||
|
|
||
| import android.content.Context | ||
| import android.net.http.HttpException | ||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import io.reactivex.Flowable | ||
| import io.reactivex.Observable | ||
| import io.reactivex.android.schedulers.AndroidSchedulers | ||
| import io.reactivex.disposables.CompositeDisposable | ||
| import io.reactivex.schedulers.Schedulers | ||
| import okio.IOException | ||
| import retrofit2.Call | ||
| import retrofit2.Callback | ||
| import retrofit2.Response | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| class CatsViewModel( | ||
| catsService: CatsService, | ||
| localCatFactsGenerator: LocalCatFactsGenerator, | ||
| context: Context | ||
| private val catsService: CatsService, | ||
| private val localCatFactsGenerator: LocalCatFactsGenerator | ||
| ) : ViewModel() { | ||
|
|
||
| private val _catsLiveData = MutableLiveData<Result>() | ||
| val catsLiveData: LiveData<Result> = _catsLiveData | ||
| private val сompositeDisposable = CompositeDisposable() | ||
|
|
||
| init { | ||
| catsService.getCatFact().enqueue(object : Callback<Fact> { | ||
| override fun onResponse(call: Call<Fact>, response: Response<Fact>) { | ||
| if (response.isSuccessful && response.body() != null) { | ||
| _catsLiveData.value = Success(response.body()!!) | ||
| } else { | ||
| _catsLiveData.value = Error( | ||
| response.errorBody()?.string() ?: context.getString( | ||
| R.string.default_error_text | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| getFacts() | ||
| } | ||
|
|
||
| override fun onFailure(call: Call<Fact>, t: Throwable) { | ||
| _catsLiveData.value = ServerError | ||
| fun getFacts() { | ||
|
|
||
| val disposable = Observable.interval(0, 2000, TimeUnit.MILLISECONDS) | ||
| .flatMapSingle { | ||
|
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. лучше добавить |
||
| catsService.getCatFact().onErrorResumeNext { localCatFactsGenerator.generateCatFact() } | ||
| } | ||
| }) | ||
| .subscribeOn(Schedulers.io()) | ||
| .distinctUntilChanged() | ||
| .observeOn(AndroidSchedulers.mainThread()) | ||
| .subscribe({ | ||
| _catsLiveData.value = Success(it) | ||
| }, { | ||
| when (it) { | ||
| is IOException -> { | ||
| _catsLiveData.value = ServerError | ||
| } | ||
| else -> { | ||
| _catsLiveData.value = Error(it.message.toString()) | ||
| } | ||
| } | ||
| }) | ||
| сompositeDisposable.add(disposable) | ||
| } | ||
|
|
||
| fun getFacts() {} | ||
| override fun onCleared() { | ||
| super.onCleared() | ||
| сompositeDisposable.dispose() | ||
| } | ||
| } | ||
|
|
||
| class CatsViewModelFactory( | ||
| private val catsRepository: CatsService, | ||
| private val localCatFactsGenerator: LocalCatFactsGenerator, | ||
| private val context: Context | ||
| private val localCatFactsGenerator: LocalCatFactsGenerator | ||
| ) : | ||
| ViewModelProvider.NewInstanceFactory() { | ||
|
|
||
| override fun <T : ViewModel> create(modelClass: Class<T>): T = | ||
| CatsViewModel(catsRepository, localCatFactsGenerator, context) as T | ||
| CatsViewModel(catsRepository, localCatFactsGenerator) as T | ||
| } | ||
|
|
||
| sealed class Result | ||
|
|
||
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
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
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.
contextбольше не нужен, можно убрать его отсюда и изCatsViewModelFactory