]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
more pronounced day/month delimiters in the transaction list
[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     @NonNls
76     private static final String HTTPS_URL_START = "https://";
77
78     /**
79      * The dummy content this fragment is presenting.
80      */
81     private MobileLedgerProfile mProfile;
82     private TextView url;
83     private Switch postingPermitted;
84     private TextInputLayout urlLayout;
85     private LinearLayout authParams;
86     private Switch useAuthentication;
87     private TextView userName;
88     private TextInputLayout userNameLayout;
89     private TextView password;
90     private TextInputLayout passwordLayout;
91     private TextView profileName;
92     private TextInputLayout profileNameLayout;
93     private TextView preferredAccountsFilter;
94     private TextInputLayout preferredAccountsFilterLayout;
95     private View huePickerView;
96     private View insecureWarningText;
97     private TextView futureDatesText;
98     private MobileLedgerProfile.FutureDates futureDates;
99     private View futureDatesLayout;
100     private TextView apiVersionText;
101     private View apiVersionLayout;
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 prva = ProfilesRecyclerViewAdapter.getInstance();
168         if (prva != null)
169             prva.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_30:
216                                futureDates = MobileLedgerProfile.FutureDates.OneMonth;
217                                break;
218                            case R.id.menu_future_dates_60:
219                                futureDates = MobileLedgerProfile.FutureDates.TwoMonths;
220                                break;
221                            case R.id.menu_future_dates_90:
222                                futureDates = MobileLedgerProfile.FutureDates.ThreeMonths;
223                                break;
224                            case R.id.menu_future_dates_180:
225                                futureDates = MobileLedgerProfile.FutureDates.SixMonths;
226                                break;
227                            case R.id.menu_future_dates_365:
228                                futureDates = MobileLedgerProfile.FutureDates.OneYear;
229                                break;
230                            case R.id.menu_future_dates_all:
231                                futureDates = MobileLedgerProfile.FutureDates.All;
232                                break;
233                            default:
234                                futureDates = MobileLedgerProfile.FutureDates.None;
235                        }
236                        futureDatesText.setText(futureDates.getText(getResources()));
237                        return true;
238                    });
239                    menu.show();
240                });
241         apiVersionLayout = context.findViewById(R.id.api_version_layout);
242         apiVersionText = context.findViewById(R.id.api_version_text);
243         apiVersionLayout.setOnClickListener(v -> {
244             MenuInflater mi = new MenuInflater(context);
245             PopupMenu menu = new PopupMenu(context, v);
246             menu.inflate(R.menu.api_version);
247             menu.setOnMenuItemClickListener(item -> {
248                 switch (item.getItemId()) {
249                     case R.id.api_version_menu_auto:
250                         apiVersion = SendTransactionTask.API.auto;
251                         break;
252                     case R.id.api_version_menu_html:
253                         apiVersion = SendTransactionTask.API.html;
254                         break;
255                     case R.id.api_version_menu_post_1_14:
256                         apiVersion = SendTransactionTask.API.post_1_14;
257                         break;
258                     case R.id.api_version_menu_pre_1_15:
259                         apiVersion = SendTransactionTask.API.pre_1_15;
260                         break;
261                     default:
262                         apiVersion = SendTransactionTask.API.auto;
263                 }
264                 apiVersionText.setText(apiVersion.getDescription(getResources()));
265                 return true;
266             });
267             menu.show();
268         });
269         authParams = context.findViewById(R.id.auth_params);
270         useAuthentication = context.findViewById(R.id.enable_http_auth);
271         userName = context.findViewById(R.id.auth_user_name);
272         userNameLayout = context.findViewById(R.id.auth_user_name_layout);
273         password = context.findViewById(R.id.password);
274         passwordLayout = context.findViewById(R.id.password_layout);
275         huePickerView = context.findViewById(R.id.btn_pick_ring_color);
276         preferredAccountsFilter = context.findViewById(R.id.preferred_accounts_filter_filter);
277         preferredAccountsFilterLayout =
278                 context.findViewById(R.id.preferred_accounts_accounts_filter_layout);
279         insecureWarningText = context.findViewById(R.id.insecure_scheme_text);
280
281         useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
282             debug("profiles", isChecked ? "auth enabled " : "auth disabled");
283             authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
284             if (isChecked)
285                 userName.requestFocus();
286             checkInsecureSchemeWithAuth();
287         });
288
289         postingPermitted.setOnCheckedChangeListener(((buttonView, isChecked) -> {
290             preferredAccountsFilterLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
291             futureDatesLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
292         }));
293
294         hookClearErrorOnFocusListener(profileName, profileNameLayout);
295         hookClearErrorOnFocusListener(url, urlLayout);
296         hookClearErrorOnFocusListener(userName, userNameLayout);
297         hookClearErrorOnFocusListener(password, passwordLayout);
298
299         int profileThemeId;
300         if (mProfile != null) {
301             profileName.setText(mProfile.getName());
302             postingPermitted.setChecked(mProfile.isPostingPermitted());
303             futureDates = mProfile.getFutureDates();
304             futureDatesText.setText(futureDates.getText(getResources()));
305             apiVersion = mProfile.getApiVersion();
306             apiVersionText.setText(apiVersion.getDescription(getResources()));
307             url.setText(mProfile.getUrl());
308             useAuthentication.setChecked(mProfile.isAuthEnabled());
309             authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
310             userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
311             password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
312             preferredAccountsFilter.setText(mProfile.getPreferredAccountsFilter());
313             profileThemeId = mProfile.getThemeId();
314         }
315         else {
316             profileName.setText("");
317             url.setText(HTTPS_URL_START);
318             postingPermitted.setChecked(true);
319             futureDates = MobileLedgerProfile.FutureDates.None;
320             futureDatesText.setText(futureDates.getText(getResources()));
321             apiVersion = SendTransactionTask.API.auto;
322             apiVersionText.setText(apiVersion.getDescription(getResources()));
323             useAuthentication.setChecked(false);
324             authParams.setVisibility(View.GONE);
325             userName.setText("");
326             password.setText("");
327             preferredAccountsFilter.setText(null);
328             profileThemeId = -1;
329         }
330
331         checkInsecureSchemeWithAuth();
332
333         url.addTextChangedListener(new TextWatcher() {
334             @Override
335             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
336
337             }
338             @Override
339             public void onTextChanged(CharSequence s, int start, int before, int count) {
340
341             }
342             @Override
343             public void afterTextChanged(Editable s) {
344                 checkInsecureSchemeWithAuth();
345             }
346         });
347
348         final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId;
349         final int profileColor = Colors.getPrimaryColorForHue(hue);
350
351         huePickerView.setBackgroundColor(profileColor);
352         huePickerView.setTag(profileThemeId);
353         huePickerView.setOnClickListener(v -> {
354             HueRingDialog d = new HueRingDialog(
355                     Objects.requireNonNull(ProfileDetailFragment.this.getContext()), profileThemeId,
356                     (Integer) v.getTag());
357             d.show();
358             d.setColorSelectedListener(this);
359         });
360
361         profileName.requestFocus();
362     }
363     private void onSaveFabClicked() {
364         if (!checkValidity())
365             return;
366
367         if (mProfile != null) {
368             updateProfileFromUI();
369 //                debug("profiles", String.format("Selected item is %d", mProfile.getThemeId()));
370             mProfile.storeInDB();
371             debug("profiles", "profile stored in DB");
372             triggerProfileChange();
373         }
374         else {
375             mProfile = new MobileLedgerProfile();
376             updateProfileFromUI();
377             mProfile.storeInDB();
378             final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
379             if (profiles == null)
380                 throw new AssertionError();
381             ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
382             newList.add(mProfile);
383             Data.profiles.setValue(newList);
384             MobileLedgerProfile.storeProfilesOrder();
385
386             // first profile ever?
387             if (newList.size() == 1)
388                 Data.profile.setValue(mProfile);
389         }
390
391         Activity activity = getActivity();
392         if (activity != null)
393             activity.finish();
394     }
395     private void updateProfileFromUI() {
396         mProfile.setName(profileName.getText());
397         mProfile.setUrl(url.getText());
398         mProfile.setPostingPermitted(postingPermitted.isChecked());
399         mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getText());
400         mProfile.setAuthEnabled(useAuthentication.isChecked());
401         mProfile.setAuthUserName(userName.getText());
402         mProfile.setAuthPassword(password.getText());
403         mProfile.setThemeId(huePickerView.getTag());
404         mProfile.setFutureDates(futureDates);
405         mProfile.setApiVersion(apiVersion);
406     }
407     @Override
408     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
409                              Bundle savedInstanceState) {
410         View rootView = inflater.inflate(R.layout.profile_detail, container, false);
411
412         return rootView;
413     }
414     private boolean checkUrlValidity() {
415         boolean valid = true;
416
417         String val = String.valueOf(url.getText())
418                            .trim();
419         if (val.isEmpty()) {
420             valid = false;
421             urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
422         }
423         try {
424             URL url = new URL(val);
425             String host = url.getHost();
426             if (host == null || host.isEmpty())
427                 throw new MalformedURLException("Missing host");
428             String protocol = url.getProtocol()
429                                  .toUpperCase();
430             if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) {
431                 valid = false;
432                 urlLayout.setError(getResources().getText(R.string.err_invalid_url));
433             }
434         }
435         catch (MalformedURLException e) {
436             valid = false;
437             urlLayout.setError(getResources().getText(R.string.err_invalid_url));
438         }
439
440         return valid;
441     }
442     private void checkInsecureSchemeWithAuth() {
443         boolean showWarning = false;
444
445         if (useAuthentication.isChecked()) {
446             String urlText = url.getText()
447                                 .toString();
448             if (urlText.startsWith("http") && !urlText.startsWith("https"))
449                 showWarning = true;
450         }
451
452         if (showWarning)
453             insecureWarningText.setVisibility(View.VISIBLE);
454         else
455             insecureWarningText.setVisibility(View.GONE);
456     }
457     private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
458         view.setOnFocusChangeListener((v, hasFocus) -> {
459             if (hasFocus)
460                 layout.setError(null);
461         });
462         view.addTextChangedListener(new TextWatcher() {
463             @Override
464             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
465             }
466             @Override
467             public void onTextChanged(CharSequence s, int start, int before, int count) {
468                 layout.setError(null);
469             }
470             @Override
471             public void afterTextChanged(Editable s) {
472             }
473         });
474     }
475     private boolean checkValidity() {
476         boolean valid = true;
477
478         String val = String.valueOf(profileName.getText());
479         if (val.trim()
480                .isEmpty())
481         {
482             valid = false;
483             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
484         }
485
486         if (!checkUrlValidity())
487             valid = false;
488
489         if (useAuthentication.isChecked()) {
490             val = String.valueOf(userName.getText());
491             if (val.trim()
492                    .isEmpty())
493             {
494                 valid = false;
495                 userNameLayout.setError(
496                         getResources().getText(R.string.err_profile_user_name_empty));
497             }
498
499             val = String.valueOf(password.getText());
500             if (val.trim()
501                    .isEmpty())
502             {
503                 valid = false;
504                 passwordLayout.setError(
505                         getResources().getText(R.string.err_profile_password_empty));
506             }
507         }
508
509         return valid;
510     }
511     @Override
512     public void onHueSelected(int hue) {
513         huePickerView.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
514         huePickerView.setTag(hue);
515     }
516 }