Updated AndroidX lifecycle

This commit is contained in:
M66B
2023-03-09 07:43:39 +01:00
parent b94054dce2
commit 1ca1bbe42c
9 changed files with 408 additions and 408 deletions

View File

@@ -38,25 +38,25 @@ import java.util.Map;
* is called for either of them, we set a new value in {@code liveDataMerger}.
*
* <pre>
* LiveData<Integer> liveData1 = ...;
* LiveData<Integer> liveData2 = ...;
* LiveData&lt;Integer&gt; liveData1 = ...;
* LiveData&lt;Integer&gt; liveData2 = ...;
*
* MediatorLiveData<Integer> liveDataMerger = new MediatorLiveData<>();
* liveDataMerger.addSource(liveData1, value -> liveDataMerger.setValue(value));
* liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value));
* MediatorLiveData&lt;Integer&gt; liveDataMerger = new MediatorLiveData&lt;&gt;();
* liveDataMerger.addSource(liveData1, value -&gt; liveDataMerger.setValue(value));
* liveDataMerger.addSource(liveData2, value -&gt; liveDataMerger.setValue(value));
* </pre>
* <p>
* Let's consider that we only want 10 values emitted by {@code liveData1}, to be
* merged in the {@code liveDataMerger}. Then, after 10 values, we can stop listening to {@code
* liveData1} and remove it as a source.
* <pre>
* liveDataMerger.addSource(liveData1, new Observer<Integer>() {
* liveDataMerger.addSource(liveData1, new Observer&lt;Integer&gt;() {
* private int count = 1;
*
* {@literal @}Override public void onChanged(@Nullable Integer s) {
* count++;
* liveDataMerger.setValue(s);
* if (count > 10) {
* if (count &gt; 10) {
* liveDataMerger.removeSource(liveData1);
* }
* }
@@ -70,8 +70,24 @@ public class MediatorLiveData<T> extends MutableLiveData<T> {
private SafeIterableMap<LiveData<?>, Source<?>> mSources = new SafeIterableMap<>();
/**
* Starts to listen the given {@code source} LiveData, {@code onChanged} observer will be called
* when {@code source} value was changed.
* Creates a MediatorLiveData with no value assigned to it.
*/
public MediatorLiveData() {
super();
}
/**
* Creates a MediatorLiveData initialized with the given {@code value}.
*
* @param value initial value
*/
public MediatorLiveData(T value) {
super(value);
}
/**
* Starts to listen to the given {@code source} LiveData, {@code onChanged} observer will be
* called when {@code source} value was changed.
* <p>
* {@code onChanged} callback will be called only when this {@code MediatorLiveData} is active.
* <p> If the given LiveData is already added as a source but with a different Observer,