android - Using Observable.interval() to advance time for "time blocks" stored in an array -


new rxjava - looking feedback.

scenario: have list of time blocks of various lengths (i.e 1 5 minutes long, 30 seconds, etc). use observable.from create observable: timeline = observable.from(timeblocks); i'd mix interval observer, observable<long> intervalobs = observable.interval(1000, timeunit.milliseconds); progress through time each time block in sequence. if first time block's duration 5 minutes , 5 minutes have passed move on next time block in sequence.

some other details:

  • the list can change (i'm not sure best way deal using rx) block can added/removed/have duration changes/re-ordered.
  • timer needs able pause/stop
  • other components need tap stream (to update ui example)

right have after trying several approaches seems work seems inefficient , looks wrong (i subscribe when time starts , unsubscribe when pausing/stopping):

intervalobs   .subscribe(new subscriber() {                 @override                 public void oncompleted() {                 }                  @override                 public void onerror(throwable e) {                  }                  @override                 public void onnext(object o) {                     timeline.first(new func1<timeblock, boolean>() {                         @override                         public boolean call(timeblock timeblock) {                             return timeblock.gettimeleft() > 0;                         }                     }).subscribe(new action1<timeblock>() {                         @override                         public void call(timeblock timeblock) {                             timeblock.addtime(1000);                         }                     });                 }             }); 

what's better way this? should structure things differently/conceptualize in different way?

let me try clear use case:

an app lets create lists of tasks , assign duration them. can import subset of tasks timer. timer increments time spent on each task starting top 1 first. when task's duration reached, move on next task. should able pause/resume/re-arrange order of tasks/edit tasks/etc , timer adjusts.

you can pre-select cycle pattern - i.e want work in blocks of 25 minutes. timer create many time blocks of 25 minutes needed complete tasks based on total duration. timer same thing described above, increasing time spent on each time block (and time spent on tasks contained in it) until duration reached moves on next.

i don't understand desired use case, perhaps example code can provide guidance:

import java.util.concurrent.timeunit;  import rx.observable;  public class timelinedelay {      public static void main(string... args) {         observable<long> timelineinmilliseconds = observable.just(                 timeunit.seconds.tomillis(5),                 timeunit.seconds.tomillis(2),                 timeunit.seconds.tomillis(10));          timelineinmilliseconds                 .flatmap(millis -> observable.timer(millis, timeunit.milliseconds).map(x -> millis))                 .map(millis -> "event after waiting " + millis + " milliseconds")                 .toblocking().foreach(system.out::println);     } } 

this output:

event after waiting 2000 milliseconds event after waiting 5000 milliseconds event after waiting 10000 milliseconds 
  • the timeline dynamic , whatever want be. emits react.
  • this wait each time block , emit.
  • subscribing (generally using map/flatmap/etc) allows on ui based on time intervals.

note not retain order uses flatmap allow multiple time blocks run concurrently. if instead want them execute sequentially, use concatmap allow 1 time block @ time waited upon.

that changes , retains order:

import java.util.concurrent.timeunit;  import rx.observable;  public class timelinedelay {      public static void main(string... args) {         observable<long> timelineinmilliseconds = observable.just(                 timeunit.seconds.tomillis(5),                 timeunit.seconds.tomillis(2),                 timeunit.seconds.tomillis(10));          timelineinmilliseconds                 .concatmap(millis -> observable.timer(millis, timeunit.milliseconds).map(x -> millis))                 .map(millis -> "event after waiting " + millis + " milliseconds")                 .toblocking().foreach(system.out::println);     } } 

so outputs this:

event after waiting 5000 milliseconds event after waiting 2000 milliseconds event after waiting 10000 milliseconds 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -