Skip to content

Commit d700421

Browse files
committed
Add Snackbar for Entry deletion, fixing #41
also removes unused view in EditorActivity layout Signed-off-by: Aron Heinecke <aron.heinecke@t-online.de>
1 parent 7e3b62b commit d700421

File tree

3 files changed

+41
-134
lines changed

3 files changed

+41
-134
lines changed

app/src/main/java/vocabletrainer/heinecke/aron/vocabletrainer/activity/EditorActivity.java

Lines changed: 33 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66
import android.support.annotation.NonNull;
77
import android.support.design.widget.FloatingActionButton;
8+
import android.support.design.widget.Snackbar;
89
import android.support.v7.app.ActionBar;
910
import android.support.v7.app.AlertDialog;
1011
import android.support.v7.app.AppCompatActivity;
@@ -58,7 +59,6 @@ public class EditorActivity extends AppCompatActivity implements VEntryEditorDia
5859
private EntryListAdapter adapter;
5960
private ListView listView;
6061
private Database db;
61-
private View undoContainer;
6262
private VEntry lastDeleted;
6363
private int deletedPosition;
6464
private int sortSetting;
@@ -104,8 +104,6 @@ protected void onCreate(Bundle savedInstanceState) {
104104
},ID_RESERVED_SKIP);
105105

106106
Intent intent = getIntent();
107-
undoContainer = findViewById(R.id.undobar);
108-
undoContainer.setVisibility(View.GONE);
109107

110108
FloatingActionButton bNewEntry = findViewById(R.id.bEditorNewEntry);
111109
bNewEntry.setOnClickListener(v -> addEntry());
@@ -296,19 +294,44 @@ private void showEntryDeleteDialog(final VEntry entry, final int position) {
296294
delDiag.setTitle(R.string.Editor_Diag_delete_Title);
297295
delDiag.setMessage(String.format(getString(R.string.Editor_Diag_delete_MSG_part) + "\n %s %s %s", entry.getAString(), entry.getBString(), entry.getTip()));
298296

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));
306298

307299
delDiag.setNegativeButton(R.string.Editor_Diag_delete_btn_CANCEL, (dialog, whichButton) -> Log.d(TAG, "canceled"));
308300

309301
delDiag.show();
310302
}
311303

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+
312335
/**
313336
* Show entry edit dialog for new vocable
314337
* @param entry
@@ -385,80 +408,6 @@ private void showTableInfoDialog() {
385408
listEditorDialog.show(getSupportFragmentManager(), VListEditorDialog.TAG);
386409
}
387410

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-
462411
@Override
463412
protected void onSaveInstanceState(Bundle outState) {
464413
super.onSaveInstanceState(outState);

app/src/main/res/layout/activity_editor.xml

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,74 +26,31 @@
2626
app:layout_behavior="@string/appbar_scrolling_view_behavior"
2727
android:id="@+id/constraintLayout">
2828

29-
<LinearLayout
30-
android:layout_width="0dp"
31-
android:layout_height="wrap_content"
32-
android:layout_marginBottom="8dp"
33-
android:layout_marginEnd="8dp"
34-
android:layout_marginLeft="8dp"
35-
android:layout_marginRight="8dp"
36-
android:layout_marginStart="8dp"
37-
android:orientation="horizontal"
38-
app:layout_constraintBottom_toBottomOf="parent"
39-
app:layout_constraintLeft_toLeftOf="parent"
40-
app:layout_constraintRight_toRightOf="parent">
41-
42-
</LinearLayout>
43-
4429
<ListView
4530
android:id="@+id/listviewEditor"
4631
android:layout_width="0dp"
4732
android:layout_height="0dp"
4833
android:layout_marginLeft="8dp"
4934
android:layout_marginRight="8dp"
50-
android:layout_marginTop="8dp"
35+
app:layout_constraintBottom_toBottomOf="parent"
5136
app:layout_constraintHorizontal_bias="0.0"
5237
app:layout_constraintLeft_toLeftOf="parent"
5338
app:layout_constraintRight_toRightOf="parent"
54-
app:layout_constraintTop_toTopOf="parent"
55-
android:layout_marginBottom="0dp"
56-
app:layout_constraintBottom_toTopOf="@+id/undobar" />
57-
58-
<LinearLayout
59-
android:id="@+id/undobar"
60-
android:layout_width="0dp"
61-
android:layout_height="wrap_content"
62-
android:layout_gravity="center_horizontal|bottom"
63-
android:layout_marginBottom="0dp"
64-
android:alpha="100"
65-
android:dividerPadding="11dp"
66-
android:gravity="center"
67-
android:padding="4dp"
68-
android:visibility="visible"
69-
app:layout_constraintBottom_toBottomOf="parent"
70-
app:layout_constraintLeft_toLeftOf="parent"
71-
app:layout_constraintRight_toRightOf="parent">
72-
73-
<Button
74-
android:layout_width="wrap_content"
75-
android:layout_height="wrap_content"
76-
android:background="#00f2f2f2"
77-
android:drawableLeft="@drawable/ic_undo_white_48dp"
78-
android:enabled="false"
79-
android:focusable="false"
80-
android:text="@string/GEN_Undo"
81-
android:textAppearance="?android:attr/textAppearanceMedium"
82-
android:textColor="@android:color/background_light" />
83-
</LinearLayout>
39+
app:layout_constraintTop_toTopOf="parent" />
8440

8541
<android.support.design.widget.FloatingActionButton
8642
android:id="@+id/bEditorNewEntry"
8743
android:layout_width="wrap_content"
8844
android:layout_height="wrap_content"
89-
android:layout_marginBottom="16dp"
90-
android:layout_marginRight="16dp"
45+
android:layout_marginEnd="24dp"
46+
android:layout_marginBottom="24dp"
9147
android:clickable="true"
9248
app:fabSize="normal"
9349
app:layout_anchor="@+id/constraintLayout"
94-
app:layout_constraintBottom_toTopOf="@+id/undobar"
50+
app:layout_constraintBottom_toBottomOf="@+id/listviewEditor"
9551
app:layout_constraintRight_toRightOf="@+id/listviewEditor"
96-
app:srcCompat="@android:drawable/ic_input_add" />
52+
app:srcCompat="@android:drawable/ic_input_add"
53+
android:focusable="true" />
9754
</android.support.constraint.ConstraintLayout>
9855

9956
</android.support.design.widget.CoordinatorLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<string name="Editor_Hint_Tip">Hint</string>
5454
<string name="Editor_Hint_Addition">Additional</string>
5555
<string name="Editor_ListSettings">List Settings</string>
56+
<string name="Editor_Entry_Deleted_Message">Entry deleted</string>
5657
<string name="CSV_Format">CSV Format</string>
5758
<string name="Select_File_btn">Select File</string>
5859
<string name="Import_Title">Import Lists</string>

0 commit comments

Comments
 (0)