diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2011-11-15 19:46:46 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2011-11-15 19:46:46 +0100 |
commit | 2d9773fa90cba4bafff3bd71b9b7d3baaaff4766 (patch) | |
tree | f3136e74ae2f759a17409479df09c31a0ab74f4f /main/src/cgeo/geocaching | |
parent | d52238fdbc20aefec62edb384e548172d5999b2b (diff) | |
download | cgeo-2d9773fa90cba4bafff3bd71b9b7d3baaaff4766.zip cgeo-2d9773fa90cba4bafff3bd71b9b7d3baaaff4766.tar.gz cgeo-2d9773fa90cba4bafff3bd71b9b7d3baaaff4766.tar.bz2 |
refactor GCVote ratings, move into own package
Diffstat (limited to 'main/src/cgeo/geocaching')
-rw-r--r-- | main/src/cgeo/geocaching/VisitCacheActivity.java | 1 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 24 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgRating.java | 7 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeopopup.java | 10 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/gcvote/GCVote.java (renamed from main/src/cgeo/geocaching/GCVote.java) | 61 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/gcvote/GCVoteRating.java | 25 |
6 files changed, 79 insertions, 49 deletions
diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java index 022209f..c93eb1f 100644 --- a/main/src/cgeo/geocaching/VisitCacheActivity.java +++ b/main/src/cgeo/geocaching/VisitCacheActivity.java @@ -3,6 +3,7 @@ package cgeo.geocaching; import cgeo.geocaching.LogTemplateProvider.LogTemplate; import cgeo.geocaching.enumerations.LogTypeTrackable; import cgeo.geocaching.enumerations.StatusCode; +import cgeo.geocaching.gcvote.GCVote; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 8470f2a..5e41083 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -9,6 +9,8 @@ import cgeo.geocaching.enumerations.LogTypeTrackable; import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.files.LocParser; +import cgeo.geocaching.gcvote.GCVote; +import cgeo.geocaching.gcvote.GCVoteRating; import cgeo.geocaching.geopoint.DistanceParser; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter.Format; @@ -903,17 +905,17 @@ public class cgBase { Log.i(Settings.tag, "Trying to get ratings for " + cids.size() + " caches"); try { - final Map<String, cgRating> ratings = GCVote.getRating(guids, null); + final Map<String, GCVoteRating> ratings = GCVote.getRating(guids, null); if (MapUtils.isNotEmpty(ratings)) { // save found cache coordinates - for (cgCache oneCache : caches.cacheList) { - if (ratings.containsKey(oneCache.getGuid())) { - cgRating thisRating = ratings.get(oneCache.getGuid()); + for (cgCache cache : caches.cacheList) { + if (ratings.containsKey(cache.getGuid())) { + GCVoteRating rating = ratings.get(cache.getGuid()); - oneCache.setRating(thisRating.rating); - oneCache.setVotes(thisRating.votes); - oneCache.setMyVote(thisRating.myVote); + cache.setRating(rating.getRating()); + cache.setVotes(rating.getVotes()); + cache.setMyVote(rating.getMyVote()); } } } @@ -1476,11 +1478,11 @@ public class cgBase { return; } sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_gcvote); - final cgRating rating = GCVote.getRating(cache.getGuid(), cache.getGeocode()); + final GCVoteRating rating = GCVote.getRating(cache.getGuid(), cache.getGeocode()); if (rating != null) { - cache.setRating(rating.rating); - cache.setVotes(rating.votes); - cache.setMyVote(rating.myVote); + cache.setRating(rating.getRating()); + cache.setVotes(rating.getVotes()); + cache.setMyVote(rating.getMyVote()); } } } diff --git a/main/src/cgeo/geocaching/cgRating.java b/main/src/cgeo/geocaching/cgRating.java deleted file mode 100644 index f0c7a76..0000000 --- a/main/src/cgeo/geocaching/cgRating.java +++ /dev/null @@ -1,7 +0,0 @@ -package cgeo.geocaching; - -public class cgRating { - public Float rating = null; - public Integer votes = null; - public Float myVote = null; -} diff --git a/main/src/cgeo/geocaching/cgeopopup.java b/main/src/cgeo/geocaching/cgeopopup.java index 1f270a7..e8d4d19 100644 --- a/main/src/cgeo/geocaching/cgeopopup.java +++ b/main/src/cgeo/geocaching/cgeopopup.java @@ -3,6 +3,8 @@ package cgeo.geocaching; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.gcvote.GCVote; +import cgeo.geocaching.gcvote.GCVoteRating; import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.lang3.StringUtils; @@ -367,17 +369,17 @@ public class cgeopopup extends AbstractActivity { @Override public void run() { - cgRating rating = GCVote.getRating(cache.getGuid(), geocode); + GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode); Message msg = new Message(); Bundle bundle = new Bundle(); - if (rating == null || rating.rating == null) { + if (rating == null) { return; } - bundle.putFloat("rating", rating.rating); - bundle.putInt("votes", rating.votes); + bundle.putFloat("rating", rating.getRating()); + bundle.putInt("votes", rating.getVotes()); msg.setData(bundle); ratingHandler.sendMessage(msg); diff --git a/main/src/cgeo/geocaching/GCVote.java b/main/src/cgeo/geocaching/gcvote/GCVote.java index f136aa9..7dec3f9 100644 --- a/main/src/cgeo/geocaching/GCVote.java +++ b/main/src/cgeo/geocaching/gcvote/GCVote.java @@ -1,4 +1,9 @@ -package cgeo.geocaching; +package cgeo.geocaching.gcvote; + +import cgeo.geocaching.Parameters; +import cgeo.geocaching.Settings; +import cgeo.geocaching.cgBase; +import cgeo.geocaching.cgCache; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -22,7 +27,7 @@ public final class GCVote { private static final Pattern patternVote = Pattern.compile("voteUser='([0-9.]+)'", Pattern.CASE_INSENSITIVE); private static final Pattern patternVoteElement = Pattern.compile("<vote ([^>]+)>", Pattern.CASE_INSENSITIVE); - public static cgRating getRating(String guid, String geocode) { + public static GCVoteRating getRating(String guid, String geocode) { List<String> guids = null; List<String> geocodes = null; @@ -36,9 +41,9 @@ public final class GCVote { return null; } - final Map<String, cgRating> ratings = getRating(guids, geocodes); + final Map<String, GCVoteRating> ratings = getRating(guids, geocodes); if (ratings != null) { - for (Entry<String, cgRating> entry : ratings.entrySet()) { + for (Entry<String, GCVoteRating> entry : ratings.entrySet()) { return entry.getValue(); } } @@ -46,12 +51,12 @@ public final class GCVote { return null; } - public static Map<String, cgRating> getRating(List<String> guids, List<String> geocodes) { + public static Map<String, GCVoteRating> getRating(List<String> guids, List<String> geocodes) { if (guids == null && geocodes == null) { return null; } - final Map<String, cgRating> ratings = new HashMap<String, cgRating>(); + final Map<String, GCVoteRating> ratings = new HashMap<String, GCVoteRating>(); try { final Parameters params = new Parameters(); @@ -67,26 +72,20 @@ public final class GCVote { params.put("waypoints", StringUtils.join(geocodes.toArray(), ',')); } params.put("version", "cgeo"); - final String votes = cgBase.getResponseData(cgBase.request("http://gcvote.com/getVotes.php", params, false, false, false)); - if (votes == null) { + final String page = cgBase.getResponseData(cgBase.request("http://gcvote.com/getVotes.php", params, false, false, false)); + if (page == null) { return null; } String voteData = null; - final Matcher matcherVoteElement = patternVoteElement.matcher(votes); + final Matcher matcherVoteElement = patternVoteElement.matcher(page); while (matcherVoteElement.find()) { - if (matcherVoteElement.groupCount() > 0) { - voteData = matcherVoteElement.group(1); - } - + voteData = matcherVoteElement.group(1); if (voteData == null) { continue; } String guid = null; - cgRating rating = new cgRating(); - boolean loggedIn = false; - try { final Matcher matcherGuid = patternGuid.matcher(voteData); if (matcherGuid.find()) { @@ -97,9 +96,13 @@ public final class GCVote { } catch (Exception e) { Log.w(Settings.tag, "cgBase.getRating: Failed to parse guid"); } + if (guid == null) { + continue; + } + boolean loggedIn = false; try { - final Matcher matcherLoggedIn = patternLogIn.matcher(votes); + final Matcher matcherLoggedIn = patternLogIn.matcher(page); if (matcherLoggedIn.find()) { if (matcherLoggedIn.groupCount() > 0) { if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) { @@ -111,35 +114,38 @@ public final class GCVote { Log.w(Settings.tag, "cgBase.getRating: Failed to parse loggedIn"); } + float rating = 0; try { final Matcher matcherRating = patternRating.matcher(voteData); if (matcherRating.find()) { - if (matcherRating.groupCount() > 0) { - rating.rating = Float.parseFloat(matcherRating.group(1)); - } + rating = Float.parseFloat(matcherRating.group(1)); } } catch (Exception e) { Log.w(Settings.tag, "cgBase.getRating: Failed to parse rating"); } + if (rating <= 0) { + continue; + } + int votes = -1; try { final Matcher matcherVotes = patternVotes.matcher(voteData); if (matcherVotes.find()) { - if (matcherVotes.groupCount() > 0) { - rating.votes = Integer.parseInt(matcherVotes.group(1)); - } + votes = Integer.parseInt(matcherVotes.group(1)); } } catch (Exception e) { Log.w(Settings.tag, "cgBase.getRating: Failed to parse vote count"); } + if (votes < 0) { + continue; + } + Float myVote = null; if (loggedIn) { try { final Matcher matcherVote = patternVote.matcher(voteData); if (matcherVote.find()) { - if (matcherVote.groupCount() > 0) { - rating.myVote = Float.parseFloat(matcherVote.group(1)); - } + myVote = Float.parseFloat(matcherVote.group(1)); } } catch (Exception e) { Log.w(Settings.tag, "cgBase.getRating: Failed to parse user's vote"); @@ -147,7 +153,8 @@ public final class GCVote { } if (StringUtils.isNotBlank(guid)) { - ratings.put(guid, rating); + GCVoteRating gcvoteRating = new GCVoteRating(rating, votes, myVote); + ratings.put(guid, gcvoteRating); } } } catch (Exception e) { diff --git a/main/src/cgeo/geocaching/gcvote/GCVoteRating.java b/main/src/cgeo/geocaching/gcvote/GCVoteRating.java new file mode 100644 index 0000000..ce70e10 --- /dev/null +++ b/main/src/cgeo/geocaching/gcvote/GCVoteRating.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.gcvote; + +public final class GCVoteRating { + private final float rating; + private final int votes; + private final Float myVote; + + public GCVoteRating(float rating, int votes, Float myVote) { + this.rating = rating; + this.votes = votes; + this.myVote = myVote; + } + + public float getRating() { + return rating; + } + + public int getVotes() { + return votes; + } + + public Float getMyVote() { + return myVote; + } +} |