/*
- * Copyright © 2019 Damyan Ivanov.
+ * 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
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 static net.ktnx.mobileledger.utils.Logger.debug;
public class ProfilesRecyclerViewAdapter
extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
private static WeakReference<ProfilesRecyclerViewAdapter> instanceRef;
+ public final MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
private final View.OnClickListener mOnClickListener = view -> {
MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
editProfile(view, profile);
};
- public MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
+ private final ItemTouchHelper rearrangeHelper;
private RecyclerView recyclerView;
- private ItemTouchHelper rearrangeHelper;
private boolean animationsEnabled = true;
public ProfilesRecyclerViewAdapter() {
instanceRef = new WeakReference<>(this);
@NonNull RecyclerView.ViewHolder viewHolder,
@NonNull RecyclerView.ViewHolder target) {
final ArrayList<MobileLedgerProfile> 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();
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.profile.getValue() != profile )
+ if (Data.getProfile() != profile)
Data.drawerOpen.setValue(false);
Data.setCurrentProfile(profile);
}
@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);
onProfileRowClicked(row);
});
holder.mColorTag.setOnClickListener(v -> {
- View row = (View) v.getParent().getParent();
+ View row = (View) v.getParent()
+ .getParent();
onProfileRowClicked(row);
});
holder.mTitle.setOnLongClickListener(v -> {
});
View.OnTouchListener dragStarter = (v, event) -> {
- if (rearrangeHelper != null && editingProfiles.getValue()) {
+ if (rearrangeHelper != null && editingProfiles()) {
rearrangeHelper.startDrag(holder);
return true;
}
@Override
public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
final ArrayList<MobileLedgerProfile> 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) ? "<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);
}
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);
}
final ArrayList<MobileLedgerProfile> 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;