]> git.ktnx.net Git - mobile-ledger.git/blob - 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
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.os.Bundle;
21 import android.view.Menu;
22 import android.view.MenuItem;
23
24 import androidx.appcompat.app.ActionBar;
25 import androidx.appcompat.widget.Toolbar;
26 import androidx.lifecycle.ViewModelProvider;
27
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.Data;
30 import net.ktnx.mobileledger.model.MobileLedgerProfile;
31 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
32 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
33 import net.ktnx.mobileledger.utils.Colors;
34
35 import org.jetbrains.annotations.NotNull;
36
37 import java.util.ArrayList;
38 import java.util.Locale;
39
40 import static net.ktnx.mobileledger.utils.Logger.debug;
41
42 /**
43  * An activity representing a single Profile detail screen. This
44  * activity is only used on narrow width devices. On tablet-size devices,
45  * item details are presented side-by-side with a list of items
46  * in a ProfileListActivity (not really).
47  */
48 public class ProfileDetailActivity extends CrashReportingActivity {
49     private MobileLedgerProfile profile = null;
50     private ProfileDetailFragment mFragment;
51     @NotNull
52     private ProfileDetailModel getModel() {
53         return new ViewModelProvider(this).get(ProfileDetailModel.class);
54     }
55     @Override
56     protected void onCreate(Bundle savedInstanceState) {
57         final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
58
59         if (index != -1) {
60             ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
61             if (profiles != null) {
62                 profile = profiles.get(index);
63                 if (profile == null)
64                     throw new AssertionError(
65                             String.format("Can't get profile " + "(index:%d) from the global list",
66                                     index));
67
68                 debug("profiles", String.format(Locale.ENGLISH, "Editing profile %s (%s); hue=%d",
69                         profile.getName(), profile.getUuid(), profile.getThemeHue()));
70             }
71         }
72
73         super.onCreate(savedInstanceState);
74         int themeHue;
75         if (profile != null)
76             themeHue = profile.getThemeHue();
77         else {
78             themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
79         }
80         Colors.setupTheme(this, themeHue);
81         final ProfileDetailModel model = getModel();
82         model.initialThemeHue = themeHue;
83         setContentView(R.layout.activity_profile_detail);
84         Toolbar toolbar = findViewById(R.id.detail_toolbar);
85         setSupportActionBar(toolbar);
86
87
88         // Show the Up button in the action bar.
89         ActionBar actionBar = getSupportActionBar();
90         if (actionBar != null) {
91             actionBar.setDisplayHomeAsUpEnabled(true);
92         }
93
94         // savedInstanceState is non-null when there is fragment state
95         // saved from previous configurations of this activity
96         // (e.g. when rotating the screen from portrait to landscape).
97         // In this case, the fragment will automatically be re-added
98         // to its container so we don't need to manually add it.
99         // For more information, see the Fragments API guide at:
100         //
101         // http://developer.android.com/guide/components/fragments.html
102         //
103         if (savedInstanceState == null) {
104             // Create the detail fragment and add it to the activity
105             // using a fragment transaction.
106             Bundle arguments = new Bundle();
107             arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
108             arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue);
109             mFragment = new ProfileDetailFragment();
110             mFragment.setArguments(arguments);
111             getSupportFragmentManager().beginTransaction()
112                                        .add(R.id.profile_detail_container, mFragment)
113                                        .commit();
114         }
115     }
116     @Override
117     public boolean onCreateOptionsMenu(Menu menu) {
118         super.onCreateOptionsMenu(menu);
119         debug("profiles", "[activity] Creating profile details options menu");
120         if (mFragment != null)
121             mFragment.onCreateOptionsMenu(menu, getMenuInflater());
122
123         return true;
124     }
125     @Override
126     public boolean onOptionsItemSelected(MenuItem item) {
127         if (item.getItemId() == android.R.id.home) {
128             finish();
129             return true;
130         }
131         return super.onOptionsItemSelected(item);
132     }
133 }