Fix #714 Swipe to delete broken

Signed-off-by: stefan-niedermann <info@niedermann.it>
This commit is contained in:
stefan-niedermann 2020-03-18 11:02:35 +01:00
parent 78c723db0a
commit 20397f7cc3
2 changed files with 43 additions and 19 deletions

View file

@ -4,10 +4,13 @@ import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.android.sso.model.SingleSignOnAccount; import com.nextcloud.android.sso.model.SingleSignOnAccount;
@ -24,15 +27,18 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper {
private static final String TAG = NotesListViewItemTouchHelper.class.getCanonicalName(); private static final String TAG = NotesListViewItemTouchHelper.class.getCanonicalName();
public NotesListViewItemTouchHelper( public NotesListViewItemTouchHelper(
SingleSignOnAccount ssoAccount, @NonNull SingleSignOnAccount ssoAccount,
Context context, @NonNull Context context,
ViewProvider viewProvider, @NonNull NotesDatabase db,
NotesDatabase db, @NonNull ItemAdapter adapter,
ItemAdapter adapter, @NonNull ISyncCallback syncCallBack,
ISyncCallback syncCallBack, @NonNull Runnable refreshLists,
Runnable refreshLists @Nullable SwipeRefreshLayout swipeRefreshLayout,
@Nullable ViewProvider viewProvider
) { ) {
super(new SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { super(new SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
private boolean swipeRefreshLayoutEnabled;
@Override @Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false; return false;
@ -67,15 +73,19 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper {
adapter.remove(dbNote); adapter.remove(dbNote);
refreshLists.run(); refreshLists.run();
Log.v(TAG, "Item deleted through swipe ----------------------------------------------"); Log.v(TAG, "Item deleted through swipe ----------------------------------------------");
Snackbar.make(viewProvider.getView(), context.getString(R.string.action_note_deleted, dbNote.getTitle()), Snackbar.LENGTH_LONG) if (viewProvider == null) {
.setAction(R.string.action_undo, (View v) -> { Toast.makeText(context, context.getString(R.string.action_note_deleted, dbNote.getTitle()), Toast.LENGTH_LONG).show();
db.getNoteServerSyncHelper().addCallbackPush(ssoAccount, refreshLists::run); } else {
db.addNoteAndSync(ssoAccount, dbNote.getAccountId(), dbNote); Snackbar.make(viewProvider.getView(), context.getString(R.string.action_note_deleted, dbNote.getTitle()), Snackbar.LENGTH_LONG)
refreshLists.run(); .setAction(R.string.action_undo, (View v) -> {
Snackbar.make(viewProvider.getView(), context.getString(R.string.action_note_restored, dbNote.getTitle()), Snackbar.LENGTH_SHORT) db.getNoteServerSyncHelper().addCallbackPush(ssoAccount, refreshLists::run);
.show(); db.addNoteAndSync(ssoAccount, dbNote.getAccountId(), dbNote);
}) refreshLists.run();
.show(); Snackbar.make(viewProvider.getView(), context.getString(R.string.action_note_restored, dbNote.getTitle()), Snackbar.LENGTH_SHORT)
.show();
})
.show();
}
break; break;
case ItemTouchHelper.RIGHT: case ItemTouchHelper.RIGHT:
final DBNote adapterNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition()); final DBNote adapterNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition());
@ -97,14 +107,28 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper {
} }
@Override @Override
public float getSwipeEscapeVelocity(float defaultValue) { public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
return defaultValue * 3; if (actionState == ACTION_STATE_SWIPE && swipeRefreshLayout != null) {
Log.i(TAG, "Start swiping, disable swipeRefreshLayout");
swipeRefreshLayoutEnabled = swipeRefreshLayout.isEnabled();
swipeRefreshLayout.setEnabled(false);
}
super.onSelectedChanged(viewHolder, actionState);
} }
@Override @Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
Log.i(TAG, "End swiping, resetting swipeRefreshLayout state");
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setEnabled(swipeRefreshLayoutEnabled);
}
getDefaultUIUtil().clearView(((ItemAdapter.NoteViewHolder) viewHolder).noteSwipeable); getDefaultUIUtil().clearView(((ItemAdapter.NoteViewHolder) viewHolder).noteSwipeable);
} }
@Override
public float getSwipeEscapeVelocity(float defaultValue) {
return defaultValue * 3;
}
}); });
} }
} }

View file

@ -214,7 +214,7 @@ public class NotesListViewActivity extends LockedActivity implements ItemAdapter
localAccount = db.getLocalAccountByAccountName(accountName); localAccount = db.getLocalAccountByAccountName(accountName);
try { try {
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()); ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext());
new NotesListViewItemTouchHelper(ssoAccount, this, this, db, adapter, syncCallBack, this::refreshLists).attachToRecyclerView(listView); new NotesListViewItemTouchHelper(ssoAccount, this, db, adapter, syncCallBack, this::refreshLists, swipeRefreshLayout, this).attachToRecyclerView(listView);
synchronize(); synchronize();
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) { } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
Log.i(TAG, "Tried to select account, but got an " + e.getClass().getSimpleName() + ". Asking for importing an account..."); Log.i(TAG, "Tried to select account, but got an " + e.getClass().getSimpleName() + ". Asking for importing an account...");