diff options
Diffstat (limited to 'main/src/cgeo/geocaching/SearchResult.java')
-rw-r--r-- | main/src/cgeo/geocaching/SearchResult.java | 119 |
1 files changed, 71 insertions, 48 deletions
diff --git a/main/src/cgeo/geocaching/SearchResult.java b/main/src/cgeo/geocaching/SearchResult.java index fdb50a1..c1e04ef 100644 --- a/main/src/cgeo/geocaching/SearchResult.java +++ b/main/src/cgeo/geocaching/SearchResult.java @@ -1,20 +1,27 @@ package cgeo.geocaching; +import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.LoadFlag; +import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.StatusCode; +import org.apache.commons.collections.CollectionUtils; + import android.os.Parcel; import android.os.Parcelable; import java.util.ArrayList; import java.util.Collections; +import java.util.EnumSet; import java.util.HashSet; import java.util.Set; public class SearchResult implements Parcelable { - final protected Set<String> geocodes; + final private Set<String> geocodes; public StatusCode error = null; - public String url = ""; + private String url = ""; public String[] viewstates = null; public int totalCnt = 0; @@ -94,78 +101,94 @@ public class SearchResult implements Parcelable { return geocodes.size(); } - public boolean addGeocode(final String geocode) { - return geocodes.add(geocode); + public StatusCode getError() { + return error; } - public static StatusCode getError(final SearchResult search) { - if (search == null) { - return null; - } - - return search.error; + public void setError(final StatusCode error) { + this.error = error; } - public static boolean setError(final SearchResult search, final StatusCode error) { - if (search == null) { - return false; - } + public String getUrl() { + return url; + } - search.error = error; + public void setUrl(String url) { + this.url = url; + } - return true; + public String[] getViewstates() { + return viewstates; } - public static String getUrl(final SearchResult search) { - if (search == null) { - return null; + public void setViewstates(String[] viewstates) { + if (cgBase.isEmpty(viewstates)) { + return; } - return search.url; + this.viewstates = viewstates; } - public static boolean setUrl(final SearchResult search, String url) { - if (search == null) { - return false; - } + public int getTotal() { + return totalCnt; + } - search.url = url; + /** + * @param excludeDisabled + * @param excludeMine + * @param cacheType + * @return + */ + public SearchResult filterSearchResults(final boolean excludeDisabled, final boolean excludeMine, final CacheType cacheType, final int listId) { - return true; - } + SearchResult result = new SearchResult(this); + result.geocodes.clear(); - public static String[] getViewstates(final SearchResult search) { - if (search == null) { - return null; + for (final String geocode : geocodes) { + cgCache cache = cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOADCACHEORDB); + // Is there any reason to exclude the cache from the list? + final boolean excludeCache = (excludeDisabled && cache.isDisabled()) || + (excludeMine && (cache.isOwn() || cache.isFound())) || + (cacheType != CacheType.ALL && cacheType != cache.getType()); + if (!excludeCache) { + cache.setListId(listId); + result.addCache(cache); + } } - - return search.viewstates; + return result; } - public static boolean setViewstates(final SearchResult search, String[] viewstates) { - if (cgBase.isEmpty(viewstates) || search == null) { - return false; + public cgCache getFirstCacheFromResult(final EnumSet<LoadFlag> loadFlags) { + if (geocodes != null && geocodes.size() >= 1) { + return cgeoapplication.getInstance().loadCache((String) geocodes.toArray()[0], loadFlags); } + return null; + } - search.viewstates = viewstates; - - return true; + public Set<cgCache> getCachesFromSearchResult(final EnumSet<LoadFlag> loadFlags) { + return cgeoapplication.getInstance().loadCaches(geocodes, loadFlags); } - public static int getTotal(final SearchResult search) { - if (search == null) { - return 0; - } + /** Add the geocode to the search. No cache is loaded into the CacheCache */ + public boolean addGeocode(final String geocode) { + return geocodes.add(geocode); + } - return search.totalCnt; + /** Add the cache geocode to the search and store the cache in the CacheCache */ + public boolean addCache(final cgCache cache) { + addGeocode(cache.getGeocode()); + return cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVECACHE)); } - public static int getCount(final SearchResult search) { - if (search == null) { - return 0; + /** Add the cache geocodes to the search and store them in the CacheCache */ + public void addCaches(final Set<cgCache> caches, final int listId) { + if (CollectionUtils.isEmpty(caches)) { + return; } - return search.getCount(); + for (final cgCache cache : caches) { + cache.setListId(listId); + addCache(cache); + } } - } |