Skip to content

Commit 64afe46

Browse files
authored
Fix dismiss iam causing action (#841)
* Fixing an issue with dragging IAM and the JS action triggering * Added two callbacks to toggle the state of dragging for an IAM messageView (DraggableRelativeLayout) * onDraggingStart will set the Boolean flag true and onDraggingEnd will set the flag false * When isDragging is true the JS action not be fired and is triggered by passing a specific drag threshold * isDragging is set false when touch down is detected on the DraggableRelativeLayout This is not finished currently, needs more detailed testing and clean up * This functionality will differentiate up and down direction of drag * Will account for all IAM types (top, bottom, center, full screen) * Wrapping up with dragging action issue with IAM * Added a dragThresholdY param to the DraggableRelativeLayout to create callback handling for onDraggingStart and onDraggingEnd * We have to calculate the threshold for each IAM type since top, bottom, center, and full screen all have different Y positions * Clean up after review * Wasn't adding threshold to the center modals and full screen IAMs * Changed DraggableListener to an interface and shortened names of onDragStart and onDragEnd * Added comments and minor clean up to callbacks being triggered * Update DraggableRelativeLayout.java
1 parent 8c5a957 commit 64afe46

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/DraggableRelativeLayout.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ class DraggableRelativeLayout extends RelativeLayout {
1616
private static final int MARGIN_PX_SIZE = dpToPx(28);
1717
private static final int EXTRA_PX_DISMISS = dpToPx(64);
1818

19-
static abstract class DraggableListener {
20-
void onDismiss() {}
19+
interface DraggableListener {
20+
// Callback for dismissing the MessageView
21+
void onDismiss();
22+
23+
// Callbacks for knowing when dragging has started and ended
24+
void onDragStart();
25+
void onDragEnd();
2126
}
2227

2328
private DraggableListener mListener;
@@ -30,6 +35,7 @@ static class Params {
3035

3136
int posY;
3237
int maxYPos;
38+
int dragThresholdY; // Y value associated with trigger for onDragStart() callback
3339
int maxXPos;
3440
int height;
3541
int messageHeight;
@@ -81,10 +87,20 @@ public boolean tryCaptureView(@NonNull View child, int pointerId) {
8187
public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
8288
lastYPos = top;
8389
if (params.dragDirection == Params.DRAGGABLE_DIRECTION_DOWN) {
90+
// Dragging down
91+
// If the top of the message is past the dragThresholdY trigger the onDragStart() callback
92+
if (top >= params.dragThresholdY && mListener != null)
93+
mListener.onDragStart();
94+
8495
if (top < params.maxYPos)
8596
return params.maxYPos;
8697
}
8798
else {
99+
// Dragging up
100+
// If the top of the message is past the dragThresholdY trigger the onDragStart() callback
101+
if (top <= params.dragThresholdY && mListener != null)
102+
mListener.onDragStart();
103+
88104
if (top > params.maxYPos)
89105
return params.maxYPos;
90106
}
@@ -105,14 +121,18 @@ public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel)
105121
if (lastYPos > params.dismissingYPos || yvel > params.dismissingYVelocity) {
106122
settleDestY = params.offScreenYPos;
107123
dismissing = true;
108-
if (mListener != null) { mListener.onDismiss(); }
124+
125+
if (mListener != null)
126+
mListener.onDismiss();
109127
}
110128
}
111129
else {
112130
if (lastYPos < params.dismissingYPos || yvel < params.dismissingYVelocity) {
113131
settleDestY = params.offScreenYPos;
114132
dismissing = true;
115-
if (mListener != null) { mListener.onDismiss(); }
133+
134+
if (mListener != null)
135+
mListener.onDismiss();
116136
}
117137
}
118138
}
@@ -129,6 +149,25 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
129149
if (dismissing)
130150
return true;
131151

152+
// Override our own touch event actions on the view
153+
switch (event.getAction()) {
154+
case MotionEvent.ACTION_DOWN:
155+
case MotionEvent.ACTION_POINTER_DOWN:
156+
// On touch downs we want to trigger the onDragEnd() callback
157+
158+
// This seems confusing, but because the JS action within the WebView will trigger on
159+
// touch up we need to stay in the onDragStart() state
160+
161+
// The fix for this is using on touch down to go to onDragEnd() state since we can
162+
// confirm that the view will be at its origin position
163+
164+
// Therefore, the logic inside of onDragEnd() would be executed before touch up
165+
// again and our JS action will execute properly
166+
if (mListener != null)
167+
mListener.onDragEnd();
168+
break;
169+
}
170+
132171
mDragHelper.processTouchEvent(event);
133172

134173
// Let child views get all touch events;

OneSignalSDK/onesignal/src/main/java/com/onesignal/InAppMessageView.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class InAppMessageView {
5353
private static final int ACTIVITY_FINISH_AFTER_DISMISS_DELAY_MS = 600;
5454
private static final int ACTIVITY_INIT_DELAY = 200;
5555
private static final int MARGIN_PX_SIZE = dpToPx(24);
56+
private static final int DRAG_THRESHOLD_PX_SIZE = dpToPx(4);
5657
private PopupWindow popupWindow;
5758

5859
interface InAppMessageViewListener {
@@ -67,6 +68,7 @@ interface InAppMessageViewListener {
6768
private double dismissDuration;
6869
private boolean hasBackground;
6970
private boolean shouldDismissWhenActive = false;
71+
private boolean isDragging = false;
7072
@NonNull private WebViewManager.Position displayLocation;
7173
private WebView webView;
7274
private RelativeLayout parentRelativeLayout;
@@ -191,17 +193,22 @@ private DraggableRelativeLayout.Params createDraggableLayoutParams(int pageHeigh
191193
draggableParams.messageHeight = pageHeight;
192194
draggableParams.height = getDisplayYSize();
193195

194-
if (displayLocation == WebViewManager.Position.FULL_SCREEN)
195-
draggableParams.messageHeight = pageHeight = getDisplayYSize() - (MARGIN_PX_SIZE * 2);
196-
197196
switch (displayLocation) {
197+
case TOP_BANNER:
198+
draggableParams.dragThresholdY = MARGIN_PX_SIZE - DRAG_THRESHOLD_PX_SIZE;
199+
break;
198200
case BOTTOM_BANNER:
199201
draggableParams.posY = getDisplayYSize() - pageHeight;
202+
draggableParams.dragThresholdY = MARGIN_PX_SIZE + DRAG_THRESHOLD_PX_SIZE;
200203
break;
201-
case CENTER_MODAL:
202204
case FULL_SCREEN:
203-
draggableParams.maxYPos = (getDisplayYSize() / 2) - (pageHeight / 2);
204-
draggableParams.posY = (getDisplayYSize() / 2) - (pageHeight / 2);
205+
draggableParams.messageHeight = pageHeight = getDisplayYSize() - (MARGIN_PX_SIZE * 2);
206+
// fall through for FULL_SCREEN since it shares similar params to CENTER_MODAL
207+
case CENTER_MODAL:
208+
int y = (getDisplayYSize() / 2) - (pageHeight / 2);
209+
draggableParams.dragThresholdY = y + DRAG_THRESHOLD_PX_SIZE;
210+
draggableParams.maxYPos = y;
211+
draggableParams.posY = y;
205212
break;
206213
}
207214

@@ -288,7 +295,7 @@ private void setUpParentLinearLayout(Context context) {
288295
parentRelativeLayout.addView(draggableRelativeLayout);
289296
}
290297

291-
private void setUpDraggableLayout(Context context,
298+
private void setUpDraggableLayout(final Context context,
292299
LinearLayout.LayoutParams linearLayoutParams,
293300
DraggableRelativeLayout.Params draggableParams) {
294301
draggableRelativeLayout = new DraggableRelativeLayout(context);
@@ -297,9 +304,19 @@ private void setUpDraggableLayout(Context context,
297304
draggableRelativeLayout.setParams(draggableParams);
298305
draggableRelativeLayout.setListener(new DraggableRelativeLayout.DraggableListener() {
299306
@Override
300-
void onDismiss() {
307+
public void onDismiss() {
301308
finishAfterDelay(null);
302309
}
310+
311+
@Override
312+
public void onDragStart() {
313+
isDragging = true;
314+
}
315+
316+
@Override
317+
public void onDragEnd() {
318+
isDragging = false;
319+
}
303320
});
304321

305322
if (webView.getParent() != null)
@@ -314,6 +331,13 @@ void onDismiss() {
314331
draggableRelativeLayout.addView(cardView);
315332
}
316333

334+
/**
335+
* Simple getter to know when the MessageView is in a dragging state
336+
*/
337+
boolean isDragging() {
338+
return isDragging;
339+
}
340+
317341
/**
318342
* To show drop shadow on WebView
319343
* Layout container for WebView is needed

OneSignalSDK/onesignal/src/main/java/com/onesignal/WebViewManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ public void postMessage(String message) {
161161

162162
if (messageType.equals(EVENT_TYPE_RENDERING_COMPLETE))
163163
handleRenderComplete(jsonObject);
164-
else if (messageType.equals(EVENT_TYPE_ACTION_TAKEN))
164+
else if (messageType.equals(EVENT_TYPE_ACTION_TAKEN) && !messageView.isDragging()) {
165+
// Added handling so that click actions won't trigger while dragging the IAM
165166
handleActionTaken(jsonObject);
167+
}
166168
} catch (JSONException e) {
167169
e.printStackTrace();
168170
}

0 commit comments

Comments
 (0)