1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
package cgeo.geocaching.sorting;
import cgeo.geocaching.R;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RunnableWithArgument;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ComparatorUserInterface {
private final Activity activity;
private final ArrayList<ComparatorEntry> registry;
private final Resources res;
private static final class ComparatorEntry {
private final String name;
private final Class<? extends CacheComparator> cacheComparator;
public ComparatorEntry(final String name, final Class<? extends CacheComparator> cacheComparator) {
this.name = name;
this.cacheComparator = cacheComparator;
}
@Override
public String toString() {
return name;
}
}
public ComparatorUserInterface(final Activity activity) {
this.activity = activity;
res = activity.getResources();
registry = new ArrayList<ComparatorUserInterface.ComparatorEntry>(20);
register(R.string.caches_sort_distance, null);
register(R.string.caches_sort_date, DateComparator.class);
register(R.string.caches_sort_difficulty, DifficultyComparator.class);
register(R.string.caches_sort_finds, FindsComparator.class);
register(R.string.caches_sort_gccode, GeocodeComparator.class);
register(R.string.caches_sort_inventory, InventoryComparator.class);
register(R.string.caches_sort_name, NameComparator.class);
register(R.string.caches_sort_favorites, PopularityComparator.class);
register(R.string.caches_sort_rating, RatingComparator.class);
register(R.string.caches_sort_size, SizeComparator.class);
register(R.string.caches_sort_state, StateComparator.class);
register(R.string.caches_sort_storage, StorageTimeComparator.class);
register(R.string.caches_sort_terrain, TerrainComparator.class);
register(R.string.caches_sort_visit, VisitComparator.class);
register(R.string.caches_sort_vote, VoteComparator.class);
// sort the menu labels alphabetically for easier reading
Collections.sort(registry, new Comparator<ComparatorEntry>() {
@Override
public int compare(ComparatorEntry lhs, ComparatorEntry rhs) {
return lhs.name.compareToIgnoreCase(rhs.name);
}
});
}
private void register(final int resourceId, Class<? extends CacheComparator> comparatorClass) {
registry.add(new ComparatorEntry(res.getString(resourceId), comparatorClass));
}
public void selectComparator(final CacheComparator current, final RunnableWithArgument<CacheComparator> runAfterwards) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.caches_sort_title);
// adapter doesn't work correctly here, therefore using the string array based method
final String[] items = new String[registry.size()];
for (int i = 0; i < items.length; i++) {
items[i] = registry.get(i).name;
}
builder.setSingleChoiceItems(items, getCurrentIndex(current), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int itemIndex) {
ComparatorEntry entry = registry.get(itemIndex);
try {
if (entry.cacheComparator == null) {
runAfterwards.run(null);
}
else {
CacheComparator comparator = entry.cacheComparator.newInstance();
runAfterwards.run(comparator);
}
} catch (Exception e) {
Log.e("selectComparator", e);
}
dialog.dismiss();
}
});
builder.create().show();
}
private int getCurrentIndex(final CacheComparator current) {
for (int index = 0; index < registry.size(); index++) {
final ComparatorEntry entry = registry.get(index);
if (current == null) {
if (entry.cacheComparator == null) {
return index;
}
}
else if (current.getClass().equals(entry.cacheComparator)) {
return index;
}
}
return -1;
}
}
|