diff --git a/README.md b/README.md index 5d04ef6f..013d507c 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/affirm/src/main/java/com/affirm/android/Affirm.java b/affirm/src/main/java/com/affirm/android/Affirm.java index da3346a2..040841e7 100755 --- a/affirm/src/main/java/com/affirm/android/Affirm.java +++ b/affirm/src/main/java/com/affirm/android/Affirm.java @@ -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 @@ -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 + ) ); } @@ -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 diff --git a/affirm/src/main/java/com/affirm/android/AffirmPromotionButton.java b/affirm/src/main/java/com/affirm/android/AffirmPromotionButton.java index 792c0191..5a6c5b87 100755 --- a/affirm/src/main/java/com/affirm/android/AffirmPromotionButton.java +++ b/affirm/src/main/java/com/affirm/android/AffirmPromotionButton.java @@ -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); } } diff --git a/affirm/src/main/java/com/affirm/android/HtmlPromotion.java b/affirm/src/main/java/com/affirm/android/HtmlPromotion.java new file mode 100644 index 00000000..c3be4baa --- /dev/null +++ b/affirm/src/main/java/com/affirm/android/HtmlPromotion.java @@ -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; + } +} \ No newline at end of file diff --git a/affirm/src/main/java/com/affirm/android/HtmlPromotionCallback.java b/affirm/src/main/java/com/affirm/android/HtmlPromotionCallback.java index c09648cf..3e850557 100644 --- a/affirm/src/main/java/com/affirm/android/HtmlPromotionCallback.java +++ b/affirm/src/main/java/com/affirm/android/HtmlPromotionCallback.java @@ -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); -} \ No newline at end of file +} diff --git a/affirm/src/main/java/com/affirm/android/PromoRequest.java b/affirm/src/main/java/com/affirm/android/PromoRequest.java index be058d16..67130d1a 100755 --- a/affirm/src/main/java/com/affirm/android/PromoRequest.java +++ b/affirm/src/main/java/com/affirm/android/PromoRequest.java @@ -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 { @@ -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); } } diff --git a/affirm/src/main/java/com/affirm/android/Promotion.java b/affirm/src/main/java/com/affirm/android/Promotion.java new file mode 100644 index 00000000..84a2ffaf --- /dev/null +++ b/affirm/src/main/java/com/affirm/android/Promotion.java @@ -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; + } +} \ No newline at end of file diff --git a/affirm/src/main/java/com/affirm/android/PromotionCallback.java b/affirm/src/main/java/com/affirm/android/PromotionCallback.java index 1c89f042..1291c66d 100644 --- a/affirm/src/main/java/com/affirm/android/PromotionCallback.java +++ b/affirm/src/main/java/com/affirm/android/PromotionCallback.java @@ -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); } \ No newline at end of file diff --git a/affirm/src/main/java/com/affirm/android/SpannablePromoCallback.java b/affirm/src/main/java/com/affirm/android/SpannablePromoCallback.java index 80d22a63..87db380e 100755 --- a/affirm/src/main/java/com/affirm/android/SpannablePromoCallback.java +++ b/affirm/src/main/java/com/affirm/android/SpannablePromoCallback.java @@ -6,6 +6,7 @@ interface SpannablePromoCallback { void onPromoWritten(@NonNull final String promoMessage, + @NonNull final String promoDescription, final boolean showPrequal); void onFailure(@NonNull AffirmException exception); diff --git a/samples-java/src/main/java/com/affirm/samples/MainFragment.java b/samples-java/src/main/java/com/affirm/samples/MainFragment.java index 72218a18..ec750532 100644 --- a/samples-java/src/main/java/com/affirm/samples/MainFragment.java +++ b/samples-java/src/main/java/com/affirm/samples/MainFragment.java @@ -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; @@ -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; @@ -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; @@ -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 @@ -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 diff --git a/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt b/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt index df5d500c..83fd170b 100755 --- a/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt +++ b/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt @@ -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.* @@ -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) {