Skip to content

Commit

Permalink
Pull request #87: custom in-app dialog button color sync with android…
Browse files Browse the repository at this point in the history
… sdk

Merge in MML/infobip-mobile-messaging-huawei from mhudicek/MM-6929-custom-button-color to master

Squashed commit of the following:

commit 410e5ff6509bb57172b4e182971588cee38a93a9
Author: Matej Hudicek <[email protected]>
Date:   Mon Nov 25 14:18:44 2024 +0100

    Remove empty lines in imports

commit cf9a183282a76f7343073c007bf785cd5ae7a607
Author: Luka Ilić <[email protected]>
Date:   Mon Nov 25 13:51:28 2024 +0100

    custom in-app dialog button color sync with android sdk
  • Loading branch information
mhudicek committed Nov 25, 2024
1 parent e8ba153 commit b3da483
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package org.infobip.mobile.messaging.interactive.inapp.view;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.R;
import org.infobip.mobile.messaging.interactive.NotificationAction;
import org.infobip.mobile.messaging.interactive.NotificationCategory;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Random;
import java.util.concurrent.Executor;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author sslavin
* @since 24/04/2018.
Expand Down Expand Up @@ -78,7 +74,7 @@ public void before() {
when(alertDialogBuilder.setNegativeButton(anyString(), any(InAppViewDialogClickListener.class))).thenReturn(alertDialogBuilder);
when(alertDialogBuilder.setNeutralButton(anyInt(), any(InAppViewDialogClickListener.class))).thenReturn(alertDialogBuilder);
when(alertDialogBuilder.create()).thenReturn(alertDialog);
when(activityWrapper.createAlertDialogBuilder(anyBoolean())).thenReturn(alertDialogBuilder);
when(activityWrapper.createAlertDialogBuilder()).thenReturn(alertDialogBuilder);
when(activityWrapper.inflateView(eq(R.layout.in_app_dialog_image))).thenReturn(dialogView);
when(activityWrapper.getActivity()).thenReturn(activity);
when(activity.getResources()).thenReturn(resources);
Expand All @@ -92,7 +88,7 @@ public void shouldNotShowDialogInNoActions() {

inAppViewDialog.show(message, category);

verify(activityWrapper, never()).createAlertDialogBuilder(anyBoolean());
verify(activityWrapper, never()).createAlertDialogBuilder();
}

@Test
Expand All @@ -108,7 +104,7 @@ public void shouldSetupViewsAccordingToMessageContents() {
verify(rlDialogImage, times(1)).setVisibility(View.VISIBLE);
verify(image, times(1)).setVisibility(View.VISIBLE);

verify(activityWrapper, times(1)).createAlertDialogBuilder(eq(true));
verify(activityWrapper, times(1)).createAlertDialogBuilder();

verify(alertDialogBuilder, times(1)).setOnDismissListener(any(InAppViewDialogDismissListener.class));
verify(alertDialogBuilder, times(1)).setView(eq(dialogView));
Expand All @@ -129,7 +125,7 @@ public void shouldSetupViewsAccordingToMessageContentsWithoutImage() {
verify(rlDialogImage, never()).setVisibility(View.VISIBLE);
verify(image, never()).setVisibility(View.VISIBLE);

verify(activityWrapper, times(1)).createAlertDialogBuilder(eq(true));
verify(activityWrapper, times(1)).createAlertDialogBuilder();

verify(alertDialogBuilder, times(1)).setOnDismissListener(any(InAppViewDialogDismissListener.class));
verify(alertDialogBuilder, times(1)).setView(eq(dialogView));
Expand All @@ -152,7 +148,7 @@ public void shouldNotDisplayTitleAndImageFieldsIfDataNotProvided() {
verify(rlDialogImage, never()).setVisibility(View.VISIBLE);
verify(image, never()).setVisibility(View.VISIBLE);

verify(activityWrapper, times(1)).createAlertDialogBuilder(eq(true));
verify(activityWrapper, times(1)).createAlertDialogBuilder();

verify(alertDialogBuilder, times(1)).setOnDismissListener(any(InAppViewDialogDismissListener.class));
verify(alertDialogBuilder, times(1)).setView(eq(dialogView));
Expand All @@ -173,8 +169,8 @@ public void shouldDisplayDialogWithBuiltInThemeIfDefaultFails() {

inAppViewDialog.show(message, category, actions);

verify(activityWrapper, times(1)).createAlertDialogBuilder(eq(true));
verify(activityWrapper, times(1)).createAlertDialogBuilder(eq(false));
verify(activityWrapper, times(1)).createAlertDialogBuilder();
verify(activityWrapper, times(1)).createAlertDialogBuilder();

verify(alertDialog, times(2)).show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.infobip.mobile.messaging.interactive.inapp.view;

import android.app.Activity;
import androidx.appcompat.app.AlertDialog;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import org.infobip.mobile.messaging.R;


Expand All @@ -23,8 +22,8 @@ public class ActivityWrapper {
this.layoutInflater = LayoutInflater.from(activity);
}

public AlertDialog.Builder createAlertDialogBuilder(boolean useAppTheme) {
return new AlertDialog.Builder(useAppTheme ? activity : new ContextThemeWrapper(activity, R.style.InAppDialog));
public AlertDialog.Builder createAlertDialogBuilder() {
return new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.InAppDialog));
}

public View inflateView(int layoutResId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.app.Activity;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
Expand Down Expand Up @@ -66,10 +64,9 @@ private void show(final Bitmap bitmap, @NonNull final Message message, final Not
@Override
public void run() {
try {
showOnUiThreadWithAppTheme(bitmap, message, category, actions);
showOnUiThread(bitmap, message, category, actions);
} catch (Exception e) {
if (e instanceof IllegalStateException && e.getMessage().contains("You need to use a Theme.AppCompat theme")) {
showOnUiThreadWithOwnTheme(bitmap, message, category, actions);
return;
}
// better not displaying in-app than crashing UI
Expand All @@ -79,15 +76,7 @@ public void run() {
});
}

private void showOnUiThreadWithOwnTheme(Bitmap bitmap, @NonNull Message message, NotificationCategory category, @NonNull NotificationAction[] actions) {
showOnUiThread(bitmap, message, category, actions, false);
}

private void showOnUiThreadWithAppTheme(Bitmap bitmap, @NonNull Message message, NotificationCategory category, @NonNull NotificationAction[] actions) {
showOnUiThread(bitmap, message, category, actions, true);
}

private void showOnUiThread(Bitmap bitmap, @NonNull Message message, NotificationCategory category, @NonNull NotificationAction[] actions, boolean useAppTheme) {
private void showOnUiThread(Bitmap bitmap, @NonNull Message message, NotificationCategory category, @NonNull NotificationAction[] actions) {
if (bitmap != null) {
image.setImageBitmap(bitmap);
rlDialogImage.setVisibility(View.VISIBLE);
Expand All @@ -100,7 +89,7 @@ private void showOnUiThread(Bitmap bitmap, @NonNull Message message, Notificatio
}
tvMessageText.setText(message.getBody());

final AlertDialog.Builder builder = activityWrapper.createAlertDialogBuilder(useAppTheme)
final AlertDialog.Builder builder = activityWrapper.createAlertDialogBuilder()
.setOnDismissListener(new InAppViewDialogDismissListener(this, callback))
.setView(dialogView);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="buttonTextColor">@android:color/white</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="buttonTextColor">@android:color/black</color>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<style name="InAppDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowNoTitle">true</item>
<item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
</style>
<style name="DialogButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/buttonTextColor</item>
</style>

<style name="IB_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Expand Down

0 comments on commit b3da483

Please sign in to comment.