Fix button flicker when selecting single media item.

This commit is contained in:
Greyson Parrelli 2019-03-19 16:11:46 -07:00
parent 2f8a7fa296
commit 932e7b4af5
7 changed files with 74 additions and 52 deletions

View File

@ -23,7 +23,8 @@
android:orientation="horizontal" android:orientation="horizontal"
android:background="@drawable/media_count_button_background" android:background="@drawable/media_count_button_background"
android:elevation="4dp" android:elevation="4dp"
tools:parentTag="android.widget.LinearLayout"> android:visibility="gone"
tools:visibility="visible">
<TextView <TextView
android:id="@+id/mediasend_count_button_text" android:id="@+id/mediasend_count_button_text"

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title=""
android:id="@+id/mediapicker_menu_end"
android:visible="true"
android:icon="@drawable/ic_create_album_filled_32"
app:showAsAction="always" />
</menu>

View File

@ -128,6 +128,7 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
if (maxSelection > 1) { if (maxSelection > 1) {
itemView.setOnLongClickListener(v -> { itemView.setOnLongClickListener(v -> {
selected.add(media); selected.add(media);
eventListener.onMediaSelectionStarted();
eventListener.onMediaSelectionChanged(new ArrayList<>(selected)); eventListener.onMediaSelectionChanged(new ArrayList<>(selected));
return true; return true;
}); });
@ -137,6 +138,7 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
selectOn.setVisibility(View.VISIBLE); selectOn.setVisibility(View.VISIBLE);
selectOverlay.setVisibility(View.VISIBLE); selectOverlay.setVisibility(View.VISIBLE);
selectOrder.setText(String.valueOf(selected.indexOf(media) + 1)); selectOrder.setText(String.valueOf(selected.indexOf(media) + 1));
itemView.setOnLongClickListener(null);
itemView.setOnClickListener(v -> { itemView.setOnClickListener(v -> {
selected.remove(media); selected.remove(media);
eventListener.onMediaSelectionChanged(new ArrayList<>(selected)); eventListener.onMediaSelectionChanged(new ArrayList<>(selected));
@ -145,6 +147,7 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
selectOff.setVisibility(View.VISIBLE); selectOff.setVisibility(View.VISIBLE);
selectOn.setVisibility(View.GONE); selectOn.setVisibility(View.GONE);
selectOverlay.setVisibility(View.GONE); selectOverlay.setVisibility(View.GONE);
itemView.setOnLongClickListener(null);
itemView.setOnClickListener(v -> { itemView.setOnClickListener(v -> {
if (selected.size() < maxSelection) { if (selected.size() < maxSelection) {
selected.add(media); selected.add(media);
@ -165,6 +168,7 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
interface EventListener { interface EventListener {
void onMediaChosen(@NonNull Media media); void onMediaChosen(@NonNull Media media);
void onMediaSelectionStarted();
void onMediaSelectionChanged(@NonNull List<Media> media); void onMediaSelectionChanged(@NonNull List<Media> media);
void onMediaSelectionOverflow(int maxSelection); void onMediaSelectionOverflow(int maxSelection);
} }

View File

@ -120,10 +120,10 @@ public class MediaPickerItemFragment extends Fragment implements MediaPickerItem
@Override @Override
public void onPrepareOptionsMenu(Menu menu) { public void onPrepareOptionsMenu(Menu menu) {
requireActivity().getMenuInflater().inflate(R.menu.mediapicker_default, menu);
if (viewModel.getCountButtonState().getValue() != null && viewModel.getCountButtonState().getValue().isVisible()) { if (viewModel.getCountButtonState().getValue() != null && viewModel.getCountButtonState().getValue().isVisible()) {
requireActivity().getMenuInflater().inflate(R.menu.mediapicker_multiselect, menu); menu.findItem(R.id.mediapicker_menu_add).setVisible(false);
} else {
requireActivity().getMenuInflater().inflate(R.menu.mediapicker_default, menu);
} }
} }
@ -146,8 +146,12 @@ public class MediaPickerItemFragment extends Fragment implements MediaPickerItem
@Override @Override
public void onMediaChosen(@NonNull Media media) { public void onMediaChosen(@NonNull Media media) {
viewModel.onSelectedMediaChanged(requireContext(), Collections.singletonList(media)); controller.onMediaSelected(media);
controller.onMediaSelected(bucketId); }
@Override
public void onMediaSelectionStarted() {
viewModel.onMultiSelectStarted();
} }
@Override @Override
@ -188,6 +192,6 @@ public class MediaPickerItemFragment extends Fragment implements MediaPickerItem
} }
public interface Controller { public interface Controller {
void onMediaSelected(@NonNull String bucketId); void onMediaSelected(@NonNull Media media);
} }
} }

View File

@ -220,7 +220,8 @@ public class MediaSendActivity extends PassphraseRequiredActionBarActivity imple
} }
@Override @Override
public void onMediaSelected(@NonNull String bucketId) { public void onMediaSelected(@NonNull Media media) {
viewModel.onSingleMediaSelected(this, media);
navigateToMediaSend(recipient, transport, dynamicLanguage.getCurrentLocale()); navigateToMediaSend(recipient, transport, dynamicLanguage.getCurrentLocale());
} }
@ -339,7 +340,9 @@ public class MediaSendActivity extends PassphraseRequiredActionBarActivity imple
if (buttonState.getCount() > 0) { if (buttonState.getCount() > 0) {
countButton.setOnClickListener(v -> navigateToMediaSend(recipient, transport, locale)); countButton.setOnClickListener(v -> navigateToMediaSend(recipient, transport, locale));
animateButtonTextChange(countButton); if (buttonState.isVisible()) {
animateButtonTextChange(countButton);
}
} else { } else {
countButton.setOnClickListener(null); countButton.setOnClickListener(null);
} }

View File

@ -241,7 +241,6 @@ public class MediaSendFragment extends Fragment implements ViewTreeObserver.OnGl
super.onStop(); super.onStop();
fragmentPagerAdapter.saveAllState(); fragmentPagerAdapter.saveAllState();
viewModel.saveDrawState(fragmentPagerAdapter.getSavedState()); viewModel.saveDrawState(fragmentPagerAdapter.getSavedState());
viewModel.onImageEditorEnded();
} }
@Override @Override

View File

@ -65,12 +65,12 @@ class MediaSendViewModel extends ViewModel {
this.cameraButtonVisibility = new MutableLiveData<>(); this.cameraButtonVisibility = new MutableLiveData<>();
this.error = new SingleLiveEvent<>(); this.error = new SingleLiveEvent<>();
this.savedDrawState = new HashMap<>(); this.savedDrawState = new HashMap<>();
this.countButtonVisibility = CountButtonState.Visibility.CONDITIONAL; this.countButtonVisibility = CountButtonState.Visibility.FORCED_OFF;
this.lastImageCapture = Optional.absent(); this.lastImageCapture = Optional.absent();
this.body = ""; this.body = "";
position.setValue(-1); position.setValue(-1);
countButtonState.setValue(new CountButtonState(0, CountButtonState.Visibility.CONDITIONAL)); countButtonState.setValue(new CountButtonState(0, countButtonVisibility));
cameraButtonVisibility.setValue(false); cameraButtonVisibility.setValue(false);
} }
@ -86,33 +86,54 @@ class MediaSendViewModel extends ViewModel {
void onSelectedMediaChanged(@NonNull Context context, @NonNull List<Media> newMedia) { void onSelectedMediaChanged(@NonNull Context context, @NonNull List<Media> newMedia) {
repository.getPopulatedMedia(context, newMedia, populatedMedia -> { repository.getPopulatedMedia(context, newMedia, populatedMedia -> {
List<Media> filteredMedia = getFilteredMedia(context, populatedMedia, mediaConstraints); Util.runOnMain(() -> {
if (filteredMedia.size() != newMedia.size()) { List<Media> filteredMedia = getFilteredMedia(context, populatedMedia, mediaConstraints);
error.postValue(Error.ITEM_TOO_LARGE);
} else if (filteredMedia.size() > maxSelection) {
filteredMedia = filteredMedia.subList(0, maxSelection);
error.postValue(Error.TOO_MANY_ITEMS);
}
if (filteredMedia.size() > 0) { if (filteredMedia.size() != newMedia.size()) {
String computedId = Stream.of(filteredMedia) error.setValue(Error.ITEM_TOO_LARGE);
.skip(1) } else if (filteredMedia.size() > maxSelection) {
.reduce(filteredMedia.get(0).getBucketId().or(Media.ALL_MEDIA_BUCKET_ID), (id, m) -> { filteredMedia = filteredMedia.subList(0, maxSelection);
if (Util.equals(id, m.getBucketId().or(Media.ALL_MEDIA_BUCKET_ID))) { error.setValue(Error.TOO_MANY_ITEMS);
return id; }
} else {
return Media.ALL_MEDIA_BUCKET_ID;
}
});
bucketId.postValue(computedId);
} else {
bucketId.postValue(Media.ALL_MEDIA_BUCKET_ID);
countButtonVisibility = CountButtonState.Visibility.CONDITIONAL;
}
selectedMedia.postValue(filteredMedia); if (filteredMedia.size() > 0) {
countButtonState.postValue(new CountButtonState(filteredMedia.size(), countButtonVisibility)); String computedId = Stream.of(filteredMedia)
.skip(1)
.reduce(filteredMedia.get(0).getBucketId().or(Media.ALL_MEDIA_BUCKET_ID), (id, m) -> {
if (Util.equals(id, m.getBucketId().or(Media.ALL_MEDIA_BUCKET_ID))) {
return id;
} else {
return Media.ALL_MEDIA_BUCKET_ID;
}
});
bucketId.setValue(computedId);
} else {
bucketId.setValue(Media.ALL_MEDIA_BUCKET_ID);
countButtonVisibility = CountButtonState.Visibility.CONDITIONAL;
}
selectedMedia.setValue(filteredMedia);
countButtonState.setValue(new CountButtonState(filteredMedia.size(), countButtonVisibility));
});
});
}
void onSingleMediaSelected(@NonNull Context context, @NonNull Media media) {
repository.getPopulatedMedia(context, Collections.singletonList(media), populatedMedia -> {
Util.runOnMain(() -> {
List<Media> filteredMedia = getFilteredMedia(context, populatedMedia, mediaConstraints);
if (filteredMedia.isEmpty()) {
error.setValue(Error.ITEM_TOO_LARGE);
}
countButtonVisibility = CountButtonState.Visibility.FORCED_OFF;
bucketId.setValue(filteredMedia.get(0).getBucketId().or(Media.ALL_MEDIA_BUCKET_ID));
selectedMedia.setValue(filteredMedia);
countButtonState.setValue(new CountButtonState(filteredMedia.size(), countButtonVisibility));
});
}); });
} }
@ -127,20 +148,21 @@ class MediaSendViewModel extends ViewModel {
cameraButtonVisibility.setValue(false); cameraButtonVisibility.setValue(false);
} }
void onImageEditorEnded() { void onCameraStarted() {
countButtonVisibility = CountButtonState.Visibility.CONDITIONAL; countButtonVisibility = CountButtonState.Visibility.CONDITIONAL;
countButtonState.setValue(new CountButtonState(getSelectedMediaOrDefault().size(), countButtonVisibility)); countButtonState.setValue(new CountButtonState(getSelectedMediaOrDefault().size(), countButtonVisibility));
}
void onCameraStarted() {
cameraButtonVisibility.setValue(false); cameraButtonVisibility.setValue(false);
} }
void onItemPickerStarted() { void onItemPickerStarted() {
countButtonVisibility = CountButtonState.Visibility.CONDITIONAL;
countButtonState.setValue(new CountButtonState(getSelectedMediaOrDefault().size(), countButtonVisibility));
cameraButtonVisibility.setValue(true); cameraButtonVisibility.setValue(true);
} }
void onFolderPickerStarted() { void onFolderPickerStarted() {
countButtonVisibility = CountButtonState.Visibility.CONDITIONAL;
countButtonState.setValue(new CountButtonState(getSelectedMediaOrDefault().size(), countButtonVisibility));
cameraButtonVisibility.setValue(true); cameraButtonVisibility.setValue(true);
} }
@ -175,7 +197,7 @@ class MediaSendViewModel extends ViewModel {
} }
if (selected.size() >= maxSelection) { if (selected.size() >= maxSelection) {
error.postValue(Error.TOO_MANY_ITEMS); error.setValue(Error.TOO_MANY_ITEMS);
return; return;
} }