|
5 | 5 | import android.os.Bundle;
|
6 | 6 | import android.support.annotation.NonNull;
|
7 | 7 | import android.support.design.widget.FloatingActionButton;
|
| 8 | +import android.support.design.widget.Snackbar; |
8 | 9 | import android.support.v7.app.ActionBar;
|
9 | 10 | import android.support.v7.app.AlertDialog;
|
10 | 11 | import android.support.v7.app.AppCompatActivity;
|
@@ -58,7 +59,6 @@ public class EditorActivity extends AppCompatActivity implements VEntryEditorDia
|
58 | 59 | private EntryListAdapter adapter;
|
59 | 60 | private ListView listView;
|
60 | 61 | private Database db;
|
61 |
| - private View undoContainer; |
62 | 62 | private VEntry lastDeleted;
|
63 | 63 | private int deletedPosition;
|
64 | 64 | private int sortSetting;
|
@@ -104,8 +104,6 @@ protected void onCreate(Bundle savedInstanceState) {
|
104 | 104 | },ID_RESERVED_SKIP);
|
105 | 105 |
|
106 | 106 | Intent intent = getIntent();
|
107 |
| - undoContainer = findViewById(R.id.undobar); |
108 |
| - undoContainer.setVisibility(View.GONE); |
109 | 107 |
|
110 | 108 | FloatingActionButton bNewEntry = findViewById(R.id.bEditorNewEntry);
|
111 | 109 | bNewEntry.setOnClickListener(v -> addEntry());
|
@@ -296,19 +294,44 @@ private void showEntryDeleteDialog(final VEntry entry, final int position) {
|
296 | 294 | delDiag.setTitle(R.string.Editor_Diag_delete_Title);
|
297 | 295 | delDiag.setMessage(String.format(getString(R.string.Editor_Diag_delete_MSG_part) + "\n %s %s %s", entry.getAString(), entry.getBString(), entry.getTip()));
|
298 | 296 |
|
299 |
| - delDiag.setPositiveButton(R.string.Editor_Diag_delete_btn_OK, (dialog, whichButton) -> { |
300 |
| - lastDeleted = entry; |
301 |
| - deletedPosition = position; |
302 |
| - adapter.remove(entry); |
303 |
| - showUndo(); |
304 |
| - Log.d(TAG, "deleted"); |
305 |
| - }); |
| 297 | + delDiag.setPositiveButton(R.string.Editor_Diag_delete_btn_OK, (dialog, whichButton) -> deleteEntry(position, entry)); |
306 | 298 |
|
307 | 299 | delDiag.setNegativeButton(R.string.Editor_Diag_delete_btn_CANCEL, (dialog, whichButton) -> Log.d(TAG, "canceled"));
|
308 | 300 |
|
309 | 301 | delDiag.show();
|
310 | 302 | }
|
311 | 303 |
|
| 304 | + /** |
| 305 | + * Perform entry deletion with undo possibility |
| 306 | + * @param deletedPosition |
| 307 | + * @param entry |
| 308 | + */ |
| 309 | + private void deleteEntry(final int deletedPosition, final VEntry entry){ |
| 310 | + adapter.remove(entry); |
| 311 | + Snackbar snackbar = Snackbar |
| 312 | + .make(listView, R.string.Editor_Entry_Deleted_Message, Snackbar.LENGTH_LONG) |
| 313 | + .setAction(R.string.GEN_Undo, view -> adapter.addEntryRendered(entry,deletedPosition)) |
| 314 | + .addCallback(new Snackbar.Callback(){ |
| 315 | + @Override |
| 316 | + public void onDismissed(Snackbar transientBottomBar, int event) { |
| 317 | + switch(event){ |
| 318 | + case DISMISS_EVENT_CONSECUTIVE: // second deletion |
| 319 | + case DISMISS_EVENT_TIMEOUT: // timeout |
| 320 | + case DISMISS_EVENT_MANUAL: // dismiss() -> view change |
| 321 | + case DISMISS_EVENT_SWIPE: // swiped away |
| 322 | + Log.d(TAG,"deleting entry"); |
| 323 | + entry.setDelete(true); |
| 324 | + ArrayList<VEntry> lst = new ArrayList<>(1); |
| 325 | + lst.add(entry); |
| 326 | + Database db = new Database(getApplicationContext()); |
| 327 | + db.upsertEntries(lst); // TODO make a single function |
| 328 | + break; |
| 329 | + } |
| 330 | + } |
| 331 | + }); |
| 332 | + snackbar.show(); |
| 333 | + } |
| 334 | + |
312 | 335 | /**
|
313 | 336 | * Show entry edit dialog for new vocable
|
314 | 337 | * @param entry
|
@@ -385,80 +408,6 @@ private void showTableInfoDialog() {
|
385 | 408 | listEditorDialog.show(getSupportFragmentManager(), VListEditorDialog.TAG);
|
386 | 409 | }
|
387 | 410 |
|
388 |
| - /** |
389 |
| - * Show undo view<br> |
390 |
| - * On viewchange during the animation we're not deleting the vocable |
391 |
| - */ |
392 |
| - private void showUndo() { |
393 |
| - undoContainer.setVisibility(View.VISIBLE); |
394 |
| - undoContainer.bringToFront(); |
395 |
| - ScaleAnimation scaleAnimation = new ScaleAnimation(0f,1f,1f,1f, |
396 |
| - Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling |
397 |
| - Animation.RELATIVE_TO_SELF, 1f); |
398 |
| - final AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f); |
399 |
| - |
400 |
| - AnimationSet animationSet = new AnimationSet(true); |
401 |
| - animationSet.addAnimation(scaleAnimation); |
402 |
| - animationSet.addAnimation(alphaAnimation); |
403 |
| - animationSet.setDuration(500); |
404 |
| - animationSet.setFillEnabled(true); |
405 |
| - |
406 |
| - animationSet.setAnimationListener(new Animation.AnimationListener() { |
407 |
| - @Override |
408 |
| - public void onAnimationStart(Animation animation) {} |
409 |
| - |
410 |
| - @Override |
411 |
| - public void onAnimationEnd(Animation animation) { |
412 |
| - AnimationSet animationSetOut = new AnimationSet(true); |
413 |
| - AlphaAnimation alphaAnimation1 = new AlphaAnimation(1f,0f); |
414 |
| - ScaleAnimation scaleAnimation1 = new ScaleAnimation(1f,0f,1f,1f, |
415 |
| - Animation.RELATIVE_TO_SELF, 1f, |
416 |
| - Animation.RELATIVE_TO_SELF, 1f); |
417 |
| - ScaleAnimation scaleAnimation2 = new ScaleAnimation(1f,0f,1f,0f, |
418 |
| - Animation.RELATIVE_TO_SELF, 1f, |
419 |
| - Animation.RELATIVE_TO_SELF, 1f); |
420 |
| - |
421 |
| - scaleAnimation2.setStartOffset(500); |
422 |
| - animationSetOut.addAnimation(alphaAnimation1); |
423 |
| - animationSetOut.addAnimation(scaleAnimation1); |
424 |
| - animationSetOut.addAnimation(scaleAnimation2); |
425 |
| - animationSetOut.setDuration(2000); |
426 |
| - animationSetOut.setStartOffset(2000); |
427 |
| - animationSetOut.setFillEnabled(true); |
428 |
| - animationSetOut.setAnimationListener(new Animation.AnimationListener() { |
429 |
| - @Override |
430 |
| - public void onAnimationStart(Animation animation) {} |
431 |
| - |
432 |
| - @Override |
433 |
| - public void onAnimationEnd(Animation animation) { |
434 |
| - undoContainer.setVisibility(View.GONE); |
435 |
| - lastDeleted.setDelete(true); |
436 |
| - ArrayList<VEntry> lst = new ArrayList<>(1); |
437 |
| - lst.add(lastDeleted); |
438 |
| - db.upsertEntries(lst); |
439 |
| - } |
440 |
| - |
441 |
| - @Override |
442 |
| - public void onAnimationRepeat(Animation animation) {} |
443 |
| - }); |
444 |
| - undoContainer.setAnimation(animationSetOut); |
445 |
| - } |
446 |
| - |
447 |
| - @Override |
448 |
| - public void onAnimationRepeat(Animation animation) {} |
449 |
| - }); |
450 |
| - undoContainer.clearAnimation(); |
451 |
| - undoContainer.setAnimation(animationSet); |
452 |
| - |
453 |
| - undoContainer.setOnClickListener(v -> { |
454 |
| - Log.d(TAG, "undoing"); |
455 |
| - undoContainer.clearAnimation(); |
456 |
| - adapter.addEntryRendered(lastDeleted, deletedPosition); |
457 |
| - undoContainer.setVisibility(View.GONE); |
458 |
| - listView.setFocusable(true); |
459 |
| - }); |
460 |
| - } |
461 |
| - |
462 | 411 | @Override
|
463 | 412 | protected void onSaveInstanceState(Bundle outState) {
|
464 | 413 | super.onSaveInstanceState(outState);
|
|
0 commit comments