Reputation: 838
I want to implement the following with RxJava on android:
The class TimeUnit contains a list of class Sequence (TimeUnit has a list of Sequences) that have a integer property "length".
The sequences have a 1 - 10 seconds length.
When the TimeUnit run() method is called the timer starts and executes the firs sequence in the list. After that sequence 2 etc.
Example:
TimeUnit
- Sequence 1 -> 10 seconds
- Sequence 2 -> 4 seconds
- Sequence 3 -> 5 seconds
How to achieve this output in RxJava :
TimeUnit started
Starting "Sequence 1"
Sequence 1 -> second 1
Sequence 1 -> second 2
Sequence 1 -> second 3
...
Starting "Sequence 2"
Sequence 2 -> second 1
....
TimeUnit completed in 19 seconds
I tried with many Rx Techniques like interval(), concatMap(), flatMap() ... but no success.
How would you setup a skeleton for this? Is Rx able to concat intervals?
I have no codesnippet as no solution worked
Upvotes: 2
Views: 153
Reputation: 12782
If you don't mind using a little bit of code that is not from RxJava, you can use this solution:
public Observable<String> createObservable(TimeUnit timeUnit){
Handler handler = new Handler();
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
for(Sequence sequence : TimeUnit.getSequenceList()){
Runnable runnable = () -> {
subscriber.onNext(sequence.length);
};
handler.postDelayed(runnable, sequence.lenth);
}
}
});
return observable;
}
It's just a example, adapt to your needs.
I think it's a good approach because you are trying to do is not very trivial, so a little bit of imperative code won't hurt.
Happy coding!
Upvotes: 1