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