+ hookTextChangeSyncRoutine(binding.profileName, model::setProfileName);
+ model.observeProfileName(viewLifecycleOwner, pn -> {
+ if (!Misc.equalStrings(pn, Misc.nullIsEmpty(binding.profileName.getText())))
+ binding.profileName.setText(pn);
+ });
+
+ hookTextChangeSyncRoutine(binding.url, model::setUrl);
+ model.observeUrl(viewLifecycleOwner, u -> {
+ if (!Misc.equalStrings(u, Misc.nullIsEmpty(binding.url.getText())))
+ binding.url.setText(u);
+ });
+
+ binding.defaultCommodityLayout.setOnClickListener(v -> {
+ CurrencySelectorFragment cpf = CurrencySelectorFragment.newInstance(
+ CurrencySelectorFragment.DEFAULT_COLUMN_COUNT, false);
+ cpf.setOnCurrencySelectedListener(model::setDefaultCommodity);
+ final AppCompatActivity activity = (AppCompatActivity) v.getContext();
+ cpf.show(activity.getSupportFragmentManager(), "currency-selector");
+ });
+
+ binding.profileShowCommodity.setOnCheckedChangeListener(
+ (buttonView, isChecked) -> model.setShowCommodityByDefault(isChecked));
+ model.observeShowCommodityByDefault(viewLifecycleOwner,
+ binding.profileShowCommodity::setChecked);
+
+ model.observePostingPermitted(viewLifecycleOwner, isChecked -> {
+ binding.profilePermitPosting.setChecked(isChecked);
+ binding.postingSubItems.setVisibility(isChecked ? View.VISIBLE : View.GONE);
+ });
+ binding.profilePermitPosting.setOnCheckedChangeListener(
+ ((buttonView, isChecked) -> model.setPostingPermitted(isChecked)));
+
+ model.observeShowCommentsByDefault(viewLifecycleOwner,
+ binding.profileShowComments::setChecked);
+ binding.profileShowComments.setOnCheckedChangeListener(
+ ((buttonView, isChecked) -> model.setShowCommentsByDefault(isChecked)));
+
+ binding.futureDatesLayout.setOnClickListener(v -> {
+ MenuInflater mi = new MenuInflater(context);
+ PopupMenu menu = new PopupMenu(context, v);
+ menu.inflate(R.menu.future_dates);
+ menu.setOnMenuItemClickListener(item -> {
+ model.setFutureDates(futureDatesSettingFromMenuItemId(item.getItemId()));
+ return true;
+ });
+ menu.show();
+ });
+ model.observeFutureDates(viewLifecycleOwner,
+ v -> binding.futureDatesText.setText(v.getText(getResources())));
+
+ model.observeApiVersion(viewLifecycleOwner,
+ apiVer -> binding.apiVersionText.setText(apiVer.getDescription(getResources())));
+ binding.apiVersionLabel.setOnClickListener(this::chooseAPIVersion);
+ binding.apiVersionText.setOnClickListener(this::chooseAPIVersion);
+
+ binding.serverVersionLabel.setOnClickListener(v -> model.triggerVersionDetection());
+ model.observeDetectedVersion(viewLifecycleOwner, ver -> {
+ if (ver == null)
+ binding.detectedServerVersionText.setText(context.getResources()
+ .getString(
+ R.string.server_version_unknown_label));
+ else if (ver.isPre_1_20_1())
+ binding.detectedServerVersionText.setText(context.getResources()
+ .getString(
+ R.string.detected_server_pre_1_20_1));
+ else
+ binding.detectedServerVersionText.setText(ver.toString());
+ });
+ binding.detectedServerVersionText.setOnClickListener(v -> model.triggerVersionDetection());
+ binding.serverVersionDetectButton.setOnClickListener(v -> model.triggerVersionDetection());
+ model.observeDetectingHledgerVersion(viewLifecycleOwner,
+ running -> binding.serverVersionDetectButton.setVisibility(
+ running ? View.VISIBLE : View.INVISIBLE));
+
+ binding.enableHttpAuth.setOnCheckedChangeListener((buttonView, isChecked) -> {
+ boolean wasOn = model.getUseAuthentication();
+ model.setUseAuthentication(isChecked);
+ if (!wasOn && isChecked)
+ binding.authUserName.requestFocus();
+ });
+ model.observeUseAuthentication(viewLifecycleOwner, isChecked -> {
+ binding.enableHttpAuth.setChecked(isChecked);
+ binding.authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
+ checkInsecureSchemeWithAuth();
+ });
+
+ model.observeUserName(viewLifecycleOwner, text -> {
+ if (!Misc.equalStrings(text, Misc.nullIsEmpty(binding.authUserName.getText())))
+ binding.authUserName.setText(text);
+ });
+ hookTextChangeSyncRoutine(binding.authUserName, model::setAuthUserName);
+
+ model.observePassword(viewLifecycleOwner, text -> {
+ if (!Misc.equalStrings(text, Misc.nullIsEmpty(binding.password.getText())))
+ binding.password.setText(text);
+ });
+ hookTextChangeSyncRoutine(binding.password, model::setAuthPassword);
+
+ model.observeThemeId(viewLifecycleOwner, themeId -> {
+ final int hue = (themeId == -1) ? Colors.DEFAULT_HUE_DEG : themeId;
+ final int profileColor = Colors.getPrimaryColorForHue(hue);
+ binding.btnPickRingColor.setBackgroundColor(profileColor);
+ binding.btnPickRingColor.setTag(hue);
+ });
+
+ model.observePreferredAccountsFilter(viewLifecycleOwner, text -> {
+ if (!Misc.equalStrings(text,
+ Misc.nullIsEmpty(binding.preferredAccountsFilter.getText())))
+ binding.preferredAccountsFilter.setText(text);
+ });
+ hookTextChangeSyncRoutine(binding.preferredAccountsFilter,
+ model::setPreferredAccountsFilter);
+
+ hookClearErrorOnFocusListener(binding.profileName, binding.profileNameLayout);
+ hookClearErrorOnFocusListener(binding.url, binding.urlLayout);
+ hookClearErrorOnFocusListener(binding.authUserName, binding.authUserNameLayout);
+ hookClearErrorOnFocusListener(binding.password, binding.passwordLayout);
+
+ binding.url.addTextChangedListener(new TextWatcher() {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int count) {}
+ @Override
+ public void afterTextChanged(Editable s) {
+ checkInsecureSchemeWithAuth();
+ }
+ });
+
+ binding.btnPickRingColor.setOnClickListener(v -> {
+ HueRingDialog d = new HueRingDialog(ProfileDetailFragment.this.requireContext(),
+ model.initialThemeHue, (Integer) v.getTag());
+ d.show();
+ d.setColorSelectedListener(model::setThemeId);
+ });
+
+ binding.profileName.requestFocus();
+ }
+ private void chooseAPIVersion(View v) {
+ Activity context = getActivity();
+ ProfileDetailModel model = getModel();
+ MenuInflater mi = new MenuInflater(context);
+ PopupMenu menu = new PopupMenu(context, v);
+ menu.inflate(R.menu.api_version);
+ menu.setOnMenuItemClickListener(item -> {
+ API apiVer;
+ int itemId = item.getItemId();
+ if (itemId == R.id.api_version_menu_html) {
+ apiVer = API.html;
+ }
+ else if (itemId == R.id.api_version_menu_1_23) {
+ apiVer = API.v1_23;
+ }
+ else if (itemId == R.id.api_version_menu_1_19_1) {
+ apiVer = API.v1_19_1;
+ }
+ else if (itemId == R.id.api_version_menu_1_15) {
+ apiVer = API.v1_15;
+ }
+ else if (itemId == R.id.api_version_menu_1_14) {
+ apiVer = API.v1_14;
+ }
+ else {
+ apiVer = API.auto;
+ }
+ model.setApiVersion(apiVer);
+ binding.apiVersionText.setText(apiVer.getDescription(getResources()));
+ return true;
+ });
+ menu.show();
+ }
+ private FutureDates futureDatesSettingFromMenuItemId(int itemId) {
+ if (itemId == R.id.menu_future_dates_7) {
+ return FutureDates.OneWeek;
+ }
+ else if (itemId == R.id.menu_future_dates_14) {
+ return FutureDates.TwoWeeks;
+ }
+ else if (itemId == R.id.menu_future_dates_30) {
+ return FutureDates.OneMonth;
+ }
+ else if (itemId == R.id.menu_future_dates_60) {
+ return FutureDates.TwoMonths;
+ }
+ else if (itemId == R.id.menu_future_dates_90) {
+ return FutureDates.ThreeMonths;
+ }
+ else if (itemId == R.id.menu_future_dates_180) {
+ return FutureDates.SixMonths;
+ }
+ else if (itemId == R.id.menu_future_dates_365) {
+ return FutureDates.OneYear;
+ }
+ else if (itemId == R.id.menu_future_dates_all) {
+ return FutureDates.All;
+ }
+ return FutureDates.None;
+ }
+ @NotNull
+ private ProfileDetailModel getModel() {
+ return new ViewModelProvider(requireActivity()).get(ProfileDetailModel.class);