X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=inline;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfilesRecyclerViewAdapter.java;h=21984333500a80d4733e684a7f7bd284967eac86;hb=7dcfecdf81f1ac19d30cfa3c5abb4ce15eb8c5c8;hp=a96517c28a0419893f7571425e130c4ba58b4de8;hpb=ebc9d1e219e7b86e35acde2c799252f5da1dc5af;p=mobile-ledger-staging.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 a96517c2..21984333 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,18 +1,18 @@
/*
- * 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.
+ * Copyright © 2021 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.
*
- * 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;
@@ -20,49 +20,47 @@ package net.ktnx.mobileledger.ui.profiles;
import android.content.Context;
import android.content.Intent;
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 net.ktnx.mobileledger.utils.ObservableValue;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
import java.util.Collections;
-import java.util.Observer;
-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;
+ public final MutableLiveData editingProfiles = new MutableLiveData<>(false);
private final View.OnClickListener mOnClickListener = view -> {
MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
editProfile(view, profile);
};
- private ObservableValue editingProfiles = new ObservableValue<>(false);
- public void addEditingProfilesObserver(Observer o) {
- editingProfiles.addObserver(o);
- }
- public void deleteEditingProfilesObserver(Observer o) {
- editingProfiles.deleteObserver(o);
- }
+ private final ItemTouchHelper rearrangeHelper;
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
@@ -74,7 +72,10 @@ 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());
@@ -86,6 +87,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);
@@ -96,61 +104,78 @@ public class ProfilesRecyclerViewAdapter
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
- if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
+ if (editingProfiles())
+ rearrangeHelper.attachToRecyclerView(recyclerView);
+ }
+ public boolean editingProfiles() {
+ final Boolean b = editingProfiles.getValue();
+ if (b == null)
+ return false;
+ return b;
}
public void startEditingProfiles() {
- if (editingProfiles.get()) return;
- this.editingProfiles.set(true);
- notifyDataSetChanged();
+ if (editingProfiles())
+ return;
+ this.editingProfiles.setValue(true);
rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void stopEditingProfiles() {
- if (!editingProfiles.get()) return;
- this.editingProfiles.set(false);
- notifyDataSetChanged();
+ if (!editingProfiles())
+ return;
+ this.editingProfiles.setValue(false);
rearrangeHelper.attachToRecyclerView(null);
}
public void flipEditingProfiles() {
- if (editingProfiles.get()) stopEditingProfiles();
- else startEditingProfiles();
+ if (editingProfiles())
+ 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);
- if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
+ if (index != -1)
+ intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
context.startActivity(intent);
}
+ private void onProfileRowClicked(View v) {
+ if (editingProfiles())
+ 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) {
View view = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.profile_list_content, parent, false);
+ .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.get()) {
+ if (rearrangeHelper != null && editingProfiles()) {
rearrangeHelper.startDrag(holder);
return true;
}
@@ -162,16 +187,21 @@ public class ProfilesRecyclerViewAdapter
}
@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(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
- else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
+ 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());
// holder.mSubTitle.setText(profile.getUrl());
@@ -179,30 +209,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.get()) {
+ holder.itemView.setBackground(
+ sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
+ if (editingProfiles()) {
+ 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 {
+ 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();
- }
- public boolean isEditingProfiles() {
- return editingProfiles.get();
+ final ArrayList profiles = Data.profiles.getValue();
+ return profiles != null ? profiles.size() : 0;
}
- class ProfileListViewHolder extends RecyclerView.ViewHolder {
+ static 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);
@@ -211,6 +253,7 @@ public class ProfilesRecyclerViewAdapter
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;
}
}
}