]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
f211139375b9cd21847e3d2c0e7e111ecd7af18b
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailFragment.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.app.Activity;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.support.annotation.NonNull;
24 import android.support.design.widget.CollapsingToolbarLayout;
25 import android.support.design.widget.FloatingActionButton;
26 import android.support.v4.app.Fragment;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.Menu;
30 import android.view.MenuInflater;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.LinearLayout;
35 import android.widget.Switch;
36 import android.widget.TextView;
37
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
42
43 /**
44  * A fragment representing a single Profile detail screen.
45  * This fragment is either contained in a {@link ProfileListActivity}
46  * in two-pane mode (on tablets) or a {@link ProfileDetailActivity}
47  * on handsets.
48  */
49 public class ProfileDetailFragment extends Fragment {
50     /**
51      * The fragment argument representing the item ID that this fragment
52      * represents.
53      */
54     public static final String ARG_ITEM_ID = "item_id";
55
56     /**
57      * The dummy content this fragment is presenting.
58      */
59     private MobileLedgerProfile mItem;
60     private TextView url;
61     private LinearLayout authParams;
62     private Switch useAuthentication;
63     private TextView userName;
64     private TextView password;
65     private FloatingActionButton fab;
66     private TextView profileName;
67
68     /**
69      * Mandatory empty constructor for the fragment manager to instantiate the
70      * fragment (e.g. upon screen orientation changes).
71      */
72     public ProfileDetailFragment() {
73     }
74     @Override
75     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
76         Log.d("profiles", "[fragment] Creating profile details options menu");
77         super.onCreateOptionsMenu(menu, inflater);
78         inflater.inflate(R.menu.profile_details, menu);
79         menu.findItem(R.id.menuDelete).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
80             @Override
81             public boolean onMenuItemClick(MenuItem item) {
82                 Log.d("profiles", String.format("[fragment] removing profile %s", mItem.getUuid()));
83                 mItem.removeFromDB();
84                 Data.profiles.remove(mItem);
85                 if (Data.profile.get().getUuid().equals(mItem.getUuid())) {
86                     Data.profile.set(Data.profiles.get(0));
87                 }
88                 return false;
89             }
90         });
91     }
92     @Override
93     public void onCreate(Bundle savedInstanceState) {
94         super.onCreate(savedInstanceState);
95
96         if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
97             // Load the dummy content specified by the fragment
98             // arguments. In a real-world scenario, use a Loader
99             // to load content from a content provider.
100             String uuid = getArguments().getString(ARG_ITEM_ID);
101             if (uuid != null)
102                 mItem = MobileLedgerProfile.loadUUIDFromDB(getArguments().getString(ARG_ITEM_ID));
103
104             Activity activity = this.getActivity();
105             if (activity == null) throw new AssertionError();
106             CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
107             if (appBarLayout != null) {
108                 if (mItem != null) appBarLayout.setTitle(mItem.getName());
109                 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
110             }
111         }
112     }
113
114     @Override
115     public void onAttach(Context context) {
116         super.onAttach(context);
117         fab = ((Activity) context).findViewById(R.id.fab);
118         fab.setOnClickListener(v -> {
119             if (mItem != null) {
120                 mItem.setName(profileName.getText());
121                 mItem.setUrl(url.getText());
122                 mItem.setAuthEnabled(useAuthentication.isChecked());
123                 mItem.setAuthUserName(userName.getText());
124                 mItem.setAuthPassword(password.getText());
125                 mItem.storeInDB();
126
127
128                 if (mItem.getUuid().equals(Data.profile.get().getUuid())) {
129                     Data.profile.set(mItem);
130                 }
131             }
132             else {
133                 mItem = new MobileLedgerProfile(profileName.getText(), url.getText(),
134                         useAuthentication.isChecked(), userName.getText(), password.getText());
135                 mItem.storeInDB();
136                 Data.profiles.add(mItem);
137             }
138
139             Activity activity = getActivity();
140             if (activity != null) activity.finish();
141         });
142     }
143     @Override
144     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
145                              Bundle savedInstanceState) {
146         View rootView = inflater.inflate(R.layout.profile_detail, container, false);
147
148         profileName = rootView.findViewById(R.id.profile_name);
149         url = rootView.findViewById(R.id.url);
150         authParams = rootView.findViewById(R.id.auth_params);
151         useAuthentication = rootView.findViewById(R.id.enable_http_auth);
152         userName = rootView.findViewById(R.id.auth_user_name);
153         password = rootView.findViewById(R.id.password);
154
155         useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
156             Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
157             authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
158         });
159
160         if (mItem != null) {
161             profileName.setText(mItem.getName());
162             url.setText(mItem.getUrl());
163             useAuthentication.setChecked(mItem.isAuthEnabled());
164             authParams.setVisibility(mItem.isAuthEnabled() ? View.VISIBLE : View.GONE);
165             userName.setText(mItem.isAuthEnabled() ? mItem.getAuthUserName() : "");
166             password.setText(mItem.isAuthEnabled() ? mItem.getAuthPassword() : "");
167         }
168         else {
169             profileName.setText("");
170             url.setText("");
171             useAuthentication.setChecked(false);
172             authParams.setVisibility(View.GONE);
173             userName.setText("");
174             password.setText("");
175         }
176
177         return rootView;
178     }
179 }