[android] Implemented subscription fragment delegate interface to separate and geralized ui logic between three different subscription screens

This commit is contained in:
Александр Зацепин 2019-11-13 19:40:46 +03:00 committed by yoksnod
parent ed8bdc034a
commit 718341da01
7 changed files with 394 additions and 380 deletions

View file

@ -22,7 +22,6 @@ import com.mapswithme.maps.dialog.AlertDialogCallback;
import com.mapswithme.maps.dialog.ResolveFragmentManagerStrategy;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.statistics.Statistics;
@ -55,6 +54,9 @@ abstract class AbstractBookmarkSubscriptionFragment extends BaseAuthFragment
@SuppressWarnings("NullableProblems")
@NonNull
private PurchaseController<PurchaseCallback> mPurchaseController;
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionFragmentDelegate mDelegate;
@Nullable
@Override
@ -73,16 +75,9 @@ abstract class AbstractBookmarkSubscriptionFragment extends BaseAuthFragment
getSubscriptionType().getYearlyProductId(),
getExtraFrom());
View root = onSubscriptionCreateView(inflater, container, savedInstanceState);
mDelegate = createFragmentDelegate(this);
if (root != null)
{
View restoreButton = root.findViewById(R.id.restore_purchase_btn);
restoreButton.setOnClickListener(v -> openSubscriptionManagementSettings());
View termsOfUse = root.findViewById(R.id.term_of_use_link);
termsOfUse.setOnClickListener(v -> openTermsOfUseLink());
View privacyPolicy = root.findViewById(R.id.privacy_policy_link);
privacyPolicy.setOnClickListener(v -> openPrivacyPolicyLink());
}
mDelegate.onCreateView(root);
return root;
}
@ -123,7 +118,7 @@ abstract class AbstractBookmarkSubscriptionFragment extends BaseAuthFragment
super.onDestroyView();
mPingCallback.detach();
BookmarkManager.INSTANCE.removeCatalogPingListener(mPingCallback);
onSubscriptionDestroyView();
mDelegate.onDestroyView();
}
@Override
@ -270,11 +265,25 @@ abstract class AbstractBookmarkSubscriptionFragment extends BaseAuthFragment
activateState(BookmarkSubscriptionPaymentState.PRODUCT_DETAILS_LOADING);
}
@Override
public void onReset()
{
mDelegate.onReset();
}
@Override
public void onPriceSelection()
{
mDelegate.onPriceSelection();
}
@Override
@CallSuper
public void onProductDetailsLoading()
{
queryProductDetails();
mDelegate.onProductDetailsLoading();
}
@Override
@ -353,35 +362,33 @@ abstract class AbstractBookmarkSubscriptionFragment extends BaseAuthFragment
@NonNull
abstract PurchaseController<PurchaseCallback> createPurchaseController();
@NonNull
abstract SubscriptionFragmentDelegate createFragmentDelegate(
@NonNull AbstractBookmarkSubscriptionFragment fragment);
@Nullable
abstract View onSubscriptionCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState);
private void openTermsOfUseLink()
{
Utils.openUrl(requireActivity(), Framework.nativeGetTermsOfUseLink());
}
private void openPrivacyPolicyLink()
{
Utils.openUrl(requireActivity(), Framework.nativeGetPrivacyPolicyLink());
}
abstract void onSubscriptionDestroyView();
@NonNull
abstract SubscriptionType getSubscriptionType();
abstract void hideButtonProgress();
@Override
public void onValidating()
{
showButtonProgress();
}
abstract void showButtonProgress();
private void showButtonProgress()
{
mDelegate.showButtonProgress();
}
private void hideButtonProgress()
{
mDelegate.hideButtonProgress();
}
@Override
public boolean onBackPressed()
@ -398,13 +405,9 @@ abstract class AbstractBookmarkSubscriptionFragment extends BaseAuthFragment
}
@NonNull
abstract PurchaseUtils.Period getSelectedPeriod();
private void openSubscriptionManagementSettings()
private PurchaseUtils.Period getSelectedPeriod()
{
Utils.openUrl(requireContext(), "https://play.google.com/store/account/subscriptions");
Statistics.INSTANCE.trackPurchaseEvent(Statistics.EventName.INAPP_PURCHASE_PREVIEW_RESTORE,
getSubscriptionType().getServerId());
return mDelegate.getSelectedPeriod();
}
void trackPayEvent()

View file

