From 8987674ab4882f7684c773fc181f5a3fac5f8f12 Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Sat, 14 Sep 2013 07:51:24 +0200 Subject: refactoring: introduce PseudoList to handle non concrete lists * should make adding a history list entry easier * includes some non null annotations * moved list code into separate package --- main/src/cgeo/geocaching/CacheDetailActivity.java | 1 + main/src/cgeo/geocaching/CacheListActivity.java | 3 + main/src/cgeo/geocaching/CachePopup.java | 1 + main/src/cgeo/geocaching/DataStore.java | 14 +- main/src/cgeo/geocaching/Geocache.java | 1 + main/src/cgeo/geocaching/GpxFileListActivity.java | 1 + main/src/cgeo/geocaching/MainActivity.java | 1 + main/src/cgeo/geocaching/StoredList.java | 224 --------------------- .../geocaching/connector/ox/OpenCachingApi.java | 2 +- .../geocaching/files/AbstractFileListActivity.java | 2 +- main/src/cgeo/geocaching/files/GPXParser.java | 2 +- main/src/cgeo/geocaching/list/AbstractList.java | 15 ++ main/src/cgeo/geocaching/list/PseudoList.java | 20 ++ main/src/cgeo/geocaching/list/StoredList.java | 217 ++++++++++++++++++++ main/src/cgeo/geocaching/maps/CGeoMap.java | 2 +- main/src/cgeo/geocaching/network/HtmlImage.java | 2 +- main/src/cgeo/geocaching/settings/Settings.java | 2 +- main/src/cgeo/geocaching/ui/DirectionImage.java | 2 +- main/src/cgeo/geocaching/ui/ImagesList.java | 2 +- .../cgeo/geocaching/ui/logs/LogsViewCreator.java | 2 +- 20 files changed, 277 insertions(+), 239 deletions(-) delete mode 100644 main/src/cgeo/geocaching/StoredList.java create mode 100644 main/src/cgeo/geocaching/list/AbstractList.java create mode 100644 main/src/cgeo/geocaching/list/PseudoList.java create mode 100644 main/src/cgeo/geocaching/list/StoredList.java (limited to 'main/src') diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index bf94b5f..4be0ad9 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -18,6 +18,7 @@ import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.geopoint.Units; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java index 470a16a..c4c7f6e 100644 --- a/main/src/cgeo/geocaching/CacheListActivity.java +++ b/main/src/cgeo/geocaching/CacheListActivity.java @@ -16,6 +16,7 @@ import cgeo.geocaching.files.GPXImporter; import cgeo.geocaching.filter.FilterUserInterface; import cgeo.geocaching.filter.IFilter; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.loaders.AbstractSearchLoader; import cgeo.geocaching.loaders.AbstractSearchLoader.CacheListLoaderType; import cgeo.geocaching.loaders.AddressGeocacheListLoader; @@ -47,6 +48,7 @@ import ch.boye.httpclientandroidlib.HttpResponse; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; import android.app.AlertDialog; import android.app.ProgressDialog; @@ -1453,6 +1455,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA new StoredList.UserInterface(this).promptForListSelection(R.string.list_title, getListSwitchingRunnable()); } + @NonNull private RunnableWithArgument getListSwitchingRunnable() { return new RunnableWithArgument() { diff --git a/main/src/cgeo/geocaching/CachePopup.java b/main/src/cgeo/geocaching/CachePopup.java index 873801e..683ebe7 100644 --- a/main/src/cgeo/geocaching/CachePopup.java +++ b/main/src/cgeo/geocaching/CachePopup.java @@ -3,6 +3,7 @@ package cgeo.geocaching; import cgeo.geocaching.activity.Progress; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.Network; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.CacheDetailsCreator; diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java index 93067b8..f151ce3 100644 --- a/main/src/cgeo/geocaching/DataStore.java +++ b/main/src/cgeo/geocaching/DataStore.java @@ -13,6 +13,8 @@ import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.files.LocalStorage; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Viewport; +import cgeo.geocaching.list.PseudoList; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.FileUtils; import cgeo.geocaching.utils.Log; @@ -2019,7 +2021,7 @@ public class DataStore { reasonIndex = 1; } String listKey; - if (list == StoredList.ALL_LIST_ID) { + if (list == PseudoList.ALL_LIST_ID) { sql.append(" and reason > 0"); listKey = "all_list"; } else { @@ -2033,7 +2035,7 @@ public class DataStore { if (cacheType != CacheType.ALL) { compiledStmnt.bindString(1, cacheType.id); } - if (list != StoredList.ALL_LIST_ID) { + if (list != PseudoList.ALL_LIST_ID) { compiledStmnt.bindLong(reasonIndex, list); } return (int) compiledStmnt.simpleQueryForLong(); @@ -2076,7 +2078,7 @@ public class DataStore { final StringBuilder selection = new StringBuilder(); selection.append("reason "); - selection.append(listId != StoredList.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; @@ -2537,8 +2539,8 @@ public class DataStore { } Resources res = CgeoApplication.getInstance().getResources(); - if (id == StoredList.ALL_LIST_ID) { - return new StoredList(StoredList.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 @@ -2653,7 +2655,7 @@ public class DataStore { } public static void moveToList(final List caches, final int listId) { - if (listId == StoredList.ALL_LIST_ID) { + if (listId == PseudoList.ALL_LIST_ID) { return; } if (caches.isEmpty()) { diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index 8798539..373b232 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -21,6 +21,7 @@ import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.files.GPXParser; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.CancellableHandler; diff --git a/main/src/cgeo/geocaching/GpxFileListActivity.java b/main/src/cgeo/geocaching/GpxFileListActivity.java index 8b10d5b..dae52c4 100644 --- a/main/src/cgeo/geocaching/GpxFileListActivity.java +++ b/main/src/cgeo/geocaching/GpxFileListActivity.java @@ -4,6 +4,7 @@ import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.connector.IConnector; import cgeo.geocaching.files.AbstractFileListActivity; import cgeo.geocaching.files.GPXImporter; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.GPXListAdapter; diff --git a/main/src/cgeo/geocaching/MainActivity.java b/main/src/cgeo/geocaching/MainActivity.java index 1db0cd4..16ce0f9 100644 --- a/main/src/cgeo/geocaching/MainActivity.java +++ b/main/src/cgeo/geocaching/MainActivity.java @@ -10,6 +10,7 @@ import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Units; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.maps.CGeoMap; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.settings.SettingsActivity; diff --git a/main/src/cgeo/geocaching/StoredList.java b/main/src/cgeo/geocaching/StoredList.java deleted file mode 100644 index 367ddcb..0000000 --- a/main/src/cgeo/geocaching/StoredList.java +++ /dev/null @@ -1,224 +0,0 @@ -package cgeo.geocaching; - -import cgeo.geocaching.activity.ActivityMixin; -import cgeo.geocaching.utils.RunnableWithArgument; - -import org.apache.commons.lang3.StringUtils; -import org.eclipse.jdt.annotation.NonNull; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.res.Resources; -import android.view.View; -import android.widget.EditText; - -import java.text.Collator; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -public final class StoredList { - public static final int TEMPORARY_LIST_ID = 0; - public static final int STANDARD_LIST_ID = 1; - public static final int ALL_LIST_ID = 2; - - public final int id; - public final String title; - private final int count; // this value is only valid as long as the list is not changed by other database operations - - public StoredList(int id, String title, int count) { - this.id = id; - this.title = title; - this.count = count; - } - - public String getTitleAndCount() { - return title + " [" + count + "]"; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + id; - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof StoredList)) { - return false; - } - return id == ((StoredList) obj).id; - } - - public static class UserInterface { - private final Activity activity; - private final CgeoApplication app; - private final Resources res; - - public UserInterface(final Activity activity) { - this.activity = activity; - app = CgeoApplication.getInstance(); - res = app.getResources(); - } - - public void promptForListSelection(final int titleId, final RunnableWithArgument runAfterwards) { - promptForListSelection(titleId, runAfterwards, false, -1); - } - - public void promptForListSelection(final int titleId, final RunnableWithArgument runAfterwards, final boolean onlyConcreteLists, final int exceptListId) { - promptForListSelection(titleId, runAfterwards, onlyConcreteLists, exceptListId, StringUtils.EMPTY); - } - - public void promptForListSelection(final int titleId, final RunnableWithArgument runAfterwards, final boolean onlyConcreteLists, final int exceptListId, final String newListName) { - final List lists = getSortedLists(); - - if (exceptListId > StoredList.TEMPORARY_LIST_ID) { - StoredList exceptList = DataStore.getList(exceptListId); - if (exceptList != null) { - lists.remove(exceptList); - } - } - - final List listsTitle = new ArrayList(); - for (StoredList list : lists) { - listsTitle.add(list.getTitleAndCount()); - } - if (!onlyConcreteLists) { - listsTitle.add("<" + res.getString(R.string.list_menu_all_lists) + ">"); - } - listsTitle.add("<" + res.getString(R.string.list_menu_create) + ">"); - - final CharSequence[] items = new CharSequence[listsTitle.size()]; - - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(res.getString(titleId)); - builder.setItems(listsTitle.toArray(items), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int itemId) { - if (itemId == lists.size() && !onlyConcreteLists) { - // all lists - runAfterwards.run(StoredList.ALL_LIST_ID); - } else if (itemId >= lists.size()) { - // create new list on the fly - promptForListCreation(runAfterwards, newListName); - } - else { - if (runAfterwards != null) { - runAfterwards.run(lists.get(itemId).id); - } - } - } - }); - builder.create().show(); - } - - @NonNull - private static List getSortedLists() { - final Collator collator = Collator.getInstance(); - final List lists = DataStore.getLists(); - Collections.sort(lists, new Comparator() { - - @Override - public int compare(StoredList lhs, StoredList rhs) { - // have the standard list at the top - if (lhs.id == STANDARD_LIST_ID) { - return -1; - } - if (rhs.id == STANDARD_LIST_ID) { - return 1; - } - // otherwise sort alphabetical - return collator.compare(lhs.getTitle(), rhs.getTitle()); - } - }); - return lists; - } - - public void promptForListCreation(final RunnableWithArgument runAfterwards, String newListName) { - handleListNameInput(newListName, R.string.list_dialog_create_title, R.string.list_dialog_create, new RunnableWithArgument() { - - @Override - public void run(final String listName) { - final int newId = DataStore.createList(listName); - - if (newId >= DataStore.customListIdOffset) { - ActivityMixin.showToast(activity, res.getString(R.string.list_dialog_create_ok)); - if (runAfterwards != null) { - runAfterwards.run(newId); - } - } else { - ActivityMixin.showToast(activity, res.getString(R.string.list_dialog_create_err)); - } - } - }); - } - - private void handleListNameInput(final String defaultValue, int dialogTitle, int buttonTitle, final RunnableWithArgument runnable) { - final AlertDialog.Builder alert = new AlertDialog.Builder(activity); - final View view = activity.getLayoutInflater().inflate(R.layout.list_create_dialog, null); - final EditText input = (EditText) view.findViewById(R.id.text); - input.setText(defaultValue); - - alert.setTitle(dialogTitle); - alert.setView(view); - alert.setPositiveButton(buttonTitle, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - // remove whitespaces added by autocompletion of Android keyboard - String listName = StringUtils.trim(input.getText().toString()); - if (StringUtils.isNotBlank(listName)) { - runnable.run(listName); - } - } - }); - alert.setNegativeButton(res.getString(R.string.list_dialog_cancel), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - dialog.dismiss(); - } - }); - - alert.show(); - } - - public void promptForListRename(final int listId, final Runnable runAfterRename) { - final StoredList list = DataStore.getList(listId); - handleListNameInput(list.title, R.string.list_dialog_rename_title, R.string.list_dialog_rename, new RunnableWithArgument() { - - @Override - public void run(final String listName) { - DataStore.renameList(listId, listName); - if (runAfterRename != null) { - runAfterRename.run(); - } - } - }); - } - } - - /** - * Get the list title. This method is not public by intention to make clients use the {@link UserInterface} class. - * - * @return - */ - protected String getTitle() { - return title; - } - - /** - * Return the given list, if it is a concrete list. Return the default list otherwise. - */ - public static int getConcreteList(int listId) { - if (listId == ALL_LIST_ID || listId == TEMPORARY_LIST_ID) { - return STANDARD_LIST_ID; - } - return listId; - } -} diff --git a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java index f25e289..40b27f0 100644 --- a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java +++ b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java @@ -1,9 +1,9 @@ package cgeo.geocaching.connector.ox; import cgeo.geocaching.Geocache; -import cgeo.geocaching.StoredList; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; import cgeo.geocaching.utils.CryptUtils; diff --git a/main/src/cgeo/geocaching/files/AbstractFileListActivity.java b/main/src/cgeo/geocaching/files/AbstractFileListActivity.java index b0aba58..012f8b3 100644 --- a/main/src/cgeo/geocaching/files/AbstractFileListActivity.java +++ b/main/src/cgeo/geocaching/files/AbstractFileListActivity.java @@ -2,8 +2,8 @@ package cgeo.geocaching.files; import cgeo.geocaching.Intents; import cgeo.geocaching.R; -import cgeo.geocaching.StoredList; import cgeo.geocaching.activity.AbstractListActivity; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.utils.FileUtils; import cgeo.geocaching.utils.Log; diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index 686bfb1..dc21552 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -4,7 +4,6 @@ import cgeo.geocaching.DataStore; import cgeo.geocaching.Geocache; import cgeo.geocaching.LogEntry; import cgeo.geocaching.R; -import cgeo.geocaching.StoredList; import cgeo.geocaching.Trackable; import cgeo.geocaching.Waypoint; import cgeo.geocaching.CgeoApplication; @@ -17,6 +16,7 @@ import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.MatcherWrapper; diff --git a/main/src/cgeo/geocaching/list/AbstractList.java b/main/src/cgeo/geocaching/list/AbstractList.java new file mode 100644 index 0000000..c75c6a1 --- /dev/null +++ b/main/src/cgeo/geocaching/list/AbstractList.java @@ -0,0 +1,15 @@ +package cgeo.geocaching.list; + +abstract class AbstractList { + + public final int id; + public final String title; + + public AbstractList(final int id, final String title) { + this.id = id; + this.title = title; + } + + public abstract String getTitleAndCount(); + +} diff --git a/main/src/cgeo/geocaching/list/PseudoList.java b/main/src/cgeo/geocaching/list/PseudoList.java new file mode 100644 index 0000000..c93f011 --- /dev/null +++ b/main/src/cgeo/geocaching/list/PseudoList.java @@ -0,0 +1,20 @@ +package cgeo.geocaching.list; + +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.R; + +public class PseudoList extends AbstractList { + + public static final int ALL_LIST_ID = 2; + public static final PseudoList ALL_LIST = new PseudoList(ALL_LIST_ID, R.string.list_all_lists); + + public PseudoList(int id, final int titleResourceId) { + super(id, CgeoApplication.getInstance().getResources().getString(titleResourceId)); + } + + @Override + public String getTitleAndCount() { + return "<" + title + ">"; + } + +} diff --git a/main/src/cgeo/geocaching/list/StoredList.java b/main/src/cgeo/geocaching/list/StoredList.java new file mode 100644 index 0000000..f1632c4 --- /dev/null +++ b/main/src/cgeo/geocaching/list/StoredList.java @@ -0,0 +1,217 @@ +package cgeo.geocaching.list; + +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.DataStore; +import cgeo.geocaching.R; +import cgeo.geocaching.activity.ActivityMixin; +import cgeo.geocaching.utils.RunnableWithArgument; + +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.res.Resources; +import android.view.View; +import android.widget.EditText; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public final class StoredList extends AbstractList { + public static final int TEMPORARY_LIST_ID = 0; + public static final int STANDARD_LIST_ID = 1; + private final int count; // this value is only valid as long as the list is not changed by other database operations + + public StoredList(int id, String title, int count) { + super(id, title); + this.count = count; + } + + @Override + public String getTitleAndCount() { + return title + " [" + count + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + id; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof StoredList)) { + return false; + } + return id == ((StoredList) obj).id; + } + + public static class UserInterface { + private final Activity activity; + private final CgeoApplication app; + private final Resources res; + + public UserInterface(final Activity activity) { + this.activity = activity; + app = CgeoApplication.getInstance(); + res = app.getResources(); + } + + public void promptForListSelection(final int titleId, @NonNull final RunnableWithArgument runAfterwards) { + promptForListSelection(titleId, runAfterwards, false, -1); + } + + public void promptForListSelection(final int titleId, @NonNull final RunnableWithArgument runAfterwards, final boolean onlyConcreteLists, final int exceptListId) { + promptForListSelection(titleId, runAfterwards, onlyConcreteLists, exceptListId, StringUtils.EMPTY); + } + + public void promptForListSelection(final int titleId, @NonNull final RunnableWithArgument runAfterwards, final boolean onlyConcreteLists, final int exceptListId, final String newListName) { + final List lists = new ArrayList(); + lists.addAll(getSortedLists()); + + if (exceptListId > StoredList.TEMPORARY_LIST_ID) { + StoredList exceptList = DataStore.getList(exceptListId); + if (exceptList != null) { + lists.remove(exceptList); + } + } + + if (!onlyConcreteLists) { + lists.add(PseudoList.ALL_LIST); + } + + final List listsTitle = new ArrayList(); + for (AbstractList list : lists) { + listsTitle.add(list.getTitleAndCount()); + } + listsTitle.add("<" + res.getString(R.string.list_menu_create) + ">"); + + final CharSequence[] items = new CharSequence[listsTitle.size()]; + + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(res.getString(titleId)); + builder.setItems(listsTitle.toArray(items), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int itemId) { + if (itemId >= lists.size()) { + // create new list on the fly + promptForListCreation(runAfterwards, newListName); + } + else { + runAfterwards.run(lists.get(itemId).id); + } + } + }); + builder.create().show(); + } + + @NonNull + private static List getSortedLists() { + final Collator collator = Collator.getInstance(); + final List lists = DataStore.getLists(); + Collections.sort(lists, new Comparator() { + + @Override + public int compare(StoredList lhs, StoredList rhs) { + // have the standard list at the top + if (lhs.id == STANDARD_LIST_ID) { + return -1; + } + if (rhs.id == STANDARD_LIST_ID) { + return 1; + } + // otherwise sort alphabetical + return collator.compare(lhs.getTitle(), rhs.getTitle()); + } + }); + return lists; + } + + public void promptForListCreation(@NonNull final RunnableWithArgument runAfterwards, String newListName) { + handleListNameInput(newListName, R.string.list_dialog_create_title, R.string.list_dialog_create, new RunnableWithArgument() { + + @Override + public void run(final String listName) { + final int newId = DataStore.createList(listName); + + if (newId >= DataStore.customListIdOffset) { + ActivityMixin.showToast(activity, res.getString(R.string.list_dialog_create_ok)); + runAfterwards.run(newId); + } else { + ActivityMixin.showToast(activity, res.getString(R.string.list_dialog_create_err)); + } + } + }); + } + + private void handleListNameInput(final String defaultValue, int dialogTitle, int buttonTitle, final RunnableWithArgument runnable) { + final AlertDialog.Builder alert = new AlertDialog.Builder(activity); + final View view = activity.getLayoutInflater().inflate(R.layout.list_create_dialog, null); + final EditText input = (EditText) view.findViewById(R.id.text); + input.setText(defaultValue); + + alert.setTitle(dialogTitle); + alert.setView(view); + alert.setPositiveButton(buttonTitle, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { + // remove whitespaces added by autocompletion of Android keyboard + String listName = StringUtils.trim(input.getText().toString()); + if (StringUtils.isNotBlank(listName)) { + runnable.run(listName); + } + } + }); + alert.setNegativeButton(res.getString(R.string.list_dialog_cancel), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { + dialog.dismiss(); + } + }); + + alert.show(); + } + + public void promptForListRename(final int listId, @NonNull final Runnable runAfterRename) { + final StoredList list = DataStore.getList(listId); + handleListNameInput(list.title, R.string.list_dialog_rename_title, R.string.list_dialog_rename, new RunnableWithArgument() { + + @Override + public void run(final String listName) { + DataStore.renameList(listId, listName); + runAfterRename.run(); + } + }); + } + } + + /** + * Get the list title. This method is not public by intention to make clients use the {@link UserInterface} class. + * + * @return + */ + protected String getTitle() { + return title; + } + + /** + * 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) { + return STANDARD_LIST_ID; + } + return listId; + } + +} diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 78c1571..ec90c8f 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -6,7 +6,6 @@ import cgeo.geocaching.Geocache; import cgeo.geocaching.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.SearchResult; -import cgeo.geocaching.StoredList; import cgeo.geocaching.Waypoint; import cgeo.geocaching.DataStore; import cgeo.geocaching.CacheListActivity; @@ -22,6 +21,7 @@ import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Viewport; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; import cgeo.geocaching.maps.interfaces.GeoPointImpl; import cgeo.geocaching.maps.interfaces.MapActivityImpl; diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index f7fbb30..953d762 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -2,10 +2,10 @@ package cgeo.geocaching.network; import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.R; -import cgeo.geocaching.StoredList; import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.files.LocalStorage; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.utils.FileUtils; import cgeo.geocaching.utils.IOUtils; import cgeo.geocaching.utils.ImageUtils; diff --git a/main/src/cgeo/geocaching/settings/Settings.java b/main/src/cgeo/geocaching/settings/Settings.java index 0479311..aaf89b0 100644 --- a/main/src/cgeo/geocaching/settings/Settings.java +++ b/main/src/cgeo/geocaching/settings/Settings.java @@ -2,7 +2,6 @@ package cgeo.geocaching.settings; import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.R; -import cgeo.geocaching.StoredList; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory.NavigationAppsEnum; import cgeo.geocaching.connector.gc.GCConstants; import cgeo.geocaching.connector.gc.Login; @@ -10,6 +9,7 @@ import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LiveMapStrategy.Strategy; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.maps.MapProviderFactory; import cgeo.geocaching.maps.google.GoogleMapProvider; import cgeo.geocaching.maps.interfaces.GeoPointImpl; diff --git a/main/src/cgeo/geocaching/ui/DirectionImage.java b/main/src/cgeo/geocaching/ui/DirectionImage.java index 9de360c..cd7695e 100644 --- a/main/src/cgeo/geocaching/ui/DirectionImage.java +++ b/main/src/cgeo/geocaching/ui/DirectionImage.java @@ -1,6 +1,6 @@ package cgeo.geocaching.ui; -import cgeo.geocaching.StoredList; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import org.apache.commons.lang3.StringUtils; diff --git a/main/src/cgeo/geocaching/ui/ImagesList.java b/main/src/cgeo/geocaching/ui/ImagesList.java index 0f860c4..fdff701 100644 --- a/main/src/cgeo/geocaching/ui/ImagesList.java +++ b/main/src/cgeo/geocaching/ui/ImagesList.java @@ -2,8 +2,8 @@ package cgeo.geocaching.ui; import cgeo.geocaching.Image; import cgeo.geocaching.R; -import cgeo.geocaching.StoredList; import cgeo.geocaching.files.LocalStorage; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.utils.IOUtils; import cgeo.geocaching.utils.Log; diff --git a/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java b/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java index 4572bc6..15634d3 100644 --- a/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java +++ b/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java @@ -4,9 +4,9 @@ import cgeo.geocaching.Image; import cgeo.geocaching.ImagesActivity; import cgeo.geocaching.LogEntry; import cgeo.geocaching.R; -import cgeo.geocaching.StoredList; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.Progress; +import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.ui.AbstractCachingPageViewCreator; import cgeo.geocaching.ui.AnchorAwareLinkMovementMethod; -- cgit v1.1