From 8c077a7373d13c527e629834e0bc3a31ba7c95cf Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 20 Nov 2016 18:49:09 +0800 Subject: [PATCH] Change search to async for smoother UI --- .../topjohnwu/magisk/MagiskHideFragment.java | 29 +++++--- .../com/topjohnwu/magisk/ReposFragment.java | 69 +++++++++++-------- 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 6ac2f60fe..4598e5534 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -63,14 +63,7 @@ public class MagiskHideFragment extends Fragment { @Override public boolean onQueryTextChange(String newText) { - fListApps.clear(); - for (ApplicationInfo info : listApps) { - if (info.loadLabel(packageManager).toString().toLowerCase().contains(newText.toLowerCase()) - || info.packageName.toLowerCase().contains(newText.toLowerCase())) { - fListApps.add(info); - } - } - appAdapter.notifyDataSetChanged(); + new FilterApps().exec(newText); return false; } }; @@ -113,6 +106,26 @@ public class MagiskHideFragment extends Fragment { } } + private class FilterApps extends Async.NormalTask { + @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() { appAdapter.notifyDataSetChanged(); recyclerView.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java index c9863f690..ec75056e4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -94,34 +94,7 @@ public class ReposFragment extends Fragment { @Override public boolean onQueryTextChange(String newText) { - 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); - } - } - updateUI(); + new FilterApps().exec(newText); return false; } }; @@ -182,4 +155,44 @@ public class ReposFragment extends Fragment { mSwipeRefreshLayout.setRefreshing(false); } + private class FilterApps extends Async.NormalTask { + @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(); + } + } + }