X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FProfileListActivity.java;h=dc36fc71a0295aa18f825331e022ea4c1c8fe62e;hb=4267ae2dcbc6fec16f1aa0fe49971f48d0e389ab;hp=06c7a1c481ef557aed6b2223155c455373f216b5;hpb=25804ffce015de8598e5885e64a5ece67a5e6a26;p=mobile-ledger.git
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 06c7a1c4..dc36fc71 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
@@ -1,18 +1,18 @@
/*
* Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * 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
* the Free Software Foundation, either version 3 of the License, or
* (at your opinion), any later version.
*
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License terms for details.
*
* You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see .
+ * along with MoLe. If not, see .
*/
package net.ktnx.mobileledger.ui.activity;
@@ -21,7 +21,7 @@ import android.content.Context;
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;
@@ -29,6 +29,7 @@ import android.support.v7.widget.Toolbar;
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;
@@ -39,10 +40,8 @@ import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
-import net.ktnx.mobileledger.utils.MLDB;
import java.util.Collections;
-import java.util.List;
/**
* An activity representing a list of Profiles. This activity
@@ -55,7 +54,8 @@ import java.util.List;
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;
/**
@@ -63,7 +63,13 @@ public class ProfileListActivity extends AppCompatActivity {
* device.
*/
private boolean mTwoPane;
+ private RecyclerView recyclerView;
+ @Override
+ public boolean onSupportNavigateUp() {
+ onBackPressed();
+ return super.onSupportNavigateUp();
+ }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -72,18 +78,16 @@ public class ProfileListActivity extends AppCompatActivity {
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);
- FloatingActionButton 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).
@@ -95,18 +99,24 @@ 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) {
- List list = MobileLedgerProfile.loadAllFromDB();
- if (index < list.size()) {
- ProfilesRecyclerViewAdapter adapter =
- (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
- if (adapter != null) adapter.editProfile(recyclerView, list.get(index));
- }
+ 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();
}
}
}
-
+ 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);
recyclerView.setAdapter(adapter);
@@ -136,28 +146,38 @@ public class ProfileListActivity extends AppCompatActivity {
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL));
}
-
+ @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 {
private final ProfileListActivity mParentActivity;
private final boolean mTwoPane;
private final View.OnClickListener mOnClickListener = view -> {
- MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
- editProfile(view, item);
+ MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
+ editProfile(view, profile);
};
ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
mParentActivity = parent;
mTwoPane = twoPane;
Data.profiles.addObserver((o, arg) -> {
Log.d("profiles", "profile list changed");
- notifyDataSetChanged();
+ if (arg == null) notifyDataSetChanged();
+ else notifyItemChanged((int) arg);
});
}
- private void editProfile(View view, MobileLedgerProfile item) {
+ private void editProfile(View view, MobileLedgerProfile profile) {
+ int index = Data.profiles.indexOf(profile);
if (mTwoPane) {
Bundle arguments = new Bundle();
- arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
+ arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
ProfileDetailFragment fragment = new ProfileDetailFragment();
fragment.setArguments(arguments);
mParentActivity.getSupportFragmentManager().beginTransaction()
@@ -166,8 +186,8 @@ public class ProfileListActivity extends AppCompatActivity {
else {
Context context = view.getContext();
Intent intent = new Intent(context, ProfileDetailActivity.class);
- intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
- (item == null) ? null : item.getUuid());
+ intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
+ if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
context.startActivity(intent);
}
@@ -178,11 +198,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(
- newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
+ 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;
}
@@ -191,22 +228,13 @@ 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);
+ (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.setOnCheckedChangeListener((buttonView, isChecked) -> {
- if (!isChecked) return;
- MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
- Data.profile.set(profile);
- });
+ holder.mRadioView.setChecked(profile.equals(currentProfile));
- holder.itemView.setTag(profile);
holder.mEditButton.setOnClickListener(mOnClickListener);
-
}
@Override
public int getItemCount() {