Improve repo adapter

This commit is contained in:
topjohnwu 2019-03-24 02:02:34 -04:00
parent f36ce905aa
commit 162eeaa0a6
2 changed files with 53 additions and 35 deletions

View File

@ -15,6 +15,7 @@ import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.ClassMap; import com.topjohnwu.magisk.ClassMap;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.BaseActivity; import com.topjohnwu.magisk.components.BaseActivity;
@ -32,6 +33,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import butterknife.BindView; import butterknife.BindView;
import java9.util.stream.StreamSupport;
public class ReposAdapter public class ReposAdapter
extends SectionedAdapter<ReposAdapter.SectionHolder, ReposAdapter.RepoHolder> extends SectionedAdapter<ReposAdapter.SectionHolder, ReposAdapter.RepoHolder>
@ -41,18 +43,19 @@ public class ReposAdapter
private static final int INSTALLED = 1; private static final int INSTALLED = 1;
private static final int OTHERS = 2; private static final int OTHERS = 2;
private Cursor repoCursor = null;
private Map<String, Module> moduleMap; private Map<String, Module> moduleMap;
private RepoDatabaseHelper repoDB; private RepoDatabaseHelper repoDB;
private List<Pair<Integer, List<Repo>>> repoPairs; private List<Pair<Integer, List<Repo>>> repoPairs;
private List<Repo> fullList;
private SearchView mSearch;
public ReposAdapter(RepoDatabaseHelper db) { public ReposAdapter() {
repoDB = db; repoDB = App.self.repoDB;
moduleMap = Collections.emptyMap(); moduleMap = Collections.emptyMap();
fullList = Collections.emptyList();
repoPairs = new ArrayList<>(); repoPairs = new ArrayList<>();
} }
@Override @Override
public int getSectionCount() { public int getSectionCount() {
return repoPairs.size(); return repoPairs.size();
@ -136,17 +139,36 @@ public class ReposAdapter
}); });
} }
public void notifyDBChanged() { private void updateLists() {
if (repoCursor != null) if (mSearch != null)
repoCursor.close(); onQueryTextChange(mSearch.getQuery().toString());
repoCursor = repoDB.getRepoCursor(); else
onQueryTextChange(""); onQueryTextChange("");
}
private static boolean noCaseContain(String a, String b) {
return a.toLowerCase().contains(b.toLowerCase());
}
public void setSearchView(SearchView view) {
mSearch = view;
mSearch.setOnQueryTextListener(this);
}
public void notifyDBChanged(boolean refresh) {
try (Cursor c = repoDB.getRepoCursor()) {
fullList = new ArrayList<>(c.getCount());
while (c.moveToNext())
fullList.add(new Repo(c));
}
if (refresh)
updateLists();
} }
@Override @Override
public void onEvent(int event) { public void onEvent(int event) {
moduleMap = Event.getResult(event); moduleMap = Event.getResult(event);
notifyDataSetChanged(); updateLists();
} }
@Override @Override
@ -165,29 +187,25 @@ public class ReposAdapter
List<Repo> installed = new ArrayList<>(); List<Repo> installed = new ArrayList<>();
List<Repo> others = new ArrayList<>(); List<Repo> others = new ArrayList<>();
repoPairs.clear(); StreamSupport.stream(fullList)
while (repoCursor.moveToNext()) { .filter(repo -> noCaseContain(repo.getName(), s)
Repo repo = new Repo(repoCursor); || noCaseContain(repo.getAuthor(), s)
if (repo.getName().toLowerCase().contains(s.toLowerCase()) || noCaseContain(repo.getDescription(), s))
|| repo.getAuthor().toLowerCase().contains(s.toLowerCase()) .forEach(repo -> {
|| repo.getDescription().toLowerCase().contains(s.toLowerCase()) Module module = moduleMap.get(repo.getId());
) { if (module != null) {
// Passed the repoFilter if (repo.getVersionCode() > module.getVersionCode()) {
Module module = moduleMap.get(repo.getId()); // Updates
if (module != null) { updates.add(repo);
if (repo.getVersionCode() > module.getVersionCode()) { } else {
// Updates installed.add(repo);
updates.add(repo); }
} else { } else {
installed.add(repo); others.add(repo);
} }
} else { });
others.add(repo);
}
}
}
repoCursor.moveToFirst();
repoPairs.clear();
if (!updates.isEmpty()) if (!updates.isEmpty())
repoPairs.add(new Pair<>(UPDATES, updates)); repoPairs.add(new Pair<>(UPDATES, updates));
if (!installed.isEmpty()) if (!installed.isEmpty())

View File

@ -48,8 +48,7 @@ public class ReposFragment extends BaseFragment {
mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> new UpdateRepos().exec(true)); mSwipeRefreshLayout.setOnRefreshListener(() -> new UpdateRepos().exec(true));
adapter = new ReposAdapter(app.repoDB); adapter = new ReposAdapter();
Event.register(adapter);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
recyclerView.setVisibility(View.GONE); recyclerView.setVisibility(View.GONE);
@ -71,7 +70,8 @@ public class ReposFragment extends BaseFragment {
@Override @Override
public void onEvent(int event) { public void onEvent(int event) {
adapter.notifyDBChanged(); adapter.notifyDBChanged(false);
Event.register(adapter);
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
boolean empty = adapter.getItemCount() == 0; boolean empty = adapter.getItemCount() == 0;
recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE); recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE);
@ -82,7 +82,7 @@ public class ReposFragment extends BaseFragment {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_repo, menu); inflater.inflate(R.menu.menu_repo, menu);
SearchView search = (SearchView) menu.findItem(R.id.repo_search).getActionView(); SearchView search = (SearchView) menu.findItem(R.id.repo_search).getActionView();
search.setOnQueryTextListener(adapter); adapter.setSearchView(search);
} }
@Override @Override
@ -93,7 +93,7 @@ public class ReposFragment extends BaseFragment {
.setSingleChoiceItems(R.array.sorting_orders, .setSingleChoiceItems(R.array.sorting_orders,
Config.get(Config.Key.REPO_ORDER), (d, which) -> { Config.get(Config.Key.REPO_ORDER), (d, which) -> {
Config.set(Config.Key.REPO_ORDER, which); Config.set(Config.Key.REPO_ORDER, which);
adapter.notifyDBChanged(); adapter.notifyDBChanged(true);
d.dismiss(); d.dismiss();
}).show(); }).show();
} }