]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java
fix creation of a new profile after Room-y of Profile adoption
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailActivity.java
index 09099732f89f7325ef3dd61f0d87f9e637224a32..1b88f480254b5129bbd921ba8efdd65395dc0634 100644 (file)
@@ -1,58 +1,89 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * Copyright © 2021 Damyan Ivanov.
+ * 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 <https://www.gnu.org/licenses/>.
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 package net.ktnx.mobileledger.ui.profiles;
 
+import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
-import android.support.design.widget.FloatingActionButton;
-import android.support.design.widget.Snackbar;
-import android.support.v7.widget.Toolbar;
-import android.view.View;
-import android.support.v7.app.AppCompatActivity;
-import android.support.v7.app.ActionBar;
+import android.view.Menu;
 import android.view.MenuItem;
 
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.ActionBar;
+import androidx.appcompat.widget.Toolbar;
+import androidx.lifecycle.ViewModelProvider;
+
+import com.google.android.material.appbar.CollapsingToolbarLayout;
+
 import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
+import net.ktnx.mobileledger.db.DB;
+import net.ktnx.mobileledger.db.Profile;
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
+import net.ktnx.mobileledger.utils.Colors;
+
+import org.jetbrains.annotations.NotNull;
+
+import static net.ktnx.mobileledger.utils.Logger.debug;
 
 /**
  * An activity representing a single Profile detail screen. This
  * activity is only used on narrow width devices. On tablet-size devices,
  * item details are presented side-by-side with a list of items
- * in a {@link ProfileListActivity}.
+ * in a ProfileListActivity (not really).
  */
-public class ProfileDetailActivity extends AppCompatActivity {
-
+public class ProfileDetailActivity extends CrashReportingActivity {
+    private ProfileDetailFragment mFragment;
+    public static void start(Context context, @Nullable Profile profile) {
+        Intent starter = new Intent(context, ProfileDetailActivity.class);
+        if (profile != null) {
+            starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getId());
+            starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getTheme());
+        }
+        context.startActivity(starter);
+    }
+    @NotNull
+    private ProfileDetailModel getModel() {
+        return new ViewModelProvider(this).get(ProfileDetailModel.class);
+    }
     @Override
     protected void onCreate(Bundle savedInstanceState) {
+        final long id = getIntent().getLongExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
+
+        DB.get()
+          .getProfileDAO()
+          .getById(id)
+          .observe(this, this::setProfile);
+
+        int themeHue = getIntent().getIntExtra(ProfileDetailFragment.ARG_HUE, -1);
+
         super.onCreate(savedInstanceState);
+        if (themeHue == -1) {
+            themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
+        }
+        Colors.setupTheme(this, themeHue);
+        final ProfileDetailModel model = getModel();
+        model.initialThemeHue = themeHue;
+        model.setThemeId(themeHue);
         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);
-        fab.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
-                        .setAction("Action", null).show();
-            }
-        });
 
         // Show the Up button in the action bar.
         ActionBar actionBar = getSupportActionBar();
@@ -73,26 +104,38 @@ public class ProfileDetailActivity extends AppCompatActivity {
             // Create the detail fragment and add it to the activity
             // using a fragment transaction.
             Bundle arguments = new Bundle();
-            arguments.putString(ProfileDetailFragment.ARG_ITEM_ID,
-                    getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID));
-            ProfileDetailFragment fragment = new ProfileDetailFragment();
-            fragment.setArguments(arguments);
+            arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue);
+            mFragment = new ProfileDetailFragment();
+            mFragment.setArguments(arguments);
             getSupportFragmentManager().beginTransaction()
-                    .add(R.id.profile_detail_container, fragment).commit();
+                                       .add(R.id.profile_detail_container, mFragment)
+                                       .commit();
         }
     }
+    private void setProfile(Profile profile) {
+        ProfileDetailModel model = new ViewModelProvider(this).get(ProfileDetailModel.class);
+        CollapsingToolbarLayout appBarLayout = findViewById(R.id.toolbar_layout);
+        if (appBarLayout != null) {
+            if (profile != null)
+                appBarLayout.setTitle(profile.getName());
+            else
+                appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
+        }
+        model.setValuesFromProfile(profile);
+    }
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        super.onCreateOptionsMenu(menu);
+        debug("profiles", "[activity] Creating profile details options menu");
+        if (mFragment != null)
+            mFragment.onCreateOptionsMenu(menu, getMenuInflater());
 
+        return true;
+    }
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
-        int id = item.getItemId();
-        if (id == android.R.id.home) {
-            // This ID represents the Home or Up button. In the case of this
-            // activity, the Up button is shown. For
-            // more details, see the Navigation pattern on Android Design:
-            //
-            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
-            //
-            navigateUpTo(new Intent(this, ProfileListActivity.class));
+        if (item.getItemId() == android.R.id.home) {
+            finish();
             return true;
         }
         return super.onOptionsItemSelected(item);