@ -4,48 +4,20 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
@SuppressWarnings("WeakerAccess")
public class BookmarkSubscriptionFragment extends AbstractBookmarkSubscriptionFragment
{
private static final int DEF_ELEVATION = 0;
@Nullable
@Override
View onSubscriptionCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
View root = inflater.inflate(R.layout.bookmark_subscription_fragment, container, false);
CardView annualPriceCard = root.findViewById(R.id.annual_price_card);
CardView monthlyPriceCard = root.findViewById(R.id.monthly_price_card);
AnnualCardClickListener annualCardListener = new AnnualCardClickListener(monthlyPriceCard,
annualPriceCard);
annualPriceCard.setOnClickListener(annualCardListener);
MonthlyCardClickListener monthlyCardListener = new MonthlyCardClickListener(monthlyPriceCard,
annualPriceCard);
monthlyPriceCard.setOnClickListener(monthlyCardListener);
View continueBtn = root.findViewById(R.id.continue_btn);
continueBtn.setOnClickListener(v -> onContinueButtonClicked());
annualPriceCard.setSelected(true);
monthlyPriceCard.setSelected(false);
annualPriceCard.setCardElevation(getResources().getDimension(R.dimen.margin_base_plus_quarter));
return root;
}
@Override
void onSubscriptionDestroyView()
{
// Do nothing by default.
return inflater.inflate(R.layout.bookmark_subscription_fragment, container, false);
}
@NonNull
@ -55,65 +27,6 @@ public class BookmarkSubscriptionFragment extends AbstractBookmarkSubscriptionFr
return SubscriptionType.BOOKMARKS_ALL;
}
private void onContinueButtonClicked()
{
pingBookmarkCatalog();
trackPayEvent();
}
private void updatePaymentButtons()
{
updateYearlyButton();
updateMonthlyButton();
}
private void updateYearlyButton()
{
ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
TextView priceView = getViewOrThrow().findViewById(R.id.annual_price);
priceView.setText(price);
TextView savingView = getViewOrThrow().findViewById(R.id.sale);
String text = getString(R.string.annual_save_component, calculateYearlySaving());
savingView.setText(text);
}
private void updateMonthlyButton()
{
ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
TextView priceView = getViewOrThrow().findViewById(R.id.monthly_price);
priceView.setText(price);
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
CardView annualCard = getViewOrThrow().findViewById(R.id.annual_price_card);
return annualCard.getCardElevation() > 0 ? PurchaseUtils.Period.P1Y : PurchaseUtils.Period.P1M;
}
@Override
public void onReset()
{
hideAllUi();
}
@Override
public void onProductDetailsLoading()
{
super.onProductDetailsLoading();
showRootScreenProgress();
}
@Override
public void onPriceSelection()
{
hideRootScreenProgress();
updatePaymentButtons();
}
@NonNull
@Override
PurchaseController<PurchaseCallback> createPurchaseController()
@ -121,94 +34,10 @@ public class BookmarkSubscriptionFragment extends AbstractBookmarkSubscriptionFr
return PurchaseFactory.createBookmarksAllSubscriptionController(requireContext());
}
@NonNull
@Override
void showButtonProgress()
SubscriptionFragmentDelegate createFragmentDelegate(@NonNull AbstractBookmarkSubscriptionFragment fragment)
{
UiUtils.hide(getViewOrThrow(), R.id.continue_btn);
UiUtils.show(getViewOrThrow(), R.id.progress);
}
@Override
void hideButtonProgress()
{
UiUtils.hide(getViewOrThrow(), R.id.progress);
UiUtils.show(getViewOrThrow(), R.id.continue_btn);
}
private void showRootScreenProgress()
{
UiUtils.show(getViewOrThrow(), R.id.root_screen_progress);
UiUtils.hide(getViewOrThrow(), R.id.content_view);
}
private void hideRootScreenProgress()
{
UiUtils.hide(getViewOrThrow(), R.id.root_screen_progress);
UiUtils.show(getViewOrThrow(), R.id.content_view);
}
private void hideAllUi()
{
UiUtils.hide(getViewOrThrow(), R.id.root_screen_progress, R.id.content_view);
}
private class AnnualCardClickListener implements View.OnClickListener
{
@NonNull
private final CardView mMonthlyPriceCard;
@NonNull
private final CardView mAnnualPriceCard;
AnnualCardClickListener(@NonNull CardView monthlyPriceCard,
@NonNull CardView annualPriceCard)
{
mMonthlyPriceCard = monthlyPriceCard;
mAnnualPriceCard = annualPriceCard;
}
@Override
public void onClick(View v)
{
mMonthlyPriceCard.setCardElevation(DEF_ELEVATION);
mAnnualPriceCard.setCardElevation(getResources().getDimension(R.dimen.margin_base_plus_quarter));
if (!mAnnualPriceCard.isSelected())
{
trackYearlyProductSelected();
}
mMonthlyPriceCard.setSelected(false);
mAnnualPriceCard.setSelected(true);
}
}
private class MonthlyCardClickListener implements View.OnClickListener
{
@NonNull
private final CardView mMonthlyPriceCard;
@NonNull
private final CardView mAnnualPriceCard;
MonthlyCardClickListener(@NonNull CardView monthlyPriceCard,
@NonNull CardView annualPriceCard)
{
mMonthlyPriceCard = monthlyPriceCard;
mAnnualPriceCard = annualPriceCard;
}
@Override
public void onClick(View v)
{
mMonthlyPriceCard.setCardElevation(getResources().getDimension(R.dimen.margin_base_plus_quarter));
mAnnualPriceCard.setCardElevation(DEF_ELEVATION);
if (!mMonthlyPriceCard.isSelected())
trackMonthlyProductSelected();
mMonthlyPriceCard.setSelected(true);
mAnnualPriceCard.setSelected(false);
}
return new TwoCardsSubscriptionFragmentDelegate(fragment);
}
}

