]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileDetailActivity.java
proile editor: fix passing of initial theme hue to the hue ring
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / ProfileDetailActivity.java
index a5c973b096dff0c0fce6e2a664d0e7346e996200..1bc17ec4278f2e335284524e88b522855c867a22 100644 (file)
 package net.ktnx.mobileledger.ui.activity;
 
 import android.os.Bundle;
-import android.util.Log;
 import android.view.Menu;
+import android.view.MenuItem;
+
+import androidx.appcompat.app.ActionBar;
+import androidx.appcompat.widget.Toolbar;
+import androidx.lifecycle.ViewModelProvider;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
 import net.ktnx.mobileledger.utils.Colors;
 
-import androidx.appcompat.app.ActionBar;
-import androidx.appcompat.widget.Toolbar;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.Locale;
+
+import static net.ktnx.mobileledger.utils.Logger.debug;
 
 /**
  * An activity representing a single Profile detail screen. This
@@ -39,25 +48,43 @@ import androidx.appcompat.widget.Toolbar;
 public class ProfileDetailActivity extends CrashReportingActivity {
     private MobileLedgerProfile profile = null;
     private ProfileDetailFragment mFragment;
+    @NotNull
+    private ProfileDetailModel getModel() {
+        return new ViewModelProvider(this).get(ProfileDetailModel.class);
+    }
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
 
         if (index != -1) {
-            profile = Data.profiles.get(index);
-            if (profile == null) throw new AssertionError(
-                    String.format("Can't get profile " + "(index:%d) from the global list", index));
+            ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+            if (profiles != null) {
+                profile = profiles.get(index);
+                if (profile == null)
+                    throw new AssertionError(
+                            String.format("Can't get profile " + "(index:%d) from the global list",
+                                    index));
 
-            Log.d("profiles", String.format("Editing profile %s (%s); hue=%d", profile.getName(),
-                    profile.getUuid(), profile.getThemeId()));
+                debug("profiles", String.format(Locale.ENGLISH, "Editing profile %s (%s); hue=%d",
+                        profile.getName(), profile.getUuid(), profile.getThemeHue()));
+            }
         }
 
         super.onCreate(savedInstanceState);
-        Colors.setupTheme(this, profile);
+        int themeHue;
+        if (profile != null)
+            themeHue = profile.getThemeHue();
+        else {
+            themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
+        }
+        Colors.setupTheme(this, themeHue);
+        final ProfileDetailModel model = getModel();
+        model.initialThemeHue = themeHue;
         setContentView(R.layout.activity_profile_detail);
         Toolbar toolbar = findViewById(R.id.detail_toolbar);
         setSupportActionBar(toolbar);
 
+
         // Show the Up button in the action bar.
         ActionBar actionBar = getSupportActionBar();
         if (actionBar != null) {
@@ -78,19 +105,29 @@ public class ProfileDetailActivity extends CrashReportingActivity {
             // using a fragment transaction.
             Bundle arguments = new Bundle();
             arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
+            arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue);
             mFragment = new ProfileDetailFragment();
             mFragment.setArguments(arguments);
             getSupportFragmentManager().beginTransaction()
-                    .add(R.id.profile_detail_container, mFragment).commit();
+                                       .add(R.id.profile_detail_container, mFragment)
+                                       .commit();
         }
     }
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         super.onCreateOptionsMenu(menu);
-        Log.d("profiles", "[activity] Creating profile details options menu");
-        if (mFragment != null) mFragment.onCreateOptionsMenu(menu, getMenuInflater());
+        debug("profiles", "[activity] Creating profile details options menu");
+        if (mFragment != null)
+            mFragment.onCreateOptionsMenu(menu, getMenuInflater());
 
         return true;
     }
-
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        if (item.getItemId() == android.R.id.home) {
+            finish();
+            return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
 }