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 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<ReposAdapter.SectionHolder, ReposAdapter.RepoHolder>
@ -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<String, Module> moduleMap;
private RepoDatabaseHelper repoDB;
private List<Pair<Integer, List<Repo>>> repoPairs;
private List<Repo> 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<Repo> installed = new ArrayList<>();
List<Repo> 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())

View File

@ -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();
}