View file

@ -21,12 +21,9 @@ import com.mapswithme.util.UiUtils;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("WeakerAccess")
public class BookmarksAllSubscriptionFragment extends AbstractBookmarkSubscriptionFragment
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionFragmentDelegate mDelegate;
@NonNull
@Override
PurchaseController<PurchaseCallback> createPurchaseController()
@ -34,6 +31,13 @@ public class BookmarksAllSubscriptionFragment extends AbstractBookmarkSubscripti
return PurchaseFactory.createBookmarksAllSubscriptionController(requireContext());
}
@NonNull
@Override
SubscriptionFragmentDelegate createFragmentDelegate(@NonNull AbstractBookmarkSubscriptionFragment fragment)
{
return new TwoButtonsSubscriptionFragmentDelegate(fragment);
}
@Nullable
@Override
View onSubscriptionCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@ -41,20 +45,12 @@ public class BookmarksAllSubscriptionFragment extends AbstractBookmarkSubscripti
{
View root = inflater.inflate(R.layout.pager_fragment_all_pass_subscription, container,
false);
mDelegate = new SubscriptionFragmentDelegate(this);
mDelegate.onSubscriptionCreateView(root);
setTopStatusBarOffset(root);
initViewPager(root);
return root;
}
@Override
void onSubscriptionDestroyView()
{
mDelegate.onSubscriptionDestroyView();
}
@NonNull
@Override
SubscriptionType getSubscriptionType()
@ -62,13 +58,6 @@ public class BookmarksAllSubscriptionFragment extends AbstractBookmarkSubscripti
return SubscriptionType.BOOKMARKS_ALL;
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
return mDelegate.getSelectedPeriod();
}
private void setTopStatusBarOffset(@NonNull View view)
{
View statusBarPlaceholder = view.findViewById(R.id.status_bar_placeholder);
@ -118,37 +107,6 @@ public class BookmarksAllSubscriptionFragment extends AbstractBookmarkSubscripti
return items;
}
@Override
public void onProductDetailsLoading()
{
super.onProductDetailsLoading();
mDelegate.onProductDetailsLoading();
}
@Override
public void onReset()
{
mDelegate.onReset();
}
@Override
public void onPriceSelection()
{
mDelegate.onPriceSelection();
}
@Override
void showButtonProgress()
{
mDelegate.showButtonProgress();
}
@Override
void hideButtonProgress()
{
mDelegate.hideButtonProgress();
}
private class ParallaxFragmentPagerAdapter extends FragmentPagerAdapter
{
@NonNull

View file

@ -9,12 +9,9 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.R;
@SuppressWarnings("WeakerAccess")
public class BookmarksSightsSubscriptionFragment extends AbstractBookmarkSubscriptionFragment
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionFragmentDelegate mDelegate;
@NonNull
@Override
PurchaseController<PurchaseCallback> createPurchaseController()
@ -22,24 +19,21 @@ public class BookmarksSightsSubscriptionFragment extends AbstractBookmarkSubscri
return PurchaseFactory.createBookmarksSightsSubscriptionController(requireContext());
}
@NonNull
@Override
SubscriptionFragmentDelegate createFragmentDelegate(@NonNull AbstractBookmarkSubscriptionFragment fragment)
{
return new TwoButtonsSubscriptionFragmentDelegate(fragment);
}
@Nullable
@Override
View onSubscriptionCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
{
View root = inflater.inflate(R.layout.fragment_sightseeing_subscription, container, false);
mDelegate = new SubscriptionFragmentDelegate(this);
mDelegate.onSubscriptionCreateView(root);
return root;
return inflater.inflate(R.layout.fragment_sightseeing_subscription, container, false);
}
@Override
void onSubscriptionDestroyView()
{
mDelegate.onSubscriptionDestroyView();
}
@NonNull
@Override
@ -47,42 +41,4 @@ public class BookmarksSightsSubscriptionFragment extends AbstractBookmarkSubscri
{
return SubscriptionType.BOOKMARKS_SIGHTS;
}
@Override
public void onProductDetailsLoading()
{
super.onProductDetailsLoading();
mDelegate.onProductDetailsLoading();
}
@Override
void hideButtonProgress()
{
mDelegate.hideButtonProgress();
}
@Override
void showButtonProgress()
{
mDelegate.showButtonProgress();
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
return mDelegate.getSelectedPeriod();
}
@Override
public void onReset()
{
mDelegate.onReset();
}
@Override
public void onPriceSelection()
{
mDelegate.onPriceSelection();
}
}

