Improve key profile adapter notification code

This commit is contained in:
Alexander Bakker 2017-12-12 21:28:09 +01:00
parent ed86d6ac8e
commit a66d94a743
3 changed files with 8 additions and 13 deletions

View file

@ -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<KeyProfileHolder> im
public void addKey(KeyProfile profile) {
_keyProfiles.add(profile);
notifyDataSetChanged();
}
public void addKeys(List<KeyProfile> profiles) {
_keyProfiles.addAll(profiles);
notifyDataSetChanged();
int position = getItemCount() - 1;
if (position == 0) {
notifyDataSetChanged();
} else {
notifyItemInserted(position);
}
}
public void removeKey(KeyProfile profile) {

View file

@ -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;
}

View file

@ -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<KeyProfile> 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() {