diff options
| -rw-r--r-- | main/src/cgeo/geocaching/DataStore.java | 17 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/list/AbstractList.java | 15 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/list/PseudoList.java | 21 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/list/StoredList.java | 12 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/list/PseudoListTest.java | 18 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/list/StoredListTest.java | 14 |
6 files changed, 81 insertions, 16 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; + } + } diff --git a/tests/src/cgeo/geocaching/list/PseudoListTest.java b/tests/src/cgeo/geocaching/list/PseudoListTest.java new file mode 100644 index 0000000..d120d81 --- /dev/null +++ b/tests/src/cgeo/geocaching/list/PseudoListTest.java @@ -0,0 +1,18 @@ +package cgeo.geocaching.list; + +import org.apache.commons.lang3.StringUtils; + +import junit.framework.TestCase; + +public class PseudoListTest extends TestCase { + + public static void testGetTitleAndCount() throws Exception { + final String title = PseudoList.ALL_LIST.title; + assertTrue("pseudo lists shall not have a number shown in their title", StringUtils.isAlpha(title.substring(1, title.length() - 1))); + } + + public static void testIsConcrete() throws Exception { + assertFalse("pseudo lists are not concrete lists", PseudoList.ALL_LIST.isConcrete()); + } + +} diff --git a/tests/src/cgeo/geocaching/list/StoredListTest.java b/tests/src/cgeo/geocaching/list/StoredListTest.java index ebe0966..985236a 100644 --- a/tests/src/cgeo/geocaching/list/StoredListTest.java +++ b/tests/src/cgeo/geocaching/list/StoredListTest.java @@ -1,21 +1,27 @@ package cgeo.geocaching.list; import cgeo.geocaching.DataStore; -import cgeo.geocaching.list.StoredList; import junit.framework.TestCase; public class StoredListTest extends TestCase { public static void testStandardListExists() { - final StoredList list = DataStore.getList(StoredList.STANDARD_LIST_ID); + final StoredList list = getStandardList(); assertNotNull(list); } + private static StoredList getStandardList() { + return DataStore.getList(StoredList.STANDARD_LIST_ID); + } + public static void testEquals() { - final StoredList list1 = DataStore.getList(StoredList.STANDARD_LIST_ID); - final StoredList list2 = DataStore.getList(StoredList.STANDARD_LIST_ID); + final StoredList list1 = getStandardList(); + final StoredList list2 = getStandardList(); assertEquals(list1, list2); } + public static void testConcrete() { + assertTrue(getStandardList().isConcrete()); + } } |
