Save the database after a drag-drop event and stop saving it on onStop

This commit is contained in:
Alexander Bakker 2017-12-12 03:14:26 +01:00
parent 17378937a9
commit dbc79b49e8
4 changed files with 32 additions and 13 deletions

View file

@ -56,6 +56,11 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
} }
@Override
public void onItemDrop(int position) {
_listener.onKeyProfileDrop(_keyProfiles.get(position));
}
@Override @Override
public void onItemMove(int firstPosition, int secondPosition) { public void onItemMove(int firstPosition, int secondPosition) {
// notify the database first // notify the database first
@ -177,5 +182,6 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
void onKeyProfileClick(KeyProfile profile); void onKeyProfileClick(KeyProfile profile);
boolean onLongKeyProfileClick(KeyProfile profile); boolean onLongKeyProfileClick(KeyProfile profile);
void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2); void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2);
void onKeyProfileDrop(KeyProfile profile);
} }
} }

View file

@ -344,12 +344,6 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
setPreferredTheme(); setPreferredTheme();
} }
@Override
protected void onStop() {
saveDatabase();
super.onStop();
}
private BottomSheetDialog createBottomSheet(KeyProfile profile) { private BottomSheetDialog createBottomSheet(KeyProfile profile) {
View bottomSheetView = getLayoutInflater().inflate(R.layout.bottom_sheet_edit_profile, null); View bottomSheetView = getLayoutInflater().inflate(R.layout.bottom_sheet_edit_profile, null);
LinearLayout copyLayout = bottomSheetView.findViewById(R.id.copy_button); LinearLayout copyLayout = bottomSheetView.findViewById(R.id.copy_button);
@ -474,16 +468,11 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
} }
if (restart) { if (restart) {
finish(); recreate();
startActivity(new Intent(this, this.getClass()));
} }
} }
private void saveDatabase() { private void saveDatabase() {
if (!_db.isDecrypted()) {
return;
}
try { try {
_db.save(); _db.save();
} catch (Exception e) { } catch (Exception e) {
@ -536,4 +525,9 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
throw new UndeclaredThrowableException(e); throw new UndeclaredThrowableException(e);
} }
} }
@Override
public void onKeyProfileDrop(KeyProfile profile) {
saveDatabase();
}
} }

View file

@ -30,4 +30,11 @@ public interface ItemTouchHelperAdapter {
* @see RecyclerView.ViewHolder#getAdapterPosition() * @see RecyclerView.ViewHolder#getAdapterPosition()
*/ */
void onItemDismiss(int position); void onItemDismiss(int position);
/**
* Called when an item has been dropped after a drag.
*
* @param position The position of the moved item.
*/
void onItemDrop(int position);
} }

View file

@ -6,6 +6,7 @@ import android.support.v7.widget.helper.ItemTouchHelper;
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback { public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter _adapter; private final ItemTouchHelperAdapter _adapter;
private boolean _positionChanged = false;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) { public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
_adapter = adapter; _adapter = adapter;
@ -32,6 +33,7 @@ public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) { RecyclerView.ViewHolder target) {
_adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition()); _adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
_positionChanged = true;
return true; return true;
} }
@ -39,4 +41,14 @@ public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
_adapter.onItemDismiss(viewHolder.getAdapterPosition()); _adapter.onItemDismiss(viewHolder.getAdapterPosition());
} }
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
if (_positionChanged) {
_adapter.onItemDrop(viewHolder.getAdapterPosition());
_positionChanged = false;
}
}
} }