Skip to content

Commit

Permalink
Update vpn pay wall changes
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppandya committed Nov 5, 2024
1 parent 5d27097 commit 2935367
Showing 1 changed file with 87 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,27 @@
import org.chromium.ui.text.SpanApplier.SpanInfo;

public class VpnPaywallActivity extends BraveVpnParentActivity {
private ProgressBar mMonthlyPlanProgress;
private ProgressBar mYearlyPlanProgress;
private LinearLayout mPlanLayout;
private boolean mShouldShowRestoreMenu;

private ProgressBar mMonthlyPlanProgress;
private LinearLayout mMonthlySelectorLayout;
private TextView mMonthlySubscriptionAmountText;


private ProgressBar mYearlyPlanProgress;
private LinearLayout mYearlySelectorLayout;
private TextView mYearlySubscriptionAmountText;
private TextView mRemovedValueText;
private TextView mYearlyText;

private Button mBtnVpnPlanAction;

enum SelectedPlanType {
YEARLY,
MONTHLY
}

private SelectedPlanType mSelectedPlanType = SelectedPlanType.YEARLY;
private ProductDetails mProductDetails;
private InAppPurchaseWrapper.SubscriptionType mSelectedPlanType =
InAppPurchaseWrapper.SubscriptionType.YEARLY;

private ProductDetails mMonthlyProductDetails;
private ProductDetails mYearlyProductDetails;

@Override
public void onResumeWithNative() {
Expand All @@ -75,11 +74,10 @@ private void initializeViews() {
actionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_close_24);
actionBar.setTitle(getResources().getString(R.string.brave_vpn));

mMonthlyPlanProgress = findViewById(R.id.monthly_plan_progress);

mYearlyPlanProgress = findViewById(R.id.yearly_plan_progress);
mPlanLayout = findViewById(R.id.plan_layout);

mMonthlyPlanProgress = findViewById(R.id.monthly_plan_progress);

mRemovedValueText = findViewById(R.id.removed_value_tv);
mRemovedValueText.setPaintFlags(
mRemovedValueText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
Expand All @@ -89,6 +87,11 @@ private void initializeViews() {
String.format(getResources().getString(R.string.monthly_subscription), ""));
mMonthlySubscriptionAmountText = findViewById(R.id.monthly_subscription_amount_text);
mMonthlySelectorLayout = findViewById(R.id.monthly_selector_layout);
mMonthlySelectorLayout.setOnClickListener(
v -> {
mSelectedPlanType = InAppPurchaseWrapper.SubscriptionType.MONTHLY;
updateSelectedPlanView();
});

TextView trialText = findViewById(R.id.trial_text);
String trialString = getResources().getString(R.string.trial_text);
Expand All @@ -108,6 +111,7 @@ private void initializeViews() {
SPAN_EXCLUSIVE_EXCLUSIVE);
trialText.setText(trialTextSpan);

mYearlyPlanProgress = findViewById(R.id.yearly_plan_progress);
mYearlySubscriptionAmountText = findViewById(R.id.yearly_subscription_amount_text);
mYearlySelectorLayout = findViewById(R.id.yearly_selector_layout);
mYearlyText = findViewById(R.id.yearly_text);
Expand All @@ -123,10 +127,18 @@ private void initializeViews() {
mBtnVpnPlanAction = findViewById(R.id.vpn_plan_action_button);
mBtnVpnPlanAction.setOnClickListener(
v -> {
if (mProductDetails != null) {
InAppPurchaseWrapper.getInstance()
.initiatePurchase(VpnPaywallActivity.this, mProductDetails);
ProductDetails productDetails = null;
if (mSelectedPlanType == InAppPurchaseWrapper.SubscriptionType.MONTHLY) {
productDetails = mMonthlyProductDetails;
} else if (mSelectedPlanType
== InAppPurchaseWrapper.SubscriptionType.YEARLY) {
productDetails = mYearlyProductDetails;
}
if (productDetails == null) {
return;
}
InAppPurchaseWrapper.getInstance()
.initiatePurchase(VpnPaywallActivity.this, productDetails);
});
}

Expand All @@ -140,7 +152,6 @@ public void finishNativeInitialization() {
verifySubscription();

getMonthlyProductDetails();
updateSelectedPlanView();
}

private void getMonthlyProductDetails() {
Expand All @@ -150,22 +161,47 @@ private void getMonthlyProductDetails() {
InAppPurchaseWrapper.getInstance()
.getMonthlyProductDetails(InAppPurchaseWrapper.SubscriptionProduct.VPN),
monthlyProductDetails -> {
if (monthlyProductDetails != null) {
runOnUiThread(
mMonthlyProductDetails = monthlyProductDetails;
updateMonthlyPlanUI();
getYearlyProductDetails();
});
}

private void getYearlyProductDetails() {
mYearlyPlanProgress.setVisibility(View.VISIBLE);
LiveDataUtil.observeOnce(
InAppPurchaseWrapper.getInstance()
.getYearlyProductDetails(InAppPurchaseWrapper.SubscriptionProduct.VPN),
yearlyProductDetails -> {
mYearlyProductDetails = yearlyProductDetails;
updateYearlyPlanUI();
});
}

private void updateSelectedPlanView() {
mYearlySelectorLayout.setBackgroundResource(
mSelectedPlanType == InAppPurchaseWrapper.SubscriptionType.YEARLY
? R.drawable.vpn_paywall_selected_bg
: R.drawable.vpn_paywall_bg);
mMonthlySelectorLayout.setBackgroundResource(
mSelectedPlanType == InAppPurchaseWrapper.SubscriptionType.MONTHLY
? R.drawable.vpn_paywall_selected_bg
: R.drawable.vpn_paywall_bg);
}

private void updateMonthlyPlanUI() {
if (mMonthlyProductDetails == null) {
return;
}
runOnUiThread(
new Runnable() {
@Override
public void run() {
mMonthlySelectorLayout.setOnClickListener(
v -> {
mSelectedPlanType = SelectedPlanType.MONTHLY;
mProductDetails = monthlyProductDetails;
updateSelectedPlanView();
});
SpannableString monthlyFormattedPrice =
InAppPurchaseWrapper.getInstance()
.getFormattedProductPrice(
VpnPaywallActivity.this,
monthlyProductDetails,
mMonthlyProductDetails,
R.string
.monthly_subscription_amount);
if (monthlyFormattedPrice != null) {
Expand All @@ -178,40 +214,28 @@ public void run() {
InAppPurchaseWrapper.getInstance()
.getFormattedFullProductPrice(
VpnPaywallActivity.this,
monthlyProductDetails);
mMonthlyProductDetails);
if (fullPrice != null) {
mRemovedValueText.setText(
fullPrice, TextView.BufferType.SPANNABLE);
}
}
});
getYearlyProductDetails(monthlyProductDetails);
}
});
}

private void getYearlyProductDetails(ProductDetails monthlyProductDetails) {
mYearlyPlanProgress.setVisibility(View.VISIBLE);
LiveDataUtil.observeOnce(
InAppPurchaseWrapper.getInstance()
.getYearlyProductDetails(InAppPurchaseWrapper.SubscriptionProduct.VPN),
yearlyProductDetails -> {
if (yearlyProductDetails != null) {
runOnUiThread(
private void updateYearlyPlanUI() {
if (mYearlyProductDetails == null) {
return;
}
runOnUiThread(
new Runnable() {
@Override
public void run() {
mYearlySelectorLayout.setOnClickListener(
v -> {
mSelectedPlanType = SelectedPlanType.YEARLY;
mProductDetails = yearlyProductDetails;
updateSelectedPlanView();
});
SpannableString yearlyFormattedPrice =
InAppPurchaseWrapper.getInstance()
.getFormattedProductPrice(
VpnPaywallActivity.this,
yearlyProductDetails,
mYearlyProductDetails,
R.string
.yearly_subscription_amount);
if (yearlyFormattedPrice != null) {
Expand All @@ -220,41 +244,27 @@ public void run() {
TextView.BufferType.SPANNABLE);
mYearlyPlanProgress.setVisibility(View.GONE);
}
mBtnVpnPlanAction.setEnabled(true);
if (mMonthlyProductDetails != null) {
String discountText =
getString(
R.string.renew_monthly_save,
InAppPurchaseWrapper.getInstance()
.getYearlyDiscountPercentage(
mMonthlyProductDetails,
mYearlyProductDetails));
SpannableString discountSpannableString =
SpanApplier.applySpans(
discountText,
new SpanInfo(
"<discount_text>",
"</discount_text>",
new StyleSpan(android.graphics.Typeface.BOLD),
new UnderlineSpan()));
mYearlyText.setText(discountSpannableString);
}
}
});
mProductDetails = yearlyProductDetails;
mBtnVpnPlanAction.setEnabled(true);
if (monthlyProductDetails != null) {
String discountText =
getString(
R.string.renew_monthly_save,
InAppPurchaseWrapper.getInstance()
.getYearlyDiscountPercentage(
monthlyProductDetails,
yearlyProductDetails));
SpannableString discountSpannableString =
SpanApplier.applySpans(
discountText,
new SpanInfo(
"<discount_text>",
"</discount_text>",
new StyleSpan(android.graphics.Typeface.BOLD),
new UnderlineSpan()));
mYearlyText.setText(discountSpannableString);
}
}
});
}

private void updateSelectedPlanView() {
mYearlySelectorLayout.setBackgroundResource(
mSelectedPlanType == SelectedPlanType.YEARLY
? R.drawable.vpn_paywall_selected_bg
: R.drawable.vpn_paywall_bg);
mMonthlySelectorLayout.setBackgroundResource(
mSelectedPlanType == SelectedPlanType.MONTHLY
? R.drawable.vpn_paywall_selected_bg
: R.drawable.vpn_paywall_bg);
}

@Override
Expand Down

0 comments on commit 2935367

Please sign in to comment.