Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncStopFavoriteAction.java
Show All 14 Lines | BusTO - Middleware components | ||||
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.net.Uri; | |||||
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.AppDataProvider; | |||||
import it.reyboz.bustorino.data.UserDB; | 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 final Context context; | private final Context context; | ||||
private final Uri FAVORITES_URI = AppDataProvider.getUriBuilderToComplete().appendPath( | |||||
AppDataProvider.FAVORITES).build(); | |||||
/** | /** | ||||
* Kind of actions available | * Kind of actions available | ||||
*/ | */ | ||||
public enum Action { ADD, REMOVE, TOGGLE }; | public enum Action { ADD, REMOVE, TOGGLE , UPDATE}; | ||||
/** | /** | ||||
* 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; | ||||
Show All 34 Lines | protected Boolean doInBackground(Stop... stops) { | ||||
} | } | ||||
// 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 if (Action.UPDATE.equals(action)){ | |||||
result = UserDB.updateStop(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) { | ||||
UserDB.notifyContentProvider(context); | |||||
// 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 if (Action.REMOVE.equals(action)) { | ||||
// 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); | listener.doStuffWithResult(result); | ||||
Show All 11 Lines |
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