import java.util.Date;
import java.util.List;
import java.util.Locale;
-import java.util.Observer;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
private DrawerLayout.SimpleDrawerListener drawerListener;
private ActionBarDrawerToggle barDrawerToggle;
private ViewPager.SimpleOnPageChangeListener pageChangeListener;
- private Observer editingProfilesObserver;
private MobileLedgerProfile profile;
@Override
protected void onStart() {
barDrawerToggle = null;
if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
pageChangeListener = null;
- if (mProfileListAdapter != null)
- mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
- editingProfilesObserver = null;
super.onDestroy();
}
@Override
if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
root.setAdapter(mProfileListAdapter);
- if (editingProfilesObserver == null) {
- editingProfilesObserver = (o, arg) -> {
- if (mProfileListAdapter.isEditingProfiles()) {
- profileListHeadMore.setVisibility(View.GONE);
- profileListHeadMore
- .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
- profileListHeadCancel.setVisibility(View.VISIBLE);
- profileListHeadCancel
- .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
- profileListHeadAddProfile.setVisibility(View.VISIBLE);
- profileListHeadAddProfile
- .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
- }
- else {
- profileListHeadCancel.setVisibility(View.GONE);
- profileListHeadCancel
- .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
- profileListHeadMore.setVisibility(View.VISIBLE);
- profileListHeadMore
- .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
- profileListHeadAddProfile.setVisibility(View.GONE);
- profileListHeadAddProfile
- .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
- }
- };
- mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
- }
+ mProfileListAdapter.editingProfiles
+ .observe(this, newValue -> {
+ if (newValue) {
+ profileListHeadMore.setVisibility(View.GONE);
+ profileListHeadCancel.setVisibility(View.VISIBLE);
+ profileListHeadAddProfile.setVisibility(View.VISIBLE);
+ if (drawer.isDrawerOpen(GravityCompat.START)) {
+ profileListHeadMore.startAnimation(AnimationUtils
+ .loadAnimation(MainActivity.this, R.anim.fade_out));
+ profileListHeadCancel.startAnimation(AnimationUtils
+ .loadAnimation(MainActivity.this, R.anim.fade_in));
+ profileListHeadAddProfile.startAnimation(AnimationUtils
+ .loadAnimation(MainActivity.this, R.anim.fade_in));
+ }
+ }
+ else {
+ profileListHeadCancel.setVisibility(View.GONE);
+ profileListHeadMore.setVisibility(View.VISIBLE);
+ profileListHeadAddProfile.setVisibility(View.GONE);
+ if (drawer.isDrawerOpen(GravityCompat.START)) {
+ profileListHeadCancel.startAnimation(AnimationUtils
+ .loadAnimation(MainActivity.this, R.anim.fade_out));
+ profileListHeadMore.startAnimation(AnimationUtils
+ .loadAnimation(MainActivity.this, R.anim.fade_in));
+ profileListHeadAddProfile.startAnimation(AnimationUtils
+ .loadAnimation(MainActivity.this, R.anim.fade_out));
+ }
+ }
+
+ mProfileListAdapter.notifyDataSetChanged();
+ });
LinearLayoutManager llm = new LinearLayoutManager(this);
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.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
-import java.util.Observer;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
+import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
editProfile(view, profile);
};
- private ObservableValue<Boolean> editingProfiles = new ObservableValue<>(false);
+ public MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
private RecyclerView recyclerView;
private ItemTouchHelper rearrangeHelper;
public ProfilesRecyclerViewAdapter() {
};
rearrangeHelper = new ItemTouchHelper(cb);
}
- public void addEditingProfilesObserver(Observer o) {
- editingProfiles.addObserver(o);
- }
- public void deleteEditingProfilesObserver(Observer o) {
- editingProfiles.deleteObserver(o);
- }
@Override
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
rearrangeHelper.attachToRecyclerView(null);
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
- if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
+ if (editingProfiles.getValue()) rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void startEditingProfiles() {
- if (editingProfiles.get()) return;
- this.editingProfiles.set(true);
+ if (editingProfiles.getValue()) return;
+ this.editingProfiles.setValue(true);
notifyDataSetChanged();
rearrangeHelper.attachToRecyclerView(recyclerView);
}
public void stopEditingProfiles() {
- if (!editingProfiles.get()) return;
- this.editingProfiles.set(false);
+ if (!editingProfiles.getValue()) return;
+ this.editingProfiles.setValue(false);
notifyDataSetChanged();
rearrangeHelper.attachToRecyclerView(null);
}
public void flipEditingProfiles() {
- if (editingProfiles.get()) stopEditingProfiles();
+ if (editingProfiles.getValue()) stopEditingProfiles();
else startEditingProfiles();
}
private void editProfile(View view, MobileLedgerProfile profile) {
context.startActivity(intent);
}
private void onProfileRowClicked(View v) {
- if (editingProfiles.get()) return;
+ if (editingProfiles.getValue()) return;
MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
if (profile == null)
throw new IllegalStateException("Profile row without associated profile");
});
View.OnTouchListener dragStarter = (v, event) -> {
- if (rearrangeHelper != null && editingProfiles.get()) {
+ if (rearrangeHelper != null && editingProfiles.getValue()) {
rearrangeHelper.startDrag(holder);
return true;
}
final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
holder.itemView
.setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
- if (editingProfiles.get()) {
+ if (editingProfiles.getValue()) {
boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
holder.mRearrangeHandle.setVisibility(View.VISIBLE);
holder.mEditButton.setVisibility(View.VISIBLE);
final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
return profiles != null ? profiles.size() : 0;
}
- public boolean isEditingProfiles() {
- return editingProfiles.get();
- }
class ProfileListViewHolder extends RecyclerView.ViewHolder {
final TextView mEditButton;
final TextView mTitle, mColorTag;