aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/list
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/list')
-rw-r--r--main/src/cgeo/geocaching/list/AbstractList.java4
-rw-r--r--main/src/cgeo/geocaching/list/PseudoList.java31
-rw-r--r--main/src/cgeo/geocaching/list/StoredList.java73
3 files changed, 77 insertions, 31 deletions
diff --git a/main/src/cgeo/geocaching/list/AbstractList.java b/main/src/cgeo/geocaching/list/AbstractList.java
index ec783eb..06f44a2 100644
--- a/main/src/cgeo/geocaching/list/AbstractList.java
+++ b/main/src/cgeo/geocaching/list/AbstractList.java
@@ -20,6 +20,10 @@ public abstract class AbstractList {
public abstract boolean isConcrete();
+ public abstract String getTitle();
+
+ public abstract int getNumberOfCaches();
+
@Nullable
public static AbstractList getListById(int listId) {
return LISTS.get(listId);
diff --git a/main/src/cgeo/geocaching/list/PseudoList.java b/main/src/cgeo/geocaching/list/PseudoList.java
index f2cc7ed..9ee920c 100644
--- a/main/src/cgeo/geocaching/list/PseudoList.java
+++ b/main/src/cgeo/geocaching/list/PseudoList.java
@@ -1,32 +1,48 @@
package cgeo.geocaching.list;
import cgeo.geocaching.CgeoApplication;
+import cgeo.geocaching.DataStore;
import cgeo.geocaching.R;
-public class PseudoList extends AbstractList {
+public abstract class PseudoList extends AbstractList {
private static final int ALL_LIST_ID = 2;
/**
* list entry to show all caches
*/
- public static final PseudoList ALL_LIST = new PseudoList(ALL_LIST_ID, R.string.list_all_lists);
+ public static final PseudoList ALL_LIST = new PseudoList(ALL_LIST_ID, R.string.list_all_lists) {
+ @Override
+ public int getNumberOfCaches() {
+ return DataStore.getAllCachesCount();
+ }
+ };
private static final int NEW_LIST_ID = 3;
/**
* list entry to create a new list
*/
- public static final AbstractList NEW_LIST = new PseudoList(NEW_LIST_ID, R.string.list_menu_create);
+ public static final AbstractList NEW_LIST = new PseudoList(NEW_LIST_ID, R.string.list_menu_create) {
+ @Override
+ public int getNumberOfCaches() {
+ return -1;
+ }
+ };
private static final int HISTORY_LIST_ID = 4;
/**
* list entry to create a new list
*/
- public static final AbstractList HISTORY_LIST = new PseudoList(HISTORY_LIST_ID, R.string.menu_history);
+ public static final AbstractList HISTORY_LIST = new PseudoList(HISTORY_LIST_ID, R.string.menu_history) {
+ @Override
+ public int getNumberOfCaches() {
+ return DataStore.getAllHistoryCachesCount();
+ }
+ };
/**
* private constructor to have all instances as constants in the class
*/
- private PseudoList(int id, final int titleResourceId) {
+ private PseudoList(final int id, final int titleResourceId) {
super(id, CgeoApplication.getInstance().getResources().getString(titleResourceId));
}
@@ -36,6 +52,11 @@ public class PseudoList extends AbstractList {
}
@Override
+ public String getTitle() {
+ return title;
+ }
+
+ @Override
public boolean isConcrete() {
return false;
}
diff --git a/main/src/cgeo/geocaching/list/StoredList.java b/main/src/cgeo/geocaching/list/StoredList.java
index e557fc8..6dac1a7 100644
--- a/main/src/cgeo/geocaching/list/StoredList.java
+++ b/main/src/cgeo/geocaching/list/StoredList.java
@@ -16,6 +16,7 @@ import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
+import java.lang.ref.WeakReference;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
@@ -58,12 +59,12 @@ public final class StoredList extends AbstractList {
}
public static class UserInterface {
- private final Activity activity;
+ private final WeakReference<Activity> activityRef;
private final CgeoApplication app;
private final Resources res;
- public UserInterface(final Activity activity) {
- this.activity = activity;
+ public UserInterface(final @NonNull Activity activity) {
+ this.activityRef = new WeakReference<Activity>(activity);
app = CgeoApplication.getInstance();
res = app.getResources();
}
@@ -77,25 +78,7 @@ public final class StoredList extends AbstractList {
}
public void promptForListSelection(final int titleId, @NonNull final Action1<Integer> runAfterwards, final boolean onlyConcreteLists, final int exceptListId, final String newListName) {
- final List<AbstractList> lists = new ArrayList<AbstractList>();
- lists.addAll(getSortedLists());
-
- if (exceptListId > StoredList.TEMPORARY_LIST_ID) {
- StoredList exceptList = DataStore.getList(exceptListId);
- if (exceptList != null) {
- lists.remove(exceptList);
- }
- }
-
- if (!onlyConcreteLists) {
- if (exceptListId != PseudoList.ALL_LIST.id) {
- lists.add(PseudoList.ALL_LIST);
- }
- if (exceptListId != PseudoList.HISTORY_LIST.id) {
- lists.add(PseudoList.HISTORY_LIST);
- }
- }
- lists.add(PseudoList.NEW_LIST);
+ final List<AbstractList> lists = getMenuLists(onlyConcreteLists, exceptListId);
final List<CharSequence> listsTitle = new ArrayList<CharSequence>();
for (AbstractList list : lists) {
@@ -104,6 +87,7 @@ public final class StoredList extends AbstractList {
final CharSequence[] items = new CharSequence[listsTitle.size()];
+ final Activity activity = activityRef.get();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(res.getString(titleId));
builder.setItems(listsTitle.toArray(items), new DialogInterface.OnClickListener() {
@@ -122,6 +106,31 @@ public final class StoredList extends AbstractList {
builder.create().show();
}
+ public static List<AbstractList> getMenuLists(boolean onlyConcreteLists, int exceptListId) {
+ final List<AbstractList> lists = new ArrayList<AbstractList>();
+ lists.addAll(getSortedLists());
+
+ if (exceptListId > StoredList.TEMPORARY_LIST_ID) {
+ StoredList exceptList = DataStore.getList(exceptListId);
+ if (exceptList != null) {
+ lists.remove(exceptList);
+ }
+ }
+
+ if (!onlyConcreteLists) {
+ if (exceptListId != PseudoList.ALL_LIST.id) {
+ lists.add(PseudoList.ALL_LIST);
+ }
+ if (exceptListId != PseudoList.HISTORY_LIST.id) {
+ lists.add(PseudoList.HISTORY_LIST);
+ }
+ }
+ if (exceptListId != PseudoList.NEW_LIST.id) {
+ lists.add(PseudoList.NEW_LIST);
+ }
+ return lists;
+ }
+
@NonNull
private static List<StoredList> getSortedLists() {
final Collator collator = Collator.getInstance();
@@ -151,6 +160,10 @@ public final class StoredList extends AbstractList {
@SuppressWarnings("unused")
@Override
public void call(final String listName) {
+ final Activity activity = activityRef.get();
+ if (activity == null) {
+ return;
+ }
final int newId = DataStore.createList(listName);
new StoredList(newId, listName, 0);
@@ -165,6 +178,10 @@ public final class StoredList extends AbstractList {
}
private void handleListNameInput(final String defaultValue, int dialogTitle, int buttonTitle, final Action1<String> runnable) {
+ final Activity activity = activityRef.get();
+ if (activity == null) {
+ return;
+ }
Dialogs.input(activity, dialogTitle, defaultValue, buttonTitle, new Action1<String>() {
@Override
@@ -193,14 +210,18 @@ public final class StoredList extends AbstractList {
}
/**
- * Get the list title. This method is not public by intention to make clients use the {@link UserInterface} class.
- *
- * @return
+ * Get the list title.
*/
- protected String getTitle() {
+ @Override
+ public String getTitle() {
return title;
}
+ @Override
+ public int getNumberOfCaches() {
+ return count;
+ }
+
/**
* Return the given list, if it is a concrete list. Return the default list otherwise.
*/