Move save and load to the adapter

This commit is contained in:
Jakob Nixdorf 2017-07-12 11:53:27 +02:00
parent e9fa576850
commit 62dd05949d
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
2 changed files with 15 additions and 18 deletions

View file

@ -60,11 +60,10 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
private ArrayList<Integer> displayedEntries;
public ViewHolderEventCallback viewHolderEventCallback;
public EntriesCardAdapter(Context context, ArrayList<Entry> entries) {
public EntriesCardAdapter(Context context) {
this.context = context;
this.entries = entries;
entriesChanged();
loadEntries();
}
@Override
@ -80,15 +79,6 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
return entries.get(i);
}
public ArrayList<Entry> getEntries() {
return entries;
}
public void setEntries(ArrayList<Entry> e) {
entries = e;
entriesChanged();
}
public void addEntry(Entry e) {
entries.add(e);
entriesChanged();
@ -102,6 +92,15 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
notifyDataSetChanged();
}
public void saveEntries() {
DatabaseHelper.store(context, entries);
}
public void loadEntries() {
entries = DatabaseHelper.load(context);
entriesChanged();
}
public ArrayList<Integer> defaultIndices() {
ArrayList<Integer> newIdx = new ArrayList<>();
for (int i = 0; i < entries.size(); i++)

View file

@ -122,8 +122,7 @@ public class MainActivity extends AppCompatActivity {
Entry e = new Entry(type, secret, period, label);
e.setCurrentOTP(TOTPHelper.generate(e.getSecret(), e.getPeriod()));
adapter.addEntry(e);
DatabaseHelper.store(getBaseContext(), adapter.getEntries());
adapter.saveEntries();
} else if (type == Entry.OTPType.HOTP) {
Toast.makeText(getBaseContext(), R.string.toast_tmp_hotp, Toast.LENGTH_LONG).show();
}
@ -221,7 +220,7 @@ public class MainActivity extends AppCompatActivity {
boolean success = DatabaseHelper.importFromJSON(this, uri);
if (success) {
adapter.setEntries(DatabaseHelper.load(this));
adapter.loadEntries();
Toast.makeText(this, R.string.toast_import_success, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.toast_import_failed, Toast.LENGTH_LONG).show();
@ -310,7 +309,7 @@ public class MainActivity extends AppCompatActivity {
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
adapter = new EntriesCardAdapter(this, DatabaseHelper.load(this));
adapter = new EntriesCardAdapter(this);
recList.setAdapter(adapter);
touchHelperCallback = new SimpleItemTouchHelperCallback(adapter);
@ -392,8 +391,7 @@ public class MainActivity extends AppCompatActivity {
Entry e = new Entry(result.getContents());
e.setCurrentOTP(TOTPHelper.generate(e.getSecret(), e.getPeriod()));
adapter.addEntry(e);
DatabaseHelper.store(this, adapter.getEntries());
adapter.saveEntries();
} catch (Exception e) {
Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show();
}