]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileDetailActivity.java
7daa3d315cbfbd479970efb26fcbc3121c285c61
[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
27 import net.ktnx.mobileledger.R;
28 import net.ktnx.mobileledger.model.Data;
29 import net.ktnx.mobileledger.model.MobileLedgerProfile;
30 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
31 import net.ktnx.mobileledger.utils.Colors;
32
33 import java.util.ArrayList;
34 import java.util.Locale;
35
36 import static net.ktnx.mobileledger.utils.Logger.debug;
37
38 /**
39  * An activity representing a single Profile detail screen. This
40  * activity is only used on narrow width devices. On tablet-size devices,
41  * item details are presented side-by-side with a list of items
42  * in a ProfileListActivity (not really).
43  */
44 public class ProfileDetailActivity extends CrashReportingActivity {
45     private MobileLedgerProfile profile = null;
46     private ProfileDetailFragment mFragment;
47     @Override
48     protected void onCreate(Bundle savedInstanceState) {
49         final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
50
51         if (index != -1) {
52             ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
53             if (profiles != null) {
54                 profile = profiles.get(index);
55                 if (profile == null)
56                     throw new AssertionError(
57                             String.format("Can't get profile " + "(index:%d) from the global list",
58                                     index));
59
60                 debug("profiles", String.format(Locale.ENGLISH, "Editing profile %s (%s); hue=%d",
61                         profile.getName(), profile.getUuid(), profile.getThemeHue()));
62             }
63         }
64
65         super.onCreate(savedInstanceState);
66         int themeHue;
67         if (profile != null)
68             themeHue = profile.getThemeHue();
69         else {
70             themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
71         }
72         Colors.setupTheme(this, themeHue);
73         setContentView(R.layout.activity_profile_detail);
74         Toolbar toolbar = findViewById(R.id.detail_toolbar);
75         setSupportActionBar(toolbar);
76
77         // Show the Up button in the action bar.
78         ActionBar actionBar = getSupportActionBar();
79         if (actionBar != null) {
80             actionBar.setDisplayHomeAsUpEnabled(true);
81         }
82
83         // savedInstanceState is non-null when there is fragment state
84         // saved from previous configurations of this activity
85         // (e.g. when rotating the screen from portrait to landscape).
86         // In this case, the fragment will automatically be re-added
87         // to its container so we don't need to manually add it.
88         // For more information, see the Fragments API guide at:
89         //
90         // http://developer.android.com/guide/components/fragments.html
91         //
92         if (savedInstanceState == null) {
93             // Create the detail fragment and add it to the activity
94             // using a fragment transaction.
95             Bundle arguments = new Bundle();
96             arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
97             arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue);
98             mFragment = new ProfileDetailFragment();
99             mFragment.setArguments(arguments);
100             getSupportFragmentManager().beginTransaction()
101                                        .add(R.id.profile_detail_container, mFragment)
102                                        .commit();
103         }
104     }
105     @Override
106     public boolean onCreateOptionsMenu(Menu menu) {
107         super.onCreateOptionsMenu(menu);
108         debug("profiles", "[activity] Creating profile details options menu");
109         if (mFragment != null)
110             mFragment.onCreateOptionsMenu(menu, getMenuInflater());
111
112         return true;
113     }
114     @Override
115     public boolean onOptionsItemSelected(MenuItem item) {
116         if (item.getItemId() == android.R.id.home) {
117             finish();
118             return true;
119         }
120         return super.onOptionsItemSelected(item);
121     }
122 }