Skip to content

Commit

Permalink
[Main] Add select all button to the bottom bar
Browse files Browse the repository at this point in the history
  • Loading branch information
MuntashirAkon committed Jul 29, 2020
1 parent 78d9f6b commit 4a231e5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ protected void onCreate(Bundle savedInstanceState) {
});
mBottomAppBar.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.action_select_all:
mAdapter.selectAll();
return true;
case R.id.action_block_trackers:
handleBatchOp(BatchOpsManager.OP_BLOCK_TRACKERS, R.string.alert_failed_to_disable_trackers);
return true;
Expand Down Expand Up @@ -641,7 +644,7 @@ static class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapte
private MainActivity mActivity;
private PackageManager mPackageManager;
private String mSearchQuery;
private List<ApplicationItem> mAdapterList;
private final List<ApplicationItem> mAdapterList = new ArrayList<>();

private static int mColorTransparent;
private static int mColorSemiTransparent;
Expand Down Expand Up @@ -669,21 +672,36 @@ static class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapte
}

void setDefaultList(List<ApplicationItem> list) {
mAdapterList = list;
mSearchQuery = mActivity.mModel.getSearchQuery();
notifyDataSetChanged();
synchronized (mAdapterList) {
mAdapterList.clear();
mAdapterList.addAll(list);
mSearchQuery = mActivity.mModel.getSearchQuery();
notifyDataSetChanged();
}
}

void clearSelection() {
final AtomicInteger itemId = new AtomicInteger();
for (ApplicationItem applicationItem: mActivity.mModel.getSelectedApplicationItems()) {
itemId.set(mAdapterList.indexOf(applicationItem));
if (itemId.get() == -1) continue;
applicationItem.isSelected = false;
mAdapterList.set(itemId.get(), applicationItem);
mActivity.runOnUiThread(() -> notifyItemChanged(itemId.get()));
synchronized (mAdapterList) {
final AtomicInteger itemId = new AtomicInteger();
for (ApplicationItem applicationItem : mActivity.mModel.getSelectedApplicationItems()) {
itemId.set(mAdapterList.indexOf(applicationItem));
if (itemId.get() == -1) continue;
applicationItem.isSelected = false;
mAdapterList.set(itemId.get(), applicationItem);
mActivity.runOnUiThread(() -> notifyItemChanged(itemId.get()));
}
mActivity.mModel.clearSelection();
}
}

void selectAll() {
synchronized (mAdapterList) {
for (int i = 0; i < mAdapterList.size(); ++i) {
mAdapterList.set(i, mActivity.mModel.select(mAdapterList.get(i)));
notifyItemChanged(i);
}
mActivity.handleSelection();
}
mActivity.mModel.clearSelection();
}

@NonNull
Expand Down Expand Up @@ -836,7 +854,7 @@ public long getItemId(int i) {

@Override
public int getItemCount() {
return mAdapterList == null ? 0 : mAdapterList.size();
return mAdapterList.size();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -48,7 +50,7 @@ public class MainViewModel extends AndroidViewModel {
private @MainActivity.SortOrder int mSortBy;
private @MainActivity.Filter int mFilterFlags;
private String searchQuery;
private List<String> selectedPackages = new LinkedList<>();
private Set<String> selectedPackages = new HashSet<>();
private List<ApplicationItem> selectedApplicationItems = new LinkedList<>();
private int flagSigningInfo;
public MainViewModel(@NonNull Application application) {
Expand Down Expand Up @@ -107,7 +109,7 @@ public void clearSelection() {
selectedApplicationItems.clear();
}

public List<String> getSelectedPackages() {
public Set<String> getSelectedPackages() {
return selectedPackages;
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_baseline_select_all_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorAccent"
android:pathData="M3,5h2L5,3c-1.1,0 -2,0.9 -2,2zM3,13h2v-2L3,11v2zM7,21h2v-2L7,19v2zM3,9h2L5,7L3,7v2zM13,3h-2v2h2L13,3zM19,3v2h2c0,-1.1 -0.9,-2 -2,-2zM5,21v-2L3,19c0,1.1 0.9,2 2,2zM3,17h2v-2L3,15v2zM9,3L7,3v2h2L9,3zM11,21h2v-2h-2v2zM19,13h2v-2h-2v2zM19,21c1.1,0 2,-0.9 2,-2h-2v2zM19,9h2L21,7h-2v2zM19,17h2v-2h-2v2zM15,21h2v-2h-2v2zM15,5h2L17,3h-2v2zM7,17h10L17,7L7,7v10zM9,9h6v6L9,15L9,9z"/>
</vector>
10 changes: 9 additions & 1 deletion app/src/main/res/menu/activity_main_selection_actions.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/action_select_all"
android:icon="@drawable/ic_baseline_select_all_24"
android:title="@string/select_all"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />

<item
android:id="@+id/action_uninstall"
android:icon="@drawable/ic_delete_black_24dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,5 @@
<string name="filter_user_apps">User apps</string>
<string name="filter_system_apps">System apps</string>
<string name="filter_apps_with_rules">Apps with rules</string>
<string name="select_all">Select All</string>
</resources>

0 comments on commit 4a231e5

Please sign in to comment.