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