/*
 * Copyright (c) 2017. Uber Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ubercab.autodispose.rxlifecycle;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.OutsideLifecycleException;
import com.uber.autodispose.ScopeProvider;

/**
 * Interop for RxLifecycle. This provides static factory methods to convert {@link
 * LifecycleProvider}s into {@link ScopeProvider} representations.
 *
 * <em>Note:</em> RxLifecycle treats the {@link OutsideLifecycleException}
 * as normal terminal event. In such cases the stream is just disposed.
 */
public final class RxLifecycleInterop {

  private RxLifecycleInterop() {
    throw new AssertionError("No Instances");
  }

  /**
   * Factory creating a {@link ScopeProvider} representation of a {@link LifecycleProvider}.
   * <p>
   * Example usage:
   * <pre><code>
   *   Observable.just(1)
   *        .to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider)).forObservable())
   *        .subscribe(...)
   * </code></pre>
   *
   * @param <E> the lifecycle event.
   * @param provider the {@link LifecycleProvider}.
   * @return a {@link ScopeProvider}
   */
  public static <E> ScopeProvider from(final LifecycleProvider<E> provider) {
    return () -> provider.lifecycle()
        .compose(provider.bindToLifecycle())
        .ignoreElements();
  }

  /**
   * Factory creating a {@link ScopeProvider} representation of a {@link LifecycleProvider}.
   * <p>
   * Example usage:
   * <pre><code>
   *   Observable.just(1)
   *        .to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider, event)).forObservable())
   *        .subscribe(...)
   * </code></pre>
   *
   * @param <E> the lifecycle event.
   * @param provider the {@link LifecycleProvider}.
   * @param event a target event to dispose upon.
   * @return a {@link ScopeProvider}
   */
  public static <E> ScopeProvider from(final LifecycleProvider<E> provider, final E event) {
    return () -> provider.lifecycle()
        .compose(provider.bindUntilEvent(event))
        .ignoreElements();
  }
}

@@ -1406,36 +1406,36 @@
 1)%0A   *        .
-to(A
+as(a
 utoDispose.with(
@@ -1419,38 +1419,36 @@
   .as(autoDispos
-e.with
+able
 (RxLifecycleInte
@@ -1475,32 +1475,16 @@
 ovider))
-.forObservable()
 )%0A   *  
@@ -2042,12 +2042,12 @@
    .
-to(A
+as(a
 utoD
@@ -2055,14 +2055,12 @@
 spos
-e.with
+able
 (RxL
@@ -2110,24 +2110,8 @@
 nt))
-.forObservable()
 )%0A  

Update example usage of RxLifecycleInterop (#280)