View file

@ -2,21 +2,15 @@ package com.mapswithme.maps.purchase;
import android.view.View;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.SubscriptionButton;
import com.mapswithme.util.Utils;
import com.mapswithme.util.statistics.Statistics;
class SubscriptionFragmentDelegate
abstract class SubscriptionFragmentDelegate
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mAnnualButton;
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mMonthlyButton;
@NonNull
private PurchaseUtils.Period mSelectedPeriod = PurchaseUtils.Period.P1Y;
@NonNull
private final AbstractBookmarkSubscriptionFragment mFragment;
@ -25,86 +19,60 @@ class SubscriptionFragmentDelegate
mFragment = fragment;
}
void onSubscriptionCreateView(@NonNull View root)
{
mAnnualButton = root.findViewById(R.id.annual_button);
mAnnualButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1Y;
mFragment.pingBookmarkCatalog();
});
mMonthlyButton = root.findViewById(R.id.monthly_button);
mMonthlyButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1M;
mFragment.pingBookmarkCatalog();
});
}
abstract void showButtonProgress();
void onSubscriptionDestroyView()
{
// Do nothing by default.
}
abstract void hideButtonProgress();
abstract void onProductDetailsLoading();
abstract void onPriceSelection();
@NonNull
PurchaseUtils.Period getSelectedPeriod()
{
return mSelectedPeriod;
}
void onProductDetailsLoading()
{
mAnnualButton.showProgress();
mMonthlyButton.showProgress();
}
void onPriceSelection()
{
mAnnualButton.hideProgress();
mMonthlyButton.hideProgress();
updatePaymentButtons();
}
abstract PurchaseUtils.Period getSelectedPeriod();
void onReset()
{
// Do nothing by default.
}
private void updatePaymentButtons()
@CallSuper
void onCreateView(@NonNull View root)
{
updateYearlyButton();
updateMonthlyButton();
View restoreButton = root.findViewById(R.id.restore_purchase_btn);
restoreButton.setOnClickListener(v -> openSubscriptionManagementSettings());
View termsOfUse = root.findViewById(R.id.term_of_use_link);
termsOfUse.setOnClickListener(v -> openTermsOfUseLink());
View privacyPolicy = root.findViewById(R.id.privacy_policy_link);
privacyPolicy.setOnClickListener(v -> openPrivacyPolicyLink());
}
private void updateMonthlyButton()
void onDestroyView()
{
ProductDetails details = mFragment.getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mAnnualButton.setPrice(price);
mAnnualButton.setName(mFragment.getString(R.string.annual_subscription_title));
String sale = mFragment.getString(R.string.annual_save_component, mFragment.calculateYearlySaving());
mAnnualButton.setSale(sale);
// Do nothing by default.
}
private void updateYearlyButton()
private void openSubscriptionManagementSettings()
{
ProductDetails details = mFragment.getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mMonthlyButton.setPrice(price);
mMonthlyButton.setName(mFragment.getString(R.string.montly_subscription_title));
Utils.openUrl(mFragment.requireContext(), "https://play.google.com/store/account/subscriptions");
Statistics.INSTANCE.trackPurchaseEvent(Statistics.EventName.INAPP_PURCHASE_PREVIEW_RESTORE,
mFragment.getSubscriptionType().getServerId());
}
void showButtonProgress()
private void openTermsOfUseLink()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.showProgress();
else
mMonthlyButton.showProgress();
Utils.openUrl(mFragment.requireContext(), Framework.nativeGetTermsOfUseLink());
}
void hideButtonProgress()
private void openPrivacyPolicyLink()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.hideProgress();
else
mMonthlyButton.hideProgress();
Utils.openUrl(mFragment.requireContext(), Framework.nativeGetPrivacyPolicyLink());
}
@NonNull
AbstractBookmarkSubscriptionFragment getFragment()
{
return mFragment;
}
}

