]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
7cbfa0e7062c20abf3b7c2d6b75a6b128fccb7e7
[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.os.Bundle;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.Nullable;
24 import android.support.design.widget.CollapsingToolbarLayout;
25 import android.support.design.widget.FloatingActionButton;
26 import android.support.design.widget.TextInputLayout;
27 import android.support.v4.app.Fragment;
28 import android.text.Editable;
29 import android.text.TextWatcher;
30 import android.util.Log;
31 import android.view.LayoutInflater;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.LinearLayout;
38 import android.widget.Switch;
39 import android.widget.TextView;
40
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.model.MobileLedgerProfile;
44 import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
45
46 /**
47  * A fragment representing a single Profile detail screen.
48  * This fragment is either contained in a {@link ProfileListActivity}
49  * in two-pane mode (on tablets) or a {@link ProfileDetailActivity}
50  * on handsets.
51  */
52 public class ProfileDetailFragment extends Fragment {
53     /**
54      * The fragment argument representing the item ID that this fragment
55      * represents.
56      */
57     public static final String ARG_ITEM_ID = "item_id";
58
59     /**
60      * The dummy content this fragment is presenting.
61      */
62     private MobileLedgerProfile mProfile;
63     private TextView url;
64     private Switch postingPermitted;
65     private TextInputLayout urlLayout;
66     private LinearLayout authParams;
67     private Switch useAuthentication;
68     private TextView userName;
69     private TextInputLayout userNameLayout;
70     private TextView password;
71     private TextInputLayout passwordLayout;
72     private TextView profileName;
73     private TextInputLayout profileNameLayout;
74     private FloatingActionButton fab;
75
76     /**
77      * Mandatory empty constructor for the fragment manager to instantiate the
78      * fragment (e.g. upon screen orientation changes).
79      */
80     public ProfileDetailFragment() {
81     }
82     @Override
83     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
84         Log.d("profiles", "[fragment] Creating profile details options menu");
85         super.onCreateOptionsMenu(menu, inflater);
86         inflater.inflate(R.menu.profile_details, menu);
87         final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
88         menuDeleteProfile.setOnMenuItemClickListener(item -> {
89             Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid()));
90             mProfile.removeFromDB();
91             Data.profiles.remove(mProfile);
92             if (Data.profile.get().equals(mProfile)) {
93                 Log.d("profiles", "[fragment] setting current profile to 0");
94                 Data.setCurrentProfile(Data.profiles.get(0));
95             }
96             return false;
97         });
98         menuDeleteProfile.setVisible((mProfile != null) && (Data.profiles.size() > 1));
99     }
100     @Override
101     public void onCreate(Bundle savedInstanceState) {
102         super.onCreate(savedInstanceState);
103
104         if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
105             int index = getArguments().getInt(ARG_ITEM_ID, -1);
106             if (index != -1) mProfile = Data.profiles.get(index);
107
108             Activity activity = this.getActivity();
109             if (activity == null) throw new AssertionError();
110             CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
111             if (appBarLayout != null) {
112                 if (mProfile != null) appBarLayout.setTitle(mProfile.getName());
113                 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
114             }
115         }
116     }
117     @Override
118     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
119         super.onActivityCreated(savedInstanceState);
120         Activity context = getActivity();
121         if (context == null) return;
122
123         fab = context.findViewById(R.id.fab);
124         fab.setOnClickListener(v -> {
125             if (!checkValidity()) return;
126
127             if (mProfile != null) {
128                 mProfile.setName(profileName.getText());
129                 mProfile.setUrl(url.getText());
130                 mProfile.setAuthEnabled(useAuthentication.isChecked());
131                 mProfile.setAuthUserName(userName.getText());
132                 mProfile.setAuthPassword(password.getText());
133                 mProfile.storeInDB();
134                 Log.d("profiles", "profile stored in DB");
135                 Data.profiles.triggerItemChangedNotification(mProfile);
136
137
138                 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
139                     // dummy update to notify the observers of the possibly new name/URL
140                     Data.profile.set(mProfile);
141                 }
142             }
143             else {
144                 mProfile = new MobileLedgerProfile(profileName.getText(), postingPermitted.isChecked(),
145                         url.getText(), useAuthentication.isChecked(), userName.getText(),
146                         password.getText());
147                 mProfile.storeInDB();
148                 Data.profiles.add(mProfile);
149                 MobileLedgerProfile.storeProfilesOrder();
150
151                 // first profile ever?
152                 if (Data.profiles.getList().size() == 1) Data.profile.set(mProfile);
153             }
154
155             Activity activity = getActivity();
156             if (activity != null) activity.finish();
157         });
158
159         profileName.requestFocus();
160     }
161     @Override
162     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
163                              Bundle savedInstanceState) {
164         View rootView = inflater.inflate(R.layout.profile_detail, container, false);
165
166         profileName = rootView.findViewById(R.id.profile_name);
167         profileNameLayout = rootView.findViewById(R.id.profile_name_layout);
168         url = rootView.findViewById(R.id.url);
169         urlLayout = rootView.findViewById(R.id.url_layout);
170         postingPermitted = rootView.findViewById(R.id.profile_permit_posting);
171         authParams = rootView.findViewById(R.id.auth_params);
172         useAuthentication = rootView.findViewById(R.id.enable_http_auth);
173         userName = rootView.findViewById(R.id.auth_user_name);
174         userNameLayout = rootView.findViewById(R.id.auth_user_name_layout);
175         password = rootView.findViewById(R.id.password);
176         passwordLayout = rootView.findViewById(R.id.password_layout);
177
178         useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
179             Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
180             authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
181             if (isChecked) userName.requestFocus();
182         });
183
184         hookClearErrorOnFocusListener(profileName, profileNameLayout);
185         hookClearErrorOnFocusListener(url, urlLayout);
186         hookClearErrorOnFocusListener(userName, userNameLayout);
187         hookClearErrorOnFocusListener(password, passwordLayout);
188
189         if (mProfile != null) {
190             profileName.setText(mProfile.getName());
191             postingPermitted.setChecked(mProfile.isPostingPermitted());
192             url.setText(mProfile.getUrl());
193             useAuthentication.setChecked(mProfile.isAuthEnabled());
194             authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
195             userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
196             password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
197         }
198         else {
199             profileName.setText("");
200             url.setText("");
201             postingPermitted.setChecked(true);
202             useAuthentication.setChecked(false);
203             authParams.setVisibility(View.GONE);
204             userName.setText("");
205             password.setText("");
206         }
207
208         return rootView;
209     }
210     private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
211         view.setOnFocusChangeListener((v, hasFocus) -> {
212             if (hasFocus) layout.setError(null);
213         });
214         view.addTextChangedListener(new TextWatcher() {
215             @Override
216             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
217             }
218             @Override
219             public void onTextChanged(CharSequence s, int start, int before, int count) {
220                 layout.setError(null);
221             }
222             @Override
223             public void afterTextChanged(Editable s) {
224             }
225         });
226     }
227     boolean checkValidity() {
228         boolean valid = true;
229
230         String val = String.valueOf(profileName.getText());
231         if (val.trim().isEmpty()) {
232             valid = false;
233             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
234         }
235
236         val = String.valueOf(url.getText());
237         if (val.trim().isEmpty()) {
238             valid = false;
239             urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
240         }
241         if (useAuthentication.isChecked()) {
242             val = String.valueOf(userName.getText());
243             if (val.trim().isEmpty()) {
244                 valid = false;
245                 userNameLayout
246                         .setError(getResources().getText(R.string.err_profile_user_name_empty));
247             }
248
249             val = String.valueOf(password.getText());
250             if (val.trim().isEmpty()) {
251                 valid = false;
252                 passwordLayout
253                         .setError(getResources().getText(R.string.err_profile_password_empty));
254             }
255         }
256
257         return valid;
258     }
259 }