Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a snackbar action, how can I be sure it's safe to permanently delete a soft-deleted record from the database?

Tags:

I am using Snackbar in android and I have implemented an action so that user can undo the action (the action is clearing all the items in the listview).Removing and adding the items back to the listview has already been done and working fine.

My issue is that, items are stored in sqlite database and how can I delete the items from tables? (How can I know that the user has not clicked the undo button, so that I can completely remove the data from database).

This is the code inside OnOptionsItemSelcted()

case R.id.action_clear:         final List<Word> temp = new ArrayList<Word>(data);         data.clear();         adapter.notifyDataSetChanged();         View view = findViewById(R.id.layoutFavWords);         Snackbar.make(view,"Deleted Saved Selection.", Snackbar.LENGTH_LONG).         setAction("Undo", new OnClickListener() {              @Override             public void onClick(View v) {                 for(Word word:temp)                     data.add(word);                 adapter.notifyDataSetChanged();              }          }).show();         break; 

So if the user has not clicked the undo button during the visible period of the snackbar, then I need to permanently delete the data from database.

Any solutions for this?

like image 608
chathura Avatar asked Jun 25 '15 17:06

chathura


2 Answers

As far as I know, it is by design. You should:

  • Delete the item as soon as the user taps the delete button;
  • Store it temporarily in a class variable;
  • If the user taps Undo, add the item again to the database.

This approach is safer and more robust; you shouldn't wait for the snackbar to be dismissed, because that action could not even happen. Just think of user force-quitting the app while the snackbar is still on: should the item be deleted or not? It should.

A more trustworthy source is g+ post by Ian Lake (deleted because of G+ deprecation). In the comments you can read:

you want your UI to react immediately (not wait for the snackbar to disappear) - most systems (particularly those that sync to an external server) have the concept of a 'soft delete' where things are marked as deleted. In those cases, an undo action would just be unmarking the record as deleted. This system works even if the user were to leave the app before the snackbar finishes (you can't assume the snackbar will always complete its animation!).

The easiest way to do that is to temporarily save the record elsewhere (even a local variable), then re-insert it if they happen to hit the undo button.

like image 116
natario Avatar answered Nov 19 '22 06:11

natario


Android Support library v23 added Snackbar.Callback which you can use to listen if the snackbar was dismissed by user or timeout.

Example borrowed from astinxs post:

Snackbar.make(getView(), "Hi there!", Snackbar.LENGTH_LONG).setCallback( new Snackbar.Callback() {             @Override             public void onDismissed(Snackbar snackbar, int event) {                 switch(event) {                     case Snackbar.Callback.DISMISS_EVENT_ACTION:                         Toast.makeText(getActivity(), "Clicked the action", Toast.LENGTH_LONG).show();                         break;                     case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:                         Toast.makeText(getActivity(), "Time out", Toast.LENGTH_LONG).show();                         break;                 }             }              @Override             public void onShown(Snackbar snackbar) {                 Toast.makeText(getActivity(), "This is my annoying step-brother", Toast.LENGTH_LONG).show();             }         }).setAction("Go away!", new View.OnClickListener() {             @Override             public void onClick(View v) {              }         }).show(); 
like image 33
Irritator Avatar answered Nov 19 '22 07:11

Irritator