View file

@ -0,0 +1,107 @@
package com.mapswithme.maps.purchase;
import android.view.View;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.SubscriptionButton;
import com.mapswithme.util.Utils;
class TwoButtonsSubscriptionFragmentDelegate extends SubscriptionFragmentDelegate
{
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mAnnualButton;
@SuppressWarnings("NullableProblems")
@NonNull
private SubscriptionButton mMonthlyButton;
@NonNull
private PurchaseUtils.Period mSelectedPeriod = PurchaseUtils.Period.P1Y;
TwoButtonsSubscriptionFragmentDelegate(@NonNull AbstractBookmarkSubscriptionFragment fragment)
{
super(fragment);
}
@Override
@CallSuper
public void onCreateView(@NonNull View root)
{
super.onCreateView(root);
mAnnualButton = root.findViewById(R.id.annual_button);
mAnnualButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1Y;
getFragment().pingBookmarkCatalog();
});
mMonthlyButton = root.findViewById(R.id.monthly_button);
mMonthlyButton.setOnClickListener(v -> {
mSelectedPeriod = PurchaseUtils.Period.P1M;
getFragment().pingBookmarkCatalog();
});
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
return mSelectedPeriod;
}
@Override
void onProductDetailsLoading()
{
mAnnualButton.showProgress();
mMonthlyButton.showProgress();
}
@Override
void onPriceSelection()
{
mAnnualButton.hideProgress();
mMonthlyButton.hideProgress();
updatePaymentButtons();
}
private void updatePaymentButtons()
{
updateYearlyButton();
updateMonthlyButton();
}
private void updateMonthlyButton()
{
ProductDetails details = getFragment().getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mAnnualButton.setPrice(price);
mAnnualButton.setName(getFragment().getString(R.string.annual_subscription_title));
String sale = getFragment().getString(R.string.annual_save_component, getFragment().calculateYearlySaving());
mAnnualButton.setSale(sale);
}
private void updateYearlyButton()
{
ProductDetails details = getFragment().getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
mMonthlyButton.setPrice(price);
mMonthlyButton.setName(getFragment().getString(R.string.montly_subscription_title));
}
@Override
void showButtonProgress()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.showProgress();
else
mMonthlyButton.showProgress();
}
@Override
void hideButtonProgress()
{
if (mSelectedPeriod == PurchaseUtils.Period.P1Y)
mAnnualButton.hideProgress();
else
mMonthlyButton.hideProgress();
}
}

View file

