From 62dd05949d89336c877015b8073900377e383c41 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Wed, 12 Jul 2017 11:53:27 +0200 Subject: [PATCH] Move save and load to the adapter --- .../flocke/andotp/EntriesCardAdapter.java | 23 +++++++++---------- .../shadowice/flocke/andotp/MainActivity.java | 10 ++++---- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java index 5ab565a8..ec7397d1 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/EntriesCardAdapter.java @@ -60,11 +60,10 @@ public class EntriesCardAdapter extends RecyclerView.Adapter displayedEntries; public ViewHolderEventCallback viewHolderEventCallback; - public EntriesCardAdapter(Context context, ArrayList entries) { + public EntriesCardAdapter(Context context) { this.context = context; - this.entries = entries; - entriesChanged(); + loadEntries(); } @Override @@ -80,15 +79,6 @@ public class EntriesCardAdapter extends RecyclerView.Adapter getEntries() { - return entries; - } - - public void setEntries(ArrayList e) { - entries = e; - entriesChanged(); - } - public void addEntry(Entry e) { entries.add(e); entriesChanged(); @@ -102,6 +92,15 @@ public class EntriesCardAdapter extends RecyclerView.Adapter defaultIndices() { ArrayList newIdx = new ArrayList<>(); for (int i = 0; i < entries.size(); i++) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java index 812567aa..04d4a043 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java @@ -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(); }