]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java
multiple profile list fixes
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailActivity.java
index 591d147a777b037a919a9f6d2a9d359c2a04d18e..1db1429acfd78b2c9c398008c4c0518b7f3d3af9 100644 (file)
@@ -46,10 +46,10 @@ public class ProfileDetailActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_profile_detail);
-        Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
+        Toolbar toolbar = findViewById(R.id.detail_toolbar);
         setSupportActionBar(toolbar);
 
-        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
+        FloatingActionButton fab = findViewById(R.id.fab);
         fab.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
@@ -74,28 +74,19 @@ public class ProfileDetailActivity extends AppCompatActivity {
         // http://developer.android.com/guide/components/fragments.html
         //
         if (savedInstanceState == null) {
-            final String profileUUID =
-                    getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID);
+            final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
 
-            if (profileUUID != null) {
-                int i = 0;
-                for (MobileLedgerProfile p : Data.profiles.getList()) {
-                    if (p.getUuid().equals(profileUUID)) {
-                        Log.d("profiles", String.format("found profile %s at %d", profileUUID, i));
-                        profile = p;
-                        break;
-                    }
-                    i++;
-                }
+            if (index != -1) {
+                profile = Data.profiles.get(index);
                 if (profile == null) throw new AssertionError(
-                        String.format("Can't get profile " + "(uuid:%s) from the " + "global list",
-                                profileUUID));
+                        String.format("Can't get profile " + "(index:%d) from the global list",
+                                index));
             }
 
             // Create the detail fragment and add it to the activity
             // using a fragment transaction.
             Bundle arguments = new Bundle();
-            arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, profileUUID);
+            arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
             ProfileDetailFragment fragment = new ProfileDetailFragment();
             fragment.setArguments(arguments);
             getSupportFragmentManager().beginTransaction()
@@ -107,15 +98,21 @@ public class ProfileDetailActivity extends AppCompatActivity {
         super.onCreateOptionsMenu(menu);
         Log.d("profiles", "[activity] Creating profile details options menu");
         getMenuInflater().inflate(R.menu.profile_details, menu);
-        menu.findItem(R.id.menuDelete).setOnMenuItemClickListener(item -> {
-            Log.d("profiles", String.format("deleting profile %s", profile.getUuid()));
+        MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
+        menuDeleteProfile.setOnMenuItemClickListener(item -> {
+            Log.d("profiles", String.format("[activity] deleting profile %s", profile.getUuid()));
             profile.removeFromDB();
             Data.profiles.remove(profile);
-            Data.profile.set(Data.profiles.get(0));
+            if (Data.profile.get().equals(profile)) {
+                Log.d("profiles", "[activity] selecting profile 0");
+                Data.setCurrentProfile(Data.profiles.get(0));
+            }
             finish();
             return true;
         });
 
+        menuDeleteProfile.setVisible((profile != null) && (Data.profiles.size() > 1));
+
         return true;
     }