From 950c6d0cf3d87bb2a858a8384510b4a761f73237 Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Mon, 25 Dec 2017 15:36:29 +0100 Subject: [PATCH] Move the recycler view to its own fragment --- .../java/me/impy/aegis/KeyProfileAdapter.java | 3 +- .../java/me/impy/aegis/KeyProfileView.java | 87 +++++++++++++++++++ .../main/java/me/impy/aegis/MainActivity.java | 48 ++++------ app/src/main/res/layout/activity_main.xml | 23 ++--- .../res/layout/fragment_keyprofile_view.xml | 11 +++ 5 files changed, 121 insertions(+), 51 deletions(-) create mode 100644 app/src/main/java/me/impy/aegis/KeyProfileView.java create mode 100644 app/src/main/res/layout/fragment_keyprofile_view.xml diff --git a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java index d50001bb..eb541009 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java @@ -15,10 +15,9 @@ public class KeyProfileAdapter extends RecyclerView.Adapter im private static Listener _listener; private boolean _showIssuer; - public KeyProfileAdapter(Listener listener, boolean showIssuer) { + public KeyProfileAdapter(Listener listener) { _keyProfiles = new ArrayList<>(); _listener = listener; - _showIssuer = showIssuer; } public void setShowIssuer(boolean showIssuer) { diff --git a/app/src/main/java/me/impy/aegis/KeyProfileView.java b/app/src/main/java/me/impy/aegis/KeyProfileView.java new file mode 100644 index 00000000..08301d9f --- /dev/null +++ b/app/src/main/java/me/impy/aegis/KeyProfileView.java @@ -0,0 +1,87 @@ +package me.impy.aegis; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import me.impy.aegis.db.DatabaseEntry; +import me.impy.aegis.helpers.SimpleItemTouchHelperCallback; + +public class KeyProfileView extends Fragment implements KeyProfileAdapter.Listener { + private KeyProfileAdapter _adapter; + private Listener _listener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + _adapter = new KeyProfileAdapter(this); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_keyprofile_view, container, false); + + // set up the recycler view + RecyclerView rvKeyProfiles = view.findViewById(R.id.rvKeyProfiles); + LinearLayoutManager mLayoutManager = new LinearLayoutManager(view.getContext()); + rvKeyProfiles.setLayoutManager(mLayoutManager); + ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(_adapter); + ItemTouchHelper touchHelper = new ItemTouchHelper(callback); + touchHelper.attachToRecyclerView(rvKeyProfiles); + rvKeyProfiles.setAdapter(_adapter); + + return view; + } + + public void setListener(Listener listener) { + _listener = listener; + } + + @Override + public void onKeyProfileClick(KeyProfile profile) { + _listener.onEntryClick(profile); + } + + @Override + public boolean onLongKeyProfileClick(KeyProfile profile) { + return false; + } + + @Override + public void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2) { + _listener.onEntryMove(profile1.getEntry(), profile2.getEntry()); + } + + @Override + public void onKeyProfileDrop(KeyProfile profile) { + _listener.onEntryDrop(profile.getEntry()); + } + + public void setShowIssuer(boolean showIssuer) { + _adapter.setShowIssuer(showIssuer); + _adapter.notifyDataSetChanged(); + } + + public void addKey(KeyProfile profile) { + _adapter.addKey(profile); + } + + public void removeKey(KeyProfile profile) { + _adapter.removeKey(profile); + } + + public void clearKeys() { + _adapter.clearKeys(); + } + + public interface Listener { + void onEntryClick(KeyProfile profile); + void onEntryMove(DatabaseEntry entry1, DatabaseEntry entry2); + void onEntryDrop(DatabaseEntry entry); + } +} diff --git a/app/src/main/java/me/impy/aegis/MainActivity.java b/app/src/main/java/me/impy/aegis/MainActivity.java index fcb1ccd2..0e007d3e 100644 --- a/app/src/main/java/me/impy/aegis/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/MainActivity.java @@ -15,10 +15,7 @@ import android.support.design.widget.BottomSheetDialog; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AlertDialog; import android.os.Bundle; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; -import android.support.v7.widget.helper.ItemTouchHelper; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -26,7 +23,6 @@ import android.widget.LinearLayout; import android.widget.Toast; import java.io.ByteArrayOutputStream; -import java.io.FileNotFoundException; import java.io.InputStream; import java.lang.reflect.UndeclaredThrowableException; import java.util.Collections; @@ -37,10 +33,9 @@ import me.impy.aegis.db.DatabaseEntry; import me.impy.aegis.db.DatabaseManager; import me.impy.aegis.helpers.PermissionHelper; import me.impy.aegis.importers.DatabaseImporter; -import me.impy.aegis.helpers.SimpleItemTouchHelperCallback; import me.impy.aegis.util.ByteInputStream; -public class MainActivity extends AegisActivity implements KeyProfileAdapter.Listener { +public class MainActivity extends AegisActivity implements KeyProfileView.Listener { // activity request codes private static final int CODE_GET_KEYINFO = 0; private static final int CODE_ADD_KEYINFO = 1; @@ -54,9 +49,9 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis private static final int CODE_PERM_IMPORT = 1; private static final int CODE_PERM_CAMERA = 2; - private KeyProfileAdapter _keyProfileAdapter; private AegisApplication _app; private DatabaseManager _db; + private KeyProfileView _keyProfileView; private boolean _nightMode = false; private Menu _menu; @@ -72,6 +67,11 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); + // set up the key profile view + _keyProfileView = (KeyProfileView) getSupportFragmentManager().findFragmentById(R.id.key_profiles); + _keyProfileView.setListener(this); + _keyProfileView.setShowIssuer(_app.getPreferences().getBoolean("pref_issuer", false)); + // init the app shortcuts and execute any pending actions initializeAppShortcuts(); doShortcutActions(); @@ -81,16 +81,6 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis fab.setEnabled(true); fab.setOnClickListener(view -> onGetKeyInfo()); - // set up the recycler view for the key profiles - _keyProfileAdapter = new KeyProfileAdapter(this, _app.getPreferences().getBoolean("pref_issuer", false)); - RecyclerView rvKeyProfiles = findViewById(R.id.rvKeyProfiles); - LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); - rvKeyProfiles.setLayoutManager(mLayoutManager); - ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(_keyProfileAdapter); - ItemTouchHelper touchHelper = new ItemTouchHelper(callback); - touchHelper.attachToRecyclerView(rvKeyProfiles); - rvKeyProfiles.setAdapter(_keyProfileAdapter); - // skip this part if this is the not initial startup and the database has been unlocked if (!_app.isRunning() && _db.isLocked()) { if (!_db.fileExists()) { @@ -180,8 +170,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis // refresh the entire key profile list if needed if (data.getBooleanExtra("needsRefresh", false)) { boolean showIssuer = _app.getPreferences().getBoolean("pref_issuer", false); - _keyProfileAdapter.setShowIssuer(showIssuer); - _keyProfileAdapter.notifyDataSetChanged(); + _keyProfileView.setShowIssuer(showIssuer); } // perform any pending actions @@ -336,7 +325,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis return; } - _keyProfileAdapter.addKey(profile); + _keyProfileView.addKey(profile); } private void onDoIntroResult(int resultCode, Intent data) { @@ -458,7 +447,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis } saveDatabase(); - _keyProfileAdapter.removeKey(profile); + _keyProfileView.removeKey(profile); }) .setNegativeButton(android.R.string.no, null) .show(); @@ -485,7 +474,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis } return true; case R.id.action_lock: - _keyProfileAdapter.clearKeys(); + _keyProfileView.clearKeys(); try { _db.lock(); } catch (Exception e) { @@ -548,7 +537,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis try { for (DatabaseEntry entry : _db.getKeys()) { - _keyProfileAdapter.addKey(new KeyProfile(entry)); + _keyProfileView.addKey(new KeyProfile(entry)); } } catch (Exception e) { e.printStackTrace(); @@ -566,19 +555,14 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis } @Override - public void onKeyProfileClick(KeyProfile profile) { + public void onEntryClick(KeyProfile profile) { createBottomSheet(profile).show(); } @Override - public boolean onLongKeyProfileClick(KeyProfile profile) { - return false; - } - - @Override - public void onKeyProfileMove(KeyProfile profile1, KeyProfile profile2) { + public void onEntryMove(DatabaseEntry entry1, DatabaseEntry entry2) { try { - _db.swapKeys(profile1.getEntry(), profile2.getEntry()); + _db.swapKeys(entry1, entry2); } catch (Exception e) { e.printStackTrace(); throw new UndeclaredThrowableException(e); @@ -586,7 +570,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis } @Override - public void onKeyProfileDrop(KeyProfile profile) { + public void onEntryDrop(DatabaseEntry entry) { saveDatabase(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2f2cb9bc..d43e04df 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,22 +30,11 @@ android:src="@drawable/ic_add_black_24dp" android:tint="@color/background"/> - + android:layout_width="match_parent" + app:layout_behavior="@string/appbar_scrolling_view_behavior"/> - - - - - - \ No newline at end of file + diff --git a/app/src/main/res/layout/fragment_keyprofile_view.xml b/app/src/main/res/layout/fragment_keyprofile_view.xml new file mode 100644 index 00000000..ac14d614 --- /dev/null +++ b/app/src/main/res/layout/fragment_keyprofile_view.xml @@ -0,0 +1,11 @@ + + + +