[android] Introduce a (Mutable)LiveEvent for single-use LiveData

parent fbe375cc
Pipeline #3276 passed with stage
in 10 minutes and 34 seconds
......@@ -49,8 +49,8 @@ public class AddContactActivity extends BriarActivity implements
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(AddContactViewModel.class);
viewModel.getRemoteLinkEntered().observe(this, entered -> {
if (entered != null && entered) {
viewModel.getRemoteLinkEntered().observeEvent(this, entered -> {
if (entered) {
NicknameFragment f = new NicknameFragment();
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.DbException;
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.logging.Logger;
......@@ -35,8 +37,8 @@ public class AddContactViewModel extends AndroidViewModel {
private final MutableLiveData<String> handshakeLink =
new MutableLiveData<>();
private final MutableLiveData<Boolean> remoteLinkEntered =
new MutableLiveData<>();
private final MutableLiveEvent<Boolean> remoteLinkEntered =
new MutableLiveEvent<>();
private final MutableLiveData<Boolean> addContactResult =
new MutableLiveData<>();
@Nullable
......@@ -81,14 +83,13 @@ public class AddContactViewModel extends AndroidViewModel {
return link != null && LINK_REGEX.matcher(link).find();
}
LiveData<Boolean> getRemoteLinkEntered() {
LiveEvent<Boolean> getRemoteLinkEntered() {
return remoteLinkEntered;
}
void onRemoteLinkEntered() {
if (remoteHandshakeLink == null) throw new IllegalStateException();
remoteLinkEntered.setValue(true);
remoteLinkEntered.postValue(false); // reset, so we can navigate back
remoteLinkEntered.setEvent(true);
}
void addContact(String nickname) {
......
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);
}
}
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));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment