aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2015-01-10 22:54:41 +0100
committerSamuel Tardieu <sam@rfc1149.net>2015-01-10 23:04:03 +0100
commit2c51a1326984fd7b22692b14701046ce09bc55af (patch)
treed46e1aa31b513a75111ecfbd57c57d513f80c92c
parentcc39b8021fa18ad5fcc658d7c368c03ddd25fcff (diff)
downloadcgeo-2c51a1326984fd7b22692b14701046ce09bc55af.zip
cgeo-2c51a1326984fd7b22692b14701046ce09bc55af.tar.gz
cgeo-2c51a1326984fd7b22692b14701046ce09bc55af.tar.bz2
fix #4608: own rating is saved even if vote could not be uploaded
-rw-r--r--main/src/cgeo/geocaching/gcvote/GCVoteDialog.java43
-rw-r--r--main/src/cgeo/geocaching/gcvote/GCVotePoster.java53
2 files changed, 41 insertions, 55 deletions
diff --git a/main/src/cgeo/geocaching/gcvote/GCVoteDialog.java b/main/src/cgeo/geocaching/gcvote/GCVoteDialog.java
index da14e0b..e5717ab 100644
--- a/main/src/cgeo/geocaching/gcvote/GCVoteDialog.java
+++ b/main/src/cgeo/geocaching/gcvote/GCVoteDialog.java
@@ -1,12 +1,19 @@
package cgeo.geocaching.gcvote;
+import cgeo.geocaching.CgeoApplication;
+import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.R;
import cgeo.geocaching.gcvote.GCVoteRatingBarUtil.OnRatingChangeListener;
import cgeo.geocaching.settings.Settings;
+import cgeo.geocaching.utils.Log;
+import cgeo.geocaching.utils.RxUtils;
import org.eclipse.jdt.annotation.Nullable;
+import rx.functions.Action1;
+import rx.functions.Func0;
+
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
@@ -17,6 +24,7 @@ import android.os.Build.VERSION_CODES;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
+import android.widget.Toast;
/**
* Small dialog showing only a rating bar to vote on GCVote.com. Confirming the dialog will send the vote over the
@@ -67,8 +75,39 @@ public class GCVoteDialog {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(GCVote.isValidRating(cache.getMyVote()));
}
- protected static void vote(final Geocache cache, final float rating, final @Nullable Runnable afterVoteSent) {
- new GCVotePoster(cache, rating, afterVoteSent).execute();
+ private static void vote(final Geocache cache, final float rating, final @Nullable Runnable afterVoteSent) {
+ RxUtils.andThenOnUi(RxUtils.networkScheduler, new Func0<Boolean>() {
+ @Override
+ public Boolean call() {
+ try {
+ if (GCVote.isValidRating(rating) && GCVote.isVotingPossible(cache)) {
+ // send over network
+ if (GCVote.setRating(cache, rating)) {
+ // store locally
+ cache.setMyVote(rating);
+ DataStore.saveChangedCache(cache);
+ return true;
+ } else {
+ Log.w("GCVoteDialog.vote: could not send vote");
+ }
+ }
+ } catch (final RuntimeException e) {
+ Log.e("GCVoteDialog.vote: could not send vote", e);
+ }
+
+ return false;
+ }
+ }, new Action1<Boolean>() {
+ @Override
+ public void call(final Boolean status) {
+ final CgeoApplication context = CgeoApplication.getInstance();
+ final String text = context.getString(status ? R.string.gcvote_sent : R.string.err_gcvote_send_rating);
+ Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
+ if (afterVoteSent != null) {
+ afterVoteSent.run();
+ }
+ }
+ });
}
}
diff --git a/main/src/cgeo/geocaching/gcvote/GCVotePoster.java b/main/src/cgeo/geocaching/gcvote/GCVotePoster.java
deleted file mode 100644
index 924aa56..0000000
--- a/main/src/cgeo/geocaching/gcvote/GCVotePoster.java
+++ /dev/null
@@ -1,53 +0,0 @@
-package cgeo.geocaching.gcvote;
-
-import cgeo.geocaching.CgeoApplication;
-import cgeo.geocaching.DataStore;
-import cgeo.geocaching.Geocache;
-import cgeo.geocaching.R;
-import cgeo.geocaching.utils.Log;
-
-import org.eclipse.jdt.annotation.Nullable;
-
-import android.os.AsyncTask;
-import android.widget.Toast;
-
-class GCVotePoster extends AsyncTask<Void, Void, Boolean> {
-
- private final Geocache cache;
- private final float rating;
- private final @Nullable Runnable afterVoteSent;
-
- public GCVotePoster(final Geocache cache, final float rating, final @Nullable Runnable afterVoteSent) {
- this.cache = cache;
- this.rating = rating;
- this.afterVoteSent = afterVoteSent;
- }
-
- @Override
- protected Boolean doInBackground(final Void... inputs) {
- try {
- if (GCVote.isValidRating(rating) && GCVote.isVotingPossible(cache)) {
- // store locally
- cache.setMyVote(rating);
- DataStore.saveChangedCache(cache);
-
- // send over network
- return GCVote.setRating(cache, rating);
- }
- } catch (final RuntimeException e) {
- Log.e("GCVoteAsyncTask.doInBackground", e);
- }
-
- return false;
- }
-
- @Override
- protected void onPostExecute(final Boolean status) {
- final CgeoApplication context = CgeoApplication.getInstance();
- final String text = context.getString(status ? R.string.gcvote_sent : R.string.err_gcvote_send_rating);
- Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
- if (afterVoteSent != null) {
- afterVoteSent.run();
- }
- }
-} \ No newline at end of file