Skip to content

Commit

Permalink
support description on Accessible mode
Browse files Browse the repository at this point in the history
  • Loading branch information
quanquan1 committed Mar 18, 2024
1 parent f3e0186 commit e0051e0
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 41 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ public void onAffirmPrequalError(String message) {
promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.getTextSize(), this, new PromotionCallback() {
@Override
public void onSuccess(@Nullable SpannableString spannableString, boolean showPrequal) {
promotionTextView.setText(spannableString);
promotionTextView.setOnClickListener(v -> Affirm.onPromotionClick(MainActivity.this, requestData, showPrequal));
public void onSuccess(@NonNull Promotion promotion) {
promotionTextView.setContentDescription(promotion.getDescription());
promotionTextView.setText(promotion.getSpannableString());
promotionTextView.setOnClickListener(v -> Affirm.onPromotionClick(MainFragment.this, requestData, promotion.isShowPrequal()));
}
@Override
Expand Down
34 changes: 20 additions & 14 deletions affirm/src/main/java/com/affirm/android/Affirm.java
Original file line number Diff line number Diff line change
Expand Up @@ -1457,10 +1457,11 @@ private static void configureWithAmount(
"AffirmPromotionButton cannot be null");
final SpannablePromoCallback callback = new SpannablePromoCallback() {
@Override
public void onPromoWritten(@NonNull final String promoMessage,
final boolean showPrequal) {
public void onPromoWritten(@NonNull String promoMessage,
@NonNull String promoDescription,
boolean showPrequal) {
promotionButton.setTag(showPrequal);
promotionButton.setLabel(promoMessage);
promotionButton.setLabel(promoMessage, promoDescription);
}

@Override
Expand Down Expand Up @@ -1542,17 +1543,21 @@ public static AffirmRequest fetchPromotion(
) {
SpannablePromoCallback promoCallback = new SpannablePromoCallback() {
@Override
public void onPromoWritten(@NonNull String promo,
public void onPromoWritten(@NonNull String promoMessage,
@NonNull String promoDescription,
boolean showPrequal) {
callback.onSuccess(
AffirmUtils.createSpannableForText(
promo,
textSize,
requestData.getAffirmLogoType(),
requestData.getAffirmColor(),
context
),
showPrequal
new Promotion(
AffirmUtils.createSpannableForText(
promoMessage,
textSize,
requestData.getAffirmLogoType(),
requestData.getAffirmColor(),
context
),
promoDescription,
showPrequal
)
);
}

Expand All @@ -1576,9 +1581,10 @@ public static AffirmRequest fetchHtmlPromotion(
) {
SpannablePromoCallback promoCallback = new SpannablePromoCallback() {
@Override
public void onPromoWritten(@NonNull String promo,
public void onPromoWritten(@NonNull String promoMessage,
@NonNull String promoDescription,
boolean showPrequal) {
callback.onSuccess(promo, showPrequal);
callback.onSuccess(new HtmlPromotion(promoMessage, promoDescription, showPrequal));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
typedArray.recycle();
}

protected void setLabel(@NonNull String text) {
this.message = text;
protected void setLabel(@NonNull String promoMessage, @NonNull String promoDescription) {
this.message = promoMessage;
removeAllViews();
if (htmlStyling) {
addView(promotionWebView);
promotionWebView.loadWebData(text, remoteCssUrl, typefaceDeclaration);
promotionWebView.loadWebData(promoMessage, remoteCssUrl, typefaceDeclaration);
promotionWebView.setContentDescription(promoDescription);
} else {
buildPromotionButtonIfNeeded();
addView(promotionButton);
promotionButton.setText(promotionButton.updateSpan(text));
promotionButton.setText(promotionButton.updateSpan(promoMessage));
promotionButton.setContentDescription(promoDescription);
}
}

Expand Down
45 changes: 45 additions & 0 deletions affirm/src/main/java/com/affirm/android/HtmlPromotion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.affirm.android;

import androidx.annotation.NonNull;

public class HtmlPromotion {
@NonNull
private String htmlPromo;
@NonNull
private String description;
private boolean showPrequal;

public HtmlPromotion(@NonNull String htmlPromo,
@NonNull String description,
boolean showPrequal) {
this.htmlPromo = htmlPromo;
this.description = description;
this.showPrequal = showPrequal;
}

@NonNull
public String getHtmlPromo() {
return htmlPromo;
}

public void setHtmlPromo(@NonNull String htmlPromo) {
this.htmlPromo = htmlPromo;
}

@NonNull
public String getDescription() {
return description;
}

public void setDescription(@NonNull String description) {
this.description = description;
}

public boolean isShowPrequal() {
return showPrequal;
}

public void setShowPrequal(boolean showPrequal) {
this.showPrequal = showPrequal;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.affirm.android;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.affirm.android.exception.AffirmException;

public interface HtmlPromotionCallback {
void onSuccess(@Nullable String htmlPromo, boolean showPrequal);
void onSuccess(HtmlPromotion promotion);

void onFailure(@NonNull AffirmException exception);
}
}
4 changes: 3 additions & 1 deletion affirm/src/main/java/com/affirm/android/PromoRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import okhttp3.Call;
import okhttp3.OkHttpClient;

import static com.affirm.android.AffirmConstants.LOGO_PLACEHOLDER;
import static com.affirm.android.AffirmConstants.PROMO_PATH;

class PromoRequest implements AffirmRequest {
Expand Down Expand Up @@ -132,10 +133,11 @@ private void handleSuccessResponse(PromoResponse promoResponse) {
final String htmlPromo = promoResponse.promo().htmlAla();

final String promoMessage = isHtmlStyle ? htmlPromo : promo;
final String promoDescription = promo.replace(LOGO_PLACEHOLDER, "affirm");
if (TextUtils.isEmpty(promoMessage)) {
handleErrorResponse(new Exception("Promo message is null or empty!"));
} else {
callback.onPromoWritten(promoMessage, showPrequal);
callback.onPromoWritten(promoMessage, promoDescription, showPrequal);
}
}

Expand Down
47 changes: 47 additions & 0 deletions affirm/src/main/java/com/affirm/android/Promotion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.affirm.android;

import android.text.SpannableString;

import androidx.annotation.NonNull;

public class Promotion {
@NonNull
private SpannableString spannableString;
@NonNull
private String description;
private boolean showPrequal;

public Promotion(@NonNull SpannableString spannableString,
@NonNull String description,
boolean showPrequal) {
this.spannableString = spannableString;
this.description = description;
this.showPrequal = showPrequal;
}

@NonNull
public SpannableString getSpannableString() {
return spannableString;
}

public void setSpannableString(@NonNull SpannableString spannableString) {
this.spannableString = spannableString;
}

@NonNull
public String getDescription() {
return description;
}

public void setDescription(@NonNull String description) {
this.description = description;
}

public boolean isShowPrequal() {
return showPrequal;
}

public void setShowPrequal(boolean showPrequal) {
this.showPrequal = showPrequal;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.affirm.android;

import android.text.SpannableString;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.affirm.android.exception.AffirmException;

public interface PromotionCallback {
void onSuccess(@Nullable SpannableString spannableString, boolean showPrequal);
void onSuccess(@NonNull Promotion promotion);

void onFailure(@NonNull AffirmException exception);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

interface SpannablePromoCallback {
void onPromoWritten(@NonNull final String promoMessage,
@NonNull final String promoDescription,
final boolean showPrequal);

void onFailure(@NonNull AffirmException exception);
Expand Down
19 changes: 10 additions & 9 deletions samples-java/src/main/java/com/affirm/samples/MainFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -15,7 +14,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;

import com.affirm.android.Affirm;
Expand All @@ -24,7 +22,9 @@
import com.affirm.android.AffirmPromotionButton;
import com.affirm.android.AffirmRequest;
import com.affirm.android.CookiesUtil;
import com.affirm.android.HtmlPromotion;
import com.affirm.android.HtmlPromotionCallback;
import com.affirm.android.Promotion;
import com.affirm.android.PromotionCallback;
import com.affirm.android.PromotionWebView;
import com.affirm.android.exception.AffirmException;
Expand Down Expand Up @@ -186,9 +186,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.getTextSize(), getContext(), new PromotionCallback() {
@Override
public void onSuccess(@Nullable SpannableString spannableString, boolean showPrequal) {
promotionTextView.setText(spannableString);
promotionTextView.setOnClickListener(v -> Affirm.onPromotionClick(MainFragment.this, requestData, showPrequal));
public void onSuccess(@NonNull Promotion promotion) {
promotionTextView.setContentDescription(promotion.getDescription());
promotionTextView.setText(promotion.getSpannableString());
promotionTextView.setOnClickListener(v -> Affirm.onPromotionClick(MainFragment.this, requestData, promotion.isShowPrequal()));
}

@Override
Expand All @@ -199,11 +200,11 @@ public void onFailure(@NonNull AffirmException exception) {

PromotionWebView htmlPromotionWebView = view.findViewById(R.id.htmlPromotionWebView);
htmlPromoRequest = Affirm.fetchHtmlPromotion(requestData, new HtmlPromotionCallback() {

@Override
public void onSuccess(@Nullable String htmlPromo, boolean showPrequal) {
htmlPromotionWebView.loadWebData(htmlPromo, "file:///android_asset/remote_promo.css", typefaceDeclaration);
htmlPromotionWebView.setWebViewClickListener(v -> Affirm.onPromotionClick(MainFragment.this, requestData, showPrequal));
public void onSuccess(HtmlPromotion promotion) {
htmlPromotionWebView.setContentDescription(promotion.getDescription());
htmlPromotionWebView.loadWebData(promotion.getHtmlPromo(), "file:///android_asset/remote_promo.css", typefaceDeclaration);
htmlPromotionWebView.setWebViewClickListener(v -> Affirm.onPromotionClick(MainFragment.this, requestData, promotion.isShowPrequal()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.affirm.android.Affirm
import com.affirm.android.AffirmRequest
import com.affirm.android.CookiesUtil
import com.affirm.android.Promotion
import com.affirm.android.PromotionCallback
import com.affirm.android.exception.AffirmException
import com.affirm.android.model.*
Expand Down Expand Up @@ -52,9 +53,10 @@ class MainActivity : AppCompatActivity(), Affirm.CheckoutCallbacks, Affirm.VcnCh
.build()

promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.textSize, this, object : PromotionCallback {
override fun onSuccess(spannableString: SpannableString?, showPrequal: Boolean) {
promotionTextView.text = spannableString
promotionTextView.setOnClickListener { Affirm.onPromotionClick(this@MainActivity, requestData, showPrequal) }
override fun onSuccess(promotion: Promotion) {
promotionTextView.contentDescription = promotion.description
promotionTextView.text = promotion.spannableString
promotionTextView.setOnClickListener { Affirm.onPromotionClick(this@MainActivity, requestData, promotion.isShowPrequal) }
}

override fun onFailure(exception: AffirmException) {
Expand Down

0 comments on commit e0051e0

Please sign in to comment.