diff options
Diffstat (limited to 'reactive-livedata/src/main/java/it')
16 files changed, 0 insertions, 778 deletions
diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/ReactiveLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/ReactiveLiveData.java deleted file mode 100644 index 664004bba..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/ReactiveLiveData.java +++ /dev/null @@ -1,217 +0,0 @@ -package it.niedermann.android.reactivelivedata; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.arch.core.util.Function; -import androidx.core.util.Consumer; -import androidx.core.util.Pair; -import androidx.core.util.Predicate; -import androidx.core.util.Supplier; -import androidx.lifecycle.LifecycleOwner; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MediatorLiveData; -import androidx.lifecycle.Observer; -import androidx.lifecycle.Transformations; - -import java.time.temporal.ChronoUnit; -import java.util.concurrent.ExecutorService; - -import it.niedermann.android.reactivelivedata.combinator.DoubleCombinatorLiveData; -import it.niedermann.android.reactivelivedata.combinator.TripleCombinatorLiveData; -import it.niedermann.android.reactivelivedata.debounce.DebounceLiveData; -import it.niedermann.android.reactivelivedata.distinct.DistinctUntilChangedLiveData; -import it.niedermann.android.reactivelivedata.filter.FilterLiveData; -import it.niedermann.android.reactivelivedata.flatmap.FlatMapLiveData; -import it.niedermann.android.reactivelivedata.map.MapLiveData; -import it.niedermann.android.reactivelivedata.merge.MergeLiveData; -import it.niedermann.android.reactivelivedata.take.TakeLiveData; -import it.niedermann.android.reactivelivedata.tap.TapLiveData; -import kotlin.Triple; -import kotlin.jvm.functions.Function1; - -/** - * @see ReactiveLiveDataBuilder - */ -public class ReactiveLiveData<T> extends MediatorLiveData<T> implements ReactiveLiveDataBuilder<T> { - - public ReactiveLiveData(@Nullable LiveData<T> source) { - if (source == null) { - setValue(null); - } else { - addSource(source, this::setValue); - } - } - - public ReactiveLiveData(@NonNull T value) { - setValue(value); - } - - public ReactiveLiveData() { - super(); - } - - /** - * Observe without getting notified about the emitted values. - */ - public void observe(@NonNull LifecycleOwner owner) { - super.observe(owner, val -> { - // Nothing to do… - }); - } - - /** - * Observe without getting getting the emitted value. - */ - public void observe(@NonNull LifecycleOwner owner, @NonNull Runnable runnable) { - super.observe(owner, val -> runnable.run()); - } - - /** - * Cancel observation directly after one value has been emitted. - */ - public void observeOnce(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) { - final var internalObserver = new Observer<T>() { - @Override - public void onChanged(T result) { - removeObserver(this); - observer.onChanged(result); - } - }; - - observe(owner, internalObserver); - } - - /** - * @see Transformations#map(LiveData, Function1) - */ - @NonNull - @Override - public <Y> ReactiveLiveData<Y> map(@NonNull Function1<T, Y> mapFunction) { - return new MapLiveData<>(this, mapFunction); - } - - /** - * @see #map(Function1) but the mapFunction will be executed on the given executor - */ - public <Y> ReactiveLiveData<Y> map(@NonNull Function1<T, Y> mapFunction, @NonNull ExecutorService executor) { - return new MapLiveData<>(this, mapFunction, executor); - } - - /** - * @see Transformations#switchMap(LiveData, Function1) - */ - @NonNull - @Override - public <Y> ReactiveLiveData<Y> flatMap(@NonNull Function1<T, LiveData<Y>> flatMapFunction) { - return new FlatMapLiveData<>(this, flatMapFunction); - } - - @NonNull - @Override - public <Y> ReactiveLiveData<Y> flatMap(@NonNull Supplier<LiveData<Y>> switchMapSupplier) { - return new FlatMapLiveData<>(this, switchMapSupplier); - } - - /** - * @see Transformations#distinctUntilChanged(LiveData) - */ - @NonNull - @Override - public ReactiveLiveData<T> distinctUntilChanged() { - return new DistinctUntilChangedLiveData<>(this); - } - - @NonNull - public ReactiveLiveData<T> filter(@NonNull Predicate<T> predicate) { - return new FilterLiveData<>(this, predicate); - } - - @NonNull - @Override - public ReactiveLiveData<T> filter(@NonNull Supplier<Boolean> supplier) { - return new FilterLiveData<>(this, supplier); - } - - @NonNull - @Override - public ReactiveLiveData<T> tap(@NonNull Consumer<T> consumer) { - return new TapLiveData<>(this, consumer); - } - - @NonNull - @Override - public ReactiveLiveData<T> tap(@NonNull Runnable runnable) { - return new TapLiveData<>(this, runnable); - } - - /** - * @see #tap(Consumer) but the tap consumer will be executed on the given executor - */ - public ReactiveLiveData<T> tap(@NonNull Consumer<T> consumer, @NonNull ExecutorService executor) { - return new TapLiveData<>(this, consumer, executor); - } - - public ReactiveLiveData<T> tap(@NonNull Runnable runnable, @NonNull ExecutorService executor) { - return new TapLiveData<>(this, runnable, executor); - } - - @NonNull - @Override - public ReactiveLiveData<T> merge(@NonNull Supplier<LiveData<T>> secondSource) { - return new MergeLiveData<>(this, secondSource); - } - - @NonNull - @Override - public ReactiveLiveData<T> take(int limit) { - return new TakeLiveData<>(this, limit); - } - - @NonNull - @Override - public <Y> ReactiveLiveData<Pair<T, Y>> combineWith(@NonNull Function<T, LiveData<Y>> secondSourceFunction) { - return new DoubleCombinatorLiveData<>(this, secondSourceFunction); - } - - @NonNull - @Override - public <Y> ReactiveLiveData<Pair<T, Y>> combineWith(@NonNull Supplier<LiveData<Y>> secondSourceSupplier) { - return new DoubleCombinatorLiveData<>(this, secondSourceSupplier); - } - - @NonNull - @Override - public <Y, Z> ReactiveLiveData<Triple<T, Y, Z>> combineWith(@NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Function<T, LiveData<Z>> thirdSourceFunction) { - return new TripleCombinatorLiveData<>(this, secondSourceFunction, thirdSourceFunction); - } - - @NonNull - @Override - public <Y, Z> ReactiveLiveData<Triple<T, Y, Z>> combineWith(@NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Supplier<LiveData<Z>> thirdSourceSupplier) { - return new TripleCombinatorLiveData<>(this, secondSourceFunction, thirdSourceSupplier); - } - - @NonNull - @Override - public <Y, Z> ReactiveLiveData<Triple<T, Y, Z>> combineWith(@NonNull Supplier<LiveData<Y>> secondSourceSupplier, @NonNull Function<T, LiveData<Z>> thirdSourceFunction) { - return new TripleCombinatorLiveData<>(this, secondSourceSupplier, thirdSourceFunction); - } - - @NonNull - @Override - public <Y, Z> ReactiveLiveData<Triple<T, Y, Z>> combineWith(@NonNull Supplier<LiveData<Y>> secondSourceSupplier, @NonNull Supplier<LiveData<Z>> thirdSourceSupplier) { - return new TripleCombinatorLiveData<>(this, secondSourceSupplier, thirdSourceSupplier); - } - - @NonNull - @Override - public ReactiveLiveData<T> debounce(long timeout, @NonNull ChronoUnit timeUnit) { - return new DebounceLiveData<>(this, timeout, timeUnit); - } - - @NonNull - @Override - public ReactiveLiveData<T> debounce(long timeout) { - return new DebounceLiveData<>(this, timeout); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/ReactiveLiveDataBuilder.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/ReactiveLiveDataBuilder.java deleted file mode 100644 index dc498a928..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/ReactiveLiveDataBuilder.java +++ /dev/null @@ -1,131 +0,0 @@ -package it.niedermann.android.reactivelivedata; - -import androidx.annotation.NonNull; -import androidx.arch.core.util.Function; -import androidx.core.util.Consumer; -import androidx.core.util.Pair; -import androidx.core.util.Predicate; -import androidx.core.util.Supplier; -import androidx.lifecycle.LiveData; - -import java.time.temporal.ChronoUnit; -import java.util.concurrent.TimeUnit; - -import kotlin.Triple; -import kotlin.jvm.functions.Function1; - -/** - * Partial implementation of <a href="https://reactivex.io/documentation/operators.html">ReactiveX</a> features - */ -public interface ReactiveLiveDataBuilder<T> { - - /** - * @see <a href="https://reactivex.io/documentation/operators/map.html">ReactiveX#map</a> - */ - @NonNull - <Y> ReactiveLiveDataBuilder<Y> map(@NonNull Function1<T, Y> mapFunction); - - /** - * @see <a href="https://reactivex.io/documentation/operators/flatmap.html">ReactiveX#flatmap</a> - */ - @NonNull - <Y> ReactiveLiveDataBuilder<Y> flatMap(@NonNull Function1<T, LiveData<Y>> flatMapFunction); - - /** - * @see #flatMap(Function1) - */ - @NonNull - <Y> ReactiveLiveDataBuilder<Y> flatMap(@NonNull Supplier<LiveData<Y>> flatMapSupplier); - - /** - * @see <a href="https://reactivex.io/documentation/operators/distinct.html">ReactiveX#distinct</a> - */ - @NonNull - ReactiveLiveDataBuilder<T> distinctUntilChanged(); - - /** - * @see <a href="https://reactivex.io/documentation/operators/filter.html">ReactiveX#filter</a> - */ - @NonNull - ReactiveLiveDataBuilder<T> filter(@NonNull Predicate<T> predicate); - - /** - * @see #filter(Predicate) - */ - @NonNull - ReactiveLiveDataBuilder<T> filter(@NonNull Supplier<Boolean> supplier); - - /** - * @see <a href="https://reactivex.io/documentation/operators/do.html">ReactiveX#do</a> - */ - @NonNull - ReactiveLiveDataBuilder<T> tap(@NonNull Consumer<T> consumer); - - /** - * @see #tap(Consumer) - */ - @NonNull - ReactiveLiveDataBuilder<T> tap(@NonNull Runnable runnable); - - /** - * @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX#merge</a> - */ - @NonNull - ReactiveLiveData<T> merge(@NonNull Supplier<LiveData<T>> liveData); - - /** - * @see <a href="https://reactivex.io/documentation/operators/take.html">ReactiveX#take</a> - */ - @NonNull - ReactiveLiveDataBuilder<T> take(int limit); - - /** - * @see <a href="https://reactivex.io/documentation/operators/combinelatest.html">ReactiveX#combinelatest</a> - */ - @NonNull - <Y> ReactiveLiveDataBuilder<Pair<T, Y>> combineWith(@NonNull Function<T, LiveData<Y>> secondSourceFunction); - - /** - * @see #combineWith(Function) - */ - @NonNull - <Y> ReactiveLiveDataBuilder<Pair<T, Y>> combineWith(@NonNull Supplier<LiveData<Y>> secondSourceSupplier); - - /** - * @see <a href="https://reactivex.io/documentation/operators/combinelatest.html">ReactiveX#combinelatest</a> - */ - @NonNull - <Y, Z> ReactiveLiveDataBuilder<Triple<T, Y, Z>> combineWith(@NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Function<T, LiveData<Z>> thirdSourceFunction); - - /** - * @see #combineWith(Function) - */ - @NonNull - <Y, Z> ReactiveLiveDataBuilder<Triple<T, Y, Z>> combineWith(@NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Supplier<LiveData<Z>> thirdSourceSupplier); - - /** - * @see #combineWith(Function) - */ - @NonNull - <Y, Z> ReactiveLiveDataBuilder<Triple<T, Y, Z>> combineWith(@NonNull Supplier<LiveData<Y>> secondSourceSupplier, @NonNull Function<T, LiveData<Z>> thirdSourceFunction); - - /** - * @see #combineWith(Function) - */ - @NonNull - <Y, Z> ReactiveLiveDataBuilder<Triple<T, Y, Z>> combineWith(@NonNull Supplier<LiveData<Y>> secondSourceSupplier, @NonNull Supplier<LiveData<Z>> thirdSourceSupplier); - - /** - * @see <a href="https://reactivex.io/documentation/operators/debounce.html">ReactiveX#debounce</a>> - */ - @NonNull - ReactiveLiveDataBuilder<T> debounce(long timeout, @NonNull ChronoUnit timeUnit); - - /** - * @param timeout defaults to {@link TimeUnit#MILLISECONDS} - * - * @see #debounce(long, ChronoUnit) - */ - @NonNull - ReactiveLiveDataBuilder<T> debounce(long timeout); -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/DoubleCombinatorLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/DoubleCombinatorLiveData.java deleted file mode 100644 index 63c704546..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/DoubleCombinatorLiveData.java +++ /dev/null @@ -1,20 +0,0 @@ -package it.niedermann.android.reactivelivedata.combinator; - -import androidx.annotation.NonNull; -import androidx.arch.core.util.Function; -import androidx.core.util.Pair; -import androidx.core.util.Supplier; -import androidx.lifecycle.LiveData; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; - -public class DoubleCombinatorLiveData<T, Y> extends ReactiveLiveData<Pair<T, Y>> { - - public DoubleCombinatorLiveData(@NonNull LiveData<T> source, @NonNull Supplier<LiveData<Y>> secondSourceSupplier) { - this(source, val -> secondSourceSupplier.get()); - } - - public DoubleCombinatorLiveData(@NonNull LiveData<T> source, @NonNull Function<T, LiveData<Y>> secondSourceFunction) { - addSource(source, new DoubleCombinatorObserver<>(this, secondSourceFunction)); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/DoubleCombinatorObserver.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/DoubleCombinatorObserver.java deleted file mode 100644 index 18c2affbb..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/DoubleCombinatorObserver.java +++ /dev/null @@ -1,45 +0,0 @@ -package it.niedermann.android.reactivelivedata.combinator; - -import androidx.annotation.NonNull; -import androidx.arch.core.util.Function; -import androidx.core.util.Pair; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MediatorLiveData; -import androidx.lifecycle.Observer; - -class DoubleCombinatorObserver<T, Y> implements Observer<T> { - private final MediatorLiveData<Pair<T, Y>> mediator; - private final Function<T, LiveData<Y>> secondSourceFunction; - private T value1; - private Y value2; - - private LiveData<Y> secondSource; - - private boolean value1emitted = false; - private boolean value2emitted = false; - - public DoubleCombinatorObserver(@NonNull MediatorLiveData<Pair<T, Y>> mediator, @NonNull Function<T, LiveData<Y>> secondSourceFunction) { - this.mediator = mediator; - this.secondSourceFunction = secondSourceFunction; - } - - @Override - public void onChanged(T emittedValue1) { - value1 = emittedValue1; - value1emitted = true; - if (value2emitted) { - mediator.setValue(new Pair<>(value1, value2)); - } - - if (secondSource == null) { - secondSource = secondSourceFunction.apply(emittedValue1); - mediator.addSource(secondSource, val2 -> { - value2 = val2; - value2emitted = true; - if (value1emitted) { - mediator.setValue(new Pair<>(value1, value2)); - } - }); - } - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/TripleCombinatorLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/TripleCombinatorLiveData.java deleted file mode 100644 index de0353c42..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/TripleCombinatorLiveData.java +++ /dev/null @@ -1,28 +0,0 @@ -package it.niedermann.android.reactivelivedata.combinator; - -import androidx.annotation.NonNull; -import androidx.arch.core.util.Function; -import androidx.core.util.Supplier; -import androidx.lifecycle.LiveData; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; -import kotlin.Triple; - -public class TripleCombinatorLiveData<T, Y, Z> extends ReactiveLiveData<Triple<T, Y, Z>> { - - public TripleCombinatorLiveData(@NonNull LiveData<T> source, @NonNull Supplier<LiveData<Y>> secondSourceSupplier, @NonNull Supplier<LiveData<Z>> thirdSourceSupplier) { - this(source, val -> secondSourceSupplier.get(), val -> thirdSourceSupplier.get()); - } - - public TripleCombinatorLiveData(@NonNull LiveData<T> source, @NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Supplier<LiveData<Z>> thirdSourceSupplier) { - this(source, secondSourceFunction, val -> thirdSourceSupplier.get()); - } - - public TripleCombinatorLiveData(@NonNull LiveData<T> source, @NonNull Supplier<LiveData<Y>> secondSourceSupplier, @NonNull Function<T, LiveData<Z>> thirdSourceFunction) { - this(source, val -> secondSourceSupplier.get(), thirdSourceFunction); - } - - public TripleCombinatorLiveData(@NonNull LiveData<T> source, @NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Function<T, LiveData<Z>> thirdSourceFunction) { - addSource(source, new TripleCombinatorObserver<>(this, secondSourceFunction, thirdSourceFunction)); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/TripleCombinatorObserver.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/TripleCombinatorObserver.java deleted file mode 100644 index f146fa9cc..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/combinator/TripleCombinatorObserver.java +++ /dev/null @@ -1,62 +0,0 @@ -package it.niedermann.android.reactivelivedata.combinator; - -import androidx.annotation.NonNull; -import androidx.arch.core.util.Function; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MediatorLiveData; -import androidx.lifecycle.Observer; - -import kotlin.Triple; - -class TripleCombinatorObserver<T, Y, Z> implements Observer<T> { - private final MediatorLiveData<Triple<T, Y, Z>> mediator; - private final Function<T, LiveData<Y>> secondSourceFunction; - private final Function<T, LiveData<Z>> thirdSourceFunction; - private T value1; - private Y value2; - private Z value3; - - private LiveData<Y> secondSource; - private LiveData<Z> thirdSource; - - private boolean value1emitted = false; - private boolean value2emitted = false; - private boolean value3emitted = false; - - public TripleCombinatorObserver(@NonNull MediatorLiveData<Triple<T, Y, Z>> mediator, @NonNull Function<T, LiveData<Y>> secondSourceFunction, @NonNull Function<T, LiveData<Z>> thirdSourceFunction) { - this.mediator = mediator; - this.secondSourceFunction = secondSourceFunction; - this.thirdSourceFunction = thirdSourceFunction; - } - - @Override - public void onChanged(T emittedValue1) { - value1 = emittedValue1; - value1emitted = true; - if (value2emitted && value3emitted) { - mediator.setValue(new Triple<>(value1, value2, value3)); - } - - if (secondSource == null) { - secondSource = secondSourceFunction.apply(emittedValue1); - mediator.addSource(secondSource, val2 -> { - value2 = val2; - value2emitted = true; - if (value1emitted && value3emitted) { - mediator.setValue(new Triple<>(value1, value2, value3)); - } - }); - } - - if (thirdSource == null) { - thirdSource = thirdSourceFunction.apply(emittedValue1); - mediator.addSource(thirdSource, val3 -> { - value3 = val3; - value3emitted = true; - if (value1emitted && value2emitted) { - mediator.setValue(new Triple<>(value1, value2, value3)); - } - }); - } - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/debounce/DebounceLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/debounce/DebounceLiveData.java deleted file mode 100644 index 22a67d59a..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/debounce/DebounceLiveData.java +++ /dev/null @@ -1,19 +0,0 @@ -package it.niedermann.android.reactivelivedata.debounce; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; - -import java.time.temporal.ChronoUnit; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; - -public class DebounceLiveData<T> extends ReactiveLiveData<T> { - - public DebounceLiveData(@NonNull LiveData<T> source, long timeout) { - this(source, timeout, ChronoUnit.MILLIS); - } - - public DebounceLiveData(@NonNull LiveData<T> source, long timeout, @NonNull ChronoUnit timeUnit) { - addSource(source, new DebounceObserver<>(this, timeout, timeUnit)); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/debounce/DebounceObserver.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/debounce/DebounceObserver.java deleted file mode 100644 index 4d18cfa0e..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/debounce/DebounceObserver.java +++ /dev/null @@ -1,79 +0,0 @@ -package it.niedermann.android.reactivelivedata.debounce; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MediatorLiveData; -import androidx.lifecycle.Observer; - -import java.time.Duration; -import java.time.Instant; -import java.time.temporal.ChronoUnit; -import java.util.Objects; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; - -class DebounceObserver<T> implements Observer<T> { - private final MediatorLiveData<T> mediator; - private final ExecutorService executor = Executors.newSingleThreadExecutor(); - private final long timeout; - private final ChronoUnit timeUnit; - private T lastEmittedValue = null; - private Instant lastEmit = Instant.now(); - private boolean firstEmit = true; - private Future<?> scheduledRecheck; - - public DebounceObserver(@NonNull MediatorLiveData<T> mediator, long timeout, @NonNull ChronoUnit timeUnit) { - this.mediator = mediator; - this.timeout = timeout; - this.timeUnit = timeUnit; - } - - @Override - public void onChanged(T value) { - final var now = Instant.now(); - - if (firstEmit) { - firstEmit = false; - emitValue(value, now); - } else { - if (lastEmit.isBefore(now.minus(timeout, timeUnit))) { - emitValue(value, now); - } else { - scheduleRecheck(value, getRemainingTimeToNextTimeout(now, lastEmit)); - } - } - } - - private void emitValue(T value, @NonNull Instant lastEmit) { - cancelScheduledRecheck(); - mediator.postValue(value); - this.lastEmit = lastEmit; - } - - private Duration getRemainingTimeToNextTimeout(@NonNull Instant now, @NonNull Instant lastEmit) { - final var millisSinceLastEmit = now.toEpochMilli() - lastEmit.toEpochMilli(); - final var millisToNextEmit = Duration.of(timeout, timeUnit).toMillis() - millisSinceLastEmit; - return Duration.ofMillis(millisToNextEmit); - } - - private void cancelScheduledRecheck() { - if (scheduledRecheck != null) { - scheduledRecheck.cancel(true); - } - } - - private synchronized void scheduleRecheck(T newValue, @NonNull Duration sleep) { - cancelScheduledRecheck(); - scheduledRecheck = executor.submit(() -> { - try { - Thread.sleep(sleep.toMillis()); - if (!Objects.equals(lastEmittedValue, newValue)) { - mediator.postValue(newValue); - lastEmittedValue = newValue; - } - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - }); - } -}
\ No newline at end of file diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/distinct/DistinctUntilChangedLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/distinct/DistinctUntilChangedLiveData.java deleted file mode 100644 index 6ec85ed85..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/distinct/DistinctUntilChangedLiveData.java +++ /dev/null @@ -1,14 +0,0 @@ -package it.niedermann.android.reactivelivedata.distinct; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.Transformations; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; - -public class DistinctUntilChangedLiveData<T> extends ReactiveLiveData<T> { - - public DistinctUntilChangedLiveData(@NonNull LiveData<T> source) { - super(Transformations.distinctUntilChanged(source)); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/filter/FilterLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/filter/FilterLiveData.java deleted file mode 100644 index fac27030f..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/filter/FilterLiveData.java +++ /dev/null @@ -1,23 +0,0 @@ -package it.niedermann.android.reactivelivedata.filter; - -import androidx.annotation.NonNull; -import androidx.core.util.Predicate; -import androidx.core.util.Supplier; -import androidx.lifecycle.LiveData; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; - -public class FilterLiveData<T> extends ReactiveLiveData<T> { - - public FilterLiveData(@NonNull LiveData<T> source, @NonNull Supplier<Boolean> supplier) { - this(source, val -> supplier.get()); - } - - public FilterLiveData(@NonNull LiveData<T> source, @NonNull Predicate<T> predicate) { - addSource(source, val -> { - if (predicate.test(val)) { - setValue(val); - } - }); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/flatmap/FlatMapLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/flatmap/FlatMapLiveData.java deleted file mode 100644 index 13593cb79..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/flatmap/FlatMapLiveData.java +++ /dev/null @@ -1,20 +0,0 @@ -package it.niedermann.android.reactivelivedata.flatmap; - -import androidx.annotation.NonNull; -import androidx.core.util.Supplier; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.Transformations; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; -import kotlin.jvm.functions.Function1; - -public class FlatMapLiveData<T, Y> extends ReactiveLiveData<Y> { - - public FlatMapLiveData(@NonNull LiveData<T> source, @NonNull Supplier<LiveData<Y>> switchMapSupplier) { - this(source, val -> switchMapSupplier.get()); - } - - public FlatMapLiveData(@NonNull LiveData<T> source, @NonNull Function1<T, LiveData<Y>> flatMapFunction) { - super(Transformations.switchMap(source, flatMapFunction)); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/map/MapLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/map/MapLiveData.java deleted file mode 100644 index a705b427b..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/map/MapLiveData.java +++ /dev/null @@ -1,21 +0,0 @@ -package it.niedermann.android.reactivelivedata.map; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.Transformations; - -import java.util.concurrent.ExecutorService; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; -import kotlin.jvm.functions.Function1; - -public class MapLiveData<T, Y> extends ReactiveLiveData<Y> { - - public MapLiveData(@NonNull LiveData<T> source, @NonNull Function1<T, Y> mapFunction) { - super(Transformations.map(source, mapFunction)); - } - - public MapLiveData(@NonNull LiveData<T> source, @NonNull Function1<T, Y> mapFunction, @NonNull ExecutorService executor) { - addSource(source, val -> executor.submit(() -> postValue(mapFunction.invoke(val)))); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/merge/MergeLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/merge/MergeLiveData.java deleted file mode 100644 index cc3c1ddd6..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/merge/MergeLiveData.java +++ /dev/null @@ -1,15 +0,0 @@ -package it.niedermann.android.reactivelivedata.merge; - -import androidx.annotation.NonNull; -import androidx.core.util.Supplier; -import androidx.lifecycle.LiveData; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; - -public class MergeLiveData<T> extends ReactiveLiveData<T> { - - public MergeLiveData(@NonNull LiveData<T> source, @NonNull Supplier<LiveData<T>> secondSource) { - addSource(source, this::setValue); - addSource(secondSource.get(), this::setValue); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/take/TakeLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/take/TakeLiveData.java deleted file mode 100644 index 0ca536db2..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/take/TakeLiveData.java +++ /dev/null @@ -1,13 +0,0 @@ -package it.niedermann.android.reactivelivedata.take; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; - -import it.niedermann.android.reactivelivedata.ReactiveLiveData; - -public class TakeLiveData<T> extends ReactiveLiveData<T> { - - public TakeLiveData(@NonNull LiveData<T> source, int limit) { - addSource(source, new TakeObserver<>(this, limit)); - } -} diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/take/TakeObserver.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/take/TakeObserver.java deleted file mode 100644 index 0f1f4e576..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/take/TakeObserver.java +++ /dev/null @@ -1,37 +0,0 @@ -package it.niedermann.android.reactivelivedata.take; - -import androidx.annotation.NonNull; -import androidx.lifecycle.MediatorLiveData; -import androidx.lifecycle.Observer; - -class TakeObserver<T> implements Observer<T> { - private final MediatorLiveData<T> mediator; - private final int limit; - private int counter = 0; - - public TakeObserver(@NonNull MediatorLiveData<T> mediator, int limit) { - if (limit == Integer.MAX_VALUE) { - throw new RuntimeException("limit must be lower than Integer.MAX_VALUE"); - } - - if (limit < 1) { - throw new RuntimeException("limit must be 1 or higher"); - } - - this.mediator = mediator; - this.limit = limit; - } - - @Override - public void onChanged(T value) { - if (counter < limit) { - mediator.setValue(value); - } - counter++; - - // Prevent integer overflow - if (counter == limit + 1) { - counter = limit; - } - } -}
\ No newline at end of file diff --git a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/tap/TapLiveData.java b/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/tap/TapLiveData.java deleted file mode 100644 index c9d08a98d..000000000 --- a/reactive-livedata/src/main/java/it/niedermann/android/reactivelivedata/tap/TapLiveData.java +++ /dev/null @@ -1,34 +0,0 @@ -package it.niedermann.android.reactivelivedata.tap; - -import androidx.annotation.NonNull; -import androidx.core.util.Consumer; -import androidx.lifecycle.LiveData; - -import java.util.concurrent.ExecutorService; - -import it.niedermann.android.reactivelivedata.map.MapLiveData; - -public class TapLiveData<T> extends MapLiveData<T, T> { - - public TapLiveData(@NonNull LiveData<T> source, @NonNull Runnable runnable) { - this(source, val -> runnable.run()); - } - - public TapLiveData(@NonNull LiveData<T> source, @NonNull Consumer<T> consumer) { - super(source, val -> { - consumer.accept(val); - return val; - }); - } - - public TapLiveData(@NonNull LiveData<T> source, @NonNull Runnable runnable, @NonNull ExecutorService executor) { - this(source, val -> runnable.run(), executor); - } - - public TapLiveData(@NonNull LiveData<T> source, @NonNull Consumer<T> consumer, @NonNull ExecutorService executor) { - super(source, val -> { - consumer.accept(val); - return val; - }, executor); - } -} |