]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java
shuffle some classes under proper packages
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailActivity.java
1 /*
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.
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.profiles;
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.activity.CrashReportingActivity;
32 import net.ktnx.mobileledger.utils.Colors;
33
34 import org.jetbrains.annotations.NotNull;
35
36 import java.util.ArrayList;
37 import java.util.Locale;
38
39 import static net.ktnx.mobileledger.utils.Logger.debug;
40
41 /**
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).
46  */
47 public class ProfileDetailActivity extends CrashReportingActivity {
48     private MobileLedgerProfile profile = null;
49     private ProfileDetailFragment mFragment;
50     @NotNull
51     private ProfileDetailModel getModel() {
52         return new ViewModelProvider(this).get(ProfileDetailModel.class);
53     }
54     @Override
55     protected void onCreate(Bundle savedInstanceState) {
56         final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
57
58         if (index != -1) {
59             ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
60             if (profiles != null) {
61                 profile = profiles.get(index);
62                 if (profile == null)
63                     throw new AssertionError(
64                             String.format("Can't get profile " + "(index:%d) from the global list",
65                                     index));
66
67                 debug("profiles", String.format(Locale.ENGLISH, "Editing profile %s (%s); hue=%d",
68                         profile.getName(), profile.getUuid(), profile.getThemeHue()));
69             }
70         }
71
72         super.onCreate(savedInstanceState);
73         int themeHue;
74         if (profile != null)
75             themeHue = profile.getThemeHue();
76         else {
77             themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
78         }
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);
85
86
87         // Show the Up button in the action bar.
88         ActionBar actionBar = getSupportActionBar();
89         if (actionBar != null) {
90             actionBar.setDisplayHomeAsUpEnabled(true);
91         }
92
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:
99         //
100         // http://developer.android.com/guide/components/fragments.html
101         //
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)
112                                        .commit();
113         }
114     }
115     @Override
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());
121
122         return true;
123     }
124     @Override
125     public boolean onOptionsItemSelected(MenuItem item) {
126         if (item.getItemId() == android.R.id.home) {
127             finish();
128             return true;
129         }
130         return super.onOptionsItemSelected(item);
131     }
132 }