From a66d94a7438aed1a196a147eb4e1b1656dc51032 Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Tue, 12 Dec 2017 21:28:09 +0100 Subject: [PATCH] Improve key profile adapter notification code --- .../main/java/me/impy/aegis/KeyProfileAdapter.java | 12 ++++++------ .../main/java/me/impy/aegis/KeyProfileHolder.java | 3 +-- app/src/main/java/me/impy/aegis/MainActivity.java | 6 +----- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java index 9b0c906d..a34bafe0 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java @@ -8,7 +8,6 @@ import android.view.ViewGroup; import java.util.ArrayList; import java.util.Collections; -import java.util.List; import me.impy.aegis.helpers.ItemTouchHelperAdapter; @@ -25,12 +24,13 @@ public class KeyProfileAdapter extends RecyclerView.Adapter im public void addKey(KeyProfile profile) { _keyProfiles.add(profile); - notifyDataSetChanged(); - } - public void addKeys(List profiles) { - _keyProfiles.addAll(profiles); - notifyDataSetChanged(); + int position = getItemCount() - 1; + if (position == 0) { + notifyDataSetChanged(); + } else { + notifyItemInserted(position); + } } public void removeKey(KeyProfile profile) { diff --git a/app/src/main/java/me/impy/aegis/KeyProfileHolder.java b/app/src/main/java/me/impy/aegis/KeyProfileHolder.java index fe8311ec..04c8418b 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileHolder.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileHolder.java @@ -33,8 +33,7 @@ public class KeyProfileHolder extends RecyclerView.ViewHolder { } public void setData(KeyProfile profile) { - _keyProfile = profile; - if (_keyProfile == null) { + if ((_keyProfile = profile) == null) { return; } diff --git a/app/src/main/java/me/impy/aegis/MainActivity.java b/app/src/main/java/me/impy/aegis/MainActivity.java index a29034f3..84bfcf01 100644 --- a/app/src/main/java/me/impy/aegis/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/MainActivity.java @@ -31,7 +31,6 @@ import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.lang.reflect.UndeclaredThrowableException; -import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -486,18 +485,15 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter private void loadKeyProfiles() { updateLockIcon(); - List profiles = new ArrayList<>(); try { for (DatabaseEntry entry : _db.getKeys()) { - profiles.add(new KeyProfile(entry)); + _keyProfileAdapter.addKey(new KeyProfile(entry)); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "An error occurred while trying to load database entries", Toast.LENGTH_SHORT).show(); return; } - - _keyProfileAdapter.addKeys(profiles); } private void updateLockIcon() {