]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileDetailActivity.java
ccdad2c7115504c1ce1b32cfb5c63818ee7aaee1
[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.content.Intent;
21 import android.os.Bundle;
22 import android.support.design.widget.FloatingActionButton;
23 import android.support.design.widget.Snackbar;
24 import android.support.v7.app.ActionBar;
25 import android.support.v7.widget.Toolbar;
26 import android.util.Log;
27 import android.view.Menu;
28 import android.view.MenuItem;
29 import android.view.View;
30
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.MobileLedgerProfile;
34 import net.ktnx.mobileledger.ui.activity.CrashReportingActivity;
35 import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
36 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
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 {@link ProfileListActivity}.
43  */
44 public class ProfileDetailActivity extends CrashReportingActivity {
45     private MobileLedgerProfile profile;
46     @Override
47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.activity_profile_detail);
50         Toolbar toolbar = findViewById(R.id.detail_toolbar);
51         setSupportActionBar(toolbar);
52
53         FloatingActionButton fab = findViewById(R.id.fab);
54         fab.setOnClickListener(new View.OnClickListener() {
55             @Override
56             public void onClick(View view) {
57                 Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
58                         .setAction("Action", null).show();
59             }
60         });
61
62         // Show the Up button in the action bar.
63         ActionBar actionBar = getSupportActionBar();
64         if (actionBar != null) {
65             actionBar.setDisplayHomeAsUpEnabled(true);
66         }
67
68         // savedInstanceState is non-null when there is fragment state
69         // saved from previous configurations of this activity
70         // (e.g. when rotating the screen from portrait to landscape).
71         // In this case, the fragment will automatically be re-added
72         // to its container so we don't need to manually add it.
73         // For more information, see the Fragments API guide at:
74         //
75         // http://developer.android.com/guide/components/fragments.html
76         //
77         if (savedInstanceState == null) {
78             final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
79
80             if (index != -1) {
81                 profile = Data.profiles.get(index);
82                 if (profile == null) throw new AssertionError(
83                         String.format("Can't get profile " + "(index:%d) from the global list",
84                                 index));
85             }
86
87             // Create the detail fragment and add it to the activity
88             // using a fragment transaction.
89             Bundle arguments = new Bundle();
90             arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
91             ProfileDetailFragment fragment = new ProfileDetailFragment();
92             fragment.setArguments(arguments);
93             getSupportFragmentManager().beginTransaction()
94                     .add(R.id.profile_detail_container, fragment).commit();
95         }
96     }
97     @Override
98     public boolean onCreateOptionsMenu(Menu menu) {
99         super.onCreateOptionsMenu(menu);
100         Log.d("profiles", "[activity] Creating profile details options menu");
101         getMenuInflater().inflate(R.menu.profile_details, menu);
102         MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
103         menuDeleteProfile.setOnMenuItemClickListener(item -> {
104             Log.d("profiles", String.format("[activity] deleting profile %s", profile.getUuid()));
105             profile.removeFromDB();
106             Data.profiles.remove(profile);
107             if (Data.profile.get().equals(profile)) {
108                 Log.d("profiles", "[activity] selecting profile 0");
109                 Data.setCurrentProfile(Data.profiles.get(0));
110             }
111             finish();
112             return true;
113         });
114
115         menuDeleteProfile.setVisible((profile != null) && (Data.profiles.size() > 1));
116
117         return true;
118     }
119
120     @Override
121     public boolean onOptionsItemSelected(MenuItem item) {
122         int id = item.getItemId();
123         if (id == android.R.id.home) {
124             // This ID represents the Home or Up button. In the case of this
125             // activity, the Up button is shown. For
126             // more details, see the Navigation pattern on Android Design:
127             //
128             // http://developer.android.com/design/patterns/navigation.html#up-vs-back
129             //
130             navigateUpTo(new Intent(this, ProfileListActivity.class));
131             return true;
132         }
133         return super.onOptionsItemSelected(item);
134     }
135 }