Improve key profile adapter notification code

pull/41/head
Alexander Bakker 8 years ago
parent ed86d6ac8e
commit a66d94a743

@ -8,7 +8,6 @@ import android.view.ViewGroup;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import me.impy.aegis.helpers.ItemTouchHelperAdapter; import me.impy.aegis.helpers.ItemTouchHelperAdapter;
@ -25,12 +24,13 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
public void addKey(KeyProfile profile) { public void addKey(KeyProfile profile) {
_keyProfiles.add(profile); _keyProfiles.add(profile);
notifyDataSetChanged();
}
public void addKeys(List<KeyProfile> profiles) { int position = getItemCount() - 1;
_keyProfiles.addAll(profiles); if (position == 0) {
notifyDataSetChanged(); notifyDataSetChanged();
} else {
notifyItemInserted(position);
}
} }
public void removeKey(KeyProfile profile) { public void removeKey(KeyProfile profile) {

@ -33,8 +33,7 @@ public class KeyProfileHolder extends RecyclerView.ViewHolder {
} }
public void setData(KeyProfile profile) { public void setData(KeyProfile profile) {
_keyProfile = profile; if ((_keyProfile = profile) == null) {
if (_keyProfile == null) {
return; return;
} }

@ -31,7 +31,6 @@ import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -486,18 +485,15 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
private void loadKeyProfiles() { private void loadKeyProfiles() {
updateLockIcon(); updateLockIcon();
List<KeyProfile> profiles = new ArrayList<>();
try { try {
for (DatabaseEntry entry : _db.getKeys()) { for (DatabaseEntry entry : _db.getKeys()) {
profiles.add(new KeyProfile(entry)); _keyProfileAdapter.addKey(new KeyProfile(entry));
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Toast.makeText(this, "An error occurred while trying to load database entries", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "An error occurred while trying to load database entries", Toast.LENGTH_SHORT).show();
return; return;
} }
_keyProfileAdapter.addKeys(profiles);
} }
private void updateLockIcon() { private void updateLockIcon() {

Loading…
Cancel
Save