Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo for "Swipe to delete" #158

Merged
merged 3 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.Snackbar;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -300,10 +301,22 @@ public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewH
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.RIGHT) {
DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
final DBNote dbNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
db.deleteNoteAndSync((dbNote).getId());
adapter.remove(dbNote);
refreshList();
Log.v("Note", "Item deleted through swipe ----------------------------------------------");
Snackbar.make(swipeRefreshLayout, R.string.action_note_deleted, Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
db.addNoteAndSync(dbNote);
refreshList();
Snackbar.make(swipeRefreshLayout, R.string.action_note_restored, Snackbar.LENGTH_SHORT)
.show();
}
})
.show();
}
}
});
Expand Down Expand Up @@ -474,7 +487,7 @@ public boolean onNoteLongClick(int position, View v) {

@Override
public void onBackPressed() {
if (searchView.isIconified()) {
if (searchView==null || searchView.isIconified()) {
super.onBackPressed();
} else {
searchView.setIconified(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,19 @@ public Context getContext() {
*/
@SuppressWarnings("UnusedReturnValue")
public long addNoteAndSync(String content) {
DBNote note = new DBNote(0, 0, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, false, DBStatus.LOCAL_EDITED);
long id = addNote(note);
OwnCloudNote note = new OwnCloudNote(0, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, false);
return addNoteAndSync(note);
}

/**
* Creates a new Note in the Database and adds a Synchronization Flag.
*
* @param note Note
*/
@SuppressWarnings("UnusedReturnValue")
public long addNoteAndSync(OwnCloudNote note) {
DBNote dbNote = new DBNote(0, 0, note.getModified(), note.getTitle(), note.getContent(), note.isFavorite(), DBStatus.LOCAL_EDITED);
long id = addNote(dbNote);
getNoteServerSyncHelper().scheduleSync(true);
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ private void pushLocalChanges() {
// if note is not new, try to edit it.
if (note.getRemoteId()>0) {
Log.d(getClass().getSimpleName(), " ...try to edit");
remoteNote = client.editNote(note);
try {
remoteNote = client.editNote(note);
} catch(FileNotFoundException e) {
// Note does not exists anymore
}
}
// However, the note may be deleted on the server meanwhile; or was never synchronized -> (re)create
// Please note, thas dbHelper.updateNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<string name="action_edit_saved">Gespeichert</string>
<string name="action_edit_cancel">Abbrechen</string>
<string name="action_select_note">Notiz auswählen</string>
<string name="action_note_deleted">Notiz wurde gelöscht</string>
<string name="action_note_restored">Notiz wurde wiederhergestellt</string>
<string name="action_undo">Rückgängig</string>
<string name="action_about">Über</string>
<string name="menu_delete">Löschen</string>
<string name="menu_copy">Kopieren</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<string name="action_edit_cancel">Cancel</string>
<string name="action_about">About</string>
<string name="action_select_note">Select note</string>
<string name="action_note_deleted">Note is deleted</string>
<string name="action_note_restored">Note is restored</string>
<string name="action_undo">Undo</string>
<string name="menu_delete">Delete</string>
<string name="menu_copy">Copy</string>
<string name="menu_edit">Edit</string>
Expand Down