Change search to async for smoother UI

This commit is contained in:
topjohnwu 2016-11-20 18:49:09 +08:00
parent 4e07b51460
commit 8c077a7373
2 changed files with 62 additions and 36 deletions

View File

@ -63,14 +63,7 @@ public class MagiskHideFragment extends Fragment {
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
fListApps.clear(); new FilterApps().exec(newText);
for (ApplicationInfo info : listApps) {
if (info.loadLabel(packageManager).toString().toLowerCase().contains(newText.toLowerCase())
|| info.packageName.toLowerCase().contains(newText.toLowerCase())) {
fListApps.add(info);
}
}
appAdapter.notifyDataSetChanged();
return false; return false;
} }
}; };
@ -113,6 +106,26 @@ public class MagiskHideFragment extends Fragment {
} }
} }
private class FilterApps extends Async.NormalTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String newText = strings[0];
fListApps.clear();
for (ApplicationInfo info : listApps) {
if (info.loadLabel(packageManager).toString().toLowerCase().contains(newText.toLowerCase())
|| info.packageName.toLowerCase().contains(newText.toLowerCase())) {
fListApps.add(info);
}
}
return null;
}
@Override
protected void onPostExecute(Void v) {
appAdapter.notifyDataSetChanged();
}
}
private void updateUI() { private void updateUI() {
appAdapter.notifyDataSetChanged(); appAdapter.notifyDataSetChanged();
recyclerView.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.VISIBLE);

View File

@ -94,34 +94,7 @@ public class ReposFragment extends Fragment {
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
fUpdateRepos.clear(); new FilterApps().exec(newText);
fInstalledRepos.clear();
fOthersRepos.clear();
for (Repo repo: mUpdateRepos) {
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
) {
fUpdateRepos.add(repo);
}
}
for (Repo repo: mInstalledRepos) {
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
) {
fInstalledRepos.add(repo);
}
}
for (Repo repo: mOthersRepos) {
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
) {
fOthersRepos.add(repo);
}
}
updateUI();
return false; return false;
} }
}; };
@ -182,4 +155,44 @@ public class ReposFragment extends Fragment {
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
} }
private class FilterApps extends Async.NormalTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String newText = strings[0];
fUpdateRepos.clear();
fInstalledRepos.clear();
fOthersRepos.clear();
for (Repo repo: mUpdateRepos) {
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
) {
fUpdateRepos.add(repo);
}
}
for (Repo repo: mInstalledRepos) {
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
) {
fInstalledRepos.add(repo);
}
}
for (Repo repo: mOthersRepos) {
if (repo.getName().toLowerCase().contains(newText.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(newText.toLowerCase())
|| repo.getDescription().toLowerCase().contains(newText.toLowerCase())
) {
fOthersRepos.add(repo);
}
}
return null;
}
@Override
protected void onPostExecute(Void v) {
updateUI();
}
}
} }