From 162eeaa0a69ce3b05def3d145b30b54fa1bd8378 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 24 Mar 2019 02:02:34 -0400 Subject: [PATCH] Improve repo adapter --- .../magisk/adapters/ReposAdapter.java | 78 ++++++++++++------- .../magisk/fragments/ReposFragment.java | 10 +-- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java index 71a7263c9..764fd3f63 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java @@ -15,6 +15,7 @@ import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; +import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.ClassMap; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.components.BaseActivity; @@ -32,6 +33,7 @@ import java.util.List; import java.util.Map; import butterknife.BindView; +import java9.util.stream.StreamSupport; public class ReposAdapter extends SectionedAdapter @@ -41,18 +43,19 @@ public class ReposAdapter private static final int INSTALLED = 1; private static final int OTHERS = 2; - private Cursor repoCursor = null; private Map moduleMap; private RepoDatabaseHelper repoDB; private List>> repoPairs; + private List fullList; + private SearchView mSearch; - public ReposAdapter(RepoDatabaseHelper db) { - repoDB = db; + public ReposAdapter() { + repoDB = App.self.repoDB; moduleMap = Collections.emptyMap(); + fullList = Collections.emptyList(); repoPairs = new ArrayList<>(); } - @Override public int getSectionCount() { return repoPairs.size(); @@ -136,17 +139,36 @@ public class ReposAdapter }); } - public void notifyDBChanged() { - if (repoCursor != null) - repoCursor.close(); - repoCursor = repoDB.getRepoCursor(); - onQueryTextChange(""); + private void updateLists() { + if (mSearch != null) + onQueryTextChange(mSearch.getQuery().toString()); + else + 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 public void onEvent(int event) { moduleMap = Event.getResult(event); - notifyDataSetChanged(); + updateLists(); } @Override @@ -165,29 +187,25 @@ public class ReposAdapter List installed = new ArrayList<>(); List others = new ArrayList<>(); - repoPairs.clear(); - while (repoCursor.moveToNext()) { - Repo repo = new Repo(repoCursor); - if (repo.getName().toLowerCase().contains(s.toLowerCase()) - || repo.getAuthor().toLowerCase().contains(s.toLowerCase()) - || repo.getDescription().toLowerCase().contains(s.toLowerCase()) - ) { - // Passed the repoFilter - Module module = moduleMap.get(repo.getId()); - if (module != null) { - if (repo.getVersionCode() > module.getVersionCode()) { - // Updates - updates.add(repo); + StreamSupport.stream(fullList) + .filter(repo -> noCaseContain(repo.getName(), s) + || noCaseContain(repo.getAuthor(), s) + || noCaseContain(repo.getDescription(), s)) + .forEach(repo -> { + Module module = moduleMap.get(repo.getId()); + if (module != null) { + if (repo.getVersionCode() > module.getVersionCode()) { + // Updates + updates.add(repo); + } else { + installed.add(repo); + } } else { - installed.add(repo); + others.add(repo); } - } else { - others.add(repo); - } - } - } - repoCursor.moveToFirst(); + }); + repoPairs.clear(); if (!updates.isEmpty()) repoPairs.add(new Pair<>(UPDATES, updates)); if (!installed.isEmpty()) diff --git a/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java index 3cc41962a..b45e2acf0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java @@ -48,8 +48,7 @@ public class ReposFragment extends BaseFragment { mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setOnRefreshListener(() -> new UpdateRepos().exec(true)); - adapter = new ReposAdapter(app.repoDB); - Event.register(adapter); + adapter = new ReposAdapter(); recyclerView.setAdapter(adapter); recyclerView.setVisibility(View.GONE); @@ -71,7 +70,8 @@ public class ReposFragment extends BaseFragment { @Override public void onEvent(int event) { - adapter.notifyDBChanged(); + adapter.notifyDBChanged(false); + Event.register(adapter); mSwipeRefreshLayout.setRefreshing(false); boolean empty = adapter.getItemCount() == 0; recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE); @@ -82,7 +82,7 @@ public class ReposFragment extends BaseFragment { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_repo, menu); SearchView search = (SearchView) menu.findItem(R.id.repo_search).getActionView(); - search.setOnQueryTextListener(adapter); + adapter.setSearchView(search); } @Override @@ -93,7 +93,7 @@ public class ReposFragment extends BaseFragment { .setSingleChoiceItems(R.array.sorting_orders, Config.get(Config.Key.REPO_ORDER), (d, which) -> { Config.set(Config.Key.REPO_ORDER, which); - adapter.notifyDBChanged(); + adapter.notifyDBChanged(true); d.dismiss(); }).show(); }