aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2013-09-14 14:21:22 +0200
committerBananeweizen <bananeweizen@gmx.de>2013-09-14 14:21:34 +0200
commit947792ed914e4309ecb5159e9878ed15ea1125c9 (patch)
treec62689ef2b6ffa7bb1568e9525ed2760e45aec9a /main/src
parent3d5d4c46327244244003982f71aa627d1407d5c3 (diff)
downloadcgeo-947792ed914e4309ecb5159e9878ed15ea1125c9.zip
cgeo-947792ed914e4309ecb5159e9878ed15ea1125c9.tar.gz
cgeo-947792ed914e4309ecb5159e9878ed15ea1125c9.tar.bz2
refactoring: StoredList
* try more encapsulation instead of ID comparisons all time
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/DataStore.java17
-rw-r--r--main/src/cgeo/geocaching/list/AbstractList.java15
-rw-r--r--main/src/cgeo/geocaching/list/PseudoList.java21
-rw-r--r--main/src/cgeo/geocaching/list/StoredList.java12
4 files changed, 53 insertions, 12 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java
index f151ce3..507b042 100644
--- a/main/src/cgeo/geocaching/DataStore.java
+++ b/main/src/cgeo/geocaching/DataStore.java
@@ -13,6 +13,7 @@ import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Viewport;
+import cgeo.geocaching.list.AbstractList;
import cgeo.geocaching.list.PseudoList;
import cgeo.geocaching.list.StoredList;
import cgeo.geocaching.settings.Settings;
@@ -2021,7 +2022,7 @@ public class DataStore {
reasonIndex = 1;
}
String listKey;
- if (list == PseudoList.ALL_LIST_ID) {
+ if (list == PseudoList.ALL_LIST.id) {
sql.append(" and reason > 0");
listKey = "all_list";
} else {
@@ -2035,7 +2036,7 @@ public class DataStore {
if (cacheType != CacheType.ALL) {
compiledStmnt.bindString(1, cacheType.id);
}
- if (list != PseudoList.ALL_LIST_ID) {
+ if (list != PseudoList.ALL_LIST.id) {
compiledStmnt.bindLong(reasonIndex, list);
}
return (int) compiledStmnt.simpleQueryForLong();
@@ -2078,7 +2079,7 @@ public class DataStore {
final StringBuilder selection = new StringBuilder();
selection.append("reason ");
- selection.append(listId != PseudoList.ALL_LIST_ID ? "=" + Math.max(listId, 1) : ">= " + StoredList.STANDARD_LIST_ID);
+ selection.append(listId != PseudoList.ALL_LIST.id ? "=" + Math.max(listId, 1) : ">= " + StoredList.STANDARD_LIST_ID);
selection.append(" and detailed = 1 ");
String[] selectionArgs = null;
@@ -2539,8 +2540,8 @@ public class DataStore {
}
Resources res = CgeoApplication.getInstance().getResources();
- if (id == PseudoList.ALL_LIST_ID) {
- return new StoredList(PseudoList.ALL_LIST_ID, res.getString(R.string.list_all_lists), getAllCachesCount());
+ if (id == PseudoList.ALL_LIST.id) {
+ return new StoredList(PseudoList.ALL_LIST.id, res.getString(R.string.list_all_lists), getAllCachesCount());
}
// fall back to standard list in case of invalid list id
@@ -2655,7 +2656,11 @@ public class DataStore {
}
public static void moveToList(final List<Geocache> caches, final int listId) {
- if (listId == PseudoList.ALL_LIST_ID) {
+ final AbstractList list = AbstractList.getListById(listId);
+ if (list == null) {
+ return;
+ }
+ if (!list.isConcrete()) {
return;
}
if (caches.isEmpty()) {
diff --git a/main/src/cgeo/geocaching/list/AbstractList.java b/main/src/cgeo/geocaching/list/AbstractList.java
index c75c6a1..ec783eb 100644
--- a/main/src/cgeo/geocaching/list/AbstractList.java
+++ b/main/src/cgeo/geocaching/list/AbstractList.java
@@ -1,15 +1,28 @@
package cgeo.geocaching.list;
-abstract class AbstractList {
+import org.eclipse.jdt.annotation.Nullable;
+
+import android.util.SparseArray;
+
+public abstract class AbstractList {
public final int id;
public final String title;
+ private static SparseArray<AbstractList> LISTS = new SparseArray<AbstractList>();
public AbstractList(final int id, final String title) {
this.id = id;
this.title = title;
+ LISTS.put(id, this);
}
public abstract String getTitleAndCount();
+ public abstract boolean isConcrete();
+
+ @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 c93f011..365d6fd 100644
--- a/main/src/cgeo/geocaching/list/PseudoList.java
+++ b/main/src/cgeo/geocaching/list/PseudoList.java
@@ -5,10 +5,22 @@ import cgeo.geocaching.R;
public class PseudoList extends AbstractList {
- public static final int ALL_LIST_ID = 2;
+ 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 PseudoList(int id, final int titleResourceId) {
+ 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);
+
+ /**
+ * private constructor to have all instances as constants in the class
+ */
+ private PseudoList(int id, final int titleResourceId) {
super(id, CgeoApplication.getInstance().getResources().getString(titleResourceId));
}
@@ -17,4 +29,9 @@ public class PseudoList extends AbstractList {
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 f1632c4..feb40c7 100644
--- a/main/src/cgeo/geocaching/list/StoredList.java
+++ b/main/src/cgeo/geocaching/list/StoredList.java
@@ -89,12 +89,12 @@ public final class StoredList extends AbstractList {
if (!onlyConcreteLists) {
lists.add(PseudoList.ALL_LIST);
}
+ lists.add(PseudoList.NEW_LIST);
final List<CharSequence> listsTitle = new ArrayList<CharSequence>();
for (AbstractList list : lists) {
listsTitle.add(list.getTitleAndCount());
}
- listsTitle.add("<" + res.getString(R.string.list_menu_create) + ">");
final CharSequence[] items = new CharSequence[listsTitle.size()];
@@ -103,7 +103,8 @@ public final class StoredList extends AbstractList {
builder.setItems(listsTitle.toArray(items), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int itemId) {
- if (itemId >= lists.size()) {
+ final AbstractList list = lists.get(itemId);
+ if (list == PseudoList.NEW_LIST) {
// create new list on the fly
promptForListCreation(runAfterwards, newListName);
}
@@ -208,10 +209,15 @@ public final class StoredList extends AbstractList {
* Return the given list, if it is a concrete list. Return the default list otherwise.
*/
public static int getConcreteList(int listId) {
- if (listId == PseudoList.ALL_LIST_ID || listId == TEMPORARY_LIST_ID) {
+ if (listId == PseudoList.ALL_LIST.id || listId == TEMPORARY_LIST_ID) {
return STANDARD_LIST_ID;
}
return listId;
}
+ @Override
+ public boolean isConcrete() {
+ return true;
+ }
+
}