Skip to content

Commit

Permalink
Merge pull request #129 from wangdaliu/promp-description
Browse files Browse the repository at this point in the history
support description on Accessible mode
  • Loading branch information
robokitten007 authored Mar 21, 2024
2 parents c351420 + 649ee2d commit 188610f
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 35 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 80 additions & 7 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 @@ -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,
Expand All @@ -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(),
Expand All @@ -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
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
Expand Up @@ -5,6 +5,7 @@

import com.affirm.android.exception.AffirmException;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
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
Expand Up @@ -7,6 +7,7 @@

import com.affirm.android.exception.AffirmException;

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

Expand Down
11 changes: 11 additions & 0 deletions affirm/src/main/java/com/affirm/android/PromotionCallbackV2.java
Original file line number Diff line number Diff line change
@@ -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);
}
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
27 changes: 14 additions & 13 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,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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 188610f

Please sign in to comment.