Skip to content

Commit 8159dd3

Browse files
authored
Merge pull request willowtreeapps#74 from ToxicBakery/feature/alphabetical-sort
willowtreeapps#73 Alphabetically sort modules in the hyperion list
2 parents fc4219e + 642ea86 commit 8159dd3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

hyperion-core/src/main/java/com/willowtreeapps/hyperion/core/internal/HyperionPluginView.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import com.willowtreeapps.hyperion.plugin.v1.MenuState;
2121
import com.willowtreeapps.hyperion.plugin.v1.PluginModule;
2222

23+
import java.util.Comparator;
2324
import java.util.Set;
25+
import java.util.TreeSet;
2426

2527
import javax.inject.Inject;
2628

@@ -98,7 +100,10 @@ public void call(Try<Plugins> result) {
98100
private void populatePluginList(Try<Plugins> result) {
99101
try {
100102
final Plugins plugins = result.get();
101-
modules = plugins.createModules();
103+
final Comparator<PluginModule> comparator = new AlphabeticalComparator(getContext());
104+
final Set<PluginModule> sortedModules = new TreeSet<>(comparator);
105+
sortedModules.addAll(plugins.createModules());
106+
this.modules = sortedModules;
102107
} catch (Throwable t) {
103108
// TODO
104109
t.printStackTrace();
@@ -125,4 +130,26 @@ protected void onDetachedFromWindow() {
125130
}
126131
pluginExtension.setHyperionMenu(null);
127132
}
133+
134+
private static final class AlphabeticalComparator implements Comparator<PluginModule> {
135+
136+
private final Context context;
137+
138+
private AlphabeticalComparator(Context context) {
139+
this.context = context;
140+
}
141+
142+
@Override
143+
public int compare(PluginModule left, PluginModule right) {
144+
String leftName = getName(left);
145+
String rightName = getName(right);
146+
return leftName.compareTo(rightName);
147+
}
148+
149+
private String getName(PluginModule pluginModule) {
150+
int resName = pluginModule.getName();
151+
if (resName == R.string.hype_module_name) return pluginModule.getClass().getSimpleName();
152+
else return context.getString(resName);
153+
}
154+
}
128155
}

hyperion-plugin/src/main/java/com/willowtreeapps/hyperion/plugin/v1/PluginModule.java

+13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import android.content.Context;
44
import android.support.annotation.NonNull;
55
import android.support.annotation.Nullable;
6+
import android.support.annotation.StringRes;
67
import android.view.LayoutInflater;
78
import android.view.View;
89
import android.view.ViewGroup;
910

11+
import com.willowtreeapps.hyperion.plugin.R;
12+
1013
public abstract class PluginModule {
1114

1215
private PluginExtension extension;
@@ -18,6 +21,16 @@ public final void create(PluginExtension extension, Context context) {
1821
onCreate();
1922
}
2023

24+
/**
25+
* The name of the plugin module. This should be the same name as displayed by the returned plugin view.
26+
*
27+
* @return the name of the plugin module
28+
*/
29+
@StringRes
30+
public int getName() {
31+
return R.string.hype_module_name;
32+
}
33+
2134
protected void onCreate() {
2235

2336
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="hype_module_name">Hyperion Plugin</string>
4+
</resources>

0 commit comments

Comments
 (0)