Skip to content

Commit

Permalink
#287: It's now possible to deselect rows by clicking again on the "Se…
Browse files Browse the repository at this point in the history
…lect all" menu.
  • Loading branch information
Keidan committed Nov 10, 2023
1 parent d1d414c commit 9cb202d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@ public abstract class SearchableListArrayAdapter extends ArrayAdapter<LineEntry>
private final LineEntries mLineEntries;
private Set<Integer> mSelectedItemsIds;

protected SearchableListArrayAdapter(final Context context,
final int layoutId,
final List<LineEntry> objects,
UserConfig userConfigPortrait,
UserConfig userConfigLandscape) {
protected SearchableListArrayAdapter(final Context context, final int layoutId, final List<LineEntry> objects, UserConfig userConfigPortrait, UserConfig userConfigLandscape) {
super(context, layoutId, objects);
mLineEntries = new LineEntries(objects);
mUserConfigPortrait = userConfigPortrait;
mUserConfigLandscape = userConfigLandscape;
mEntryFilter = new EntryFilter(context, this, this, mLineEntries,
userConfigPortrait, userConfigLandscape);
mEntryFilter = new EntryFilter(context, this, this, mLineEntries, userConfigPortrait, userConfigLandscape);
mSelectedItemsIds = new HashSet<>();
}

Expand Down Expand Up @@ -99,6 +94,15 @@ public List<Integer> getSelectedIds() {
return li;
}

/**
* Returns the selected count.
*
* @return count
*/
public int getSelectedCount() {
return mSelectedItemsIds.size();
}

/**
* Returns the line entries.
*
Expand Down Expand Up @@ -173,8 +177,7 @@ public void addAll(@NonNull Collection<? extends LineEntry> collection) {
* @param convertView This value may be null.
* @return The view.
*/
protected abstract @NonNull
View inflateView(final View convertView);
protected abstract @NonNull View inflateView(final View convertView);

/**
* Fills the view.
Expand All @@ -193,9 +196,7 @@ public void addAll(@NonNull Collection<? extends LineEntry> collection) {
* @return This value cannot be null.
*/
@Override
public @NonNull
View getView(final int position, final View convertView,
@NonNull final ViewGroup parent) {
public @NonNull View getView(final int position, final View convertView, @NonNull final ViewGroup parent) {
View v = inflateView(convertView);
fillView(v, position);
return v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;

import java.util.concurrent.atomic.AtomicBoolean;

import fr.ralala.hexviewer.ApplicationCtx;
import fr.ralala.hexviewer.R;
import fr.ralala.hexviewer.ui.activities.MainActivity;
Expand All @@ -42,6 +40,7 @@ public abstract class GenericMultiChoiceCallback implements AbsListView.MultiCho
protected final SearchableListArrayAdapter mAdapter;
protected final MainActivity mActivity;
private final ClipboardManager mClipboard;
private int mFirstSelection = -1;
private final AlertDialog mProgress;

@SuppressLint("InflateParams")
Expand Down Expand Up @@ -138,6 +137,8 @@ public void onItemCheckedStateChanged(ActionMode mode, int position, long id, bo
final int checkedCount = mListView.getCheckedItemCount();
mode.setTitle(String.format(mActivity.getString(R.string.items_selected), checkedCount));
mAdapter.toggleSelection(position, checked);
if (checkedCount == 1)
mFirstSelection = mAdapter.getSelectedIds().get(0);
}

/**
Expand All @@ -146,10 +147,13 @@ public void onItemCheckedStateChanged(ActionMode mode, int position, long id, bo
* @param item The item that was clicked.
*/
private void actionSelectAll(MenuItem item) {
final boolean checked = mAdapter.getSelectedCount() != mAdapter.getCount();
setActionView(item, () -> {
final int count = mAdapter.getCount();
for (int i = 0; i < count; i++) {
mListView.setItemChecked(i, true);
if (mFirstSelection == i && !checked)
continue;
mListView.setItemChecked(i, checked);
}
});
}
Expand Down Expand Up @@ -208,12 +212,12 @@ protected void displayError(@StringRes int message) {
*/
protected void setActionView(final MenuItem item, final Runnable action) {
UIHelper.showCircularProgressDialog(mProgress);
AtomicBoolean checkable = new AtomicBoolean(false);
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(() -> {
action.run();
if (item != null) {
item.setCheckable(checkable.get());
item.setCheckable(true);
item.setChecked(mAdapter.getSelectedCount() == mAdapter.getCount());
View view = item.getActionView();
if (view != null) {
view.clearAnimation();
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/main_hex_multi_choice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
<item android:id="@+id/action_select_all"
android:icon="@drawable/ic_select_all"
android:title="@string/menu_select_all"
android:checkable="true"
app:showAsAction="ifRoom"/>
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/menu/main_plain_multi_choice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<item android:id="@+id/action_select_all"
android:icon="@drawable/ic_select_all"
android:title="@string/menu_select_all"
android:checkable="true"
app:showAsAction="ifRoom"/>
</menu>

0 comments on commit 9cb202d

Please sign in to comment.