]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
1ca52ddd371c7a6e32ad05b664ed781e1afb47f4
[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 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.profiles;
19
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.view.LayoutInflater;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.LinearLayout;
32 import android.widget.PopupMenu;
33 import android.widget.Switch;
34 import android.widget.TextView;
35
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.fragment.app.Fragment;
39 import androidx.fragment.app.FragmentActivity;
40
41 import com.google.android.material.appbar.CollapsingToolbarLayout;
42 import com.google.android.material.floatingactionbutton.FloatingActionButton;
43 import com.google.android.material.textfield.TextInputLayout;
44
45 import net.ktnx.mobileledger.BuildConfig;
46 import net.ktnx.mobileledger.R;
47 import net.ktnx.mobileledger.async.SendTransactionTask;
48 import net.ktnx.mobileledger.model.Data;
49 import net.ktnx.mobileledger.model.MobileLedgerProfile;
50 import net.ktnx.mobileledger.ui.HueRingDialog;
51 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
52 import net.ktnx.mobileledger.utils.Colors;
53
54 import org.jetbrains.annotations.NonNls;
55 import org.jetbrains.annotations.NotNull;
56
57 import java.net.MalformedURLException;
58 import java.net.URL;
59 import java.util.ArrayList;
60 import java.util.Objects;
61
62 import static net.ktnx.mobileledger.utils.Logger.debug;
63
64 /**
65  * A fragment representing a single Profile detail screen.
66  * a {@link ProfileDetailActivity}
67  * on handsets.
68  */
69 public class ProfileDetailFragment extends Fragment implements HueRingDialog.HueSelectedListener {
70     /**
71      * The fragment argument representing the item ID that this fragment
72      * represents.
73      */
74     public static final String ARG_ITEM_ID = "item_id";
75     public static final String ARG_HUE = "hue";
76     @NonNls
77     private static final String HTTPS_URL_START = "https://";
78
79     /**
80      * The dummy content this fragment is presenting.
81      */
82     private MobileLedgerProfile mProfile;
83     private TextView url;
84     private Switch postingPermitted;
85     private TextInputLayout urlLayout;
86     private LinearLayout authParams;
87     private Switch useAuthentication;
88     private TextView userName;
89     private TextInputLayout userNameLayout;
90     private TextView password;
91     private TextInputLayout passwordLayout;
92     private TextView profileName;
93     private TextInputLayout profileNameLayout;
94     private TextView preferredAccountsFilter;
95     private TextInputLayout preferredAccountsFilterLayout;
96     private View huePickerView;
97     private View insecureWarningText;
98     private TextView futureDatesText;
99     private MobileLedgerProfile.FutureDates futureDates;
100     private View futureDatesLayout;
101     private TextView apiVersionText;
102     private SendTransactionTask.API apiVersion;
103
104     /**
105      * Mandatory empty constructor for the fragment manager to instantiate the
106      * fragment (e.g. upon screen orientation changes).
107      */
108     public ProfileDetailFragment() {
109     }
110     @Override
111     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
112         debug("profiles", "[fragment] Creating profile details options menu");
113         super.onCreateOptionsMenu(menu, inflater);
114         inflater.inflate(R.menu.profile_details, menu);
115         final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
116         menuDeleteProfile.setOnMenuItemClickListener(item -> {
117             AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
118             builder.setTitle(mProfile.getName());
119             builder.setMessage(R.string.remove_profile_dialog_message);
120             builder.setPositiveButton(R.string.Remove, (dialog, which) -> {
121                 debug("profiles",
122                         String.format("[fragment] removing profile %s", mProfile.getUuid()));
123                 mProfile.removeFromDB();
124                 ArrayList<MobileLedgerProfile> oldList = Data.profiles.getValue();
125                 if (oldList == null)
126                     throw new AssertionError();
127                 ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
128                 newList.remove(mProfile);
129                 Data.profiles.setValue(newList);
130                 if (mProfile.equals(Data.profile.getValue())) {
131                     debug("profiles", "[fragment] setting current profile to 0");
132                     Data.setCurrentProfile(newList.get(0));
133                 }
134
135                 final FragmentActivity activity = getActivity();
136                 if (activity != null)
137                     activity.finish();
138             });
139             builder.show();
140             return false;
141         });
142         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
143         menuDeleteProfile.setVisible(
144                 (mProfile != null) && (profiles != null) && (profiles.size() > 1));
145
146         if (BuildConfig.DEBUG) {
147             final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
148             menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked());
149             menuWipeProfileData.setVisible(mProfile != null);
150         }
151     }
152     private boolean onWipeDataMenuClicked() {
153         // this is a development option, so no confirmation
154         mProfile.wipeAllData();
155         if (mProfile.equals(Data.profile.getValue()))
156             triggerProfileChange();
157         return true;
158     }
159     private void triggerProfileChange() {
160         int index = Data.getProfileIndex(mProfile);
161         MobileLedgerProfile newProfile = new MobileLedgerProfile(mProfile);
162         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
163         if (profiles == null)
164             throw new AssertionError();
165         profiles.set(index, newProfile);
166
167         ProfilesRecyclerViewAdapter viewAdapter = ProfilesRecyclerViewAdapter.getInstance();
168         if (viewAdapter != null)
169             viewAdapter.notifyItemChanged(index);
170
171         if (mProfile.equals(Data.profile.getValue()))
172             Data.profile.setValue(newProfile);
173     }
174     @Override
175     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
176         super.onActivityCreated(savedInstanceState);
177         Activity context = getActivity();
178         if (context == null)
179             return;
180
181         if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
182             int index = getArguments().getInt(ARG_ITEM_ID, -1);
183             ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
184             if ((profiles != null) && (index != -1) && (index < profiles.size()))
185                 mProfile = profiles.get(index);
186
187             Activity activity = this.getActivity();
188             if (activity == null)
189                 throw new AssertionError();
190             CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
191             if (appBarLayout != null) {
192                 if (mProfile != null)
193                     appBarLayout.setTitle(mProfile.getName());
194                 else
195                     appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
196             }
197         }
198
199         FloatingActionButton fab = context.findViewById(R.id.fab);
200         fab.setOnClickListener(v -> onSaveFabClicked());
201         profileName = context.findViewById(R.id.profile_name);
202         profileNameLayout = context.findViewById(R.id.profile_name_layout);
203         url = context.findViewById(R.id.url);
204         urlLayout = context.findViewById(R.id.url_layout);
205         postingPermitted = context.findViewById(R.id.profile_permit_posting);
206         futureDatesLayout = context.findViewById(R.id.future_dates_layout);
207         futureDatesText = context.findViewById(R.id.future_dates_text);
208         context.findViewById(R.id.future_dates_layout)
209                .setOnClickListener(v -> {
210                    MenuInflater mi = new MenuInflater(context);
211                    PopupMenu menu = new PopupMenu(context, v);
212                    menu.inflate(R.menu.future_dates);
213                    menu.setOnMenuItemClickListener(item -> {
214                        switch (item.getItemId()) {
215                            case R.id.menu_future_dates_7:
216                                futureDates = MobileLedgerProfile.FutureDates.OneWeek;
217                                break;
218                            case R.id.menu_future_dates_14:
219                                futureDates = MobileLedgerProfile.FutureDates.TwoWeeks;
220                                break;
221                            case R.id.menu_future_dates_30:
222                                futureDates = MobileLedgerProfile.FutureDates.OneMonth;
223                                break;
224                            case R.id.menu_future_dates_60:
225                                futureDates = MobileLedgerProfile.FutureDates.TwoMonths;
226                                break;
227                            case R.id.menu_future_dates_90:
228                                futureDates = MobileLedgerProfile.FutureDates.ThreeMonths;
229                                break;
230                            case R.id.menu_future_dates_180:
231                                futureDates = MobileLedgerProfile.FutureDates.SixMonths;
232                                break;
233                            case R.id.menu_future_dates_365:
234                                futureDates = MobileLedgerProfile.FutureDates.OneYear;
235                                break;
236                            case R.id.menu_future_dates_all:
237                                futureDates = MobileLedgerProfile.FutureDates.All;
238                                break;
239                            default:
240                                futureDates = MobileLedgerProfile.FutureDates.None;
241                        }
242                        futureDatesText.setText(futureDates.getText(getResources()));
243                        return true;
244                    });
245                    menu.show();
246                });
247         apiVersionText = context.findViewById(R.id.api_version_text);
248         context.findViewById(R.id.api_version_layout)
249                .setOnClickListener(v -> {
250                    MenuInflater mi = new MenuInflater(context);
251                    PopupMenu menu = new PopupMenu(context, v);
252                    menu.inflate(R.menu.api_version);
253                    menu.setOnMenuItemClickListener(item -> {
254                        switch (item.getItemId()) {
255                            case R.id.api_version_menu_html:
256                                apiVersion = SendTransactionTask.API.html;
257                                break;
258                            case R.id.api_version_menu_post_1_14:
259                                apiVersion = SendTransactionTask.API.post_1_14;
260                                break;
261                            case R.id.api_version_menu_pre_1_15:
262                                apiVersion = SendTransactionTask.API.pre_1_15;
263                                break;
264                            case R.id.api_version_menu_auto:
265                            default:
266                                apiVersion = SendTransactionTask.API.auto;
267                        }
268                        apiVersionText.setText(apiVersion.getDescription(getResources()));
269                        return true;
270                    });
271                    menu.show();
272                });
273         authParams = context.findViewById(R.id.auth_params);
274         useAuthentication = context.findViewById(R.id.enable_http_auth);
275         userName = context.findViewById(R.id.auth_user_name);
276         userNameLayout = context.findViewById(R.id.auth_user_name_layout);
277         password = context.findViewById(R.id.password);
278         passwordLayout = context.findViewById(R.id.password_layout);
279         huePickerView = context.findViewById(R.id.btn_pick_ring_color);
280         preferredAccountsFilter = context.findViewById(R.id.preferred_accounts_filter_filter);
281         preferredAccountsFilterLayout =
282                 context.findViewById(R.id.preferred_accounts_accounts_filter_layout);
283         insecureWarningText = context.findViewById(R.id.insecure_scheme_text);
284
285         useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
286             debug("profiles", isChecked ? "auth enabled " : "auth disabled");
287             authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
288             if (isChecked)
289                 userName.requestFocus();
290             checkInsecureSchemeWithAuth();
291         });
292
293         postingPermitted.setOnCheckedChangeListener(((buttonView, isChecked) -> {
294             preferredAccountsFilterLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
295             futureDatesLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
296         }));
297
298         hookClearErrorOnFocusListener(profileName, profileNameLayout);
299         hookClearErrorOnFocusListener(url, urlLayout);
300         hookClearErrorOnFocusListener(userName, userNameLayout);
301         hookClearErrorOnFocusListener(password, passwordLayout);
302
303         final int profileThemeId;
304         if (mProfile != null) {
305             profileName.setText(mProfile.getName());
306             postingPermitted.setChecked(mProfile.isPostingPermitted());
307             futureDates = mProfile.getFutureDates();
308             futureDatesText.setText(futureDates.getText(getResources()));
309             apiVersion = mProfile.getApiVersion();
310             apiVersionText.setText(apiVersion.getDescription(getResources()));
311             url.setText(mProfile.getUrl());
312             useAuthentication.setChecked(mProfile.isAuthEnabled());
313             authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
314             userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
315             password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
316             preferredAccountsFilter.setText(mProfile.getPreferredAccountsFilter());
317             profileThemeId = mProfile.getThemeHue();
318         }
319         else {
320             profileName.setText("");
321             url.setText(HTTPS_URL_START);
322             postingPermitted.setChecked(true);
323             futureDates = MobileLedgerProfile.FutureDates.None;
324             futureDatesText.setText(futureDates.getText(getResources()));
325             apiVersion = SendTransactionTask.API.auto;
326             apiVersionText.setText(apiVersion.getDescription(getResources()));
327             useAuthentication.setChecked(false);
328             authParams.setVisibility(View.GONE);
329             userName.setText("");
330             password.setText("");
331             preferredAccountsFilter.setText(null);
332             profileThemeId = getArguments().getInt(ARG_HUE, -1);
333         }
334
335         checkInsecureSchemeWithAuth();
336
337         url.addTextChangedListener(new TextWatcher() {
338             @Override
339             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
340
341             }
342             @Override
343             public void onTextChanged(CharSequence s, int start, int before, int count) {
344
345             }
346             @Override
347             public void afterTextChanged(Editable s) {
348                 checkInsecureSchemeWithAuth();
349             }
350         });
351
352         final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId;
353         final int profileColor = Colors.getPrimaryColorForHue(hue);
354
355         huePickerView.setBackgroundColor(profileColor);
356         huePickerView.setTag(profileThemeId);
357         huePickerView.setOnClickListener(v -> {
358             HueRingDialog d = new HueRingDialog(
359                     Objects.requireNonNull(ProfileDetailFragment.this.getContext()), profileThemeId,
360                     (Integer) v.getTag());
361             d.show();
362             d.setColorSelectedListener(this);
363         });
364
365         profileName.requestFocus();
366     }
367     private void onSaveFabClicked() {
368         if (!checkValidity())
369             return;
370
371         if (mProfile != null) {
372             updateProfileFromUI();
373 //                debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue()));
374             mProfile.storeInDB();
375             debug("profiles", "profile stored in DB");
376             triggerProfileChange();
377         }
378         else {
379             mProfile = new MobileLedgerProfile();
380             updateProfileFromUI();
381             mProfile.storeInDB();
382             final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
383             if (profiles == null)
384                 throw new AssertionError();
385             ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
386             newList.add(mProfile);
387             Data.profiles.setValue(newList);
388             MobileLedgerProfile.storeProfilesOrder();
389
390             // first profile ever?
391             if (newList.size() == 1)
392                 Data.profile.setValue(mProfile);
393         }
394
395         Activity activity = getActivity();
396         if (activity != null)
397             activity.finish();
398     }
399     private void updateProfileFromUI() {
400         mProfile.setName(profileName.getText());
401         mProfile.setUrl(url.getText());
402         mProfile.setPostingPermitted(postingPermitted.isChecked());
403         mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getText());
404         mProfile.setAuthEnabled(useAuthentication.isChecked());
405         mProfile.setAuthUserName(userName.getText());
406         mProfile.setAuthPassword(password.getText());
407         mProfile.setThemeHue(huePickerView.getTag());
408         mProfile.setFutureDates(futureDates);
409         mProfile.setApiVersion(apiVersion);
410     }
411     @Override
412     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
413                              Bundle savedInstanceState) {
414
415         return inflater.inflate(R.layout.profile_detail, container, false);
416     }
417     private boolean checkUrlValidity() {
418         boolean valid = true;
419
420         String val = String.valueOf(url.getText())
421                            .trim();
422         if (val.isEmpty()) {
423             valid = false;
424             urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
425         }
426         try {
427             URL url = new URL(val);
428             String host = url.getHost();
429             if (host == null || host.isEmpty())
430                 throw new MalformedURLException("Missing host");
431             String protocol = url.getProtocol()
432                                  .toUpperCase();
433             if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) {
434                 valid = false;
435                 urlLayout.setError(getResources().getText(R.string.err_invalid_url));
436             }
437         }
438         catch (MalformedURLException e) {
439             valid = false;
440             urlLayout.setError(getResources().getText(R.string.err_invalid_url));
441         }
442
443         return valid;
444     }
445     private void checkInsecureSchemeWithAuth() {
446         boolean showWarning = false;
447
448         if (useAuthentication.isChecked()) {
449             String urlText = url.getText()
450                                 .toString();
451             if (urlText.startsWith("http") && !urlText.startsWith("https"))
452                 showWarning = true;
453         }
454
455         if (showWarning)
456             insecureWarningText.setVisibility(View.VISIBLE);
457         else
458             insecureWarningText.setVisibility(View.GONE);
459     }
460     private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
461         view.setOnFocusChangeListener((v, hasFocus) -> {
462             if (hasFocus)
463                 layout.setError(null);
464         });
465         view.addTextChangedListener(new TextWatcher() {
466             @Override
467             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
468             }
469             @Override
470             public void onTextChanged(CharSequence s, int start, int before, int count) {
471                 layout.setError(null);
472             }
473             @Override
474             public void afterTextChanged(Editable s) {
475             }
476         });
477     }
478     private boolean checkValidity() {
479         boolean valid = true;
480
481         String val = String.valueOf(profileName.getText());
482         if (val.trim()
483                .isEmpty())
484         {
485             valid = false;
486             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
487         }
488
489         if (!checkUrlValidity())
490             valid = false;
491
492         if (useAuthentication.isChecked()) {
493             val = String.valueOf(userName.getText());
494             if (val.trim()
495                    .isEmpty())
496             {
497                 valid = false;
498                 userNameLayout.setError(
499                         getResources().getText(R.string.err_profile_user_name_empty));
500             }
501
502             val = String.valueOf(password.getText());
503             if (val.trim()
504                    .isEmpty())
505             {
506                 valid = false;
507                 passwordLayout.setError(
508                         getResources().getText(R.string.err_profile_password_empty));
509             }
510         }
511
512         return valid;
513     }
514     @Override
515     public void onHueSelected(int hue) {
516         huePickerView.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
517         huePickerView.setTag(hue);
518     }
519 }