Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit af02c41

Browse files
committed
Implement Online repo alphabetical sorting. (Implement #8)
1 parent 4ced345 commit af02c41

File tree

5 files changed

+104
-30
lines changed

5 files changed

+104
-30
lines changed

app/src/main/java/com/fox2code/mmm/ModuleHolder.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.content.pm.PackageManager;
55
import android.util.Log;
6+
import android.view.View;
67

78
import androidx.annotation.NonNull;
89
import androidx.annotation.StringRes;
@@ -24,6 +25,7 @@ public final class ModuleHolder implements Comparable<ModuleHolder> {
2425
public final NotificationType notificationType;
2526
public final Type separator;
2627
public final int footerPx;
28+
public View.OnClickListener onClickListener;
2729
public LocalModuleInfo moduleInfo;
2830
public RepoModule repoModule;
2931
public int filterLevel;
@@ -205,54 +207,56 @@ public int compareTo(ModuleHolder o) {
205207
}
206208

207209
public enum Type implements Comparator<ModuleHolder> {
208-
SEPARATOR(R.string.loading, false) {
210+
SEPARATOR(R.string.loading, false, false) {
209211
@Override
210212
@SuppressWarnings("ConstantConditions")
211213
public int compare(ModuleHolder o1, ModuleHolder o2) {
212214
return o1.separator.compareTo(o2.separator);
213215
}
214216
},
215-
NOTIFICATION(R.string.loading, true) {
217+
NOTIFICATION(R.string.loading, true, false) {
216218
@Override
217219
@SuppressWarnings("ConstantConditions")
218220
public int compare(ModuleHolder o1, ModuleHolder o2) {
219221
return o1.notificationType.compareTo(o2.notificationType);
220222
}
221223
},
222-
UPDATABLE(R.string.updatable, true) {
224+
UPDATABLE(R.string.updatable, true, true) {
223225
@Override
224226
public int compare(ModuleHolder o1, ModuleHolder o2) {
225227
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
226228
if (cmp != 0) return cmp;
227229
return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated);
228230
}
229231
},
230-
INSTALLED(R.string.installed, true) {
232+
INSTALLED(R.string.installed, true, true) {
231233
@Override
232234
public int compare(ModuleHolder o1, ModuleHolder o2) {
233235
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
234236
if (cmp != 0) return cmp;
235237
return o1.getMainModuleName().compareTo(o2.getMainModuleName());
236238
}
237239
},
238-
SPECIAL_NOTIFICATIONS(R.string.loading, true),
239-
INSTALLABLE(R.string.online_repo, true) {
240+
SPECIAL_NOTIFICATIONS(R.string.loading, true, false),
241+
INSTALLABLE(R.string.online_repo, true, true) {
240242
@Override
241243
public int compare(ModuleHolder o1, ModuleHolder o2) {
242244
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
243245
if (cmp != 0) return cmp;
244246
return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated);
245247
}
246248
},
247-
FOOTER(R.string.loading, false);
249+
FOOTER(R.string.loading, false, false);
248250

249251
@StringRes
250252
public final int title;
251253
public final boolean hasBackground;
254+
public final boolean moduleHolder;
252255

253-
Type(@StringRes int title, boolean hasBackground) {
256+
Type(@StringRes int title, boolean hasBackground, boolean moduleHolder) {
254257
this.title = title;
255258
this.hasBackground = hasBackground;
259+
this.moduleHolder = moduleHolder;
256260
}
257261

258262
// Note: This method should only be called if both element have the same type
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fox2code.mmm;
2+
3+
import androidx.annotation.DrawableRes;
4+
5+
import java.util.Comparator;
6+
7+
public enum ModuleSorter implements Comparator<ModuleHolder> {
8+
UPDATE(R.drawable.ic_baseline_update_24) {
9+
@Override
10+
public ModuleSorter next() {
11+
return ALPHA;
12+
}
13+
},
14+
ALPHA(R.drawable.ic_baseline_sort_by_alpha_24) {
15+
@Override
16+
public int compare(ModuleHolder holder1, ModuleHolder holder2) {
17+
ModuleHolder.Type type1 = holder1.getType();
18+
ModuleHolder.Type type2 = holder2.getType();
19+
if (type1 == type2 && type1 == ModuleHolder.Type.INSTALLABLE) {
20+
int compare = Integer.compare(holder1.filterLevel, holder2.filterLevel);
21+
if (compare != 0) return compare;
22+
compare = holder1.getMainModuleName()
23+
.compareTo(holder2.getMainModuleName());
24+
if (compare != 0) return compare;
25+
}
26+
return super.compare(holder1, holder2);
27+
}
28+
29+
@Override
30+
public ModuleSorter next() {
31+
return UPDATE;
32+
}
33+
};
34+
35+
@DrawableRes
36+
public final int icon;
37+
38+
ModuleSorter(int icon) {
39+
this.icon = icon;
40+
}
41+
42+
@Override
43+
public int compare(ModuleHolder holder1, ModuleHolder holder2) {
44+
return holder1.compareTo(holder2);
45+
}
46+
47+
public abstract ModuleSorter next();
48+
}

app/src/main/java/com/fox2code/mmm/ModuleViewAdapter.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ public ViewHolder(@NonNull View itemView) {
9393
// Apply default
9494
this.cardView.setOnClickListener(v -> {
9595
ModuleHolder moduleHolder = this.moduleHolder;
96-
if (moduleHolder != null &&
97-
moduleHolder.notificationType != null) {
98-
View.OnClickListener onClickListener =
99-
moduleHolder.notificationType.onClickListener;
100-
if (onClickListener != null)
96+
if (moduleHolder != null) {
97+
View.OnClickListener onClickListener = moduleHolder.onClickListener;
98+
if (onClickListener != null) {
10199
onClickListener.onClick(v);
100+
} else if (moduleHolder.notificationType != null) {
101+
onClickListener = moduleHolder.notificationType.onClickListener;
102+
if (onClickListener != null) onClickListener.onClick(v);
103+
}
102104
}
103105
});
104106
this.switchMaterial.setEnabled(false);
@@ -239,9 +241,14 @@ public boolean update(ModuleHolder moduleHolder) {
239241
this.titleText.setTypeface(Typeface.DEFAULT);
240242
}
241243
} else {
242-
this.buttonAction.setVisibility(
243-
type == ModuleHolder.Type.NOTIFICATION ?
244-
View.VISIBLE : View.GONE);
244+
if (type == ModuleHolder.Type.SEPARATOR && moduleHolder.filterLevel != 0) {
245+
this.buttonAction.setVisibility(View.VISIBLE);
246+
this.buttonAction.setImageResource(moduleHolder.filterLevel);
247+
} else {
248+
this.buttonAction.setVisibility(
249+
type == ModuleHolder.Type.NOTIFICATION ?
250+
View.VISIBLE : View.GONE);
251+
}
245252
this.switchMaterial.setVisibility(View.GONE);
246253
this.creditText.setVisibility(View.GONE);
247254
this.descriptionText.setVisibility(View.GONE);
@@ -258,11 +265,13 @@ public boolean update(ModuleHolder moduleHolder) {
258265
NotificationType notificationType = moduleHolder.notificationType;
259266
this.titleText.setText(notificationType.textId);
260267
this.buttonAction.setImageResource(notificationType.iconId);
261-
this.cardView.setClickable(notificationType.onClickListener != null);
268+
this.cardView.setClickable(
269+
notificationType.onClickListener != null ||
270+
moduleHolder.onClickListener != null);
262271
this.titleText.setTypeface(notificationType.special ?
263272
Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
264273
} else {
265-
this.cardView.setClickable(false);
274+
this.cardView.setClickable(moduleHolder.onClickListener != null);
266275
this.titleText.setTypeface(Typeface.DEFAULT);
267276
}
268277
}

app/src/main/java/com/fox2code/mmm/ModuleViewListBuilder.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public class ModuleViewListBuilder {
3030
private final Activity activity;
3131
@NonNull
3232
private String query = "";
33-
private boolean noUpdate;
33+
private boolean updating;
3434
private int footerPx;
35+
private ModuleSorter moduleSorter = ModuleSorter.UPDATE;
3536

3637
public ModuleViewListBuilder(Activity activity) {
3738
this.activity = activity;
@@ -94,9 +95,9 @@ public void appendRemoteModules() {
9495
}
9596
}
9697

97-
public void applyTo(RecyclerView moduleList, ModuleViewAdapter moduleViewAdapter) {
98-
if (this.noUpdate) return;
99-
this.noUpdate = true;
98+
public void applyTo(final RecyclerView moduleList,final ModuleViewAdapter moduleViewAdapter) {
99+
if (this.updating) return;
100+
this.updating = true;
100101
final ArrayList<ModuleHolder> moduleHolders;
101102
final int newNotificationsLen;
102103
try {
@@ -128,22 +129,35 @@ public void applyTo(RecyclerView moduleList, ModuleViewAdapter moduleViewAdapter
128129
ModuleHolder.Type type = moduleHolder.getType();
129130
if (matchFilter(moduleHolder)) {
130131
if (headerTypes.add(type)) {
131-
moduleHolders.add(new ModuleHolder(type));
132+
ModuleHolder separator = new ModuleHolder(type);
133+
if (type == ModuleHolder.Type.INSTALLABLE) {
134+
ModuleSorter moduleSorter = this.moduleSorter;
135+
separator.filterLevel = this.moduleSorter.icon;
136+
separator.onClickListener = v -> {
137+
if (this.updating || this.moduleSorter != moduleSorter)
138+
return; // Do not allow spams calls
139+
this.moduleSorter = this.moduleSorter.next();
140+
new Thread(() -> // Apply async
141+
this.applyTo(moduleList, moduleViewAdapter),
142+
"Sorter apply Thread").start();
143+
};
144+
}
145+
moduleHolders.add(separator);
132146
}
133147
moduleHolders.add(moduleHolder);
134148
}
135149
}
136150
}
137151
}
138-
Collections.sort(moduleHolders, ModuleHolder::compareTo);
152+
Collections.sort(moduleHolders, this.moduleSorter);
139153
if (this.footerPx != 0) { // Footer is always last
140154
moduleHolders.add(new ModuleHolder(this.footerPx));
141155
}
142156
Log.i(TAG, "Got " + moduleHolders.size() + " entries!");
143157
// Build end
144158
}
145159
} finally {
146-
this.noUpdate = false;
160+
this.updating = false;
147161
}
148162
this.activity.runOnUiThread(() -> {
149163
final EnumSet<NotificationType> oldNotifications =
@@ -272,7 +286,6 @@ public void setFooterPx(int footerPx) {
272286
synchronized (this.updateLock) {
273287
this.footerPx = footerPx;
274288
}
275-
this.noUpdate = false;
276289
}
277290
}
278291
}

app/src/main/res/values/themes.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<item name="android:activityCloseExitAnimation">@*android:anim/slide_out_right</item> -->
2222
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
2323
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
24-
<item name="android:dialogCornerRadius" tools:targetApi="p">8dp</item>
25-
<item name="dialogCornerRadius">8dp</item>
24+
<item name="android:dialogCornerRadius" tools:targetApi="p">@dimen/card_corner_radius</item>
25+
<item name="dialogCornerRadius">@dimen/card_corner_radius</item>
2626
</style>
2727

2828
<!-- Base application theme. -->
@@ -48,8 +48,8 @@
4848
<item name="android:activityCloseExitAnimation">@*android:anim/slide_out_right</item> -->
4949
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
5050
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
51-
<item name="android:dialogCornerRadius" tools:targetApi="p">6dp</item>
52-
<item name="dialogCornerRadius">6dp</item>
51+
<item name="android:dialogCornerRadius" tools:targetApi="p">@dimen/card_corner_radius</item>
52+
<item name="dialogCornerRadius">@dimen/card_corner_radius</item>
5353
</style>
5454

5555
<!-- Base application theme. -->

0 commit comments

Comments
 (0)