diff --git a/README.md b/README.md index 07c239f7..318e6e0f 100755 --- a/README.md +++ b/README.md @@ -227,11 +227,12 @@ public void onAffirmPrequalError(String message) { .setPageType(null) .build(); - promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.getTextSize(), this, new PromotionCallback() { + promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.getTextSize(), this, new PromotionCallbackV2() { @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(MainActivity.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..6450322a 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 @@ -1534,6 +1535,49 @@ public void onViewDetachedFromWindow(View v) { * @param context the context being used * @param callback a class that's called when the request completes */ + public static AffirmRequest fetchPromotion( + @NonNull PromoRequestData requestData, + float textSize, + @NonNull Context context, + @NonNull final PromotionCallbackV2 callback + ) { + SpannablePromoCallback promoCallback = new SpannablePromoCallback() { + @Override + public void onPromoWritten(@NonNull String promoMessage, + @NonNull String promoDescription, + boolean showPrequal) { + callback.onSuccess( + new Promotion( + AffirmUtils.createSpannableForText( + promoMessage, + textSize, + requestData.getAffirmLogoType(), + requestData.getAffirmColor(), + context + ), + promoDescription, + showPrequal + ) + ); + } + + @Override + public void onFailure(@NonNull AffirmException exception) { + callback.onFailure(exception); + } + }; + return buildPromoRequest(requestData, promoCallback, false); + } + + /** + * Fetch promotional message, you can display it yourself + * + * @param requestData a class containing data about the request to make + * @param textSize the textSize for the span + * @param context the context being used + * @param callback a class that's called when the request completes + */ + @Deprecated public static AffirmRequest fetchPromotion( @NonNull PromoRequestData requestData, float textSize, @@ -1542,11 +1586,12 @@ 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, + promoMessage, textSize, requestData.getAffirmLogoType(), requestData.getAffirmColor(), @@ -1570,15 +1615,43 @@ public void onFailure(@NonNull AffirmException exception) { * @param requestData a class containing data about the request to make * @param callback a class that's called when the request completes */ + public static AffirmRequest fetchHtmlPromotion( + @NonNull PromoRequestData requestData, + @NonNull final HtmlPromotionCallbackV2 callback + ) { + SpannablePromoCallback promoCallback = new SpannablePromoCallback() { + @Override + public void onPromoWritten(@NonNull String promoMessage, + @NonNull String promoDescription, + boolean showPrequal) { + callback.onSuccess(new HtmlPromotion(promoMessage, promoDescription, showPrequal)); + } + + @Override + public void onFailure(@NonNull AffirmException exception) { + callback.onFailure(exception); + } + }; + return buildPromoRequest(requestData, promoCallback, true); + } + + /** + * Fetch promotional html message, you can display it yourself + * + * @param requestData a class containing data about the request to make + * @param callback a class that's called when the request completes + */ + @Deprecated public static AffirmRequest fetchHtmlPromotion( @NonNull PromoRequestData requestData, @NonNull final HtmlPromotionCallback callback ) { 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(promoMessage, 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..be4e4c3b 100644 --- a/affirm/src/main/java/com/affirm/android/HtmlPromotionCallback.java +++ b/affirm/src/main/java/com/affirm/android/HtmlPromotionCallback.java @@ -5,6 +5,7 @@ import com.affirm.android.exception.AffirmException; +@Deprecated public interface HtmlPromotionCallback { void onSuccess(@Nullable String htmlPromo, boolean showPrequal); diff --git a/affirm/src/main/java/com/affirm/android/HtmlPromotionCallbackV2.java b/affirm/src/main/java/com/affirm/android/HtmlPromotionCallbackV2.java new file mode 100644 index 00000000..afddf73a --- /dev/null +++ b/affirm/src/main/java/com/affirm/android/HtmlPromotionCallbackV2.java @@ -0,0 +1,11 @@ +package com.affirm.android; + +import androidx.annotation.NonNull; + +import com.affirm.android.exception.AffirmException; + +public interface HtmlPromotionCallbackV2 { + void onSuccess(HtmlPromotion promotion); + + void onFailure(@NonNull AffirmException exception); +} 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..1afe1653 100644 --- a/affirm/src/main/java/com/affirm/android/PromotionCallback.java +++ b/affirm/src/main/java/com/affirm/android/PromotionCallback.java @@ -7,6 +7,7 @@ import com.affirm.android.exception.AffirmException; +@Deprecated public interface PromotionCallback { void onSuccess(@Nullable SpannableString spannableString, boolean showPrequal); diff --git a/affirm/src/main/java/com/affirm/android/PromotionCallbackV2.java b/affirm/src/main/java/com/affirm/android/PromotionCallbackV2.java new file mode 100644 index 00000000..43a04d4f --- /dev/null +++ b/affirm/src/main/java/com/affirm/android/PromotionCallbackV2.java @@ -0,0 +1,11 @@ +package com.affirm.android; + +import androidx.annotation.NonNull; + +import com.affirm.android.exception.AffirmException; + +public interface PromotionCallbackV2 { + 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..8b3ac255 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,8 +22,10 @@ import com.affirm.android.AffirmPromotionButton; import com.affirm.android.AffirmRequest; import com.affirm.android.CookiesUtil; -import com.affirm.android.HtmlPromotionCallback; -import com.affirm.android.PromotionCallback; +import com.affirm.android.HtmlPromotion; +import com.affirm.android.HtmlPromotionCallbackV2; +import com.affirm.android.Promotion; +import com.affirm.android.PromotionCallbackV2; import com.affirm.android.PromotionWebView; import com.affirm.android.exception.AffirmException; import com.affirm.android.model.Address; @@ -184,11 +184,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat .setItems(items) .build(); - promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.getTextSize(), getContext(), new PromotionCallback() { + promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.getTextSize(), getContext(), new PromotionCallbackV2() { @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 @@ -198,12 +199,12 @@ public void onFailure(@NonNull AffirmException exception) { }); PromotionWebView htmlPromotionWebView = view.findViewById(R.id.htmlPromotionWebView); - htmlPromoRequest = Affirm.fetchHtmlPromotion(requestData, new HtmlPromotionCallback() { - + htmlPromoRequest = Affirm.fetchHtmlPromotion(requestData, new HtmlPromotionCallbackV2() { @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..ba3750ca 100755 --- a/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt +++ b/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt @@ -2,13 +2,13 @@ package com.affirm.sampleskt import android.content.Intent import android.os.Bundle -import android.text.SpannableString import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.affirm.android.Affirm import com.affirm.android.AffirmRequest import com.affirm.android.CookiesUtil -import com.affirm.android.PromotionCallback +import com.affirm.android.Promotion +import com.affirm.android.PromotionCallbackV2 import com.affirm.android.exception.AffirmException import com.affirm.android.model.* import com.affirm.android.model.Currency @@ -51,10 +51,11 @@ class MainActivity : AppCompatActivity(), Affirm.CheckoutCallbacks, Affirm.VcnCh .setPageType(null) .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) } + promoRequest = Affirm.fetchPromotion(requestData, promotionTextView.textSize, this, object : PromotionCallbackV2 { + 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) {