X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfilesRecyclerViewAdapter.java;h=b2b5cb7fe18f9ee86867b041ad134c1625c6270e;hb=0ce370cea3c5c980b6eeb14acf965188ae951f51;hp=16bc932b8057d1352340cbc2a921343021a77510;hpb=058f03aa72c457595a829ac0ae1c5806cdcf41ef;p=mobile-ledger.git
diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
index 16bc932b..b2b5cb7f 100644
--- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
+++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
@@ -1,60 +1,68 @@
/*
* 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 .
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see .
*/
package net.ktnx.mobileledger.ui.profiles;
import android.content.Context;
import android.content.Intent;
-import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
-import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.view.animation.Animation;
+import android.view.animation.AnimationUtils;
import android.widget.ImageView;
+import android.widget.LinearLayout;
import android.widget.TextView;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.constraintlayout.widget.ConstraintLayout;
+import androidx.lifecycle.MutableLiveData;
+import androidx.recyclerview.widget.ItemTouchHelper;
+import androidx.recyclerview.widget.RecyclerView;
+
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
import net.ktnx.mobileledger.utils.Colors;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.Locale;
-import androidx.annotation.NonNull;
-import androidx.recyclerview.widget.ItemTouchHelper;
-import androidx.recyclerview.widget.RecyclerView;
+import static net.ktnx.mobileledger.utils.Logger.debug;
public class ProfilesRecyclerViewAdapter
extends RecyclerView.Adapter {
+ private static WeakReference instanceRef;
private final View.OnClickListener mOnClickListener = view -> {
MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
editProfile(view, profile);
};
- private boolean editingProfiles = false;
+ public MutableLiveData editingProfiles = new MutableLiveData<>(false);
private RecyclerView recyclerView;
private ItemTouchHelper rearrangeHelper;
+ private boolean animationsEnabled = true;
public ProfilesRecyclerViewAdapter() {
- Data.profiles.addObserver((o, arg) -> {
- Log.d("profiles", "profile list changed");
- if (arg == null) notifyDataSetChanged();
- else notifyItemChanged((int) arg);
- });
+ instanceRef = new WeakReference<>(this);
+ debug("flow", "ProfilesRecyclerViewAdapter.new()");
ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
@Override
@@ -66,7 +74,9 @@ public class ProfilesRecyclerViewAdapter
public boolean onMove(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder,
@NonNull RecyclerView.ViewHolder target) {
- Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
+ final ArrayList profiles = Data.profiles.getValue();
+ if (profiles == null) throw new AssertionError();
+ Collections.swap(profiles, viewHolder.getAdapterPosition(),
target.getAdapterPosition());
MobileLedgerProfile.storeProfilesOrder();
notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
@@ -78,6 +88,13 @@ public class ProfilesRecyclerViewAdapter
};
rearrangeHelper = new ItemTouchHelper(cb);
}
+ public static @Nullable
+ ProfilesRecyclerViewAdapter getInstance() {
+ return instanceRef.get();
+ }
+ public void setAnimationsEnabled(boolean animationsEnabled) {
+ this.animationsEnabled = animationsEnabled;
+ }
@Override
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
rearrangeHelper.attachToRecyclerView(null);
@@ -88,29 +105,24 @@ public class ProfilesRecyclerViewAdapter
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.getValue()) rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void startEditingProfiles() {
- if (editingProfiles) return;
- this.editingProfiles = true;
- notifyDataSetChanged();
+ if (editingProfiles.getValue()) return;
+ this.editingProfiles.setValue(true);
rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void stopEditingProfiles() {
- if (!editingProfiles) return;
- this.editingProfiles = false;
- notifyDataSetChanged();
+ if (!editingProfiles.getValue()) return;
+ this.editingProfiles.setValue(false);
rearrangeHelper.attachToRecyclerView(null);
}
public void flipEditingProfiles() {
- if (editingProfiles) stopEditingProfiles();
+ if (editingProfiles.getValue()) stopEditingProfiles();
else startEditingProfiles();
}
private void editProfile(View view, MobileLedgerProfile profile) {
- int index = Data.profiles.indexOf(profile);
+ int index = Data.getProfileIndex(profile);
Context context = view.getContext();
Intent intent = new Intent(context, ProfileDetailActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
@@ -118,6 +130,16 @@ public class ProfilesRecyclerViewAdapter
context.startActivity(intent);
}
+ private void onProfileRowClicked(View v) {
+ if (editingProfiles.getValue()) return;
+ MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
+ if (profile == null)
+ throw new IllegalStateException("Profile row without associated profile");
+ debug("profiles", "Setting profile to " + profile.getName());
+ if (Data.getProfile() != profile)
+ Data.drawerOpen.setValue(false);
+ Data.setCurrentProfile(profile);
+ }
@NonNull
@Override
public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@@ -125,36 +147,44 @@ public class ProfilesRecyclerViewAdapter
.inflate(R.layout.profile_list_content, parent, false);
ProfileListViewHolder holder = new ProfileListViewHolder(view);
+ holder.mRow.setOnClickListener(this::onProfileRowClicked);
holder.mTitle.setOnClickListener(v -> {
View row = (View) v.getParent();
- MobileLedgerProfile profile = (MobileLedgerProfile) row.getTag();
- if (profile == null)
- throw new IllegalStateException("Profile row without associated profile");
- Log.d("profiles", "Setting profile to " + profile.getName());
- Data.setCurrentProfile(profile);
+ onProfileRowClicked(row);
+ });
+ holder.mColorTag.setOnClickListener(v -> {
+ View row = (View) v.getParent().getParent();
+ onProfileRowClicked(row);
});
holder.mTitle.setOnLongClickListener(v -> {
flipEditingProfiles();
return true;
});
- Data.profile.addObserver((o, arg) -> {
- 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.getValue()) {
+ rearrangeHelper.startDrag(holder);
+ return true;
+ }
+ return false;
+ };
+
+ holder.tagAndHandleLayout.setOnTouchListener(dragStarter);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
- final MobileLedgerProfile profile = Data.profiles.get(position);
- final MobileLedgerProfile currentProfile = Data.profile.get();
- Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
- (currentProfile == null) ? "" : currentProfile.getUuid()));
+ final ArrayList profiles = Data.profiles.getValue();
+ if (profiles == null) throw new AssertionError();
+ final MobileLedgerProfile profile = profiles.get(position);
+ final MobileLedgerProfile currentProfile = Data.getProfile();
+ debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
+ profile.getUuid(), currentProfile.getUuid()));
holder.itemView.setTag(profile);
- int hue = profile.getThemeId();
- if (hue == -1) holder.mColorTag.setBackgroundColor(Color.TRANSPARENT);
+ int hue = profile.getThemeHue();
+ if (hue == -1) holder.mColorTag
+ .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
holder.mTitle.setText(profile.getName());
@@ -163,26 +193,42 @@ public class ProfilesRecyclerViewAdapter
holder.mEditButton.setOnClickListener(mOnClickListener);
final boolean sameProfile = currentProfile.equals(profile);
- holder.itemView.setAlpha(sameProfile ? 1 : 0.5f);
holder.itemView
.setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
- if (editingProfiles) {
+ if (editingProfiles.getValue()) {
+ boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
holder.mRearrangeHandle.setVisibility(View.VISIBLE);
holder.mEditButton.setVisibility(View.VISIBLE);
+ if (wasHidden && animationsEnabled) {
+ Animation a = AnimationUtils
+ .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_in);
+ holder.mRearrangeHandle.startAnimation(a);
+ holder.mEditButton.startAnimation(a);
+ }
}
else {
- holder.mRearrangeHandle.setVisibility(View.GONE);
+ boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE;
+ holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
holder.mEditButton.setVisibility(View.GONE);
+ if (wasShown && animationsEnabled) {
+ Animation a = AnimationUtils
+ .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_out);
+ holder.mRearrangeHandle.startAnimation(a);
+ holder.mEditButton.startAnimation(a);
+ }
}
}
@Override
public int getItemCount() {
- return Data.profiles.size();
+ final ArrayList profiles = Data.profiles.getValue();
+ return profiles != null ? profiles.size() : 0;
}
class ProfileListViewHolder extends RecyclerView.ViewHolder {
final TextView mEditButton;
final TextView mTitle, mColorTag;
+ final LinearLayout tagAndHandleLayout;
final ImageView mRearrangeHandle;
+ final ConstraintLayout mRow;
ProfileListViewHolder(View view) {
super(view);
@@ -190,6 +236,8 @@ public class ProfilesRecyclerViewAdapter
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);
+ mRow = (ConstraintLayout) view;
}
}
}