From f36ce905aae1986691c191c0fdb261d36aa695ab Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 23 Mar 2019 23:18:26 -0400 Subject: [PATCH] Improve repo adapter Fix #435 --- .../com/topjohnwu/magisk/MainActivity.java | 8 +-- .../com/topjohnwu/magisk/SplashActivity.java | 2 +- .../magisk/adapters/ReposAdapter.java | 36 +++++++++++--- .../magisk/fragments/ReposFragment.java | 49 +++++++------------ .../com/topjohnwu/magisk/utils/Utils.java | 12 ++++- app/src/main/res/menu/drawer.xml | 6 ++- 6 files changed, 63 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index a9ca5ddca..50fd84651 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -58,7 +58,7 @@ public class MainActivity extends BaseActivity super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new MainActivity_ViewBinding(this); - + checkHideSection(); setSupportActionBar(toolbar); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.magisk, R.string.magisk) { @@ -90,12 +90,6 @@ public class MainActivity extends BaseActivity navigationView.setNavigationItemSelectedListener(this); } - @Override - protected void onResume() { - super.onResume(); - checkHideSection(); - } - @Override public void onBackPressed() { if (drawer.isDrawerOpen(navigationView)) { diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index add260199..0f8bdcf6c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -74,7 +74,7 @@ public class SplashActivity extends BaseActivity { // Magisk working as expected if (Shell.rootAccess() && Config.magiskVersionCode > 0) { // Load modules - Utils.loadModules(); + Utils.loadModules(false); // Load repos if (Networking.checkNetworkStatus(this)) new UpdateRepos().exec(); 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 01ac6ab48..71a7263c9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java @@ -10,6 +10,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.SearchView; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; @@ -23,14 +24,18 @@ import com.topjohnwu.magisk.container.Repo; import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.dialogs.CustomAlertDialog; import com.topjohnwu.magisk.uicomponents.MarkDownWindow; +import com.topjohnwu.magisk.utils.Event; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import butterknife.BindView; -public class ReposAdapter extends SectionedAdapter { +public class ReposAdapter + extends SectionedAdapter + implements Event.AutoListener, SearchView.OnQueryTextListener { private static final int UPDATES = 0; private static final int INSTALLED = 1; @@ -41,11 +46,10 @@ public class ReposAdapter extends SectionedAdapter>> repoPairs; - public ReposAdapter(RepoDatabaseHelper db, Map map) { + public ReposAdapter(RepoDatabaseHelper db) { repoDB = db; - moduleMap = map; + moduleMap = Collections.emptyMap(); repoPairs = new ArrayList<>(); - notifyDBChanged(); } @@ -136,10 +140,27 @@ public class ReposAdapter extends SectionedAdapter updates = new ArrayList<>(); List installed = new ArrayList<>(); List others = new ArrayList<>(); @@ -150,7 +171,7 @@ public class ReposAdapter extends SectionedAdapter(OTHERS, others)); notifyDataSetChanged(); + return false; } static class SectionHolder extends RecyclerView.ViewHolder { 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 d415d860b..3cc41962a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/fragments/ReposFragment.java @@ -46,56 +46,43 @@ public class ReposFragment extends BaseFragment { unbinder = new ReposFragment_ViewBinding(this, view); mSwipeRefreshLayout.setRefreshing(true); - recyclerView.setVisibility(View.GONE); - mSwipeRefreshLayout.setOnRefreshListener(() -> new UpdateRepos().exec(true)); + adapter = new ReposAdapter(app.repoDB); + Event.register(adapter); + recyclerView.setAdapter(adapter); + recyclerView.setVisibility(View.GONE); + requireActivity().setTitle(R.string.downloads); return view; } + @Override + public void onDestroyView() { + super.onDestroyView(); + Event.unregister(adapter); + } + @Override public int[] getListeningEvents() { - return new int[] {Event.MODULE_LOAD_DONE, Event.REPO_LOAD_DONE}; + return new int[] {Event.REPO_LOAD_DONE}; } @Override public void onEvent(int event) { - switch (event) { - case Event.MODULE_LOAD_DONE: - adapter = new ReposAdapter(app.repoDB, Event.getResult(event)); - recyclerView.setAdapter(adapter); - break; - case Event.REPO_LOAD_DONE: - if (adapter != null) - adapter.notifyDBChanged(); - break; - } - if (Event.isTriggered(this)) { - mSwipeRefreshLayout.setRefreshing(false); - boolean empty = adapter.getItemCount() == 0; - recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE); - emptyRv.setVisibility(empty ? View.VISIBLE : View.GONE); - } + adapter.notifyDBChanged(); + mSwipeRefreshLayout.setRefreshing(false); + boolean empty = adapter.getItemCount() == 0; + recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE); + emptyRv.setVisibility(empty ? View.VISIBLE : View.GONE); } @Override 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(new SearchView.OnQueryTextListener() { - @Override - public boolean onQueryTextSubmit(String query) { - return false; - } - - @Override - public boolean onQueryTextChange(String newText) { - adapter.filter(newText); - return false; - } - }); + search.setOnQueryTextListener(adapter); } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index f398a3065..4d3c8653b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -95,8 +95,12 @@ public class Utils { } public static void loadModules() { + loadModules(true); + } + + public static void loadModules(boolean async) { Event.reset(Event.MODULE_LOAD_DONE); - App.THREAD_POOL.execute(() -> { + Runnable run = () -> { Map moduleMap = new ValueSortedMap<>(); SuFile path = new SuFile(Const.MAGISK_PATH); SuFile[] modules = path.listFiles( @@ -107,7 +111,11 @@ public class Utils { moduleMap.put(module.getId(), module); } Event.trigger(Event.MODULE_LOAD_DONE, moduleMap); - }); + }; + if (async) + App.THREAD_POOL.execute(run); + else + run.run(); } public static boolean showSuperUser() { diff --git a/app/src/main/res/menu/drawer.xml b/app/src/main/res/menu/drawer.xml index db37e41fc..40a26c32b 100644 --- a/app/src/main/res/menu/drawer.xml +++ b/app/src/main/res/menu/drawer.xml @@ -18,7 +18,8 @@ + android:title="@string/magiskhide" + android:visible="false"/> @@ -29,7 +30,8 @@ + android:title="@string/modules" + android:visible="false"/>