Commit 66971969 authored by Pavel Yatsuk's avatar Pavel Yatsuk Committed by Commit Bot

[Messages] Dismiss message on primary action click

This CL adds logic in SingleActionMessage to dismiss the message when
the user taps on primary button.

BUG=1152520
R=lazzzis@chromium.org

Change-Id: I6f32c69278cd613f47839af7c70e8d41db99a848
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2560829
Commit-Queue: Pavel Yatsuk <pavely@chromium.org>
Reviewed-by: default avatarLijin Shen <lazzzis@google.com>
Cr-Commit-Position: refs/heads/master@{#831156}
parent 5930c965
...@@ -18,7 +18,7 @@ import org.chromium.ui.modelutil.PropertyModel.WritableObjectPropertyKey; ...@@ -18,7 +18,7 @@ import org.chromium.ui.modelutil.PropertyModel.WritableObjectPropertyKey;
public class MessageBannerProperties { public class MessageBannerProperties {
public static final WritableObjectPropertyKey<String> PRIMARY_BUTTON_TEXT = public static final WritableObjectPropertyKey<String> PRIMARY_BUTTON_TEXT =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
public static final WritableObjectPropertyKey<OnClickListener> PRIMARY_BUTTON_CLICK_LISTENER = public static final WritableObjectPropertyKey<Runnable> ON_PRIMARY_ACTION =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
public static final WritableObjectPropertyKey<String> TITLE = new WritableObjectPropertyKey<>(); public static final WritableObjectPropertyKey<String> TITLE = new WritableObjectPropertyKey<>();
public static final WritableObjectPropertyKey<String> DESCRIPTION = public static final WritableObjectPropertyKey<String> DESCRIPTION =
...@@ -35,18 +35,26 @@ public class MessageBannerProperties { ...@@ -35,18 +35,26 @@ public class MessageBannerProperties {
public static final WritableObjectPropertyKey<Runnable> ON_DISMISSED = public static final WritableObjectPropertyKey<Runnable> ON_DISMISSED =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
// TRANSLATION_Y and ALPHA should only be accessed by the message banner component. // Following properties should only be accessed by the message banner component.
static final WritableFloatPropertyKey TRANSLATION_Y = new WritableFloatPropertyKey(); static final WritableFloatPropertyKey TRANSLATION_Y = new WritableFloatPropertyKey();
static final WritableFloatPropertyKey ALPHA = new WritableFloatPropertyKey(); static final WritableFloatPropertyKey ALPHA = new WritableFloatPropertyKey();
static final WritableObjectPropertyKey<Runnable> ON_TOUCH_RUNNABLE = static final WritableObjectPropertyKey<Runnable> ON_TOUCH_RUNNABLE =
new WritableObjectPropertyKey<>(); new WritableObjectPropertyKey<>();
// PRIMARY_BUTTON_CLICK_LISTENER is SingleActionMessage's handler attached to primary button
// view. SingleActionMessage calls ON_PRIMARY_ACTION from the handler.
static final WritableObjectPropertyKey<OnClickListener> PRIMARY_BUTTON_CLICK_LISTENER =
new WritableObjectPropertyKey<>();
// TODO(pavely): There is no need to maintain two lists of property keys. Remove one and clean
// up references.
public static final PropertyKey[] ALL_KEYS = public static final PropertyKey[] ALL_KEYS =
new PropertyKey[] {PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE, new PropertyKey[] {PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE,
DESCRIPTION, ICON, ICON_RESOURCE_ID, SECONDARY_ICON, DESCRIPTION, ICON, ICON_RESOURCE_ID, SECONDARY_ICON,
SECONDARY_ICON_CONTENT_DESCRIPTION, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE}; SECONDARY_ICON_CONTENT_DESCRIPTION, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE,
ON_PRIMARY_ACTION};
public static final PropertyKey[] SINGLE_ACTION_MESSAGE_KEYS = new PropertyKey[] { public static final PropertyKey[] SINGLE_ACTION_MESSAGE_KEYS =
PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE, DESCRIPTION, ICON, new PropertyKey[] {PRIMARY_BUTTON_TEXT, PRIMARY_BUTTON_CLICK_LISTENER, TITLE,
ICON_RESOURCE_ID, ON_DISMISSED, TRANSLATION_Y, ALPHA, ON_TOUCH_RUNNABLE}; DESCRIPTION, ICON, ICON_RESOURCE_ID, ON_DISMISSED, TRANSLATION_Y, ALPHA,
ON_TOUCH_RUNNABLE, ON_PRIMARY_ACTION};
} }
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
package org.chromium.components.messages; package org.chromium.components.messages;
import android.view.View;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
...@@ -35,8 +33,7 @@ public final class MessageWrapper { ...@@ -35,8 +33,7 @@ public final class MessageWrapper {
mNativeMessageWrapper = nativeMessageWrapper; mNativeMessageWrapper = nativeMessageWrapper;
mMessageProperties = mMessageProperties =
new PropertyModel.Builder(MessageBannerProperties.SINGLE_ACTION_MESSAGE_KEYS) new PropertyModel.Builder(MessageBannerProperties.SINGLE_ACTION_MESSAGE_KEYS)
.with(MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER, .with(MessageBannerProperties.ON_PRIMARY_ACTION, this::handleActionClick)
this::handleActionClick)
.with(MessageBannerProperties.ON_DISMISSED, this::handleMessageDismissed) .with(MessageBannerProperties.ON_DISMISSED, this::handleMessageDismissed)
.build(); .build();
} }
...@@ -91,7 +88,7 @@ public final class MessageWrapper { ...@@ -91,7 +88,7 @@ public final class MessageWrapper {
mNativeMessageWrapper = 0; mNativeMessageWrapper = 0;
} }
private void handleActionClick(View v) { private void handleActionClick() {
if (mNativeMessageWrapper == 0) return; if (mNativeMessageWrapper == 0) return;
MessageWrapperJni.get().handleActionClick(mNativeMessageWrapper); MessageWrapperJni.get().handleActionClick(mNativeMessageWrapper);
} }
......
...@@ -76,7 +76,7 @@ public class MessageWrapperTest { ...@@ -76,7 +76,7 @@ public class MessageWrapperTest {
final long nativePtr = 1; final long nativePtr = 1;
MessageWrapper message = MessageWrapper.create(nativePtr); MessageWrapper message = MessageWrapper.create(nativePtr);
PropertyModel messageProperties = message.getMessageProperties(); PropertyModel messageProperties = message.getMessageProperties();
messageProperties.get(MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER).onClick(null); messageProperties.get(MessageBannerProperties.ON_PRIMARY_ACTION).run();
Mockito.verify(mNativeMock).handleActionClick(nativePtr); Mockito.verify(mNativeMock).handleActionClick(nativePtr);
messageProperties.get(MessageBannerProperties.ON_DISMISSED).run(); messageProperties.get(MessageBannerProperties.ON_DISMISSED).run();
Mockito.verify(mNativeMock).handleDismissCallback(nativePtr); Mockito.verify(mNativeMock).handleDismissCallback(nativePtr);
...@@ -93,7 +93,7 @@ public class MessageWrapperTest { ...@@ -93,7 +93,7 @@ public class MessageWrapperTest {
PropertyModel messageProperties = message.getMessageProperties(); PropertyModel messageProperties = message.getMessageProperties();
message.clearNativePtr(); message.clearNativePtr();
messageProperties.get(MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER).onClick(null); messageProperties.get(MessageBannerProperties.ON_PRIMARY_ACTION).run();
Mockito.verify(mNativeMock, never()).handleActionClick(nativePtr); Mockito.verify(mNativeMock, never()).handleActionClick(nativePtr);
messageProperties.get(MessageBannerProperties.ON_DISMISSED).run(); messageProperties.get(MessageBannerProperties.ON_DISMISSED).run();
Mockito.verify(mNativeMock, never()).handleDismissCallback(nativePtr); Mockito.verify(mNativeMock, never()).handleDismissCallback(nativePtr);
......
...@@ -7,6 +7,7 @@ package org.chromium.components.messages; ...@@ -7,6 +7,7 @@ package org.chromium.components.messages;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -42,6 +43,9 @@ public class SingleActionMessage implements MessageStateHandler { ...@@ -42,6 +43,9 @@ public class SingleActionMessage implements MessageStateHandler {
mDismissHandler = dismissHandler; mDismissHandler = dismissHandler;
mAutoDismissTimer = new MessageAutoDismissTimer(10 * DateUtils.SECOND_IN_MILLIS); mAutoDismissTimer = new MessageAutoDismissTimer(10 * DateUtils.SECOND_IN_MILLIS);
mMaxTranslationSupplier = maxTranslationSupplier; mMaxTranslationSupplier = maxTranslationSupplier;
mModel.set(
MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER, this::handlePrimaryAction);
} }
/** /**
...@@ -97,6 +101,11 @@ public class SingleActionMessage implements MessageStateHandler { ...@@ -97,6 +101,11 @@ public class SingleActionMessage implements MessageStateHandler {
if (onDismissed != null) onDismissed.run(); if (onDismissed != null) onDismissed.run();
} }
private void handlePrimaryAction(View v) {
mModel.get(MessageBannerProperties.ON_PRIMARY_ACTION).run();
mDismissHandler.onResult(mModel);
}
@VisibleForTesting @VisibleForTesting
void setMessageBannerForTesting(MessageBannerCoordinator messageBanner) { void setMessageBannerForTesting(MessageBannerCoordinator messageBanner) {
mMessageBanner = messageBanner; mMessageBanner = messageBanner;
......
...@@ -108,7 +108,7 @@ public class SingleActionMessageTest extends DummyUiActivityTestCase { ...@@ -108,7 +108,7 @@ public class SingleActionMessageTest extends DummyUiActivityTestCase {
.with(MessageBannerProperties.ICON, .with(MessageBannerProperties.ICON,
ApiCompatibilityUtils.getDrawable( ApiCompatibilityUtils.getDrawable(
activity.getResources(), android.R.drawable.ic_menu_add)) activity.getResources(), android.R.drawable.ic_menu_add))
.with(MessageBannerProperties.PRIMARY_BUTTON_CLICK_LISTENER, (v) -> {}) .with(MessageBannerProperties.ON_PRIMARY_ACTION, () -> {})
.with(MessageBannerProperties.ON_TOUCH_RUNNABLE, () -> {}) .with(MessageBannerProperties.ON_TOUCH_RUNNABLE, () -> {})
.with(MessageBannerProperties.ON_DISMISSED, .with(MessageBannerProperties.ON_DISMISSED,
() -> { mDismissCallback.notifyCalled(); }) () -> { mDismissCallback.notifyCalled(); })
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment