From dd6c7c07e0269e6f5aa51c351ed96d8edb99e6fd Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 24 Apr 2020 16:07:42 +0300 Subject: [PATCH] Refactor --- .../layout/purchase_dialog_card_button_ex.xml | 32 ++++++++ .../chooseplan/ChoosePlanDialogFragment.java | 80 +++++++++---------- .../net/osmand/plus/inapp/InAppPurchases.java | 8 +- 3 files changed, 76 insertions(+), 44 deletions(-) diff --git a/OsmAnd/res/layout/purchase_dialog_card_button_ex.xml b/OsmAnd/res/layout/purchase_dialog_card_button_ex.xml index 2d261055dd..a0dd5f8f8e 100644 --- a/OsmAnd/res/layout/purchase_dialog_card_button_ex.xml +++ b/OsmAnd/res/layout/purchase_dialog_card_button_ex.xml @@ -92,6 +92,38 @@ + + + + + + + + + visibleSubscriptions = purchaseHelper.getLiveUpdates().getVisibleSubscriptions(); - InAppSubscription subscriptionMaxDiscount = null; + InAppSubscription maxDiscountSubscription = null; double maxDiscount = 0; boolean anyPurchased = false; for (final InAppSubscription s : visibleSubscriptions) { @@ -364,12 +362,12 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment } double discount = s.getDiscountPercent(purchaseHelper.getMonthlyLiveUpdates()); if (discount > maxDiscount) { - subscriptionMaxDiscount = s; + maxDiscountSubscription = s; maxDiscount = discount; } } - boolean maxDiscountAction = subscriptionMaxDiscount != null - && (subscriptionMaxDiscount.getIntroductoryInfo() != null || subscriptionMaxDiscount.isUpgrade()); + boolean maxDiscountAction = maxDiscountSubscription != null + && (maxDiscountSubscription.getIntroductoryInfo() != null || maxDiscountSubscription.isUpgrade()); for (final InAppSubscription s : visibleSubscriptions) { InAppSubscriptionIntroductoryInfo introductoryInfo = s.getIntroductoryInfo(); boolean hasIntroductoryInfo = introductoryInfo != null; @@ -389,7 +387,11 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment CharSequence priceTitle = hasIntroductoryInfo ? introductoryInfo.getFormattedDescription(ctx, buttonTitle.getCurrentTextColor()) : s.getPriceWithPeriod(ctx); title.setText(s.getTitle(ctx)); - description.setText(descriptionText); + if (Algorithms.isEmpty(descriptionText.toString())) { + description.setVisibility(View.GONE); + } else { + description.setText(descriptionText); + } buttonTitle.setText(priceTitle); buttonView.setVisibility(View.VISIBLE); buttonCancelView.setVisibility(View.GONE); @@ -437,20 +439,32 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment TextViewEx description = (TextViewEx) button.findViewById(R.id.description); TextViewEx descriptionContribute = (TextViewEx) button.findViewById(R.id.description_contribute); descriptionContribute.setVisibility(s.isDonationSupported() ? View.VISIBLE : View.GONE); + + View buttonView = button.findViewById(R.id.button_view); View buttonExView = button.findViewById(R.id.button_ex_view); + TextViewEx buttonTitle = (TextViewEx) button.findViewById(R.id.button_title); TextViewEx buttonExTitle = (TextViewEx) button.findViewById(R.id.button_ex_title); - if (maxDiscountAction && s.equals(subscriptionMaxDiscount)) { - createSolidButton(ctx, buttonExView, buttonExTitle); + + boolean showSolidButton = !anyPurchased + && (!maxDiscountAction || hasIntroductoryInfo || maxDiscountSubscription.isUpgrade()); + if (!showSolidButton && hasIntroductoryInfo) { + buttonExTitle.setTextColor(buttonTitle.getCurrentTextColor()); } + buttonView.setVisibility(!showSolidButton ? View.VISIBLE : View.GONE); + buttonExView.setVisibility(showSolidButton ? View.VISIBLE : View.GONE); + View div = button.findViewById(R.id.div); + CharSequence priceTitle = hasIntroductoryInfo ? introductoryInfo.getFormattedDescription(ctx, buttonExTitle.getCurrentTextColor()) : s.getPriceWithPeriod(ctx); - buttonExTitle.setText(priceTitle); title.setText(s.getTitle(ctx)); if (Algorithms.isEmpty(descriptionText.toString())) { description.setVisibility(View.GONE); } else { description.setText(descriptionText); } + buttonTitle.setText(priceTitle); + buttonExTitle.setText(priceTitle); + TextViewEx buttonDiscountTitle = (TextViewEx) button.findViewById(R.id.button_discount_title); View buttonDiscountView = button.findViewById(R.id.button_discount_view); String discountTitle = s.getDiscountTitle(ctx, purchaseHelper.getMonthlyLiveUpdates()); @@ -458,7 +472,7 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment buttonDiscountTitle.setText(discountTitle); buttonDiscountView.setVisibility(View.VISIBLE); } - if (s.equals(subscriptionMaxDiscount)) { + if (s.equals(maxDiscountSubscription) || maxDiscountSubscription != null && maxDiscountSubscription.isUpgrade()) { int saveTextColor = R.color.color_osm_edit_delete; if (hasIntroductoryInfo) { saveTextColor = R.color.active_buttons_and_links_text_light; @@ -466,21 +480,22 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment ContextCompat.getColor(ctx, R.color.color_osm_edit_delete))); } buttonDiscountTitle.setTextColor(ContextCompat.getColor(ctx, saveTextColor)); + } + if (!showSolidButton) { + buttonView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + subscribe(s.getSku()); + } + }); } else { - if (maxDiscountAction) { - createOutlineButton(ctx, buttonExView, buttonExTitle); - } + buttonExView.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + subscribe(s.getSku()); + } + }); } - if (anyPurchased) { - createOutlineButton(ctx, buttonExView, buttonExTitle); - } - buttonExView.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - subscribe(s.getSku()); - } - }); - View div = button.findViewById(R.id.div); div.setVisibility(View.VISIBLE); osmLiveCardButtonsContainer.addView(button); lastBtn = button; @@ -499,23 +514,6 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment } } - private void createSolidButton(Context ctx, View buttonExView, TextViewEx buttonExTitle) { - Resources.Theme theme = ctx.getTheme(); - TypedValue typedValue = new TypedValue(); - theme.resolveAttribute(R.attr.wikivoyage_primary_btn_bg, typedValue, true); - buttonExView.setBackgroundResource(typedValue.resourceId); - buttonExTitle.setTextColor(ContextCompat.getColor(ctx, R.color.active_buttons_and_links_text_light)); - } - - private void createOutlineButton(Context ctx, View buttonExView, TextViewEx buttonExTitle) { - Resources.Theme theme = ctx.getTheme(); - TypedValue typedValue = new TypedValue(); - theme.resolveAttribute(R.attr.purchase_dialog_outline_btn_bg, typedValue, true); - buttonExView.setBackgroundResource(typedValue.resourceId); - theme.resolveAttribute(R.attr.color_dialog_buttons, typedValue, true); - buttonExTitle.setTextColor(ContextCompat.getColor(ctx, typedValue.resourceId)); - } - private void showDonationSettings() { FragmentActivity activity = getActivity(); if (activity != null) { diff --git a/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java b/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java index 5fb2e0baa3..d3c790966f 100644 --- a/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java +++ b/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java @@ -732,9 +732,6 @@ public class InAppPurchases { @Override public CharSequence getDescription(@NonNull Context ctx) { - if (getMonthlyPriceValue() == getPriceValue()) { - return ""; - } if (getMonthlyPriceValue() == 0) { return ctx.getString(R.string.osm_live_payment_month_cost_descr, getDefaultMonthlyPrice(ctx)); } else { @@ -901,6 +898,11 @@ public class InAppPurchases { public CharSequence getRenewDescription(@NonNull Context ctx) { return ctx.getString(R.string.osm_live_payment_renews_monthly); } + + @Override + public CharSequence getDescription(@NonNull Context ctx) { + return ""; + } } public static class InAppPurchaseLiveUpdatesMonthlyFull extends InAppPurchaseLiveUpdatesMonthly {