Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncStopFavoriteAction.java
Show All 18 Lines | |||||
package it.reyboz.bustorino.middleware; | package it.reyboz.bustorino.middleware; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.database.sqlite.SQLiteDatabase; | import android.database.sqlite.SQLiteDatabase; | ||||
import android.os.AsyncTask; | import android.os.AsyncTask; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.data.UserDB; | |||||
/** | /** | ||||
* Handler to add or remove or toggle a Stop in your favorites | * Handler to add or remove or toggle a Stop in your favorites | ||||
*/ | */ | ||||
public class AsyncStopFavoriteAction extends AsyncTask<Stop, Void, Boolean> { | public class AsyncStopFavoriteAction extends AsyncTask<Stop, Void, Boolean> { | ||||
private Context context; | private final Context context; | ||||
/** | /** | ||||
* Kind of actions available | * Kind of actions available | ||||
*/ | */ | ||||
public enum Action { ADD, REMOVE, TOGGLE }; | public enum Action { ADD, REMOVE, TOGGLE }; | ||||
/** | /** | ||||
* Action chosen | * Action chosen | ||||
* | * | ||||
* Note that TOGGLE is not converted to ADD or REMOVE. | * Note that TOGGLE is not converted to ADD or REMOVE. | ||||
*/ | */ | ||||
private Action action; | private Action action; | ||||
// extra stuff to do after we've done it | |||||
private ResultListener listener; | |||||
/** | /** | ||||
* Constructor | * Constructor | ||||
* | * | ||||
* @param context | * @param context | ||||
* @param action | * @param action | ||||
*/ | */ | ||||
public AsyncStopFavoriteAction(Context context, Action action) { | public AsyncStopFavoriteAction(Context context, Action action, ResultListener listener) { | ||||
this.context = context.getApplicationContext(); | this.context = context.getApplicationContext(); | ||||
this.action = action; | this.action = action; | ||||
this.listener = listener; | |||||
} | } | ||||
@Override | @Override | ||||
protected Boolean doInBackground(Stop... stops) { | protected Boolean doInBackground(Stop... stops) { | ||||
boolean result = false; | boolean result = false; | ||||
Stop stop = stops[0]; | Stop stop = stops[0]; | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | protected void onPostExecute(Boolean result) { | ||||
} else { | } else { | ||||
// now removed | // now removed | ||||
Toast.makeText(this.context, R.string.removed_from_favorites, Toast.LENGTH_SHORT).show(); | Toast.makeText(this.context, R.string.removed_from_favorites, Toast.LENGTH_SHORT).show(); | ||||
} | } | ||||
} else { | } else { | ||||
// wtf | // wtf | ||||
Toast.makeText(this.context, R.string.cant_add_to_favorites, Toast.LENGTH_SHORT).show(); | Toast.makeText(this.context, R.string.cant_add_to_favorites, Toast.LENGTH_SHORT).show(); | ||||
} | } | ||||
listener.doStuffWithResult(result); | |||||
} | } | ||||
public interface ResultListener{ | |||||
/** | |||||
* Do what you need to to update the UI with the result | |||||
* @param result true if the action is done | |||||
*/ | |||||
void doStuffWithResult(Boolean result); | |||||
} | |||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register