-
Notifications
You must be signed in to change notification settings - Fork 186
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
Rx Homework #52
base: master
Are you sure you want to change the base?
Rx Homework #52
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package otus.homework.reactivecats | ||
|
||
import io.reactivex.Single | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface CatsService { | ||
|
||
@GET("random?animal_type=cat") | ||
fun getCatFact(): Call<Fact> | ||
fun getCatFact(): Single<Fact> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package otus.homework.reactivecats | ||
|
||
import android.content.Context | ||
import io.reactivex.BackpressureStrategy | ||
import io.reactivex.Flowable | ||
import io.reactivex.Single | ||
import kotlin.random.Random | ||
|
@@ -15,7 +16,9 @@ class LocalCatFactsGenerator( | |
* обернутую в подходящий стрим(Flowable/Single/Observable и т.п) | ||
*/ | ||
fun generateCatFact(): Single<Fact> { | ||
return Single.never() | ||
return Single.just ( | ||
Fact(context.resources.getStringArray(R.array.local_cat_facts)[Random.nextInt(5)]) | ||
) | ||
} | ||
|
||
/** | ||
|
@@ -25,6 +28,9 @@ class LocalCatFactsGenerator( | |
*/ | ||
fun generateCatFactPeriodically(): Flowable<Fact> { | ||
val success = Fact(context.resources.getStringArray(R.array.local_cat_facts)[Random.nextInt(5)]) | ||
return Flowable.empty() | ||
return Flowable.create<Fact?>( { emitter -> | ||
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. тут получается мы создаем поток данных один раз берем факт о коте, засыпаем на две секунды и больше ничего не происходит, можно посмотреть в сторону Flowable.interval() 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. Поправил |
||
emitter.onNext(success) | ||
Thread.sleep(2000) | ||
}, BackpressureStrategy.BUFFER).distinctUntilChanged() | ||
} | ||
} |
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.
Тут есть проблема, что если перевести девайс в авиа режим то берется только один факт из локального списка и обновление прекращается, а должны продолжать обновляться каждые 2 секунды
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.
Поправил