Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncStopFavoriteAction.java
/* | /* | ||||
BusTO - Middleware components | BusTO - Middleware components | ||||
Copyright (C) 2016 Fabio Mazza | Copyright (C) 2016 Fabio Mazza | ||||
This program is free software: you can redistribute it and/or modify | This program is free software: you can redistribute it and/or modify | ||||
it under the terms of the GNU General Public License as published by | it under the terms of the GNU General Public License as published by | ||||
the Free Software Foundation, either version 3 of the License, or | the Free Software Foundation, either version 3 of the License, or | ||||
(at your option) any later version. | (at your option) any later version. | ||||
This program is distributed in the hope that it will be useful, | This program is distributed in the hope that it will be useful, | ||||
but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
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; | ||||
/** | /** | ||||
* 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 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; | ||||
/** | /** | ||||
* Constructor | * Constructor | ||||
* | * | ||||
* @param context | * @param context | ||||
* @param action | * @param action | ||||
*/ | */ | ||||
public AsyncStopFavoriteAction(Context context, Action action) { | public AsyncStopFavoriteAction(Context context, Action action) { | ||||
this.context = context.getApplicationContext(); | this.context = context.getApplicationContext(); | ||||
this.action = action; | this.action = action; | ||||
} | } | ||||
@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]; | ||||
// check if the request has sense | // check if the request has sense | ||||
if(stop != null) { | if(stop != null) { | ||||
// get a writable database | // get a writable database | ||||
UserDB userDatabase = new UserDB(context); | UserDB userDatabase = new UserDB(context); | ||||
SQLiteDatabase db = userDatabase.getWritableDatabase(); | SQLiteDatabase db = userDatabase.getWritableDatabase(); | ||||
// eventually toggle the status | // eventually toggle the status | ||||
if(Action.TOGGLE.equals(action)) { | if(Action.TOGGLE.equals(action)) { | ||||
if(UserDB.isStopInFavorites(db, stop.ID)) { | if(UserDB.isStopInFavorites(db, stop.ID)) { | ||||
action = Action.REMOVE; | action = Action.REMOVE; | ||||
} else { | } else { | ||||
action = Action.ADD; | action = Action.ADD; | ||||
} | } | ||||
} | } | ||||
// at this point the action is just ADD or REMOVE | // at this point the action is just ADD or REMOVE | ||||
// add or remove? | // add or remove? | ||||
if(Action.ADD.equals(action)) { | if(Action.ADD.equals(action)) { | ||||
// add | // add | ||||
result = UserDB.addOrUpdateStop(stop, db); | result = UserDB.addOrUpdateStop(stop, db); | ||||
} else { | } else { | ||||
// remove | // remove | ||||
result = UserDB.deleteStop(stop, db); | result = UserDB.deleteStop(stop, db); | ||||
} | } | ||||
// please sir, close the door | // please sir, close the door | ||||
db.close(); | db.close(); | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
/** | /** | ||||
* Callback fired when everything was done | * Callback fired when everything was done | ||||
* | * | ||||
* @param result | * @param result | ||||
*/ | */ | ||||
@Override | @Override | ||||
protected void onPostExecute(Boolean result) { | protected void onPostExecute(Boolean result) { | ||||
super.onPostExecute(result); | super.onPostExecute(result); | ||||
if(result) { | if(result) { | ||||
// at this point the action should be just ADD or REMOVE | // at this point the action should be just ADD or REMOVE | ||||
if(Action.ADD.equals(action)) { | if(Action.ADD.equals(action)) { | ||||
// now added | // now added | ||||
Toast.makeText(this.context, R.string.added_in_favorites, Toast.LENGTH_SHORT).show(); | Toast.makeText(this.context, R.string.added_in_favorites, Toast.LENGTH_SHORT).show(); | ||||
} 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(); | ||||
} | } | ||||
} | } | ||||
} | } |
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