@Test public void testDistinctUntilChanged_dedupesValues() { MutableLiveData<String> originalLiveData = new MutableLiveData<>(); LiveData<String> dedupedLiveData = Transformations.distinctUntilChanged(originalLiveData); assertThat(dedupedLiveData.getValue(), is(nullValue())); CountingObserver<String> observer = new CountingObserver<>(); dedupedLiveData.observe(mOwner, observer); assertThat(observer.mTimesUpdated, is(0)); String value = "new value"; originalLiveData.setValue(value); assertThat(dedupedLiveData.getValue(), is(value)); assertThat(observer.mTimesUpdated, is(1)); originalLiveData.setValue(value); assertThat(dedupedLiveData.getValue(), is(value)); assertThat(observer.mTimesUpdated, is(1)); String newerValue = "newer value"; originalLiveData.setValue(newerValue); assertThat(dedupedLiveData.getValue(), is(newerValue)); assertThat(observer.mTimesUpdated, is(2)); dedupedLiveData.removeObservers(mOwner); }
@MainThread @NonNull public static <X> LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source) { final MediatorLiveData<X> outputLiveData = new MediatorLiveData<>(); outputLiveData.addSource(source, new Observer<X>() { boolean mFirstTime = true; @Override public void onChanged(X currentValue) { final X previousValue = outputLiveData.getValue(); if (mFirstTime || (previousValue == null && currentValue != null) || (previousValue != null && !previousValue.equals(currentValue))) { mFirstTime = false; outputLiveData.setValue(currentValue); } } }); return outputLiveData; }
Transformations { @MainThread @NonNull public static <X> LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source) { final MediatorLiveData<X> outputLiveData = new MediatorLiveData<>(); outputLiveData.addSource(source, new Observer<X>() { boolean mFirstTime = true; @Override public void onChanged(X currentValue) { final X previousValue = outputLiveData.getValue(); if (mFirstTime || (previousValue == null && currentValue != null) || (previousValue != null && !previousValue.equals(currentValue))) { mFirstTime = false; outputLiveData.setValue(currentValue); } } }); return outputLiveData; } }
Transformations { @MainThread @NonNull public static <X> LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source) { final MediatorLiveData<X> outputLiveData = new MediatorLiveData<>(); outputLiveData.addSource(source, new Observer<X>() { boolean mFirstTime = true; @Override public void onChanged(X currentValue) { final X previousValue = outputLiveData.getValue(); if (mFirstTime || (previousValue == null && currentValue != null) || (previousValue != null && !previousValue.equals(currentValue))) { mFirstTime = false; outputLiveData.setValue(currentValue); } } }); return outputLiveData; } private  Transformations(); }
Transformations { @MainThread @NonNull public static <X> LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source) { final MediatorLiveData<X> outputLiveData = new MediatorLiveData<>(); outputLiveData.addSource(source, new Observer<X>() { boolean mFirstTime = true; @Override public void onChanged(X currentValue) { final X previousValue = outputLiveData.getValue(); if (mFirstTime || (previousValue == null && currentValue != null) || (previousValue != null && !previousValue.equals(currentValue))) { mFirstTime = false; outputLiveData.setValue(currentValue); } } }); return outputLiveData; } private  Transformations(); @MainThread @NonNull static LiveData<Y> map(
            @NonNull LiveData<X> source,
            @NonNull final Function<X, Y> mapFunction); @MainThread @NonNull static LiveData<Y> switchMap(
            @NonNull LiveData<X> source,
            @NonNull final Function<X, LiveData<Y>> switchMapFunction); @MainThread @NonNull static LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source); }
Transformations { @MainThread @NonNull public static <X> LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source) { final MediatorLiveData<X> outputLiveData = new MediatorLiveData<>(); outputLiveData.addSource(source, new Observer<X>() { boolean mFirstTime = true; @Override public void onChanged(X currentValue) { final X previousValue = outputLiveData.getValue(); if (mFirstTime || (previousValue == null && currentValue != null) || (previousValue != null && !previousValue.equals(currentValue))) { mFirstTime = false; outputLiveData.setValue(currentValue); } } }); return outputLiveData; } private  Transformations(); @MainThread @NonNull static LiveData<Y> map(
            @NonNull LiveData<X> source,
            @NonNull final Function<X, Y> mapFunction); @MainThread @NonNull static LiveData<Y> switchMap(
            @NonNull LiveData<X> source,
            @NonNull final Function<X, LiveData<Y>> switchMapFunction); @MainThread @NonNull static LiveData<X> distinctUntilChanged(@NonNull LiveData<X> source);  }