diff --git a/app/src/main/java/com/fox2code/mmm/ModuleHolder.java b/app/src/main/java/com/fox2code/mmm/ModuleHolder.java index 3d5948e..c9194f5 100644 --- a/app/src/main/java/com/fox2code/mmm/ModuleHolder.java +++ b/app/src/main/java/com/fox2code/mmm/ModuleHolder.java @@ -3,6 +3,7 @@ package com.fox2code.mmm; import android.content.Context; import android.content.pm.PackageManager; import android.util.Log; +import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.StringRes; @@ -24,6 +25,7 @@ public final class ModuleHolder implements Comparable { public final NotificationType notificationType; public final Type separator; public final int footerPx; + public View.OnClickListener onClickListener; public LocalModuleInfo moduleInfo; public RepoModule repoModule; public int filterLevel; @@ -205,21 +207,21 @@ public final class ModuleHolder implements Comparable { } public enum Type implements Comparator { - SEPARATOR(R.string.loading, false) { + SEPARATOR(R.string.loading, false, false) { @Override @SuppressWarnings("ConstantConditions") public int compare(ModuleHolder o1, ModuleHolder o2) { return o1.separator.compareTo(o2.separator); } }, - NOTIFICATION(R.string.loading, true) { + NOTIFICATION(R.string.loading, true, false) { @Override @SuppressWarnings("ConstantConditions") public int compare(ModuleHolder o1, ModuleHolder o2) { return o1.notificationType.compareTo(o2.notificationType); } }, - UPDATABLE(R.string.updatable, true) { + UPDATABLE(R.string.updatable, true, true) { @Override public int compare(ModuleHolder o1, ModuleHolder o2) { int cmp = Integer.compare(o1.filterLevel, o2.filterLevel); @@ -227,7 +229,7 @@ public final class ModuleHolder implements Comparable { return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated); } }, - INSTALLED(R.string.installed, true) { + INSTALLED(R.string.installed, true, true) { @Override public int compare(ModuleHolder o1, ModuleHolder o2) { int cmp = Integer.compare(o1.filterLevel, o2.filterLevel); @@ -235,8 +237,8 @@ public final class ModuleHolder implements Comparable { return o1.getMainModuleName().compareTo(o2.getMainModuleName()); } }, - SPECIAL_NOTIFICATIONS(R.string.loading, true), - INSTALLABLE(R.string.online_repo, true) { + SPECIAL_NOTIFICATIONS(R.string.loading, true, false), + INSTALLABLE(R.string.online_repo, true, true) { @Override public int compare(ModuleHolder o1, ModuleHolder o2) { int cmp = Integer.compare(o1.filterLevel, o2.filterLevel); @@ -244,15 +246,17 @@ public final class ModuleHolder implements Comparable { return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated); } }, - FOOTER(R.string.loading, false); + FOOTER(R.string.loading, false, false); @StringRes public final int title; public final boolean hasBackground; + public final boolean moduleHolder; - Type(@StringRes int title, boolean hasBackground) { + Type(@StringRes int title, boolean hasBackground, boolean moduleHolder) { this.title = title; this.hasBackground = hasBackground; + this.moduleHolder = moduleHolder; } // Note: This method should only be called if both element have the same type diff --git a/app/src/main/java/com/fox2code/mmm/ModuleSorter.java b/app/src/main/java/com/fox2code/mmm/ModuleSorter.java new file mode 100644 index 0000000..193576d --- /dev/null +++ b/app/src/main/java/com/fox2code/mmm/ModuleSorter.java @@ -0,0 +1,48 @@ +package com.fox2code.mmm; + +import androidx.annotation.DrawableRes; + +import java.util.Comparator; + +public enum ModuleSorter implements Comparator { + UPDATE(R.drawable.ic_baseline_update_24) { + @Override + public ModuleSorter next() { + return ALPHA; + } + }, + ALPHA(R.drawable.ic_baseline_sort_by_alpha_24) { + @Override + public int compare(ModuleHolder holder1, ModuleHolder holder2) { + ModuleHolder.Type type1 = holder1.getType(); + ModuleHolder.Type type2 = holder2.getType(); + if (type1 == type2 && type1 == ModuleHolder.Type.INSTALLABLE) { + int compare = Integer.compare(holder1.filterLevel, holder2.filterLevel); + if (compare != 0) return compare; + compare = holder1.getMainModuleName() + .compareTo(holder2.getMainModuleName()); + if (compare != 0) return compare; + } + return super.compare(holder1, holder2); + } + + @Override + public ModuleSorter next() { + return UPDATE; + } + }; + + @DrawableRes + public final int icon; + + ModuleSorter(int icon) { + this.icon = icon; + } + + @Override + public int compare(ModuleHolder holder1, ModuleHolder holder2) { + return holder1.compareTo(holder2); + } + + public abstract ModuleSorter next(); +} diff --git a/app/src/main/java/com/fox2code/mmm/ModuleViewAdapter.java b/app/src/main/java/com/fox2code/mmm/ModuleViewAdapter.java index 7f227a8..7fbd2ef 100644 --- a/app/src/main/java/com/fox2code/mmm/ModuleViewAdapter.java +++ b/app/src/main/java/com/fox2code/mmm/ModuleViewAdapter.java @@ -93,12 +93,14 @@ public final class ModuleViewAdapter extends RecyclerView.Adapter { ModuleHolder moduleHolder = this.moduleHolder; - if (moduleHolder != null && - moduleHolder.notificationType != null) { - View.OnClickListener onClickListener = - moduleHolder.notificationType.onClickListener; - if (onClickListener != null) + if (moduleHolder != null) { + View.OnClickListener onClickListener = moduleHolder.onClickListener; + if (onClickListener != null) { onClickListener.onClick(v); + } else if (moduleHolder.notificationType != null) { + onClickListener = moduleHolder.notificationType.onClickListener; + if (onClickListener != null) onClickListener.onClick(v); + } } }); this.switchMaterial.setEnabled(false); @@ -239,9 +241,14 @@ public final class ModuleViewAdapter extends RecyclerView.Adapter moduleHolders; final int newNotificationsLen; try { @@ -128,14 +129,27 @@ public class ModuleViewListBuilder { ModuleHolder.Type type = moduleHolder.getType(); if (matchFilter(moduleHolder)) { if (headerTypes.add(type)) { - moduleHolders.add(new ModuleHolder(type)); + ModuleHolder separator = new ModuleHolder(type); + if (type == ModuleHolder.Type.INSTALLABLE) { + ModuleSorter moduleSorter = this.moduleSorter; + separator.filterLevel = this.moduleSorter.icon; + separator.onClickListener = v -> { + if (this.updating || this.moduleSorter != moduleSorter) + return; // Do not allow spams calls + this.moduleSorter = this.moduleSorter.next(); + new Thread(() -> // Apply async + this.applyTo(moduleList, moduleViewAdapter), + "Sorter apply Thread").start(); + }; + } + moduleHolders.add(separator); } moduleHolders.add(moduleHolder); } } } } - Collections.sort(moduleHolders, ModuleHolder::compareTo); + Collections.sort(moduleHolders, this.moduleSorter); if (this.footerPx != 0) { // Footer is always last moduleHolders.add(new ModuleHolder(this.footerPx)); } @@ -143,7 +157,7 @@ public class ModuleViewListBuilder { // Build end } } finally { - this.noUpdate = false; + this.updating = false; } this.activity.runOnUiThread(() -> { final EnumSet oldNotifications = @@ -272,7 +286,6 @@ public class ModuleViewListBuilder { synchronized (this.updateLock) { this.footerPx = footerPx; } - this.noUpdate = false; } } } diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index da01a99..e29632c 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -21,8 +21,8 @@ @*android:anim/slide_out_right --> @android:anim/fade_in @android:anim/fade_out - 8dp - 8dp + @dimen/card_corner_radius + @dimen/card_corner_radius @@ -48,8 +48,8 @@ @*android:anim/slide_out_right --> @android:anim/fade_in @android:anim/fade_out - 6dp - 6dp + @dimen/card_corner_radius + @dimen/card_corner_radius