2 * Copyright © 2021 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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.profiles;
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.graphics.Typeface;
23 import android.os.Bundle;
24 import android.text.Editable;
25 import android.text.TextWatcher;
26 import android.view.LayoutInflater;
27 import android.view.Menu;
28 import android.view.MenuInflater;
29 import android.view.MenuItem;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.PopupMenu;
33 import android.widget.TextView;
35 import androidx.annotation.NonNull;
36 import androidx.annotation.Nullable;
37 import androidx.appcompat.app.AppCompatActivity;
38 import androidx.fragment.app.Fragment;
39 import androidx.fragment.app.FragmentActivity;
40 import androidx.lifecycle.LifecycleOwner;
41 import androidx.lifecycle.ViewModelProvider;
43 import com.google.android.material.appbar.CollapsingToolbarLayout;
44 import com.google.android.material.floatingactionbutton.FloatingActionButton;
45 import com.google.android.material.textfield.TextInputLayout;
47 import net.ktnx.mobileledger.BuildConfig;
48 import net.ktnx.mobileledger.R;
49 import net.ktnx.mobileledger.databinding.ProfileDetailBinding;
50 import net.ktnx.mobileledger.json.API;
51 import net.ktnx.mobileledger.model.Data;
52 import net.ktnx.mobileledger.model.MobileLedgerProfile;
53 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
54 import net.ktnx.mobileledger.ui.HueRingDialog;
55 import net.ktnx.mobileledger.utils.Colors;
56 import net.ktnx.mobileledger.utils.Misc;
58 import org.jetbrains.annotations.NonNls;
59 import org.jetbrains.annotations.NotNull;
61 import java.net.MalformedURLException;
63 import java.util.ArrayList;
64 import java.util.Objects;
66 import static net.ktnx.mobileledger.utils.Logger.debug;
69 * A fragment representing a single Profile detail screen.
70 * a {@link ProfileDetailActivity}
73 public class ProfileDetailFragment extends Fragment {
75 * The fragment argument representing the item ID that this fragment
78 public static final String ARG_ITEM_ID = "item_id";
79 public static final String ARG_HUE = "hue";
82 private MobileLedgerProfile mProfile;
83 private boolean defaultCommoditySet;
84 private boolean syncingModelFromUI = false;
85 private ProfileDetailBinding binding;
87 * Mandatory empty constructor for the fragment manager to instantiate the
88 * fragment (e.g. upon screen orientation changes).
90 public ProfileDetailFragment() {
91 super(R.layout.profile_detail);
94 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
95 debug("profiles", "[fragment] Creating profile details options menu");
96 super.onCreateOptionsMenu(menu, inflater);
97 inflater.inflate(R.menu.profile_details, menu);
98 final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
99 menuDeleteProfile.setOnMenuItemClickListener(item -> onDeleteProfile());
100 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
102 if (BuildConfig.DEBUG) {
103 final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
104 menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked());
105 menuWipeProfileData.setVisible(mProfile != null);
108 private boolean onDeleteProfile() {
109 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
110 builder.setTitle(mProfile.getName());
111 builder.setMessage(R.string.remove_profile_dialog_message);
112 builder.setPositiveButton(R.string.Remove, (dialog, which) -> {
113 debug("profiles", String.format("[fragment] removing profile %s", mProfile.getId()));
114 mProfile.removeFromDB();
115 ArrayList<MobileLedgerProfile> oldList = Data.profiles.getValue();
117 throw new AssertionError();
118 ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
119 newList.remove(mProfile);
120 Data.profiles.setValue(newList);
121 if (mProfile.equals(Data.getProfile())) {
122 debug("profiles", "[fragment] setting current profile to 0");
123 Data.setCurrentProfile(newList.get(0));
126 final FragmentActivity activity = getActivity();
127 if (activity != null)
133 private boolean onWipeDataMenuClicked() {
134 // this is a development option, so no confirmation
135 mProfile.wipeAllData();
136 if (mProfile.equals(Data.getProfile()))
137 triggerProfileChange();
140 private void triggerProfileChange() {
141 int index = Data.getProfileIndex(mProfile);
142 MobileLedgerProfile newProfile = new MobileLedgerProfile(mProfile);
143 final ArrayList<MobileLedgerProfile> profiles =
144 Objects.requireNonNull(Data.profiles.getValue());
145 profiles.set(index, newProfile);
147 ProfilesRecyclerViewAdapter viewAdapter = ProfilesRecyclerViewAdapter.getInstance();
148 if (viewAdapter != null)
149 viewAdapter.notifyItemChanged(index);
151 if (mProfile.equals(Data.getProfile()))
152 Data.setCurrentProfile(newProfile);
154 private void hookTextChangeSyncRoutine(TextView view, TextChangeSyncRoutine syncRoutine) {
155 view.addTextChangedListener(new TextWatcher() {
157 public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
159 public void onTextChanged(CharSequence s, int start, int before, int count) {}
161 public void afterTextChanged(Editable s) { syncRoutine.onTextChanged(s.toString());}
166 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
167 @Nullable Bundle savedInstanceState) {
168 binding = ProfileDetailBinding.inflate(inflater, container, false);
170 return binding.getRoot();
173 public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
174 super.onViewCreated(view, savedInstanceState);
175 Activity context = getActivity();
179 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
180 int index = getArguments().getInt(ARG_ITEM_ID, -1);
181 ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
182 if ((profiles != null) && (index != -1) && (index < profiles.size()))
183 mProfile = profiles.get(index);
185 Activity activity = this.getActivity();
186 if (activity == null)
187 throw new AssertionError();
188 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
189 if (appBarLayout != null) {
190 if (mProfile != null)
191 appBarLayout.setTitle(mProfile.getName());
193 appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
197 final LifecycleOwner viewLifecycleOwner = getViewLifecycleOwner();
198 final ProfileDetailModel model = getModel();
200 model.observeDefaultCommodity(viewLifecycleOwner, c -> {
202 setDefaultCommodity(c.getName());
204 resetDefaultCommodity();
207 FloatingActionButton fab = context.findViewById(R.id.fabAdd);
208 fab.setOnClickListener(v -> onSaveFabClicked());
210 hookTextChangeSyncRoutine(binding.profileName, model::setProfileName);
211 model.observeProfileName(viewLifecycleOwner, pn -> {
212 if (!Misc.equalStrings(pn, Misc.nullIsEmpty(binding.profileName.getText())))
213 binding.profileName.setText(pn);
216 hookTextChangeSyncRoutine(binding.url, model::setUrl);
217 model.observeUrl(viewLifecycleOwner, u -> {
218 if (!Misc.equalStrings(u, Misc.nullIsEmpty(binding.url.getText())))
219 binding.url.setText(u);
222 binding.defaultCommodityLayout.setOnClickListener(v -> {
223 CurrencySelectorFragment cpf = CurrencySelectorFragment.newInstance(
224 CurrencySelectorFragment.DEFAULT_COLUMN_COUNT, false);
225 cpf.setOnCurrencySelectedListener(model::setDefaultCommodity);
226 final AppCompatActivity activity = (AppCompatActivity) v.getContext();
227 cpf.show(activity.getSupportFragmentManager(), "currency-selector");
230 binding.profileShowCommodity.setOnCheckedChangeListener(
231 (buttonView, isChecked) -> model.setShowCommodityByDefault(isChecked));
232 model.observeShowCommodityByDefault(viewLifecycleOwner,
233 binding.profileShowCommodity::setChecked);
235 model.observePostingPermitted(viewLifecycleOwner, isChecked -> {
236 binding.profilePermitPosting.setChecked(isChecked);
237 binding.postingSubItems.setVisibility(isChecked ? View.VISIBLE : View.GONE);
239 binding.profilePermitPosting.setOnCheckedChangeListener(
240 ((buttonView, isChecked) -> model.setPostingPermitted(isChecked)));
242 model.observeShowCommentsByDefault(viewLifecycleOwner,
243 binding.profileShowComments::setChecked);
244 binding.profileShowComments.setOnCheckedChangeListener(
245 ((buttonView, isChecked) -> model.setShowCommentsByDefault(isChecked)));
247 binding.futureDatesLayout.setOnClickListener(v -> {
248 MenuInflater mi = new MenuInflater(context);
249 PopupMenu menu = new PopupMenu(context, v);
250 menu.inflate(R.menu.future_dates);
251 menu.setOnMenuItemClickListener(item -> {
252 model.setFutureDates(futureDatesSettingFromMenuItemId(item.getItemId()));
257 model.observeFutureDates(viewLifecycleOwner,
258 v -> binding.futureDatesText.setText(v.getText(getResources())));
260 model.observeApiVersion(viewLifecycleOwner,
261 apiVer -> binding.apiVersionText.setText(apiVer.getDescription(getResources())));
262 binding.apiVersionLabel.setOnClickListener(this::chooseAPIVersion);
263 binding.apiVersionText.setOnClickListener(this::chooseAPIVersion);
265 binding.serverVersionLabel.setOnClickListener(v -> model.triggerVersionDetection());
266 model.observeDetectedVersion(viewLifecycleOwner, ver -> {
268 binding.detectedServerVersionText.setText(context.getResources()
270 R.string.server_version_unknown_label));
271 else if (ver.isPre_1_20_1())
272 binding.detectedServerVersionText.setText(context.getResources()
274 R.string.detected_server_pre_1_20_1));
276 binding.detectedServerVersionText.setText(ver.toString());
278 binding.detectedServerVersionText.setOnClickListener(v -> model.triggerVersionDetection());
279 binding.serverVersionDetectButton.setOnClickListener(v -> model.triggerVersionDetection());
280 model.observeDetectingHledgerVersion(viewLifecycleOwner,
281 running -> binding.serverVersionDetectButton.setVisibility(
282 running ? View.VISIBLE : View.INVISIBLE));
284 binding.enableHttpAuth.setOnCheckedChangeListener((buttonView, isChecked) -> {
285 boolean wasOn = model.getUseAuthentication();
286 model.setUseAuthentication(isChecked);
287 if (!wasOn && isChecked)
288 binding.authUserName.requestFocus();
290 model.observeUseAuthentication(viewLifecycleOwner, isChecked -> {
291 binding.enableHttpAuth.setChecked(isChecked);
292 binding.authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
293 checkInsecureSchemeWithAuth();
296 model.observeUserName(viewLifecycleOwner, text -> {
297 if (!Misc.equalStrings(text, Misc.nullIsEmpty(binding.authUserName.getText())))
298 binding.authUserName.setText(text);
300 hookTextChangeSyncRoutine(binding.authUserName, model::setAuthUserName);
302 model.observePassword(viewLifecycleOwner, text -> {
303 if (!Misc.equalStrings(text, Misc.nullIsEmpty(binding.password.getText())))
304 binding.password.setText(text);
306 hookTextChangeSyncRoutine(binding.password, model::setAuthPassword);
308 model.observeThemeId(viewLifecycleOwner, themeId -> {
309 final int hue = (themeId == -1) ? Colors.DEFAULT_HUE_DEG : themeId;
310 final int profileColor = Colors.getPrimaryColorForHue(hue);
311 binding.btnPickRingColor.setBackgroundColor(profileColor);
312 binding.btnPickRingColor.setTag(hue);
315 model.observePreferredAccountsFilter(viewLifecycleOwner, text -> {
316 if (!Misc.equalStrings(text,
317 Misc.nullIsEmpty(binding.preferredAccountsFilter.getText())))
318 binding.preferredAccountsFilter.setText(text);
320 hookTextChangeSyncRoutine(binding.preferredAccountsFilter,
321 model::setPreferredAccountsFilter);
323 hookClearErrorOnFocusListener(binding.profileName, binding.profileNameLayout);
324 hookClearErrorOnFocusListener(binding.url, binding.urlLayout);
325 hookClearErrorOnFocusListener(binding.authUserName, binding.authUserNameLayout);
326 hookClearErrorOnFocusListener(binding.password, binding.passwordLayout);
328 if (savedInstanceState == null) {
329 model.setValuesFromProfile(mProfile, getArguments().getInt(ARG_HUE, -1));
331 checkInsecureSchemeWithAuth();
333 binding.url.addTextChangedListener(new TextWatcher() {
335 public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
337 public void onTextChanged(CharSequence s, int start, int before, int count) {}
339 public void afterTextChanged(Editable s) {
340 checkInsecureSchemeWithAuth();
344 binding.btnPickRingColor.setOnClickListener(v -> {
345 HueRingDialog d = new HueRingDialog(ProfileDetailFragment.this.requireContext(),
346 model.initialThemeHue, (Integer) v.getTag());
348 d.setColorSelectedListener(model::setThemeId);
351 binding.profileName.requestFocus();
353 private void chooseAPIVersion(View v) {
354 Activity context = getActivity();
355 ProfileDetailModel model = getModel();
356 MenuInflater mi = new MenuInflater(context);
357 PopupMenu menu = new PopupMenu(context, v);
358 menu.inflate(R.menu.api_version);
359 menu.setOnMenuItemClickListener(item -> {
361 int itemId = item.getItemId();
362 if (itemId == R.id.api_version_menu_html) {
365 else if (itemId == R.id.api_version_menu_1_19_1) {
366 apiVer = API.v1_19_1;
368 else if (itemId == R.id.api_version_menu_1_15) {
371 else if (itemId == R.id.api_version_menu_1_14) {
377 model.setApiVersion(apiVer);
378 binding.apiVersionText.setText(apiVer.getDescription(getResources()));
383 private MobileLedgerProfile.FutureDates futureDatesSettingFromMenuItemId(int itemId) {
384 if (itemId == R.id.menu_future_dates_7) {
385 return MobileLedgerProfile.FutureDates.OneWeek;
387 else if (itemId == R.id.menu_future_dates_14) {
388 return MobileLedgerProfile.FutureDates.TwoWeeks;
390 else if (itemId == R.id.menu_future_dates_30) {
391 return MobileLedgerProfile.FutureDates.OneMonth;
393 else if (itemId == R.id.menu_future_dates_60) {
394 return MobileLedgerProfile.FutureDates.TwoMonths;
396 else if (itemId == R.id.menu_future_dates_90) {
397 return MobileLedgerProfile.FutureDates.ThreeMonths;
399 else if (itemId == R.id.menu_future_dates_180) {
400 return MobileLedgerProfile.FutureDates.SixMonths;
402 else if (itemId == R.id.menu_future_dates_365) {
403 return MobileLedgerProfile.FutureDates.OneYear;
405 else if (itemId == R.id.menu_future_dates_all) {
406 return MobileLedgerProfile.FutureDates.All;
408 return MobileLedgerProfile.FutureDates.None;
411 private ProfileDetailModel getModel() {
412 return new ViewModelProvider(requireActivity()).get(ProfileDetailModel.class);
414 private void onSaveFabClicked() {
415 if (!checkValidity())
418 ProfileDetailModel model = getModel();
419 final ArrayList<MobileLedgerProfile> profiles =
420 Objects.requireNonNull(Data.profiles.getValue());
422 if (mProfile != null) {
423 int pos = Data.profiles.getValue()
425 mProfile = new MobileLedgerProfile(mProfile);
426 model.updateProfile(mProfile);
427 mProfile.storeInDB();
428 debug("profiles", "profile stored in DB");
429 profiles.set(pos, mProfile);
430 // debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue()));
432 final MobileLedgerProfile currentProfile = Data.getProfile();
433 if (mProfile.getId() == currentProfile.getId()) {
434 Data.setCurrentProfile(mProfile);
437 ProfilesRecyclerViewAdapter viewAdapter = ProfilesRecyclerViewAdapter.getInstance();
438 if (viewAdapter != null)
439 viewAdapter.notifyItemChanged(pos);
442 mProfile = new MobileLedgerProfile(0);
443 model.updateProfile(mProfile);
444 mProfile.storeInDB();
445 final ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
446 newList.add(mProfile);
447 Data.profiles.setValue(newList);
448 MobileLedgerProfile.storeProfilesOrder();
450 // first profile ever?
451 if (newList.size() == 1)
452 Data.setCurrentProfile(mProfile);
455 Activity activity = getActivity();
456 if (activity != null)
459 private boolean checkUrlValidity() {
460 boolean valid = true;
462 ProfileDetailModel model = getModel();
464 String val = model.getUrl()
468 binding.urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
471 URL url = new URL(val);
472 String host = url.getHost();
473 if (host == null || host.isEmpty())
474 throw new MalformedURLException("Missing host");
475 String protocol = url.getProtocol()
477 if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) {
479 binding.urlLayout.setError(getResources().getText(R.string.err_invalid_url));
482 catch (MalformedURLException e) {
484 binding.urlLayout.setError(getResources().getText(R.string.err_invalid_url));
489 private void checkInsecureSchemeWithAuth() {
490 boolean showWarning = false;
492 final ProfileDetailModel model = getModel();
494 if (model.getUseAuthentication()) {
495 String urlText = model.getUrl();
496 if (urlText.startsWith("http://") ||
497 urlText.length() >= 8 && !urlText.startsWith("https://"))
502 binding.insecureSchemeText.setVisibility(View.VISIBLE);
504 binding.insecureSchemeText.setVisibility(View.GONE);
506 private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
507 view.setOnFocusChangeListener((v, hasFocus) -> {
509 layout.setError(null);
511 view.addTextChangedListener(new TextWatcher() {
513 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
516 public void onTextChanged(CharSequence s, int start, int before, int count) {
517 layout.setError(null);
520 public void afterTextChanged(Editable s) {
524 private void syncModelFromUI() {
525 if (syncingModelFromUI)
528 syncingModelFromUI = true;
531 ProfileDetailModel model = getModel();
533 model.setProfileName(binding.profileName.getText());
534 model.setUrl(binding.url.getText());
535 model.setPreferredAccountsFilter(binding.preferredAccountsFilter.getText());
536 model.setAuthUserName(binding.authUserName.getText());
537 model.setAuthPassword(binding.password.getText());
540 syncingModelFromUI = false;
543 private boolean checkValidity() {
544 boolean valid = true;
546 String val = String.valueOf(binding.profileName.getText());
551 binding.profileNameLayout.setError(
552 getResources().getText(R.string.err_profile_name_empty));
555 if (!checkUrlValidity())
558 if (binding.enableHttpAuth.isChecked()) {
559 val = String.valueOf(binding.authUserName.getText());
564 binding.authUserNameLayout.setError(
565 getResources().getText(R.string.err_profile_user_name_empty));
568 val = String.valueOf(binding.password.getText());
573 binding.passwordLayout.setError(
574 getResources().getText(R.string.err_profile_password_empty));
580 private void resetDefaultCommodity() {
581 defaultCommoditySet = false;
582 binding.defaultCommodityText.setText(R.string.btn_no_currency);
583 binding.defaultCommodityText.setTypeface(binding.defaultCommodityText.getTypeface(),
586 private void setDefaultCommodity(@NonNull @NotNull String name) {
587 defaultCommoditySet = true;
588 binding.defaultCommodityText.setText(name);
589 binding.defaultCommodityText.setTypeface(Typeface.DEFAULT);
591 interface TextChangeSyncRoutine {
592 void onTextChanged(String text);