]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
a welcome screen directs to the new profile activity when there are no profiles defined
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / ProfileListActivity.java
index 03088b473a1abd6f463bca9966b40e100ee8c88e..98afa5191f74185ab043668ca659951a06a51de2 100644 (file)
@@ -31,7 +31,6 @@ import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.CompoundButton;
 import android.widget.RadioButton;
 import android.widget.TextView;
 
@@ -40,12 +39,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;
-import java.util.Observable;
-import java.util.Observer;
 
 /**
  * An activity representing a list of Profiles. This activity
@@ -58,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;
     /**
@@ -66,6 +62,7 @@ public class ProfileListActivity extends AppCompatActivity {
      * device.
      */
     private boolean mTwoPane;
+    private FloatingActionButton fab;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -80,14 +77,11 @@ public class ProfileListActivity extends AppCompatActivity {
         if (recyclerView == null) throw new AssertionError();
         setupRecyclerView(recyclerView);
 
-        FloatingActionButton fab = findViewById(R.id.fab);
-        fab.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                ProfilesRecyclerViewAdapter adapter =
-                        (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
-                if (adapter != null) adapter.editProfile(recyclerView, null);
-            }
+        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) {
@@ -101,18 +95,26 @@ 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<MobileLedgerProfile> 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();
             }
         }
     }
 
+    @Override
+    protected void onStart() {
+        super.onStart();
+        fab.show();
+    }
     private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
         final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
         recyclerView.setAdapter(adapter);
@@ -148,25 +150,24 @@ public class ProfileListActivity extends AppCompatActivity {
 
         private final ProfileListActivity mParentActivity;
         private final boolean mTwoPane;
-        private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
-                editProfile(view, item);
-            }
+        private final View.OnClickListener mOnClickListener = view -> {
+            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()
@@ -175,8 +176,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);
             }
@@ -187,13 +188,27 @@ 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(new Observer() {
-                @Override
-                public void update(Observable o, Object arg) {
-                    MobileLedgerProfile newProfile = Data.profile.get();
-                    MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
-                    holder.mRadioView.setChecked(
-                            newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
+
+            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) {
+                    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;
@@ -203,26 +218,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) ? "<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(new CompoundButton.OnCheckedChangeListener() {
-                        @Override
-                        public void onCheckedChanged(CompoundButton buttonView, boolean 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() {