import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
-import android.support.design.widget.FloatingActionButton;
+import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
+import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
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;
/**
* device.
*/
private boolean mTwoPane;
- private FloatingActionButton fab;
+ private RecyclerView recyclerView;
+ @Override
+ public boolean onSupportNavigateUp() {
+ onBackPressed();
+ return super.onSupportNavigateUp();
+ }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
+ final ActionBar supportActionBar = getSupportActionBar();
+ if (supportActionBar != null) {
+ supportActionBar.setDisplayHomeAsUpEnabled(true);
+ supportActionBar.setDisplayShowHomeEnabled(true);
+ }
- RecyclerView recyclerView = findViewById(R.id.profile_list);
+ recyclerView = findViewById(R.id.profile_list);
if (recyclerView == null) throw new AssertionError();
setupRecyclerView(recyclerView);
- fab = findViewById(R.id.fab);
- fab.setOnClickListener(view -> {
- ProfilesRecyclerViewAdapter adapter =
- (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
- if (adapter != null) adapter.editProfile(recyclerView, null);
- });
-
if (findViewById(R.id.profile_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
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();
}
}
}
-
- @Override
- protected void onStart() {
- super.onStart();
- fab.show();
+ void launchNewProfileActivity() {
+ ProfilesRecyclerViewAdapter adapter =
+ (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
+ if (adapter != null) adapter.editProfile(recyclerView, null);
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
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();
- }
+ }
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.profile_list, menu);
+ menu.findItem(R.id.menu_add_profile).setOnMenuItemClickListener(item -> {
+ launchNewProfileActivity();
+ return true;
});
+ return super.onCreateOptionsMenu(menu);
}
-
public static class ProfilesRecyclerViewAdapter
extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
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);
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();
- MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
- holder.mRadioView.setChecked(profile.equals(newProfile));
- });
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();
- if (profile != null) Data.setCurrentProfile(profile);
+ 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 profile = (MobileLedgerProfile) holder.itemView.getTag();
- holder.mRadioView.setChecked(Data.profile.get().equals(profile));
+ 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;
}
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()));
+ (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
+ holder.itemView.setTag(profile);
holder.mTitle.setText(profile.getName());
holder.mSubTitle.setText(profile.getUrl());
- holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
+ holder.mRadioView.setChecked(profile.equals(currentProfile));
- holder.itemView.setTag(profile);
holder.mEditButton.setOnClickListener(mOnClickListener);
}
@Override