Abstract the key profile list away from MainActivity

This commit is contained in:
Alexander Bakker 2017-12-12 02:19:29 +01:00
parent 461f321626
commit f952ee7df9
2 changed files with 24 additions and 12 deletions

View file

@ -28,13 +28,29 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
private Handler _uiHandler; private Handler _uiHandler;
private static Listener _listener; private static Listener _listener;
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles, Listener listener) { public KeyProfileAdapter(Listener listener) {
_keyProfiles = keyProfiles; _keyProfiles = new ArrayList<>();
_holders = new ArrayList<>(); _holders = new ArrayList<>();
_uiHandler = new Handler(); _uiHandler = new Handler();
_listener = listener; _listener = listener;
} }
public void addKey(KeyProfile profile) {
_keyProfiles.add(profile);
notifyDataSetChanged();
}
public void addKeys(List<KeyProfile> profiles) {
_keyProfiles.addAll(profiles);
notifyDataSetChanged();
}
public void removeKey(KeyProfile profile) {
int position = _keyProfiles.indexOf(profile);
_keyProfiles.remove(position);
notifyItemRemoved(position);
}
@Override @Override
public void onItemDismiss(int position) { public void onItemDismiss(int position) {

View file

@ -51,7 +51,6 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
private static final int CODE_PREFERENCES = 5; private static final int CODE_PREFERENCES = 5;
private KeyProfileAdapter _keyProfileAdapter; private KeyProfileAdapter _keyProfileAdapter;
private ArrayList<KeyProfile> _keyProfiles;
private DatabaseManager _db; private DatabaseManager _db;
private boolean _nightMode = false; private boolean _nightMode = false;
@ -113,8 +112,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
rvKeyProfiles.setLayoutManager(mLayoutManager); rvKeyProfiles.setLayoutManager(mLayoutManager);
_keyProfiles = new ArrayList<>(); _keyProfileAdapter = new KeyProfileAdapter(this);
_keyProfileAdapter = new KeyProfileAdapter(_keyProfiles, this);
if (_db.isDecrypted()) { if (_db.isDecrypted()) {
loadKeyProfiles(); loadKeyProfiles();
} }
@ -282,8 +280,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
return; return;
} }
_keyProfiles.add(profile); _keyProfileAdapter.addKey(profile);
_keyProfileAdapter.notifyDataSetChanged();
} }
private void onDoIntroResult(int resultCode, Intent data) { private void onDoIntroResult(int resultCode, Intent data) {
@ -400,9 +397,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
Toast.makeText(this, "An error occurred while trying to delete an entry", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "An error occurred while trying to delete an entry", Toast.LENGTH_SHORT).show();
return; return;
} }
int position = _keyProfiles.indexOf(profile); _keyProfileAdapter.removeKey(profile);
_keyProfiles.remove(profile);
_keyProfileAdapter.notifyItemRemoved(position);
}) })
.setNegativeButton(android.R.string.no, null) .setNegativeButton(android.R.string.no, null)
.show(); .show();
@ -501,9 +496,10 @@ 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()) {
_keyProfiles.add(new KeyProfile(entry)); profiles.add(new KeyProfile(entry));
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -511,7 +507,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
return; return;
} }
_keyProfileAdapter.notifyDataSetChanged(); _keyProfileAdapter.addKeys(profiles);
} }
private void updateLockIcon() { private void updateLockIcon() {