Skip to content

Commit 352756d

Browse files
authored
refactor: prevent destroying modal on close (#3)
1 parent db05089 commit 352756d

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

library/src/main/java/com/paypal/messages/ModalFragment.kt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.paypal.messages
22

33
import android.annotation.SuppressLint
44
import android.app.Dialog
5-
import android.content.DialogInterface
65
import android.content.Intent
76
import android.graphics.Color
87
import android.graphics.PorterDuff
@@ -49,10 +48,8 @@ import com.paypal.messages.utils.LogCat
4948
import java.net.URI
5049
import java.util.UUID
5150
import kotlin.system.measureTimeMillis
52-
import com.google.android.material.R as MaterialR
5351
import com.paypal.messages.config.PayPalMessageOfferType as OfferType
5452

55-
5653
@RequiresApi(Build.VERSION_CODES.M)
5754
internal class ModalFragment constructor(
5855
private val clientId: String,
@@ -76,6 +73,7 @@ internal class ModalFragment constructor(
7673
private var currentUrl: String? = null
7774
private var webView: WebView? = null
7875
private var rootView: View? = null
76+
private var dialog: BottomSheetDialog? = null
7977
private var closeButtonData: ModalCloseButton? = null
8078
private var instanceId = UUID.randomUUID()
8179

@@ -136,12 +134,12 @@ internal class ModalFragment constructor(
136134

137135
closeButton?.setOnClickListener {
138136
logEvent(TrackingEvent(eventType = EventType.MODAL_CLOSE))
139-
this.dismiss()
137+
dialog?.hide()
140138
}
141139

142140
// If we already have a WebView, don't reset it
143141
LogCat.debug(TAG, "Configuring WebView Settings and Handlers")
144-
val webView = rootView.findViewById<WebView>(R.id.ModalWebView)
142+
val webView = rootView.findViewById<RoundedWebView>(R.id.ModalWebView)
145143

146144
// Programmatically set bottom margin instead of in XML since we also apply it to the
147145
// dialog behavior below to control it in a single location. The expanded offset below shifts
@@ -250,23 +248,24 @@ internal class ModalFragment constructor(
250248
}
251249

252250
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
253-
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
254-
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
255-
dialog.behavior.isFitToContents = false
256-
dialog.behavior.expandedOffset = offsetTop
257-
dialog.behavior.isHideable = false
258-
dialog.behavior.isDraggable = false
259-
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
260-
dialog.setOnShowListener { setupBottomSheet(it) }
251+
setStyle(STYLE_NO_FRAME, R.style.BottomSheetDialog)
252+
253+
val dialog = (super.onCreateDialog(savedInstanceState) as BottomSheetDialog).apply {
254+
window?.setBackgroundDrawableResource(android.R.color.transparent)
255+
behavior.isFitToContents = false
256+
behavior.expandedOffset = offsetTop
257+
behavior.isHideable = true
258+
behavior.isDraggable = true
259+
behavior.state = BottomSheetBehavior.STATE_EXPANDED
260+
}
261+
262+
this.dialog = dialog
261263

262264
return dialog
263265
}
264266

265-
private fun setupBottomSheet(dialogInterface: DialogInterface) {
266-
val bottomSheetDialog = dialogInterface as BottomSheetDialog
267-
val bottomSheet = bottomSheetDialog.findViewById<View>(MaterialR.id.design_bottom_sheet)
268-
if (bottomSheet === null) return
269-
bottomSheet.setBackgroundColor(Color.TRANSPARENT)
267+
fun expand() {
268+
this.dialog?.show()
270269
}
271270

272271
fun init(config: ModalConfig) {

library/src/main/java/com/paypal/messages/PayPalMessageView.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,9 @@ class PayPalMessageView @JvmOverloads constructor(
324324
}
325325

326326
private fun showWebView(response: ActionResponse) {
327-
// Does an instance of the modal exist already?
328-
if (modal != null) {
329-
modal!!.show((context as AppCompatActivity).supportFragmentManager, modal!!.tag)
330-
}
331-
else {
332-
// if it doesn't, instantiate the modal
333-
val modal = ModalFragment(clientId)
334-
val fragmentManager = (context as AppCompatActivity).supportFragmentManager
335327

328+
val modal = modal ?: run {
329+
val modal = ModalFragment(clientId)
336330
// Build modal config
337331
val modalConfig = ModalConfig(
338332
amount = amount,
@@ -352,8 +346,23 @@ class PayPalMessageView @JvmOverloads constructor(
352346

353347
modal.init(modalConfig)
354348
modal.show((context as AppCompatActivity).supportFragmentManager, modal.tag)
349+
355350
this.modal = modal
351+
352+
modal
356353
}
354+
355+
// modal.show() above will display the modal on initial view, but if the user closes the modal
356+
// it will become visually hidden and this method will re-display the modal without
357+
// attempting to reattach it
358+
modal.expand()
359+
}
360+
361+
override fun onDetachedFromWindow() {
362+
super.onDetachedFromWindow()
363+
// The modal will not dismiss (destroy) itself, it will only hide/show when opening and closing
364+
// so we need to cleanup the modal instance if the message is removed
365+
this.modal?.dismiss()
357366
}
358367

359368
fun refresh() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.BottomSheetDialog">
4+
<item name="bottomSheetStyle">@style/BottomSheetModal</item>
5+
</style>
6+
7+
<style name="BottomSheetModal" parent="Widget.Design.BottomSheet.Modal">
8+
<item name="android:background">@android:color/transparent</item>
9+
</style>
10+
</resources>

0 commit comments

Comments
 (0)