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.
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.activity;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.View;
26 import com.google.android.material.floatingactionbutton.FloatingActionButton;
27 import com.google.android.material.snackbar.Snackbar;
29 import net.ktnx.mobileledger.R;
30 import net.ktnx.mobileledger.model.Data;
31 import net.ktnx.mobileledger.model.MobileLedgerProfile;
32 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
33 import net.ktnx.mobileledger.utils.Colors;
35 import androidx.appcompat.app.ActionBar;
36 import androidx.appcompat.widget.Toolbar;
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 {@link ProfileListActivity}.
44 public class ProfileDetailActivity extends CrashReportingActivity {
45 private MobileLedgerProfile profile = null;
47 protected void onCreate(Bundle savedInstanceState) {
48 final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
51 profile = Data.profiles.get(index);
52 if (profile == null) throw new AssertionError(
53 String.format("Can't get profile " + "(index:%d) from the global list", index));
55 Log.d("profiles", String.format("Editing profile %s (%s); hue=%d", profile.getName(),
56 profile.getUuid(), profile.getThemeId()));
59 super.onCreate(savedInstanceState);
60 Colors.setupTheme(this, profile);
61 setContentView(R.layout.activity_profile_detail);
62 Toolbar toolbar = findViewById(R.id.detail_toolbar);
63 setSupportActionBar(toolbar);
65 FloatingActionButton fab = findViewById(R.id.fab);
66 fab.setOnClickListener(new View.OnClickListener() {
68 public void onClick(View view) {
69 Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
70 .setAction("Action", null).show();
74 // Show the Up button in the action bar.
75 ActionBar actionBar = getSupportActionBar();
76 if (actionBar != null) {
77 actionBar.setDisplayHomeAsUpEnabled(true);
80 // savedInstanceState is non-null when there is fragment state
81 // saved from previous configurations of this activity
82 // (e.g. when rotating the screen from portrait to landscape).
83 // In this case, the fragment will automatically be re-added
84 // to its container so we don't need to manually add it.
85 // For more information, see the Fragments API guide at:
87 // http://developer.android.com/guide/components/fragments.html
89 if (savedInstanceState == null) {
90 // Create the detail fragment and add it to the activity
91 // using a fragment transaction.
92 Bundle arguments = new Bundle();
93 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
94 ProfileDetailFragment fragment = new ProfileDetailFragment();
95 fragment.setArguments(arguments);
96 getSupportFragmentManager().beginTransaction()
97 .add(R.id.profile_detail_container, fragment).commit();
101 public boolean onCreateOptionsMenu(Menu menu) {
102 super.onCreateOptionsMenu(menu);
103 Log.d("profiles", "[activity] Creating profile details options menu");
104 getMenuInflater().inflate(R.menu.profile_details, menu);
105 MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
106 menuDeleteProfile.setOnMenuItemClickListener(item -> {
107 Log.d("profiles", String.format("[activity] deleting profile %s", profile.getUuid()));
108 profile.removeFromDB();
109 Data.profiles.remove(profile);
110 if (Data.profile.get().equals(profile)) {
111 Log.d("profiles", "[activity] selecting profile 0");
112 Data.setCurrentProfile(Data.profiles.get(0));
118 menuDeleteProfile.setVisible((profile != null) && (Data.profiles.size() > 1));