That fuck shit the fascists are using
1/*
2 * Copyright 2017 Google Inc.
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
17package org.tm.archive.util;
18
19import androidx.annotation.MainThread;
20import androidx.annotation.NonNull;
21import androidx.annotation.Nullable;
22import androidx.lifecycle.LifecycleOwner;
23import androidx.lifecycle.MutableLiveData;
24import androidx.lifecycle.Observer;
25
26import org.signal.core.util.logging.Log;
27
28import java.util.concurrent.atomic.AtomicBoolean;
29
30/**
31 * A lifecycle-aware observable that sends only new updates after subscription, used for events like
32 * navigation and Snackbar messages.
33 * <p>
34 * This avoids a common problem with events: on configuration change (like rotation) an update
35 * can be emitted if the observer is active. This LiveData only calls the observable if there's an
36 * explicit call to setValue() or call().
37 * <p>
38 * Note that only one observer is going to be notified of changes.
39 *
40 * @deprecated Use a PublishSubject or PublishProcessor instead.
41 */
42@Deprecated
43public class SingleLiveEvent<T> extends MutableLiveData<T> {
44
45 private static final String TAG = Log.tag(SingleLiveEvent.class);
46
47 private final AtomicBoolean mPending = new AtomicBoolean(false);
48
49 @MainThread
50 public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer<? super T> observer) {
51 if (hasActiveObservers()) {
52 Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
53 }
54
55 // Observe the internal MutableLiveData
56 super.observe(owner, t -> {
57 if (mPending.compareAndSet(true, false)) {
58 observer.onChanged(t);
59 }
60 });
61 }
62
63 @MainThread
64 public void setValue(@Nullable T t) {
65 mPending.set(true);
66 super.setValue(t);
67 }
68
69 /**
70 * Used for cases where T is Void, to make calls cleaner.
71 */
72 @MainThread
73 public void call() {
74 setValue(null);
75 }
76}