Commit 33fdca4a authored by akwizgran's avatar akwizgran

Merge branch 'live-event' into 'master'

Migrate existing uses of event-like LiveData to LiveEvent

See merge request !1090
parents 6f0ab8b6 5b63eab3
Pipeline #3287 passed with stage
in 11 minutes and 18 seconds
......@@ -363,7 +363,7 @@ public class ConversationActivity extends BriarActivity
if (enable != null && enable) {
menu.findItem(R.id.action_introduction).setEnabled(true);
// show introduction onboarding, if needed
observeOnce(viewModel.showIntroductionOnboarding(), this,
viewModel.showIntroductionOnboarding().observeEvent(this,
this::showIntroductionOnboarding);
}
});
......@@ -474,9 +474,8 @@ public class ConversationActivity extends BriarActivity
if (revision == adapter.getRevision()) {
adapter.incrementRevision();
textInputView.setReady(true);
// start observing onboarding after enabling (only once, because
// we only update this when an onboarding should be shown)
observeOnce(viewModel.showImageOnboarding(), this,
// start observing onboarding after enabling
viewModel.showImageOnboarding().observeEvent(this,
this::showImageOnboarding);
List<ConversationItem> items = createItems(headers);
adapter.addAll(items);
......@@ -499,7 +498,6 @@ public class ConversationActivity extends BriarActivity
* <p>
* Attention: Call this only after contactName has been initialized.
*/
@SuppressWarnings("ConstantConditions")
private List<ConversationItem> createItems(
Collection<ConversationMessageHeader> headers) {
List<ConversationItem> items = new ArrayList<>(headers.size());
......@@ -715,8 +713,8 @@ public class ConversationActivity extends BriarActivity
});
}
private void showImageOnboarding(@Nullable Boolean show) {
if (show == null || !show) return;
private void showImageOnboarding(Boolean show) {
if (!show) return;
if (SDK_INT >= 21) {
// show onboarding only after the enter transition has ended
// otherwise the tap target animation won't play
......
......@@ -28,6 +28,8 @@ import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.android.util.UiUtils;
import org.briarproject.briar.android.viewmodel.LiveEvent;
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
import org.briarproject.briar.api.messaging.Attachment;
import org.briarproject.briar.api.messaging.AttachmentHeader;
import org.briarproject.briar.api.messaging.MessagingManager;
......@@ -87,10 +89,10 @@ public class ConversationViewModel extends AndroidViewModel {
Transformations.map(contact, UiUtils::getContactDisplayName);
private final MutableLiveData<Boolean> imageSupport =
new MutableLiveData<>();
private final MutableLiveData<Boolean> showImageOnboarding =
new MutableLiveData<>();
private final MutableLiveData<Boolean> showIntroductionOnboarding =
new MutableLiveData<>();
private final MutableLiveEvent<Boolean> showImageOnboarding =
new MutableLiveEvent<>();
private final MutableLiveEvent<Boolean> showIntroductionOnboarding =
new MutableLiveEvent<>();
private final MutableLiveData<Boolean> showIntroductionAction =
new MutableLiveData<>();
private final MutableLiveData<Boolean> contactDeleted =
......@@ -212,32 +214,30 @@ public class ConversationViewModel extends AndroidViewModel {
if (imagesSupported &&
settings.getBoolean(SHOW_ONBOARDING_IMAGE, true)) {
// check if we should show onboarding, only if images are supported
showImageOnboarding.postValue(true);
showImageOnboarding.postEvent(true);
// allow observer to stop listening for changes
showIntroductionOnboarding.postValue(false);
showIntroductionOnboarding.postEvent(false);
} else {
// allow observer to stop listening for changes
showImageOnboarding.postValue(false);
showImageOnboarding.postEvent(false);
// we only show one onboarding dialog at a time
if (introductionSupported &&
settings.getBoolean(SHOW_ONBOARDING_INTRODUCTION, true)) {
showIntroductionOnboarding.postValue(true);
showIntroductionOnboarding.postEvent(true);
} else {
// allow observer to stop listening for changes
showIntroductionOnboarding.postValue(false);
showIntroductionOnboarding.postEvent(false);
}
}
}
@UiThread
void onImageOnboardingSeen() {
showImageOnboarding.setValue(false);
onOnboardingSeen(SHOW_ONBOARDING_IMAGE);
}
@UiThread
void onIntroductionOnboardingSeen() {
showIntroductionOnboarding.setValue(false);
onOnboardingSeen(SHOW_ONBOARDING_INTRODUCTION);
}
......@@ -365,11 +365,11 @@ public class ConversationViewModel extends AndroidViewModel {
return imageSupport;
}
LiveData<Boolean> showImageOnboarding() {
LiveEvent<Boolean> showImageOnboarding() {
return showImageOnboarding;
}
LiveData<Boolean> showIntroductionOnboarding() {
LiveEvent<Boolean> showIntroductionOnboarding() {
return showIntroductionOnboarding;
}
......
......@@ -100,7 +100,8 @@ public class ImageActivity extends BriarActivity
// get View Model
viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ImageViewModel.class);
viewModel.getSaveState().observe(this, this::onImageSaveStateChanged);
viewModel.getSaveState().observeEvent(this,
this::onImageSaveStateChanged);
// inflate layout
setContentView(R.layout.activity_image);
......@@ -141,7 +142,8 @@ public class ImageActivity extends BriarActivity
viewPager.setCurrentItem(position);
if (SDK_INT >= 16) {
viewModel.getOnImageClicked().observe(this, this::onImageClicked);
viewModel.getOnImageClicked()
.observeEvent(this, this::onImageClicked);
window.getDecorView().setSystemUiVisibility(UI_FLAGS_DEFAULT);
}
}
......@@ -222,7 +224,6 @@ public class ImageActivity extends BriarActivity
private void onImageClicked(@Nullable Boolean clicked) {
if (clicked != null && clicked) {
toggleSystemUi();
viewModel.onOnImageClickSeen();
}
}
......@@ -312,7 +313,6 @@ public class ImageActivity extends BriarActivity
.setBackgroundColor(colorRes)
.make(layout, stringRes, LENGTH_LONG)
.show();
viewModel.onSaveStateSeen();
}
AttachmentItem getVisibleAttachment() {
......
......@@ -2,8 +2,6 @@ package org.briarproject.briar.android.conversation;
import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.Nullable;
......@@ -15,6 +13,8 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.android.viewmodel.LiveEvent;
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
import org.briarproject.briar.api.messaging.Attachment;
import org.briarproject.briar.api.messaging.MessagingManager;
......@@ -53,9 +53,10 @@ public class ImageViewModel extends AndroidViewModel {
/**
* true means there was an error saving the image, false if image was saved.
*/
private final MutableLiveData<Boolean> saveState = new MutableLiveData<>();
private final MutableLiveData<Boolean> imageClicked =
new MutableLiveData<>();
private final MutableLiveEvent<Boolean> saveState =
new MutableLiveEvent<>();
private final MutableLiveEvent<Boolean> imageClicked =
new MutableLiveEvent<>();
private int toolbarTop, toolbarBottom;
@Inject
......@@ -70,24 +71,17 @@ public class ImageViewModel extends AndroidViewModel {
}
void clickImage() {
imageClicked.setValue(true);
imageClicked.setEvent(true);
}
/**
* A LiveData that is true if the image was clicked,
* A LiveEvent that is true if the image was clicked,
* false if it wasn't.
*
* Call {@link #onOnImageClickSeen()} after consuming an update.
*/
LiveData<Boolean> getOnImageClicked() {
LiveEvent<Boolean> getOnImageClicked() {
return imageClicked;
}
@UiThread
void onOnImageClickSeen() {
imageClicked.setValue(false);
}
void setToolbarPosition(int top, int bottom) {
toolbarTop = top;
toolbarBottom = bottom;
......@@ -111,26 +105,18 @@ public class ImageViewModel extends AndroidViewModel {
/**
* A LiveData that is true if there was an error
* and false if the image was saved.
* It can be null otherwise, if no image was saved recently.
*
* Call {@link #onSaveStateSeen()} after consuming an update.
*/
LiveData<Boolean> getSaveState() {
LiveEvent<Boolean> getSaveState() {
return saveState;
}
@UiThread
void onSaveStateSeen() {
saveState.setValue(null);
}
/**
* Saves the attachment to a writeable {@link Uri}.
*/
@UiThread
void saveImage(AttachmentItem attachment, @Nullable Uri uri) {
if (uri == null) {
saveState.setValue(true);
saveState.setEvent(true);
} else {
saveImage(attachment, () -> getOutputStream(uri), null);
}
......@@ -155,7 +141,7 @@ public class ImageViewModel extends AndroidViewModel {
copyImageFromDb(a, osp, afterCopy);
} catch (DbException e) {
logException(LOG, WARNING, e);
saveState.postValue(true);
saveState.postEvent(true);
}
});
}
......@@ -168,10 +154,10 @@ public class ImageViewModel extends AndroidViewModel {
OutputStream os = osp.getOutputStream();
copyAndClose(is, os);
if (afterCopy != null) afterCopy.run();
saveState.postValue(false);
saveState.postEvent(false);
} catch (IOException e) {
logException(LOG, WARNING, e);
saveState.postValue(true);
saveState.postEvent(true);
}
});
}
......
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