mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 14:02:49 +00:00
Rearranged code and improved listview
This commit is contained in:
parent
9204e530b0
commit
606ce09fc7
5 changed files with 140 additions and 68 deletions
|
@ -23,7 +23,6 @@ android {
|
||||||
dependencies {
|
dependencies {
|
||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
compile 'com.android.support:appcompat-v7:24.2.1'
|
compile 'com.android.support:appcompat-v7:24.2.1'
|
||||||
compile 'com.github.nisrulz:recyclerviewhelper:24.1.1'
|
|
||||||
compile 'com.android.support:design:24.2.1'
|
compile 'com.android.support:design:24.2.1'
|
||||||
compile 'agency.tango.android:material-intro-screen:0.0.3'
|
compile 'agency.tango.android:material-intro-screen:0.0.3'
|
||||||
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
|
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
|
||||||
|
|
|
@ -20,24 +20,26 @@ import java.util.List;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
import github.nisrulz.recyclerviewhelper.RVHAdapter;
|
|
||||||
import me.impy.aegis.crypto.OTP;
|
import me.impy.aegis.crypto.OTP;
|
||||||
|
import me.impy.aegis.helpers.ItemTouchHelperAdapter;
|
||||||
|
|
||||||
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements RVHAdapter {
|
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements ItemTouchHelperAdapter {
|
||||||
private ArrayList<KeyProfile> mKeyProfiles;
|
|
||||||
private final List<KeyProfileHolder> lstHolders;
|
private final List<KeyProfileHolder> lstHolders;
|
||||||
|
private ArrayList<KeyProfile> mKeyProfiles;
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
private Handler uiHandler;
|
private Handler uiHandler;
|
||||||
|
|
||||||
@Override
|
// Provide a suitable constructor (depends on the kind of dataset)
|
||||||
public boolean onItemMove(int fromPosition, int toPosition) {
|
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
|
||||||
swap(fromPosition, toPosition);
|
mKeyProfiles = keyProfiles;
|
||||||
return false;
|
lstHolders = new ArrayList<>();
|
||||||
|
timer = new Timer();
|
||||||
|
uiHandler = new Handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onItemDismiss(int position, int direction) {
|
public void onItemDismiss(int position) {
|
||||||
remove(position);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper functions you might want to implement to make changes in the list as an event is fired
|
// Helper functions you might want to implement to make changes in the list as an event is fired
|
||||||
|
@ -46,11 +48,52 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
|
||||||
notifyItemRemoved(position);
|
notifyItemRemoved(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void swap(int firstPosition, int secondPosition) {
|
@Override
|
||||||
|
public void onItemMove(int firstPosition, int secondPosition) {
|
||||||
Collections.swap(mKeyProfiles, firstPosition, secondPosition);
|
Collections.swap(mKeyProfiles, firstPosition, secondPosition);
|
||||||
notifyItemMoved(firstPosition, secondPosition);
|
notifyItemMoved(firstPosition, secondPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create new views (invoked by the layout manager)
|
||||||
|
@Override
|
||||||
|
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
|
// create a new view
|
||||||
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
|
||||||
|
// set the view's size, margins, paddings and layout parameters
|
||||||
|
|
||||||
|
KeyProfileHolder vh = new KeyProfileHolder(v);
|
||||||
|
return vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the contents of a view (invoked by the layout manager)
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
|
||||||
|
holder.setData(mKeyProfiles.get(position));
|
||||||
|
holder.updateCode();
|
||||||
|
lstHolders.add(holder);
|
||||||
|
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
uiHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// check if this key profile still exists
|
||||||
|
if (lstHolders.contains(holder)) {
|
||||||
|
holder.updateCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, holder.keyProfile.Info.getMillisTillNextRotation(), holder.keyProfile.Info.getPeriod() * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the size of your dataset (invoked by the layout manager)
|
||||||
|
@Override
|
||||||
|
public int getItemCount() {
|
||||||
|
return mKeyProfiles.size();
|
||||||
|
}
|
||||||
|
|
||||||
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
|
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
|
||||||
TextView profileName;
|
TextView profileName;
|
||||||
TextView profileCode;
|
TextView profileCode;
|
||||||
|
@ -110,52 +153,4 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
|
||||||
return newDrawable;
|
return newDrawable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide a suitable constructor (depends on the kind of dataset)
|
|
||||||
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
|
|
||||||
mKeyProfiles = keyProfiles;
|
|
||||||
lstHolders = new ArrayList<>();
|
|
||||||
timer = new Timer();
|
|
||||||
uiHandler = new Handler();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new views (invoked by the layout manager)
|
|
||||||
@Override
|
|
||||||
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
||||||
// create a new view
|
|
||||||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
|
|
||||||
// set the view's size, margins, paddings and layout parameters
|
|
||||||
|
|
||||||
KeyProfileHolder vh = new KeyProfileHolder(v);
|
|
||||||
return vh;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace the contents of a view (invoked by the layout manager)
|
|
||||||
@Override
|
|
||||||
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
|
|
||||||
holder.setData(mKeyProfiles.get(position));
|
|
||||||
holder.updateCode();
|
|
||||||
lstHolders.add(holder);
|
|
||||||
|
|
||||||
timer.schedule(new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
uiHandler.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
// check if this key profile still exists
|
|
||||||
if (lstHolders.contains(holder)) {
|
|
||||||
holder.updateCode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, holder.keyProfile.Info.getMillisTillNextRotation(), holder.keyProfile.Info.getPeriod() * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the size of your dataset (invoked by the layout manager)
|
|
||||||
@Override
|
|
||||||
public int getItemCount() {
|
|
||||||
return mKeyProfiles.size();
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -24,12 +24,10 @@ import com.yarolegovich.lovelydialog.LovelyTextInputDialog;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import github.nisrulz.recyclerviewhelper.RVHItemClickListener;
|
|
||||||
import github.nisrulz.recyclerviewhelper.RVHItemDividerDecoration;
|
|
||||||
import github.nisrulz.recyclerviewhelper.RVHItemTouchHelperCallback;
|
|
||||||
import me.impy.aegis.crypto.CryptoUtils;
|
import me.impy.aegis.crypto.CryptoUtils;
|
||||||
import me.impy.aegis.crypto.OTP;
|
import me.impy.aegis.crypto.OTP;
|
||||||
import me.impy.aegis.db.Database;
|
import me.impy.aegis.db.Database;
|
||||||
|
import me.impy.aegis.helpers.SimpleItemTouchHelperCallback;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
@ -90,7 +88,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
rvKeyProfiles.setLayoutManager(mLayoutManager);
|
rvKeyProfiles.setLayoutManager(mLayoutManager);
|
||||||
|
|
||||||
final Context context = this.getApplicationContext();
|
final Context context = this.getApplicationContext();
|
||||||
rvKeyProfiles.addOnItemTouchListener(new RVHItemClickListener(this, new RVHItemClickListener.OnItemClickListener() {
|
/*rvKeyProfiles.addOnItemTouchListener(new RVHItemClickListener(this, new RVHItemClickListener.OnItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(View view, int position) {
|
public void onItemClick(View view, int position) {
|
||||||
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
@ -99,14 +97,18 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
Toast.makeText(context, "Code successfully copied to the clipboard", Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, "Code successfully copied to the clipboard", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}));
|
}));*/
|
||||||
|
|
||||||
mKeyProfileAdapter = new KeyProfileAdapter(mKeyProfiles);
|
mKeyProfileAdapter = new KeyProfileAdapter(mKeyProfiles);
|
||||||
rvKeyProfiles.addItemDecoration(new RVHItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
|
//rvKeyProfiles.addItemDecoration(new RVHItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
|
||||||
|
|
||||||
ItemTouchHelper.Callback callback = new RVHItemTouchHelperCallback(mKeyProfileAdapter, true, false, false);
|
//ItemTouchHelper.Callback callback = new RVHItemTouchHelperCallback(mKeyProfileAdapter, true, false, false);
|
||||||
ItemTouchHelper helper = new ItemTouchHelper(callback);
|
//ItemTouchHelper helper = new ItemTouchHelper(callback);
|
||||||
helper.attachToRecyclerView(rvKeyProfiles);
|
//helper.attachToRecyclerView(rvKeyProfiles);
|
||||||
|
|
||||||
|
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mKeyProfileAdapter);
|
||||||
|
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
|
||||||
|
touchHelper.attachToRecyclerView(rvKeyProfiles);
|
||||||
|
|
||||||
rvKeyProfiles.setAdapter(mKeyProfileAdapter);
|
rvKeyProfiles.setAdapter(mKeyProfileAdapter);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package me.impy.aegis.helpers;
|
||||||
|
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
|
||||||
|
public interface ItemTouchHelperAdapter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an item has been dragged far enough to trigger a move. This is called every time
|
||||||
|
* an item is shifted, and <strong>not</strong> at the end of a "drop" event.<br/>
|
||||||
|
* <br/>
|
||||||
|
* Implementations should call {@link RecyclerView.Adapter#notifyItemMoved(int, int)} after
|
||||||
|
* adjusting the underlying data to reflect this move.
|
||||||
|
*
|
||||||
|
* @param fromPosition The start position of the moved item.
|
||||||
|
* @param toPosition Then resolved position of the moved item.
|
||||||
|
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
|
||||||
|
* @see RecyclerView.ViewHolder#getAdapterPosition()
|
||||||
|
*/
|
||||||
|
void onItemMove(int fromPosition, int toPosition);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an item has been dismissed by a swipe.<br/>
|
||||||
|
* <br/>
|
||||||
|
* Implementations should call {@link RecyclerView.Adapter#notifyItemRemoved(int)} after
|
||||||
|
* adjusting the underlying data to reflect this removal.
|
||||||
|
*
|
||||||
|
* @param position The position of the item dismissed.
|
||||||
|
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
|
||||||
|
* @see RecyclerView.ViewHolder#getAdapterPosition()
|
||||||
|
*/
|
||||||
|
void onItemDismiss(int position);
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package me.impy.aegis.helpers;
|
||||||
|
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.support.v7.widget.helper.ItemTouchHelper;
|
||||||
|
|
||||||
|
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
|
||||||
|
|
||||||
|
private final ItemTouchHelperAdapter mAdapter;
|
||||||
|
|
||||||
|
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
|
||||||
|
mAdapter = adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isLongPressDragEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isItemViewSwipeEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
||||||
|
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
|
||||||
|
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
|
||||||
|
return makeMovementFlags(dragFlags, swipeFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
|
||||||
|
RecyclerView.ViewHolder target) {
|
||||||
|
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
|
||||||
|
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue