]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java
more pronounced day/month delimiters in the transaction list
[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.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.view.Menu;
24 import android.view.MenuItem;
25
26 import androidx.annotation.Nullable;
27 import androidx.appcompat.app.ActionBar;
28 import androidx.appcompat.widget.Toolbar;
29 import androidx.lifecycle.ViewModelProvider;
30
31 import com.google.android.material.appbar.CollapsingToolbarLayout;
32
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;
39 import net.ktnx.mobileledger.utils.Logger;
40
41 import org.jetbrains.annotations.NotNull;
42
43 import java.util.Locale;
44
45 import static net.ktnx.mobileledger.utils.Logger.debug;
46
47 /**
48  * An activity representing a single Profile detail screen. This
49  * activity is only used on narrow width devices. On tablet-size devices,
50  * item details are presented side-by-side with a list of items
51  * in a ProfileListActivity (not really).
52  */
53 public class ProfileDetailActivity extends CrashReportingActivity {
54     private static final String TAG = "profile-det-act";
55     private ProfileDetailFragment mFragment;
56     public static void start(Context context, @Nullable Profile profile) {
57         Intent starter = new Intent(context, ProfileDetailActivity.class);
58         if (profile != null) {
59             starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getId());
60             starter.putExtra(ProfileDetailFragment.ARG_HUE, profile.getTheme());
61             Logger.debug(TAG,
62                     String.format(Locale.ROOT, "Starting profile editor for profile %d, theme %d",
63                             profile.getId(), profile.getTheme()));
64         }
65         else
66             Logger.debug(TAG, "Starting empty profile editor");
67         context.startActivity(starter);
68     }
69     @NotNull
70     private ProfileDetailModel getModel() {
71         return new ViewModelProvider(this).get(ProfileDetailModel.class);
72     }
73     @Override
74     protected void onCreate(Bundle savedInstanceState) {
75         final long id = getIntent().getLongExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
76
77         DB.get()
78           .getProfileDAO()
79           .getById(id)
80           .observe(this, this::setProfile);
81
82         int themeHue = getIntent().getIntExtra(ProfileDetailFragment.ARG_HUE, -1);
83
84         super.onCreate(savedInstanceState);
85         if (themeHue == -1) {
86             themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue());
87         }
88         Colors.setupTheme(this, themeHue);
89         final ProfileDetailModel model = getModel();
90         model.initialThemeHue = themeHue;
91         model.setThemeId(themeHue);
92         setContentView(R.layout.activity_profile_detail);
93         Toolbar toolbar = findViewById(R.id.detail_toolbar);
94         setSupportActionBar(toolbar);
95
96
97         // Show the Up button in the action bar.
98         ActionBar actionBar = getSupportActionBar();
99         if (actionBar != null) {
100             actionBar.setDisplayHomeAsUpEnabled(true);
101         }
102
103         // savedInstanceState is non-null when there is fragment state
104         // saved from previous configurations of this activity
105         // (e.g. when rotating the screen from portrait to landscape).
106         // In this case, the fragment will automatically be re-added
107         // to its container so we don't need to manually add it.
108         // For more information, see the Fragments API guide at:
109         //
110         // http://developer.android.com/guide/components/fragments.html
111         //
112         if (savedInstanceState == null) {
113             // Create the detail fragment and add it to the activity
114             // using a fragment transaction.
115             Bundle arguments = new Bundle();
116             arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue);
117             mFragment = new ProfileDetailFragment();
118             mFragment.setArguments(arguments);
119             getSupportFragmentManager().beginTransaction()
120                                        .add(R.id.profile_detail_container, mFragment)
121                                        .commit();
122         }
123     }
124     private void setProfile(Profile profile) {
125         ProfileDetailModel model = new ViewModelProvider(this).get(ProfileDetailModel.class);
126         CollapsingToolbarLayout appBarLayout = findViewById(R.id.toolbar_layout);
127         if (appBarLayout != null) {
128             if (profile != null)
129                 appBarLayout.setTitle(profile.getName());
130             else
131                 appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
132         }
133         model.setValuesFromProfile(profile);
134     }
135     @Override
136     public boolean onCreateOptionsMenu(Menu menu) {
137         super.onCreateOptionsMenu(menu);
138         debug("profiles", "[activity] Creating profile details options menu");
139         if (mFragment != null)
140             mFragment.onCreateOptionsMenu(menu, getMenuInflater());
141
142         return true;
143     }
144     @Override
145     public boolean onOptionsItemSelected(MenuItem item) {
146         if (item.getItemId() == android.R.id.home) {
147             finish();
148             return true;
149         }
150         return super.onOptionsItemSelected(item);
151     }
152 }