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.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.view.Menu;
24 import android.view.MenuItem;
26 import androidx.annotation.Nullable;
27 import androidx.appcompat.app.ActionBar;
28 import androidx.appcompat.widget.Toolbar;
29 import androidx.lifecycle.ViewModelProvider;
31 import com.google.android.material.appbar.CollapsingToolbarLayout;
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.db.DB;
35 import net.ktnx.mobileledger.db.Profile;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
38 import net.ktnx.mobileledger.utils.Colors;
40 import org.jetbrains.annotations.NotNull;
42 import static net.ktnx.mobileledger.utils.Logger.debug;
45 * An activity representing a single Profile detail screen. This
46 * activity is only used on narrow width devices. On tablet-size devices,
47 * item details are presented side-by-side with a list of items
48 * in a ProfileListActivity (not really).
50 public class ProfileDetailActivity extends CrashReportingActivity {
51 private ProfileDetailFragment mFragment;
52 public static void start(Context context, @Nullable Profile profile) {
53 Intent starter = new Intent(context, ProfileDetailActivity.class);
54 if (profile != null) {
55 starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getId());
56 starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getTheme());
58 context.startActivity(starter);
61 private ProfileDetailModel getModel() {
62 return new ViewModelProvider(this).get(ProfileDetailModel.class);
65 protected void onCreate(Bundle savedInstanceState) {
66 final long id = getIntent().getLongExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
71 .observe(this, this::setProfile);
73 int themeHue = getIntent().getIntExtra(ProfileDetailFragment.ARG_HUE, -1);
75 super.onCreate(savedInstanceState);
77 themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
79 Colors.setupTheme(this, themeHue);
80 final ProfileDetailModel model = getModel();
81 model.initialThemeHue = themeHue;
82 model.setThemeId(themeHue);
83 setContentView(R.layout.activity_profile_detail);
84 Toolbar toolbar = findViewById(R.id.detail_toolbar);
85 setSupportActionBar(toolbar);
88 // Show the Up button in the action bar.
89 ActionBar actionBar = getSupportActionBar();
90 if (actionBar != null) {
91 actionBar.setDisplayHomeAsUpEnabled(true);
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:
101 // http://developer.android.com/guide/components/fragments.html
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_HUE, themeHue);
108 mFragment = new ProfileDetailFragment();
109 mFragment.setArguments(arguments);
110 getSupportFragmentManager().beginTransaction()
111 .add(R.id.profile_detail_container, mFragment)
115 private void setProfile(Profile profile) {
116 ProfileDetailModel model = new ViewModelProvider(this).get(ProfileDetailModel.class);
117 CollapsingToolbarLayout appBarLayout = findViewById(R.id.toolbar_layout);
118 if (appBarLayout != null) {
120 appBarLayout.setTitle(profile.getName());
122 appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
124 model.setValuesFromProfile(profile);
127 public boolean onCreateOptionsMenu(Menu menu) {
128 super.onCreateOptionsMenu(menu);
129 debug("profiles", "[activity] Creating profile details options menu");
130 if (mFragment != null)
131 mFragment.onCreateOptionsMenu(menu, getMenuInflater());
136 public boolean onOptionsItemSelected(MenuItem item) {
137 if (item.getItemId() == android.R.id.home) {
141 return super.onOptionsItemSelected(item);