Skip to content

Commit

Permalink
[Refactor] Fix updating MultiSelectionView when visibility of an opti…
Browse files Browse the repository at this point in the history
…on changes

Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
  • Loading branch information
MuntashirAkon committed Jul 8, 2023
1 parent 6a25cc4 commit abfb83d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}

@Override
public void onSelectionChange(int selectionCount) {
public boolean onSelectionChange(int selectionCount) {
// TODO: 7/8/22
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public BatchOpsHandler(@NonNull MultiSelectionView multiSelectionView) {
}

@Override
public void onSelectionChange(int selectionCount) {
public boolean onSelectionChange(int selectionCount) {
boolean nonZeroSelection = selectionCount > 0;
boolean canRead = mFolderShortInfo != null && mFolderShortInfo.canRead;
boolean canWrite = mFolderShortInfo != null && mFolderShortInfo.canWrite;
Expand All @@ -883,6 +883,7 @@ public void onSelectionChange(int selectionCount) {
mCutMenu.setEnabled(nonZeroSelection && canWrite);
mCopyMenu.setEnabled(nonZeroSelection && canRead);
mCopyPathsMenu.setEnabled(nonZeroSelection);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
} else if (selectionCount == 0) {
mViewModel.resumeLogcat();
}
return false;
});
mViewModel.startLogcat(new WeakReference<>(this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ protected void onResume() {
if (viewModel != null) viewModel.onResume();
if (mAdapter != null && mBatchOpsHandler != null && mAdapter.isInSelectionMode()) {
mBatchOpsHandler.updateConstraints();
mBatchOpsHandler.onSelectionChange(0); // count is irrelevant
mMultiSelectionView.updateCounter(false);
}
registerReceiver(mBatchOpsBroadCastReceiver, new IntentFilter(BatchOpsService.ACTION_BATCH_OPS_COMPLETED));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void updateConstraints() {
}

@Override
public void onSelectionChange(int selectionCount) {
public boolean onSelectionChange(int selectionCount) {
Collection<ApplicationItem> selectedItems = mViewModel.getSelectedApplicationItems();
boolean nonZeroSelection = selectedItems.size() > 0;
// It was ensured that the algorithm is greedy
Expand Down Expand Up @@ -108,5 +108,6 @@ public void onSelectionChange(int selectionCount) {
mPreventBackgroundMenu.setVisible(mCanModifyAppOpMode && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N);
mNetPolicyMenu.setVisible(mCanModifyNetPolicy);
mBlockUnblockTrackersMenu.setVisible(mCanModifyComponentState);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,10 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
}

@Override
public void onSelectionChange(int selectionCount) {
if (mSelectionMenu == null || mAdapter == null) return;
public boolean onSelectionChange(int selectionCount) {
if (mSelectionMenu == null || mAdapter == null) {
return false;
}
ArrayList<ProcessItem> selectedItems = mAdapter.getSelectedItems();
MenuItem kill = mSelectionMenu.findItem(R.id.action_kill);
MenuItem forceStop = mSelectionMenu.findItem(R.id.action_force_stop);
Expand All @@ -378,6 +380,7 @@ public void onSelectionChange(int selectionCount) {
}
}
kill.setEnabled(selectedItems.size() != 0 && killEnabled);
return true;
}

private void handleBatchOp(@BatchOpsManager.OpType int op) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ public MenuView getMenuView() {
return mMenuAdapter;
}

public void updateMenuView() {
mMenuAdapter.updateMenuView();
}

/**
* Inflate a menu resource into this navigation view.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@
@SuppressLint("RestrictedApi")
public class MultiSelectionView extends MaterialCardView implements OnApplyWindowInsetsListener {
public interface OnSelectionChangeListener {
/**
* Called when the number of selections has changed or an update is required internally or via
* {@link #updateCounter(boolean)}.
*
* @param selectionCount Present selection count
* @return {@code true} if it's necessary to update the visibility of menu items, or {@code false} otherwise.
*/
@UiThread
void onSelectionChange(int selectionCount);
boolean onSelectionChange(int selectionCount);
}

private final SelectionActionsView mSelectionActionsView;
Expand Down Expand Up @@ -356,8 +363,8 @@ public void updateCounter(boolean hideOnEmpty) {
int selectionCount = mAdapter.getSelectedItemCount();
if (selectionCount <= 0 && hideOnEmpty) {
if (getVisibility() != GONE) hide();
if (mSelectionChangeListener != null) {
mSelectionChangeListener.onSelectionChange(0);
if (mSelectionChangeListener != null && mSelectionChangeListener.onSelectionChange(0)) {
mSelectionActionsView.updateMenuView();
}
return;
}
Expand All @@ -366,8 +373,8 @@ public void updateCounter(boolean hideOnEmpty) {
}
mSelectionCounter.setText(String.format(Locale.getDefault(), "%d/%d", selectionCount, mAdapter.getTotalItemCount()));
mSelectAllView.setChecked(mAdapter.areAllSelected(), false);
if (mSelectionChangeListener != null) {
mSelectionChangeListener.onSelectionChange(selectionCount);
if (mSelectionChangeListener != null && mSelectionChangeListener.onSelectionChange(selectionCount)) {
mSelectionActionsView.updateMenuView();
}
if (!mAdapter.isInSelectionMode()) {
// Special check to avoid displaying the selection panel on resizing the view
Expand Down

0 comments on commit abfb83d

Please sign in to comment.