mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
[android] Introduce a (Mutable)LiveEvent for single-use LiveData
This commit is contained in:
@@ -49,8 +49,8 @@ public class AddContactActivity extends BriarActivity implements
|
|||||||
|
|
||||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||||
.get(AddContactViewModel.class);
|
.get(AddContactViewModel.class);
|
||||||
viewModel.getRemoteLinkEntered().observe(this, entered -> {
|
viewModel.getRemoteLinkEntered().observeEvent(this, entered -> {
|
||||||
if (entered != null && entered) {
|
if (entered) {
|
||||||
NicknameFragment f = new NicknameFragment();
|
NicknameFragment f = new NicknameFragment();
|
||||||
showNextFragment(f);
|
showNextFragment(f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
|||||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||||
|
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -35,8 +37,8 @@ public class AddContactViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
private final MutableLiveData<String> handshakeLink =
|
private final MutableLiveData<String> handshakeLink =
|
||||||
new MutableLiveData<>();
|
new MutableLiveData<>();
|
||||||
private final MutableLiveData<Boolean> remoteLinkEntered =
|
private final MutableLiveEvent<Boolean> remoteLinkEntered =
|
||||||
new MutableLiveData<>();
|
new MutableLiveEvent<>();
|
||||||
private final MutableLiveData<Boolean> addContactResult =
|
private final MutableLiveData<Boolean> addContactResult =
|
||||||
new MutableLiveData<>();
|
new MutableLiveData<>();
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -81,14 +83,13 @@ public class AddContactViewModel extends AndroidViewModel {
|
|||||||
return link != null && LINK_REGEX.matcher(link).find();
|
return link != null && LINK_REGEX.matcher(link).find();
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<Boolean> getRemoteLinkEntered() {
|
LiveEvent<Boolean> getRemoteLinkEntered() {
|
||||||
return remoteLinkEntered;
|
return remoteLinkEntered;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRemoteLinkEntered() {
|
void onRemoteLinkEntered() {
|
||||||
if (remoteHandshakeLink == null) throw new IllegalStateException();
|
if (remoteHandshakeLink == null) throw new IllegalStateException();
|
||||||
remoteLinkEntered.setValue(true);
|
remoteLinkEntered.setEvent(true);
|
||||||
remoteLinkEntered.postValue(false); // reset, so we can navigate back
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void addContact(String nickname) {
|
void addContact(String nickname) {
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package org.briarproject.briar.android.viewmodel;
|
||||||
|
|
||||||
|
import android.arch.lifecycle.LifecycleOwner;
|
||||||
|
import android.arch.lifecycle.LiveData;
|
||||||
|
import android.arch.lifecycle.Observer;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class LiveEvent<T> extends LiveData<LiveEvent.ConsumableEvent<T>> {
|
||||||
|
|
||||||
|
public void observeEvent(LifecycleOwner owner,
|
||||||
|
LiveEventHandler<T> handler) {
|
||||||
|
LiveEventObserver<T> observer = new LiveEventObserver<>(handler);
|
||||||
|
super.observe(owner, observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ConsumableEvent<T> {
|
||||||
|
private final T content;
|
||||||
|
private boolean consumed = false;
|
||||||
|
|
||||||
|
public ConsumableEvent(T content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public T getContentIfNotConsumed() {
|
||||||
|
if (consumed) return null;
|
||||||
|
else {
|
||||||
|
consumed = true;
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
public static class LiveEventObserver<T>
|
||||||
|
implements Observer<ConsumableEvent<T>> {
|
||||||
|
private final LiveEventHandler<T> handler;
|
||||||
|
|
||||||
|
public LiveEventObserver(LiveEventHandler<T> handler) {
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged(@Nullable ConsumableEvent<T> consumableEvent) {
|
||||||
|
if (consumableEvent != null) {
|
||||||
|
T content = consumableEvent.getContentIfNotConsumed();
|
||||||
|
if (content != null) handler.onEventUnconsumedContent(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface LiveEventHandler<T> {
|
||||||
|
void onEventUnconsumedContent(T t);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.briarproject.briar.android.viewmodel;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class MutableLiveEvent<T> extends LiveEvent<T> {
|
||||||
|
|
||||||
|
public void postEvent(T value) {
|
||||||
|
super.postValue(new ConsumableEvent<>(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEvent(T value) {
|
||||||
|
super.setValue(new ConsumableEvent<>(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user