X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FProfileListActivity.java;h=98afa5191f74185ab043668ca659951a06a51de2;hp=cb37f5f18d0456c16609bacbdd4647169951334c;hb=9e8a289a85a65d372b47ccd2c2261299972ae4dd;hpb=b06a7a291e35add2dfc89313d226c5efd1bae3b3 diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java index cb37f5f1..98afa519 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java @@ -41,8 +41,6 @@ import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity; import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment; import java.util.Collections; -import java.util.Observable; -import java.util.Observer; /** * An activity representing a list of Profiles. This activity @@ -55,7 +53,8 @@ import java.util.Observer; public class ProfileListActivity extends AppCompatActivity { public static final String ARG_ACTION = "action"; - public static final String ARG_PROFILE_INDEX = "profile_uuid"; + public static final String ARG_PROFILE_INDEX = "profile_index"; + public static final int PROFILE_INDEX_NONE = -1; public static final int ACTION_EDIT_PROFILE = 1; public static final int ACTION_INVALID = -1; /** @@ -96,12 +95,17 @@ public class ProfileListActivity extends AppCompatActivity { int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID); if (action == ACTION_EDIT_PROFILE) { Log.d("profiles", "got edit profile action"); - int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1); - if (index >= 0) { - MobileLedgerProfile profile = Data.profiles.get(index); - ProfilesRecyclerViewAdapter adapter = - (ProfilesRecyclerViewAdapter) recyclerView.getAdapter(); - if (adapter != null) adapter.editProfile(recyclerView, profile); + int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, PROFILE_INDEX_NONE); + + MobileLedgerProfile profile = (index >= 0) ? Data.profiles.get(index) : null; + ProfilesRecyclerViewAdapter adapter = + (ProfilesRecyclerViewAdapter) recyclerView.getAdapter(); + if (adapter != null) { + adapter.editProfile(recyclerView, profile); + + // if invoked from the initial screen, get out so that when the new profile + // activity finishes the user i navigated to the main activity + if ((profile == null) && Data.profiles.getList().isEmpty()) finish(); } } } @@ -139,25 +143,6 @@ public class ProfileListActivity extends AppCompatActivity { new ItemTouchHelper(cb).attachToRecyclerView(recyclerView); recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL)); - - recyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() { - @Override - public boolean onFling(int dX, int dY) { - Log.d("tmp", String.format("fling %d %d", dX, dY)); - if (dY > 0) fab.hide(); - if (dY < 0) fab.show(); - return false; - } - }); - - recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - if (dy > 0) fab.hide(); - if (dy < 0) fab.show(); - } - }); } public static class ProfilesRecyclerViewAdapter @@ -191,6 +176,7 @@ public class ProfileListActivity extends AppCompatActivity { else { 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); context.startActivity(intent); @@ -202,10 +188,28 @@ public class ProfileListActivity extends AppCompatActivity { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.profile_list_content, parent, false); ProfileListViewHolder holder = new ProfileListViewHolder(view); - Data.profile.addObserver((o, arg) -> { - MobileLedgerProfile newProfile = Data.profile.get(); + + holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> { + if (!isChecked) return; + Log.d("profiles", + String.format("Item %d got checked", holder.getAdapterPosition())); MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag(); - holder.mRadioView.setChecked(profile.equals(newProfile)); + if (profile != null) { + Log.d("profiles", + String.format("Setting current profile to %s", profile.getUuid())); + Data.setCurrentProfile(profile); + } + }); + View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true); + holder.mTitle.setOnClickListener(profileSelector); + holder.mSubTitle.setOnClickListener(profileSelector); + Data.profile.addObserver((o, arg) -> { + MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag(); + final MobileLedgerProfile currentProfile = Data.profile.get(); + final boolean sameProfile = currentProfile.equals(myProfile); + if (holder.mRadioView.isChecked() != sameProfile) { + holder.mRadioView.setChecked(sameProfile); + } }); return holder; } @@ -214,24 +218,12 @@ public class ProfileListActivity extends AppCompatActivity { 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.getUuid())); - View.OnClickListener profileSelector = v -> { - holder.mRadioView.setChecked(true); - Data.setCurrentProfile(profile); - }; - Data.profile.addObserver(new Observer() { - @Override - public void update(Observable o, Object arg) { - holder.mRadioView.setChecked(Data.profile.get().equals(profile)); - } - }); + (currentProfile == null) ? "" : currentProfile.getUuid())); + holder.itemView.setTag(profile); holder.mTitle.setText(profile.getName()); - holder.mTitle.setOnClickListener(profileSelector); holder.mSubTitle.setText(profile.getUrl()); - holder.mSubTitle.setOnClickListener(profileSelector); - holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid())); + holder.mRadioView.setChecked(profile.equals(currentProfile)); - holder.itemView.setTag(profile); holder.mEditButton.setOnClickListener(mOnClickListener); } @Override