2 * Copyright © 2021 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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.profiles;
20 import android.os.Bundle;
21 import android.view.Menu;
22 import android.view.MenuItem;
24 import androidx.appcompat.app.ActionBar;
25 import androidx.appcompat.widget.Toolbar;
26 import androidx.lifecycle.ViewModelProvider;
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.activity.CrashReportingActivity;
32 import net.ktnx.mobileledger.utils.Colors;
34 import org.jetbrains.annotations.NotNull;
36 import java.util.ArrayList;
37 import java.util.Locale;
39 import static net.ktnx.mobileledger.utils.Logger.debug;
42 * An activity representing a single Profile detail screen. This
43 * activity is only used on narrow width devices. On tablet-size devices,
44 * item details are presented side-by-side with a list of items
45 * in a ProfileListActivity (not really).
47 public class ProfileDetailActivity extends CrashReportingActivity {
48 private MobileLedgerProfile profile = null;
49 private ProfileDetailFragment mFragment;
51 private ProfileDetailModel getModel() {
52 return new ViewModelProvider(this).get(ProfileDetailModel.class);
55 protected void onCreate(Bundle savedInstanceState) {
56 final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
59 ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
60 if (profiles != null) {
61 profile = profiles.get(index);
63 throw new AssertionError(
64 String.format("Can't get profile " + "(index:%d) from the global list",
67 debug("profiles", String.format(Locale.ENGLISH, "Editing profile %s (%s); hue=%d",
68 profile.getName(), profile.getId(), profile.getThemeHue()));
72 super.onCreate(savedInstanceState);
75 themeHue = profile.getThemeHue();
77 themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
79 Colors.setupTheme(this, themeHue);
80 final ProfileDetailModel model = getModel();
81 model.initialThemeHue = themeHue;
82 setContentView(R.layout.activity_profile_detail);
83 Toolbar toolbar = findViewById(R.id.detail_toolbar);
84 setSupportActionBar(toolbar);
87 // Show the Up button in the action bar.
88 ActionBar actionBar = getSupportActionBar();
89 if (actionBar != null) {
90 actionBar.setDisplayHomeAsUpEnabled(true);
93 // savedInstanceState is non-null when there is fragment state
94 // saved from previous configurations of this activity
95 // (e.g. when rotating the screen from portrait to landscape).
96 // In this case, the fragment will automatically be re-added
97 // to its container so we don't need to manually add it.
98 // For more information, see the Fragments API guide at:
100 // http://developer.android.com/guide/components/fragments.html
102 if (savedInstanceState == null) {
103 // Create the detail fragment and add it to the activity
104 // using a fragment transaction.
105 Bundle arguments = new Bundle();
106 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
107 arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue);
108 mFragment = new ProfileDetailFragment();
109 mFragment.setArguments(arguments);
110 getSupportFragmentManager().beginTransaction()
111 .add(R.id.profile_detail_container, mFragment)
116 public boolean onCreateOptionsMenu(Menu menu) {
117 super.onCreateOptionsMenu(menu);
118 debug("profiles", "[activity] Creating profile details options menu");
119 if (mFragment != null)
120 mFragment.onCreateOptionsMenu(menu, getMenuInflater());
125 public boolean onOptionsItemSelected(MenuItem item) {
126 if (item.getItemId() == android.R.id.home) {
130 return super.onOptionsItemSelected(item);