@ -0,0 +1,193 @@
package com.mapswithme.maps.purchase;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
public class TwoCardsSubscriptionFragmentDelegate extends SubscriptionFragmentDelegate
{
private static final int DEF_ELEVATION = 0;
TwoCardsSubscriptionFragmentDelegate(@NonNull AbstractBookmarkSubscriptionFragment fragment)
{
super(fragment);
}
@Override
void onCreateView(@NonNull View root)
{
super.onCreateView(root);
CardView annualPriceCard = root.findViewById(R.id.annual_price_card);
CardView monthlyPriceCard = root.findViewById(R.id.monthly_price_card);
AnnualCardClickListener annualCardListener = new AnnualCardClickListener(monthlyPriceCard,
annualPriceCard);
annualPriceCard.setOnClickListener(annualCardListener);
MonthlyCardClickListener monthlyCardListener = new MonthlyCardClickListener(monthlyPriceCard,
annualPriceCard);
monthlyPriceCard.setOnClickListener(monthlyCardListener);
View continueBtn = root.findViewById(R.id.continue_btn);
continueBtn.setOnClickListener(v -> onContinueButtonClicked());
annualPriceCard.setSelected(true);
monthlyPriceCard.setSelected(false);
annualPriceCard.setCardElevation(getFragment().getResources()
.getDimension(R.dimen.margin_base_plus_quarter));
}
private void updatePaymentButtons()
{
updateYearlyButton();
updateMonthlyButton();
}
private void updateYearlyButton()
{
ProductDetails details = getFragment().getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
TextView priceView = getFragment().getViewOrThrow().findViewById(R.id.annual_price);
priceView.setText(price);
TextView savingView = getFragment().getViewOrThrow().findViewById(R.id.sale);
String text = getFragment().getString(R.string.annual_save_component,
getFragment().calculateYearlySaving());
savingView.setText(text);
}
private void updateMonthlyButton()
{
ProductDetails details = getFragment().getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
TextView priceView = getFragment().getViewOrThrow().findViewById(R.id.monthly_price);
priceView.setText(price);
}
@NonNull
@Override
PurchaseUtils.Period getSelectedPeriod()
{
CardView annualCard = getFragment().getViewOrThrow().findViewById(R.id.annual_price_card);
return annualCard.getCardElevation() > 0 ? PurchaseUtils.Period.P1Y : PurchaseUtils.Period.P1M;
}
@Override
public void onReset()
{
hideAllUi();
}
private void hideAllUi()
{
UiUtils.hide(getFragment().getViewOrThrow(), R.id.root_screen_progress, R.id.content_view);
}
private void onContinueButtonClicked()
{
getFragment().pingBookmarkCatalog();
getFragment().trackPayEvent();
}
@Override
void showButtonProgress()
{
UiUtils.hide(getFragment().getViewOrThrow(), R.id.continue_btn);
UiUtils.show(getFragment().getViewOrThrow(), R.id.progress);
}
@Override
void hideButtonProgress()
{
UiUtils.hide(getFragment().getViewOrThrow(), R.id.progress);
UiUtils.show(getFragment().getViewOrThrow(), R.id.continue_btn);
}
private void showRootScreenProgress()
{
UiUtils.show(getFragment().getViewOrThrow(), R.id.root_screen_progress);
UiUtils.hide(getFragment().getViewOrThrow(), R.id.content_view);
}
private void hideRootScreenProgress()
{
UiUtils.hide(getFragment().getViewOrThrow(), R.id.root_screen_progress);
UiUtils.show(getFragment().getViewOrThrow(), R.id.content_view);
}
@Override
void onProductDetailsLoading()
{
showRootScreenProgress();
}
@Override
void onPriceSelection()
{
hideRootScreenProgress();
updatePaymentButtons();
}
private class AnnualCardClickListener implements View.OnClickListener
{
@NonNull
private final CardView mMonthlyPriceCard;
@NonNull
private final CardView mAnnualPriceCard;
AnnualCardClickListener(@NonNull CardView monthlyPriceCard,
@NonNull CardView annualPriceCard)
{
mMonthlyPriceCard = monthlyPriceCard;
mAnnualPriceCard = annualPriceCard;
}
@Override
public void onClick(View v)
{
mMonthlyPriceCard.setCardElevation(DEF_ELEVATION);
mAnnualPriceCard.setCardElevation(getFragment().getResources()
.getDimension(R.dimen.margin_base_plus_quarter));
if (!mAnnualPriceCard.isSelected())
getFragment().trackYearlyProductSelected();
mMonthlyPriceCard.setSelected(false);
mAnnualPriceCard.setSelected(true);
}
}
private class MonthlyCardClickListener implements View.OnClickListener
{
@NonNull
private final CardView mMonthlyPriceCard;
@NonNull
private final CardView mAnnualPriceCard;
MonthlyCardClickListener(@NonNull CardView monthlyPriceCard,
@NonNull CardView annualPriceCard)
{
mMonthlyPriceCard = monthlyPriceCard;
mAnnualPriceCard = annualPriceCard;
}
@Override
public void onClick(View v)
{
mMonthlyPriceCard.setCardElevation(getFragment().getResources()
.getDimension(R.dimen.margin_base_plus_quarter));
mAnnualPriceCard.setCardElevation(DEF_ELEVATION);
if (!mMonthlyPriceCard.isSelected())
getFragment().trackMonthlyProductSelected();
mMonthlyPriceCard.setSelected(true);
mAnnualPriceCard.setSelected(false);
}
}
}