/*
* Copyright © 2019 Damyan Ivanov.
- * This file is part of MoLe.
- * MoLe is free software: you can distribute it and/or modify it
- * under the term of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your opinion), any later version.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
*
- * MoLe is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License terms for details.
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
*
- * You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
*/
package net.ktnx.mobileledger.ui.profiles;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
+import android.widget.LinearLayout;
import android.widget.TextView;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
import net.ktnx.mobileledger.utils.Colors;
+import net.ktnx.mobileledger.utils.ObservableValue;
import java.util.Collections;
+import java.util.Observer;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
editProfile(view, profile);
};
- private boolean editingProfiles = false;
+ private ObservableValue<Boolean> editingProfiles = new ObservableValue<>(false);
private RecyclerView recyclerView;
private ItemTouchHelper rearrangeHelper;
public ProfilesRecyclerViewAdapter() {
- Data.profiles.addObserver((o, arg) -> {
- Log.d("profiles", "profile list changed");
- if (arg == null) notifyDataSetChanged();
- else notifyItemChanged((int) arg);
- });
+ Log.d("flow", "ProfilesRecyclerViewAdapter.new()");
ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder,
@NonNull RecyclerView.ViewHolder target) {
- Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
- target.getAdapterPosition());
- MobileLedgerProfile.storeProfilesOrder();
- notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+ Data.profiles.blockNotifications();
+ try {
+ Collections.swap(Data.profiles, viewHolder.getAdapterPosition(), target.getAdapterPosition());
+ MobileLedgerProfile.storeProfilesOrder();
+ notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+ }
+ finally {
+ Data.profiles.unblockNotifications();
+ }
return true;
}
@Override
};
rearrangeHelper = new ItemTouchHelper(cb);
}
+ public void addEditingProfilesObserver(Observer o) {
+ editingProfiles.addObserver(o);
+ }
+ public void deleteEditingProfilesObserver(Observer o) {
+ editingProfiles.deleteObserver(o);
+ }
@Override
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
rearrangeHelper.attachToRecyclerView(null);
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
- if (editingProfiles) rearrangeHelper.attachToRecyclerView(recyclerView);
- }
- public boolean editingProfiles() {
- return this.editingProfiles;
+ if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void startEditingProfiles() {
- if (editingProfiles) return;
- this.editingProfiles = true;
+ if (editingProfiles.get()) return;
+ this.editingProfiles.set(true);
notifyDataSetChanged();
rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void stopEditingProfiles() {
- if (!editingProfiles) return;
- this.editingProfiles = false;
+ if (!editingProfiles.get()) return;
+ this.editingProfiles.set(false);
notifyDataSetChanged();
rearrangeHelper.attachToRecyclerView(null);
}
public void flipEditingProfiles() {
- if (editingProfiles) stopEditingProfiles();
+ if (editingProfiles.get()) stopEditingProfiles();
else startEditingProfiles();
}
private void editProfile(View view, MobileLedgerProfile profile) {
MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
final MobileLedgerProfile currentProfile = Data.profile.get();
final boolean sameProfile = currentProfile.equals(myProfile);
- view.setAlpha(sameProfile ? 1 : 0.5f);
});
+
+ View.OnTouchListener dragStarter = (v, event) -> {
+ if (rearrangeHelper != null && editingProfiles.get()) {
+ rearrangeHelper.startDrag(holder);
+ return true;
+ }
+ return false;
+ };
+
+ holder.tagAndHandleLayout.setOnTouchListener(dragStarter);
return holder;
}
@Override
holder.mEditButton.setOnClickListener(mOnClickListener);
- final boolean sameProfile = currentProfile.equals(profile);
- holder.itemView.setAlpha(sameProfile ? 1 : 0.5f);
+ final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
holder.itemView
.setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
- if (editingProfiles) {
+ if (editingProfiles.get()) {
holder.mRearrangeHandle.setVisibility(View.VISIBLE);
holder.mEditButton.setVisibility(View.VISIBLE);
}
else {
- holder.mRearrangeHandle.setVisibility(View.GONE);
+ holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
holder.mEditButton.setVisibility(View.GONE);
}
}
public int getItemCount() {
return Data.profiles.size();
}
+ public boolean isEditingProfiles() {
+ return editingProfiles.get();
+ }
class ProfileListViewHolder extends RecyclerView.ViewHolder {
final TextView mEditButton;
final TextView mTitle, mColorTag;
+ final LinearLayout tagAndHandleLayout;
final ImageView mRearrangeHandle;
ProfileListViewHolder(View view) {
mTitle = view.findViewById(R.id.title);
mColorTag = view.findViewById(R.id.colorTag);
mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
+ tagAndHandleLayout = view.findViewById(R.id.handle_and_tag);
}
}
}