X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfileDetailFragment.java;h=6eb5239d95cbb36ce8a41a63b2323db2a2e64220;hb=bb789332571609eeb1bef6e39b7ad359227d1045;hp=fafd8b41123b1014cb8b05cc4143de16edd55aff;hpb=55dffdf66e7a8dc32b00df4cfd0c9d399c17aaaa;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index fafd8b41..6eb5239d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -29,7 +29,6 @@ import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import android.widget.PopupMenu; -import android.widget.Switch; import android.widget.TextView; import androidx.annotation.NonNull; @@ -42,11 +41,12 @@ import androidx.lifecycle.ViewModelProvider; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.google.android.material.switchmaterial.SwitchMaterial; import com.google.android.material.textfield.TextInputLayout; import net.ktnx.mobileledger.BuildConfig; import net.ktnx.mobileledger.R; -import net.ktnx.mobileledger.async.SendTransactionTask; +import net.ktnx.mobileledger.json.API; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.CurrencySelectorFragment; @@ -86,7 +86,7 @@ public class ProfileDetailFragment extends Fragment { private boolean defaultCommoditySet; private TextInputLayout urlLayout; private LinearLayout authParams; - private Switch useAuthentication; + private SwitchMaterial useAuthentication; private TextView userName; private TextInputLayout userNameLayout; private TextView password; @@ -112,32 +112,7 @@ public class ProfileDetailFragment extends Fragment { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.profile_details, menu); final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete); - menuDeleteProfile.setOnMenuItemClickListener(item -> { - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(mProfile.getName()); - builder.setMessage(R.string.remove_profile_dialog_message); - builder.setPositiveButton(R.string.Remove, (dialog, which) -> { - debug("profiles", - String.format("[fragment] removing profile %s", mProfile.getUuid())); - mProfile.removeFromDB(); - ArrayList oldList = Data.profiles.getValue(); - if (oldList == null) - throw new AssertionError(); - ArrayList newList = new ArrayList<>(oldList); - newList.remove(mProfile); - Data.profiles.setValue(newList); - if (mProfile.equals(Data.getProfile())) { - debug("profiles", "[fragment] setting current profile to 0"); - Data.setCurrentProfile(newList.get(0)); - } - - final FragmentActivity activity = getActivity(); - if (activity != null) - activity.finish(); - }); - builder.show(); - return false; - }); + menuDeleteProfile.setOnMenuItemClickListener(item -> onDeleteProfile()); final ArrayList profiles = Data.profiles.getValue(); if (BuildConfig.DEBUG) { @@ -146,6 +121,31 @@ public class ProfileDetailFragment extends Fragment { menuWipeProfileData.setVisible(mProfile != null); } } + private boolean onDeleteProfile() { + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(mProfile.getName()); + builder.setMessage(R.string.remove_profile_dialog_message); + builder.setPositiveButton(R.string.Remove, (dialog, which) -> { + debug("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid())); + mProfile.removeFromDB(); + ArrayList oldList = Data.profiles.getValue(); + if (oldList == null) + throw new AssertionError(); + ArrayList newList = new ArrayList<>(oldList); + newList.remove(mProfile); + Data.profiles.setValue(newList); + if (mProfile.equals(Data.getProfile())) { + debug("profiles", "[fragment] setting current profile to 0"); + Data.setCurrentProfile(newList.get(0)); + } + + final FragmentActivity activity = getActivity(); + if (activity != null) + activity.finish(); + }); + builder.show(); + return false; + } private boolean onWipeDataMenuClicked() { // this is a development option, so no confirmation mProfile.wipeAllData(); @@ -178,8 +178,8 @@ public class ProfileDetailFragment extends Fragment { }); } @Override - public void onActivityCreated(@Nullable Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); + public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); Activity context = getActivity(); if (context == null) return; @@ -242,14 +242,14 @@ public class ProfileDetailFragment extends Fragment { cpf.show(activity.getSupportFragmentManager(), "currency-selector"); }); - Switch showCommodityByDefault = context.findViewById(R.id.profile_show_commodity); + SwitchMaterial showCommodityByDefault = context.findViewById(R.id.profile_show_commodity); showCommodityByDefault.setOnCheckedChangeListener( (buttonView, isChecked) -> model.setShowCommodityByDefault(isChecked)); model.observeShowCommodityByDefault(viewLifecycleOwner, showCommodityByDefault::setChecked); View postingSubItems = context.findViewById(R.id.posting_sub_items); - Switch postingPermitted = context.findViewById(R.id.profile_permit_posting); + SwitchMaterial postingPermitted = context.findViewById(R.id.profile_permit_posting); model.observePostingPermitted(viewLifecycleOwner, isChecked -> { postingPermitted.setChecked(isChecked); postingSubItems.setVisibility(isChecked ? View.VISIBLE : View.GONE); @@ -257,7 +257,7 @@ public class ProfileDetailFragment extends Fragment { postingPermitted.setOnCheckedChangeListener( ((buttonView, isChecked) -> model.setPostingPermitted(isChecked))); - Switch showCommentsByDefault = context.findViewById(R.id.profile_show_comments); + SwitchMaterial showCommentsByDefault = context.findViewById(R.id.profile_show_comments); model.observeShowCommentsByDefault(viewLifecycleOwner, showCommentsByDefault::setChecked); showCommentsByDefault.setOnCheckedChangeListener( ((buttonView, isChecked) -> model.setShowCommentsByDefault(isChecked))); @@ -282,33 +282,33 @@ public class ProfileDetailFragment extends Fragment { apiVersionText = context.findViewById(R.id.api_version_text); model.observeApiVersion(viewLifecycleOwner, apiVer -> apiVersionText.setText(apiVer.getDescription(getResources()))); - context.findViewById(R.id.api_version_layout) - .setOnClickListener(v -> { - MenuInflater mi = new MenuInflater(context); - PopupMenu menu = new PopupMenu(context, v); - menu.inflate(R.menu.api_version); - menu.setOnMenuItemClickListener(item -> { - SendTransactionTask.API apiVer; - switch (item.getItemId()) { - case R.id.api_version_menu_html: - apiVer = SendTransactionTask.API.html; - break; - case R.id.api_version_menu_post_1_14: - apiVer = SendTransactionTask.API.post_1_14; - break; - case R.id.api_version_menu_pre_1_15: - apiVer = SendTransactionTask.API.pre_1_15; - break; - case R.id.api_version_menu_auto: - default: - apiVer = SendTransactionTask.API.auto; - } - model.setApiVersion(apiVer); - apiVersionText.setText(apiVer.getDescription(getResources())); - return true; - }); - menu.show(); - }); + context.findViewById(R.id.api_version_label) + .setOnClickListener(this::chooseAPIVersion); + context.findViewById(R.id.api_version_text) + .setOnClickListener(this::chooseAPIVersion); + + context.findViewById(R.id.server_version_label) + .setOnClickListener(v -> model.triggerVersionDetection()); + TextView detectedServerVersion = context.findViewById(R.id.detected_server_version_text); + model.observeDetectedVersion(viewLifecycleOwner, ver -> { + if (ver == null) + detectedServerVersion.setText(context.getResources() + .getString( + R.string.server_version_unknown_label)); + else if (ver.isPre_1_20_1()) + detectedServerVersion.setText(context.getResources() + .getString( + R.string.detected_server_pre_1_20_1)); + else + detectedServerVersion.setText(ver.toString()); + }); + detectedServerVersion.setOnClickListener(v -> model.triggerVersionDetection()); + final View detectButton = context.findViewById(R.id.server_version_detect_button); + detectButton.setOnClickListener(v -> model.triggerVersionDetection()); + model.observeDetectingHledgerVersion(viewLifecycleOwner, running -> { + detectButton.setVisibility(running ? View.VISIBLE : View.INVISIBLE); + }); + authParams = context.findViewById(R.id.auth_params); useAuthentication = context.findViewById(R.id.enable_http_auth); @@ -386,6 +386,37 @@ public class ProfileDetailFragment extends Fragment { 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; + switch (item.getItemId()) { + case R.id.api_version_menu_html: + apiVer = API.html; + break; + case R.id.api_version_menu_1_19_1: + apiVer = API.v1_19_1; + break; + case R.id.api_version_menu_1_15: + apiVer = API.v1_15; + break; + case R.id.api_version_menu_1_14: + apiVer = API.v1_14; + break; + case R.id.api_version_menu_auto: + default: + apiVer = API.auto; + } + model.setApiVersion(apiVer); + apiVersionText.setText(apiVer.getDescription(getResources())); + return true; + }); + menu.show(); + } private MobileLedgerProfile.FutureDates futureDatesSettingFromMenuItemId(int itemId) { switch (itemId) { case R.id.menu_future_dates_7: @@ -417,22 +448,35 @@ public class ProfileDetailFragment extends Fragment { return; ProfileDetailModel model = getModel(); + final ArrayList profiles = + Objects.requireNonNull(Data.profiles.getValue()); if (mProfile != null) { + int pos = Data.profiles.getValue() + .indexOf(mProfile); + mProfile = new MobileLedgerProfile(mProfile); model.updateProfile(mProfile); -// debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue())); mProfile.storeInDB(); debug("profiles", "profile stored in DB"); - triggerProfileChange(); + profiles.set(pos, mProfile); +// debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue())); + + final MobileLedgerProfile currentProfile = Data.getProfile(); + if (mProfile.getUuid() + .equals(currentProfile.getUuid())) + { + Data.setCurrentProfile(mProfile); + } + + ProfilesRecyclerViewAdapter viewAdapter = ProfilesRecyclerViewAdapter.getInstance(); + if (viewAdapter != null) + viewAdapter.notifyItemChanged(pos); } else { mProfile = new MobileLedgerProfile(String.valueOf(UUID.randomUUID())); model.updateProfile(mProfile); mProfile.storeInDB(); - final ArrayList profiles = Data.profiles.getValue(); - if (profiles == null) - throw new AssertionError(); - ArrayList newList = new ArrayList<>(profiles); + final ArrayList newList = new ArrayList<>(profiles); newList.add(mProfile); Data.profiles.setValue(newList); MobileLedgerProfile.storeProfilesOrder();