1 /*
2  * Copyright 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.work.impl.utils;
18 
19 import android.arch.core.util.Function;
20 import android.arch.lifecycle.LiveData;
21 import android.arch.lifecycle.MediatorLiveData;
22 import android.arch.lifecycle.Observer;
23 import android.support.annotation.NonNull;
24 import android.support.annotation.Nullable;
25 import android.support.annotation.RestrictTo;
26 
27 import androidx.work.impl.utils.taskexecutor.WorkManagerTaskExecutor;
28 
29 /**
30  * Utility methods for {@link LiveData}.
31  *
32  * @hide
33  */
34 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
35 public class LiveDataUtils {
36 
37     /**
38      * Creates a new {@link LiveData} object that maps the values of {@code inputLiveData} using
39      * {@code mappingMethod} on a background thread, but only triggers its observers when the mapped
40      * values actually change.
41      *
42      * @param inputLiveData An input {@link LiveData}
43      * @param mappingMethod A {@link Function} that maps input of type {@code In} to output of type
44      *                      {@code Out}
45      * @param <In> The type of data for {@code inputLiveData}
46      * @param <Out> The type of data to output
47      * @return A new {@link LiveData} of type {@code Out}
48      */
dedupedMappedLiveDataFor( @onNull LiveData<In> inputLiveData, @NonNull final Function<In, Out> mappingMethod)49     public static <In, Out> LiveData<Out> dedupedMappedLiveDataFor(
50             @NonNull LiveData<In> inputLiveData,
51             @NonNull final Function<In, Out> mappingMethod) {
52         final MediatorLiveData<Out> outputLiveData = new MediatorLiveData<>();
53         outputLiveData.addSource(inputLiveData, new Observer<In>() {
54             @Override
55             public void onChanged(@Nullable final In input) {
56                 WorkManagerTaskExecutor.getInstance().executeOnBackgroundThread(new Runnable() {
57                     @Override
58                     public void run() {
59                         synchronized (outputLiveData) {
60                             Out newOutput = mappingMethod.apply(input);
61                             Out previousOutput = outputLiveData.getValue();
62                             if (previousOutput == null && newOutput != null) {
63                                 outputLiveData.postValue(newOutput);
64                             } else if (
65                                     previousOutput != null && !previousOutput.equals(newOutput)) {
66                                 outputLiveData.postValue(newOutput);
67                             }
68                         }
69                     }
70                 });
71             }
72         });
73         return outputLiveData;
74     }
75 
LiveDataUtils()76     private LiveDataUtils() {
77     }
78 }
79