Fix key profile sorting and simplify its logic a bit

This commit is contained in:
Alexander Bakker 2017-11-26 22:10:10 +01:00
parent 74536dbda6
commit 277d76e467
2 changed files with 6 additions and 26 deletions

View file

@ -41,29 +41,13 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
}
private void remove(int position) {
_keyProfiles.remove(position);
notifyItemRemoved(position);
}
@Override
public void onItemMove(int firstPosition, int secondPosition) {
Collections.swap(_keyProfiles, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition);
_keyProfiles.get(firstPosition).getEntry().setOrder(secondPosition);
adjustOrder(secondPosition);
}
private void adjustOrder(int startPosition) {
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override
public int compare(KeyProfile keyProfile, KeyProfile t1) {
return keyProfile.getEntry().getOrder() - t1.getEntry().getOrder();
}
};
for (int i = startPosition; i < _keyProfiles.size(); i++) {
// update the order of all key profiles
for (int i = 0; i < _keyProfiles.size(); i++) {
_keyProfiles.get(i).getEntry().setOrder(i + 1);
}
}

View file

@ -122,15 +122,7 @@ public class MainActivity extends AppCompatActivity {
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(_keyProfileAdapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(rvKeyProfiles);
rvKeyProfiles.setAdapter(_keyProfileAdapter);
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override
public int compare(KeyProfile keyProfile, KeyProfile t1) {
return keyProfile.getEntry().getOrder() - t1.getEntry().getOrder();
}
};
Collections.sort(_keyProfiles, comparator);
}
@Override
@ -445,6 +437,10 @@ public class MainActivity extends AppCompatActivity {
} catch (Exception e) {
e.printStackTrace();
}
Collections.sort(_keyProfiles, (p1, p2) -> {
return p1.getEntry().getOrder() >= p2.getEntry().getOrder() ? 1 : -1;
});
}
private void updateLockIcon() {