Skip to content

Commit 8e15c0a

Browse files
author
“Akshay
committed
Code refactor
1 parent 723c6c9 commit 8e15c0a

File tree

1 file changed

+78
-94
lines changed

1 file changed

+78
-94
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppFragmentHTMLNotification.java

Lines changed: 78 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ public class IterableInAppFragmentHTMLNotification extends DialogFragment implem
5252

5353
private static final int DELAY_THRESHOLD_MS = 500;
5454

55-
@Nullable static IterableInAppFragmentHTMLNotification notification;
56-
@Nullable static IterableHelper.IterableUrlCallback clickCallback;
57-
@Nullable static IterableInAppLocation location;
55+
@Nullable
56+
static IterableInAppFragmentHTMLNotification notification;
57+
@Nullable
58+
static IterableHelper.IterableUrlCallback clickCallback;
59+
@Nullable
60+
static IterableInAppLocation location;
5861

5962
private IterableWebView webView;
6063
private boolean loaded;
6164
private OrientationEventListener orientationListener;
6265
private boolean callbackOnCancel = false;
6366
private String htmlString;
6467
private String messageId;
65-
68+
6669
// Resize debouncing fields
6770
private Handler resizeHandler = new Handler();
6871
private Runnable pendingResizeRunnable;
@@ -99,6 +102,7 @@ public static IterableInAppFragmentHTMLNotification createInstance(@NonNull Stri
99102

100103
/**
101104
* Returns the notification instance currently being shown
105+
*
102106
* @return notification instance
103107
*/
104108
public static IterableInAppFragmentHTMLNotification getInstance() {
@@ -115,33 +119,15 @@ public IterableInAppFragmentHTMLNotification() {
115119
insetPadding = new Rect();
116120
this.setStyle(DialogFragment.STYLE_NO_FRAME, androidx.appcompat.R.style.Theme_AppCompat_NoActionBar);
117121
}
118-
122+
119123
@Override
120124
public void onStart() {
121125
super.onStart();
122-
126+
123127
// Set dialog positioning after the dialog is created and shown
124128
Dialog dialog = getDialog();
125129
if (dialog != null) {
126-
Window window = dialog.getWindow();
127-
if (window != null) {
128-
WindowManager.LayoutParams windowParams = window.getAttributes();
129-
int startGravity = getVerticalLocation(insetPadding);
130-
131-
if (startGravity == Gravity.CENTER_VERTICAL) {
132-
windowParams.gravity = Gravity.CENTER;
133-
IterableLogger.d(TAG, "Set dialog gravity to CENTER in onStart");
134-
} else if (startGravity == Gravity.TOP) {
135-
windowParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
136-
IterableLogger.d(TAG, "Set dialog gravity to TOP in onStart");
137-
} else if (startGravity == Gravity.BOTTOM) {
138-
windowParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
139-
IterableLogger.d(TAG, "Set dialog gravity to BOTTOM in onStart");
140-
}
141-
142-
window.setAttributes(windowParams);
143-
IterableLogger.d(TAG, "Applied window gravity in onStart: " + windowParams.gravity);
144-
}
130+
applyWindowGravity(dialog.getWindow(), "onStart");
145131
}
146132
}
147133

@@ -183,25 +169,10 @@ public void onCancel(DialogInterface dialog) {
183169
}
184170
});
185171
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
186-
172+
187173
// Set window gravity for the dialog
188-
Window window = dialog.getWindow();
189-
WindowManager.LayoutParams windowParams = window.getAttributes();
190-
int dialogGravity = getVerticalLocation(insetPadding);
191-
192-
if (dialogGravity == Gravity.CENTER_VERTICAL) {
193-
windowParams.gravity = Gravity.CENTER;
194-
IterableLogger.d(TAG, "Set dialog gravity to CENTER in onCreateDialog");
195-
} else if (dialogGravity == Gravity.TOP) {
196-
windowParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
197-
IterableLogger.d(TAG, "Set dialog gravity to TOP in onCreateDialog");
198-
} else if (dialogGravity == Gravity.BOTTOM) {
199-
windowParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
200-
IterableLogger.d(TAG, "Set dialog gravity to BOTTOM in onCreateDialog");
201-
}
202-
203-
window.setAttributes(windowParams);
204-
174+
applyWindowGravity(dialog.getWindow(), "onCreateDialog");
175+
205176
if (getInAppLayout(insetPadding) == InAppLayout.FULLSCREEN) {
206177
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
207178
} else if (getInAppLayout(insetPadding) != InAppLayout.TOP) {
@@ -220,45 +191,30 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
220191
if (getInAppLayout(insetPadding) == InAppLayout.FULLSCREEN) {
221192
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
222193
}
223-
194+
224195
// Set initial window gravity based on inset padding
225-
Window window = getDialog().getWindow();
226-
WindowManager.LayoutParams windowParams = window.getAttributes();
227-
int windowGravity = getVerticalLocation(insetPadding);
228-
229-
if (windowGravity == Gravity.CENTER_VERTICAL) {
230-
windowParams.gravity = Gravity.CENTER;
231-
IterableLogger.d(TAG, "Set initial CENTER window gravity in onCreateView");
232-
} else if (windowGravity == Gravity.TOP) {
233-
windowParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
234-
IterableLogger.d(TAG, "Set initial TOP window gravity in onCreateView");
235-
} else if (windowGravity == Gravity.BOTTOM) {
236-
windowParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
237-
IterableLogger.d(TAG, "Set initial BOTTOM window gravity in onCreateView");
238-
}
239-
240-
window.setAttributes(windowParams);
196+
applyWindowGravity(getDialog().getWindow(), "onCreateView");
241197

242198
webView = new IterableWebView(getContext());
243199
webView.setId(R.id.webView);
244-
200+
245201
// Debug the HTML content
246202
IterableLogger.d(TAG, "HTML content preview: " + (htmlString.length() > 200 ? htmlString.substring(0, 200) + "..." : htmlString));
247-
203+
248204
webView.createWithHtml(this, htmlString);
249205

250206
if (orientationListener == null) {
251207
orientationListener = new OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) {
252208
private int lastOrientation = -1;
253-
209+
254210
// Resize the webView on device rotation
255211
public void onOrientationChanged(int orientation) {
256212
if (loaded && webView != null) {
257213
// Only trigger on significant orientation changes (90 degree increments)
258214
int currentOrientation = ((orientation + 45) / 90) * 90;
259215
if (currentOrientation != lastOrientation && lastOrientation != -1) {
260216
lastOrientation = currentOrientation;
261-
217+
262218
// Use longer delay for orientation changes to allow layout to stabilize
263219
final Handler handler = new Handler();
264220
handler.postDelayed(new Runnable() {
@@ -280,19 +236,19 @@ public void run() {
280236

281237
// Create a FrameLayout as the main container for better positioning control
282238
FrameLayout frameLayout = new FrameLayout(this.getContext());
283-
239+
284240
// Create a RelativeLayout as a wrapper for the WebView
285241
RelativeLayout webViewContainer = new RelativeLayout(this.getContext());
286-
242+
287243
int gravity = getVerticalLocation(insetPadding);
288244
IterableLogger.d(TAG, "Initial setup - gravity: " + gravity + " for inset padding: " + insetPadding);
289-
245+
290246
// Set FrameLayout gravity based on positioning
291247
FrameLayout.LayoutParams containerParams = new FrameLayout.LayoutParams(
292-
FrameLayout.LayoutParams.MATCH_PARENT,
248+
FrameLayout.LayoutParams.MATCH_PARENT,
293249
FrameLayout.LayoutParams.WRAP_CONTENT
294250
);
295-
251+
296252
if (gravity == Gravity.CENTER_VERTICAL) {
297253
containerParams.gravity = Gravity.CENTER;
298254
IterableLogger.d(TAG, "Applied CENTER gravity to container");
@@ -303,20 +259,20 @@ public void run() {
303259
containerParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
304260
IterableLogger.d(TAG, "Applied BOTTOM gravity to container");
305261
}
306-
262+
307263
// Add WebView to the RelativeLayout container with WRAP_CONTENT for proper sizing
308264
RelativeLayout.LayoutParams webViewParams = new RelativeLayout.LayoutParams(
309-
RelativeLayout.LayoutParams.WRAP_CONTENT,
265+
RelativeLayout.LayoutParams.WRAP_CONTENT,
310266
RelativeLayout.LayoutParams.WRAP_CONTENT
311267
);
312268
webViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
313269
webViewContainer.addView(webView, webViewParams);
314-
270+
315271
IterableLogger.d(TAG, "Added WebView with WRAP_CONTENT and CENTER_IN_PARENT rule");
316-
272+
317273
// Add the container to the FrameLayout
318274
frameLayout.addView(webViewContainer, containerParams);
319-
275+
320276
IterableLogger.d(TAG, "Created FrameLayout with positioned RelativeLayout container");
321277

322278
if (savedInstanceState == null || !savedInstanceState.getBoolean(IN_APP_OPEN_TRACKED, false)) {
@@ -519,7 +475,7 @@ private void hideWebView() {
519475

520476
try {
521477
Animation anim = AnimationUtils.loadAnimation(getContext(),
522-
animationResource);
478+
animationResource);
523479
anim.setDuration(IterableConstants.ITERABLE_IN_APP_ANIMATION_DURATION);
524480
webView.startAnimation(anim);
525481
} catch (Exception e) {
@@ -563,50 +519,51 @@ public void runResizeScript() {
563519
if (pendingResizeRunnable != null) {
564520
resizeHandler.removeCallbacks(pendingResizeRunnable);
565521
}
566-
522+
567523
// Schedule a debounced resize operation
568524
pendingResizeRunnable = new Runnable() {
569525
@Override
570526
public void run() {
571527
performResizeWithValidation();
572528
}
573529
};
574-
530+
575531
resizeHandler.postDelayed(pendingResizeRunnable, RESIZE_DEBOUNCE_DELAY_MS);
576532
}
577-
533+
578534
private void performResizeWithValidation() {
579535
if (webView == null) {
580536
IterableLogger.w(TAG, "WebView is null, skipping resize");
581537
return;
582538
}
583-
539+
584540
float currentHeight = webView.getContentHeight();
585-
541+
586542
// Validate content height
587543
if (currentHeight <= 0) {
588544
IterableLogger.w(TAG, "Invalid content height: " + currentHeight + "dp, skipping resize");
589545
return;
590546
}
591-
547+
592548
// Check if height has stabilized (avoid unnecessary resizes for same height)
593549
if (Math.abs(currentHeight - lastContentHeight) < 1.0f) {
594550
IterableLogger.d(TAG, "Content height unchanged (" + currentHeight + "dp), skipping resize");
595551
return;
596552
}
597-
553+
598554
lastContentHeight = currentHeight;
599-
555+
600556
IterableLogger.d(
601-
TAG,
602-
"💚 Resizing in-app to height: " + currentHeight + "dp"
557+
TAG,
558+
"💚 Resizing in-app to height: " + currentHeight + "dp"
603559
);
604-
560+
605561
resize(currentHeight);
606562
}
607563

608564
/**
609565
* Resizes the dialog window based upon the size of its webView HTML content
566+
*
610567
* @param height
611568
*/
612569
public void resize(final float height) {
@@ -621,7 +578,7 @@ public void run() {
621578
try {
622579
// Since this is run asynchronously, notification might've been dismissed already
623580
if (getContext() == null || notification == null || notification.getDialog() == null ||
624-
notification.getDialog().getWindow() == null || !notification.getDialog().isShowing()) {
581+
notification.getDialog().getWindow() == null || !notification.getDialog().isShowing()) {
625582
return;
626583
}
627584

@@ -654,14 +611,14 @@ public void run() {
654611
float relativeHeight = height * getResources().getDisplayMetrics().density;
655612
int newWebViewWidth = getResources().getDisplayMetrics().widthPixels;
656613
int newWebViewHeight = (int) relativeHeight;
657-
614+
658615
// Set WebView to explicit size
659616
RelativeLayout.LayoutParams webViewParams = new RelativeLayout.LayoutParams(newWebViewWidth, newWebViewHeight);
660-
617+
661618
// Apply positioning based on gravity
662619
int resizeGravity = getVerticalLocation(insetPadding);
663620
IterableLogger.d(TAG, "Resizing WebView directly - gravity: " + resizeGravity + " size: " + newWebViewWidth + "x" + newWebViewHeight + "px for inset padding: " + insetPadding);
664-
621+
665622
if (resizeGravity == Gravity.CENTER_VERTICAL) {
666623
webViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
667624
IterableLogger.d(TAG, "Applied CENTER_IN_PARENT to WebView");
@@ -674,19 +631,19 @@ public void run() {
674631
webViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
675632
IterableLogger.d(TAG, "Applied BOTTOM alignment to WebView");
676633
}
677-
634+
678635
// Make dialog full screen to allow proper positioning
679636
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
680-
637+
681638
// Apply the new layout params to WebView
682639
webView.setLayoutParams(webViewParams);
683-
640+
684641
// Force layout updates
685642
webView.requestLayout();
686643
if (webView.getParent() instanceof ViewGroup) {
687644
((ViewGroup) webView.getParent()).requestLayout();
688645
}
689-
646+
690647
IterableLogger.d(TAG, "Applied explicit size and positioning to WebView: " + newWebViewWidth + "x" + newWebViewHeight);
691648
}
692649
} catch (IllegalArgumentException e) {
@@ -698,6 +655,7 @@ public void run() {
698655

699656
/**
700657
* Returns the vertical position of the dialog for the given padding
658+
*
701659
* @param padding
702660
* @return
703661
*/
@@ -711,6 +669,32 @@ int getVerticalLocation(Rect padding) {
711669
return gravity;
712670
}
713671

672+
/**
673+
* Sets the window gravity based on inset padding
674+
*
675+
* @param window The dialog window to configure
676+
* @param context Debug context string for logging
677+
*/
678+
private void applyWindowGravity(Window window, String context) {
679+
if (window == null) {
680+
return;
681+
}
682+
683+
WindowManager.LayoutParams windowParams = window.getAttributes();
684+
int gravity = getVerticalLocation(insetPadding);
685+
686+
if (gravity == Gravity.CENTER_VERTICAL) {
687+
windowParams.gravity = Gravity.CENTER;
688+
} else if (gravity == Gravity.TOP) {
689+
windowParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
690+
} else if (gravity == Gravity.BOTTOM) {
691+
windowParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
692+
}
693+
694+
window.setAttributes(windowParams);
695+
IterableLogger.d(TAG, "Set window gravity in " + context + ": " + windowParams.gravity);
696+
}
697+
714698
InAppLayout getInAppLayout(Rect padding) {
715699
if (padding.top == 0 && padding.bottom == 0) {
716700
return InAppLayout.FULLSCREEN;

0 commit comments

Comments
 (0)