X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfilesRecyclerViewAdapter.java;h=cef8d8fbf6803e71c08491bf4d5805b121473441;hb=20c03b7a5eb152d42fbbe9ecbaae27530563b398;hp=dfffc8f233743553a9d3689d4911d82673f90f64;hpb=83cac114e375728080194fb09758b49c50a8119b;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 dfffc8f2..cef8d8fb 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,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 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 @@ -20,49 +20,48 @@ 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 +73,10 @@ public class ProfilesRecyclerViewAdapter public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { - Collections.swap(Data.profiles, 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 +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); @@ -96,60 +105,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.OnTouchListener dragStarter = (v, event) -> { - if (rearrangeHelper != null && editingProfiles.get()) { + if (rearrangeHelper != null && editingProfiles()) { rearrangeHelper.startDrag(holder); return true; } @@ -161,46 +188,64 @@ 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()); holder.mEditButton.setOnClickListener(mOnClickListener); - final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile); - holder.itemView - .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null); - if (editingProfiles.get()) { + final boolean sameProfile = currentProfile.equals(profile); + 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); @@ -209,6 +254,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; } } }