美文网首页
AutoDispose2之LifecycleEventsObse

AutoDispose2之LifecycleEventsObse

作者: 咚咚_Coding | 来源:发表于2021-08-05 19:58 被阅读0次
  @RestrictTo(LIBRARY_GROUP)
  class LifecycleEventsObservable extends Observable<Event> {

  private final Lifecycle lifecycle;
   private final BehaviorSubject<Event> eventsObservable = BehaviorSubject.create();

  @SuppressWarnings("CheckReturnValue")
  LifecycleEventsObservable(Lifecycle lifecycle) {
this.lifecycle = lifecycle;
  }

  Event getValue() {
return eventsObservable.getValue();

}

  /**
   * Backfill if already created for boundary checking. We do a trick here for     corresponding events
   * where we pretend something is created upon initialized state so that it assumes the
   * corresponding event is DESTROY.
   */
  void backfillEvents() {
@Nullable Lifecycle.Event correspondingEvent;
switch (lifecycle.getCurrentState()) {
  case INITIALIZED:
    correspondingEvent = ON_CREATE;
    break;
  case CREATED:
    correspondingEvent = ON_START;
    break;
  case STARTED:
  case RESUMED:
    correspondingEvent = ON_RESUME;
    break;
  case DESTROYED:
  default:
    correspondingEvent = ON_DESTROY;
    break;
}
eventsObservable.onNext(correspondingEvent);
  }

  @Override
  protected void subscribeActual(Observer<? super Event> observer) {
AutoDisposeLifecycleObserver lifecycleObserver =
    new AutoDisposeLifecycleObserver(lifecycle, observer, eventsObservable);
observer.onSubscribe(lifecycleObserver);
if (!isMainThread()) {
  observer.onError(
      new IllegalStateException("Lifecycles can only be bound to on the main thread!"));
  return;
    }
lifecycle.addObserver(lifecycleObserver);
if (lifecycleObserver.isDisposed()) {
  lifecycle.removeObserver(lifecycleObserver);
    }
  }

  static final class AutoDisposeLifecycleObserver extends MainThreadDisposable
  implements LifecycleObserver {
private final Lifecycle lifecycle;
private final Observer<? super Event> observer;
private final BehaviorSubject<Event> eventsObservable;

AutoDisposeLifecycleObserver(
    Lifecycle lifecycle,
    Observer<? super Event> observer,
    BehaviorSubject<Event> eventsObservable) {
  this.lifecycle = lifecycle;
  this.observer = observer;
  this.eventsObservable = eventsObservable;
}

@Override
protected void onDispose() {
  lifecycle.removeObserver(this);
}

@OnLifecycleEvent(Event.ON_ANY)
void onStateChange(@SuppressWarnings("unused") LifecycleOwner owner, Event event) {
  if (!isDisposed()) {
    if (!(event == ON_CREATE && eventsObservable.getValue() == event)) {
      // Due to the INITIALIZED->ON_CREATE mapping trick we do in backfill(),
      // we fire this conditionally to avoid duplicate CREATE events.
      eventsObservable.onNext(event);
    }
    observer.onNext(event);
  }
    }
  }
}

相关文章

  • AutoDispose2之LifecycleEventsObse

    }

  • Autodispose2 Core

    Core Class

  • 十之

    博学之,审问之,慎思之,明辨之,笃行之。 励志之,健身之,涅槃之,弘毅之,自强之!

  • 读记|唐诗人:诗心煎红尘(二)

    愈之挫之 险之退之 借之济之 忠之犯之 勇之夺之 衰之立之 坚之韧之 载之言之 一代宗师 成之传之 字曰子厚 道解...

  • 《寄君归》

    思之念之 见之不忘 吾亦求之 求之不得 吾亦念之 兮之盼之 来之归之 欲予离之 得之兮之 心思念之 盼来归之 归之...

  • 飘零

    艾雪儿 难耐心中怦然之 抑之,控之,思之,忘之 能否任之,弃之,拥之,念之 山河为鉴 赴之,游之,悦之,相守之…

  • 《美》

    刚之美,软之美,善之美,心之美。 水之美,声之美,爱之美,景之美,笑之美,物之美,月之美。 仁之美,慈之美,德之美...

  • 众说纷云

    众说纷云 文‖曾之一 20200220 古人说 真之假之善之恶之美之丑之 今人说 真之假之善之恶之美之丑之 后人说...

  • 安沨

    博学之,审问之,慎思之,明辨之,笃行之。

  • 2019-01-13

    博学之、明辨之、慎思之、审问之、笃行之

网友评论

      本文标题:AutoDispose2之LifecycleEventsObse

      本文链接:https://www.haomeiwen.com/subject/llkvvltx.html