X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfilesRecyclerViewAdapter.java;h=34b9064a8f4a34023f4810c24dde2fc5a48519d4;hb=5bba2c06a81c87327fdcf3f2a85c3206d932c2f9;hp=84dab3c9b7d44d6c014e0b2bef352a19ad18f143;hpb=1738aee062f23cc9a69ed11cade1cb048f083def;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 84dab3c9..34b9064a 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 @@ -45,7 +45,6 @@ import net.ktnx.mobileledger.utils.Colors; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; -import java.util.Locale; import static net.ktnx.mobileledger.utils.Logger.debug; @@ -56,7 +55,7 @@ public class ProfilesRecyclerViewAdapter MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag(); editProfile(view, profile); }; - public MutableLiveData editingProfiles = new MutableLiveData<>(false); + public final MutableLiveData editingProfiles = new MutableLiveData<>(false); private RecyclerView recyclerView; private ItemTouchHelper rearrangeHelper; private boolean animationsEnabled = true; @@ -75,7 +74,8 @@ public class ProfilesRecyclerViewAdapter @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { final ArrayList profiles = Data.profiles.getValue(); - if (profiles == null) throw new AssertionError(); + if (profiles == null) + throw new AssertionError(); Collections.swap(profiles, viewHolder.getAdapterPosition(), target.getAdapterPosition()); MobileLedgerProfile.storeProfilesOrder(); @@ -105,44 +105,59 @@ public class ProfilesRecyclerViewAdapter public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); this.recyclerView = recyclerView; - if (editingProfiles.getValue()) 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.getValue()) return; + if (editingProfiles()) + return; this.editingProfiles.setValue(true); rearrangeHelper.attachToRecyclerView(recyclerView); } public void stopEditingProfiles() { - if (!editingProfiles.getValue()) return; + if (!editingProfiles()) + return; this.editingProfiles.setValue(false); rearrangeHelper.attachToRecyclerView(null); } public void flipEditingProfiles() { - if (editingProfiles.getValue()) stopEditingProfiles(); - else startEditingProfiles(); + if (editingProfiles()) + stopEditingProfiles(); + else + startEditingProfiles(); } private void editProfile(View view, MobileLedgerProfile 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.getValue()) return; + 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); @@ -151,7 +166,8 @@ public class ProfilesRecyclerViewAdapter onProfileRowClicked(row); }); holder.mColorTag.setOnClickListener(v -> { - View row = (View) v.getParent().getParent(); + View row = (View) v.getParent() + .getParent(); onProfileRowClicked(row); }); holder.mTitle.setOnLongClickListener(v -> { @@ -160,7 +176,7 @@ public class ProfilesRecyclerViewAdapter }); View.OnTouchListener dragStarter = (v, event) -> { - if (rearrangeHelper != null && editingProfiles.getValue()) { + if (rearrangeHelper != null && editingProfiles()) { rearrangeHelper.startDrag(holder); return true; } @@ -173,33 +189,36 @@ public class ProfilesRecyclerViewAdapter @Override public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) { final ArrayList profiles = Data.profiles.getValue(); - if (profiles == null) throw new AssertionError(); + if (profiles == null) + throw new AssertionError(); final MobileLedgerProfile profile = profiles.get(position); - final MobileLedgerProfile currentProfile = Data.profile.getValue(); - debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position, - profile.getUuid(), (currentProfile == null) ? "" : currentProfile.getUuid())); + 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.getThemeHue(); - if (hue == -1) holder.mColorTag - .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG)); - else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue)); + 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.getValue()) { + 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); + Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(), + R.anim.fade_in); holder.mRearrangeHandle.startAnimation(a); holder.mEditButton.startAnimation(a); } @@ -209,8 +228,8 @@ public class ProfilesRecyclerViewAdapter holder.mRearrangeHandle.setVisibility(View.INVISIBLE); holder.mEditButton.setVisibility(View.GONE); if (wasShown && animationsEnabled) { - Animation a = AnimationUtils - .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_out); + Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(), + R.anim.fade_out); holder.mRearrangeHandle.startAnimation(a); holder.mEditButton.startAnimation(a); } @@ -221,7 +240,7 @@ public class ProfilesRecyclerViewAdapter 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;