Reorder for profiles list introduced

This commit is contained in:
Yaroslav Berezanskyi 2015-08-28 20:42:31 +03:00
parent 85a0ab95a9
commit c9ad385f4d
5 changed files with 137 additions and 29 deletions

View File

@ -66,7 +66,6 @@ public abstract class BaseActivity extends AppCompatActivity implements
private EditText mSearchTextView;
private DrawerLayout mDrawerLayout;
private ProfileDrawerAdapter mProfileDrawerAdapter;
private View mFrontView;
private View mBackView;
@ -81,10 +80,12 @@ public abstract class BaseActivity extends AppCompatActivity implements
};
private ImageView mHeaderImageView;
private DAppDrawerAdapter mDAppsDrawerAdapter;
private ProfileDrawerAdapter mProfileDrawerAdapter;
protected abstract void onDAppClick(Dapp dapp);
private ItemTouchHelper mItemTouchHelper;
private ItemTouchHelper mDAppsTouchHelper;
private ItemTouchHelper mProfilesTouchHelper;
@SuppressLint("InflateParams")
@Override
@ -110,6 +111,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
super.onDrawerClosed(drawerView);
GeneralUtil.hideKeyBoard(mSearchTextView, BaseActivity.this);
mDAppsDrawerAdapter.setEditModeEnabled(false);
mProfileDrawerAdapter.setEditModeEnabled(false);
if (!isDrawerFrontViewActive()) {
flipDrawer();
}
@ -132,8 +134,17 @@ public abstract class BaseActivity extends AppCompatActivity implements
RecyclerView profilesRecyclerView = (RecyclerView) findViewById(R.id.profile_drawer_recycler_view);
RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(this);
profilesRecyclerView.setLayoutManager(layoutManager2);
mProfileDrawerAdapter = new ProfileDrawerAdapter(this, new ArrayList<Profile>(), this);
mProfileDrawerAdapter = new ProfileDrawerAdapter(this, this, new ProfileDrawerAdapter.OnStartDragListener() {
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
mProfilesTouchHelper.startDrag(viewHolder);
}
});
profilesRecyclerView.setAdapter(mProfileDrawerAdapter);
ItemTouchHelper.Callback profilesCallback = new SimpleItemTouchHelperCallback(mProfileDrawerAdapter);
mProfilesTouchHelper = new ItemTouchHelper(profilesCallback);
mProfilesTouchHelper.attachToRecyclerView(profilesRecyclerView);
updateCurrentProfileName();
populateProfiles();
@ -144,14 +155,14 @@ public abstract class BaseActivity extends AppCompatActivity implements
mDAppsDrawerAdapter = new DAppDrawerAdapter(this, this, new DAppDrawerAdapter.OnStartDragListener() {
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
mItemTouchHelper.startDrag(viewHolder);
mDAppsTouchHelper.startDrag(viewHolder);
}
});
dAppsRecyclerView.setAdapter(mDAppsDrawerAdapter);
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mDAppsDrawerAdapter);
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(dAppsRecyclerView);
ItemTouchHelper.Callback dAppsCallback = new SimpleItemTouchHelperCallback(mDAppsDrawerAdapter);
mDAppsTouchHelper = new ItemTouchHelper(dAppsCallback);
mDAppsTouchHelper.attachToRecyclerView(dAppsRecyclerView);
populateDApps();
@ -298,6 +309,8 @@ public abstract class BaseActivity extends AppCompatActivity implements
if (isDrawerFrontViewActive()) {
mDAppsDrawerAdapter.setEditModeEnabled(false);
GeneralUtil.hideKeyBoard(mSearchTextView, BaseActivity.this);
}else{
mProfileDrawerAdapter.setEditModeEnabled(false);
}
flipDrawer();
break;
@ -356,7 +369,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
@SuppressWarnings("ConstantConditions")
@Override
public void onProfilePress(final Profile profile) {
public void onProfileEdit(final Profile profile) {
if (ProfileManager.getCurrentProfile().getId().equals(profile.getId())) {
ProfileDialogFragment dialogFragment = ProfileDialogFragment.newInstance(profile);
dialogFragment.show(getSupportFragmentManager(), "profile_dialog");
@ -405,7 +418,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
}
@Override
public void onNewProfile() {
public void onProfileAdd() {
GeneralUtil.showProfileCreateDialog(this);
}

View File

@ -158,7 +158,6 @@ public class DAppDrawerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
mEditModeEnabled = editModeEnabled;
notifyDataSetChanged();
}
}
@Override

View File

@ -2,8 +2,10 @@ package io.syng.adapter;
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
@ -11,35 +13,46 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.syng.R;
import io.syng.adapter.helper.ItemTouchHelperAdapter;
import io.syng.entity.Profile;
import io.syng.util.ProfileManager;
public class ProfileDrawerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class ProfileDrawerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperAdapter {
private static final int TYPE_HEADER = 10;
private static final int TYPE_SIMPLE_ITEM = 20;
private final OnProfileClickListener mListener;
private final OnProfileClickListener mProfileClickListener;
private final OnStartDragListener mDragListener;
public interface OnProfileClickListener {
void onProfileClick(Profile profile);
void onProfilePress(Profile profile);
void onProfileEdit(Profile profile);
void onProfileImport();
void onNewProfile();
void onProfileAdd();
}
public interface OnStartDragListener {
void onStartDrag(RecyclerView.ViewHolder viewHolder);
}
private final Context mContext;
private List<Profile> mDataSet;
private boolean mEditModeEnabled;
public ProfileDrawerAdapter(Context context, List<Profile> data, OnProfileClickListener listener) {
this.mDataSet = data;
public ProfileDrawerAdapter(Context context, OnProfileClickListener profileClickListener, OnStartDragListener dragListener) {
this.mDataSet = new ArrayList<>();
mContext = context;
mListener = listener;
mProfileClickListener = profileClickListener;
mDragListener = dragListener;
}
@Override
@ -59,35 +72,62 @@ public class ProfileDrawerAdapter extends RecyclerView.Adapter<RecyclerView.View
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof SimpleViewHolder) {
final Profile profile = mDataSet.get(position - 1);// -1 because of the header
SimpleViewHolder myHolder = (SimpleViewHolder) holder;
final SimpleViewHolder myHolder = (SimpleViewHolder) holder;
myHolder.setting.setVisibility(mEditModeEnabled ? View.VISIBLE : View.GONE);
myHolder.reorder.setVisibility(mEditModeEnabled ? View.VISIBLE : View.GONE);
Glide.with(mContext).load(R.drawable.profile).into(myHolder.profileIcon);
myHolder.nameTextView.setText(profile.getName());
myHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onProfileClick(profile);
if (!mEditModeEnabled) {
if (mProfileClickListener != null) {
mProfileClickListener.onProfileClick(profile);
}
} else {
setEditModeEnabled(false);
}
}
});
myHolder.view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mListener != null) {
mListener.onProfilePress(profile);
}
mEditModeEnabled = !mEditModeEnabled;
notifyDataSetChanged();
return true;
}
});
myHolder.setting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mProfileClickListener != null) {
mProfileClickListener.onProfileEdit(profile);
}
}
});
myHolder.reorder.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragListener.onStartDrag(myHolder);
}
return false;
}
});
}
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder myHolder = (HeaderViewHolder) holder;
myHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onNewProfile();
if (mProfileClickListener != null) {
mProfileClickListener.onProfileAdd();
}
}
});
@ -95,6 +135,13 @@ public class ProfileDrawerAdapter extends RecyclerView.Adapter<RecyclerView.View
}
}
public void setEditModeEnabled(boolean editModeEnabled) {
if (editModeEnabled != mEditModeEnabled) {
mEditModeEnabled = editModeEnabled;
notifyDataSetChanged();
}
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
@ -117,12 +164,16 @@ public class ProfileDrawerAdapter extends RecyclerView.Adapter<RecyclerView.View
private TextView nameTextView;
private ImageView profileIcon;
private View view;
private ImageView reorder;
private ImageView setting;
public SimpleViewHolder(View v) {
super(v);
nameTextView = (TextView) v.findViewById(R.id.tv_account_name);
profileIcon = (ImageView) v.findViewById(R.id.iv_profile_icon);
view = v.findViewById(R.id.ll_account);
reorder = (ImageView) v.findViewById(R.id.iv_reorder);
setting = (ImageView) v.findViewById(R.id.iv_settings);
}
}
@ -143,4 +194,15 @@ public class ProfileDrawerAdapter extends RecyclerView.Adapter<RecyclerView.View
notifyDataSetChanged();
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
int realFromPosition = fromPosition - 1;//-1 because of the header
int realToPosition = toPosition - 1;
Collections.swap(mDataSet, realFromPosition, realToPosition);
notifyItemMoved(fromPosition, toPosition);
ProfileManager.reorderProfiles(realFromPosition, realToPosition);
return true;
}
}

View File

@ -128,6 +128,12 @@ public final class ProfileManager {
}
}
public static void reorderProfiles(int fromPosition, int toPosition) {
List<Profile> profiles = ProfileManager.getProfiles();
Collections.swap(profiles, fromPosition, toPosition);
PrefsUtil.saveProfiles(new ArrayList<>(profiles));
}
@Nullable
public static Profile getProfileById(String profileId) {
List<Profile> profiles = ProfileManager.getProfiles();

View File

@ -9,11 +9,11 @@
<ImageView
android:id="@+id/iv_profile_icon"
android:tint="@color/accent"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"/>
android:layout_marginStart="16dp"
android:tint="@color/accent"/>
<TextView
android:id="@+id/tv_account_name"
@ -22,7 +22,35 @@
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="@android:color/black"
android:text="Add account"/>
android:text="Add account"
android:textColor="@android:color/black"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:gravity="center_vertical|end"
>
<ImageView
android:id="@+id/iv_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_settings_black_24dp"
android:tint="@color/drawer_icon_color"/>
<ImageView
android:id="@+id/iv_reorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_reorder_black_24dp"
android:tint="@color/drawer_icon_color"/>
</LinearLayout>
</LinearLayout>