Skip to content

Commit

Permalink
refactor: prevent destroying modal on close (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seavenly committed Sep 8, 2023
1 parent db05089 commit ae7e032
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
35 changes: 17 additions & 18 deletions library/src/main/java/com/paypal/messages/ModalFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.paypal.messages

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Color
import android.graphics.PorterDuff
Expand Down Expand Up @@ -49,10 +48,8 @@ import com.paypal.messages.utils.LogCat
import java.net.URI
import java.util.UUID
import kotlin.system.measureTimeMillis
import com.google.android.material.R as MaterialR
import com.paypal.messages.config.PayPalMessageOfferType as OfferType


@RequiresApi(Build.VERSION_CODES.M)
internal class ModalFragment constructor(
private val clientId: String,
Expand All @@ -76,6 +73,7 @@ internal class ModalFragment constructor(
private var currentUrl: String? = null
private var webView: WebView? = null
private var rootView: View? = null
private var dialog: BottomSheetDialog? = null
private var closeButtonData: ModalCloseButton? = null
private var instanceId = UUID.randomUUID()

Expand Down Expand Up @@ -136,12 +134,12 @@ internal class ModalFragment constructor(

closeButton?.setOnClickListener {
logEvent(TrackingEvent(eventType = EventType.MODAL_CLOSE))
this.dismiss()
dialog?.hide()
}

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

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

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.behavior.isFitToContents = false
dialog.behavior.expandedOffset = offsetTop
dialog.behavior.isHideable = false
dialog.behavior.isDraggable = false
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
dialog.setOnShowListener { setupBottomSheet(it) }
setStyle(STYLE_NO_FRAME, R.style.BottomSheetDialog)

val dialog = (super.onCreateDialog(savedInstanceState) as BottomSheetDialog).apply {
window?.setBackgroundDrawableResource(android.R.color.transparent)
behavior.isFitToContents = false
behavior.expandedOffset = offsetTop
behavior.isHideable = true
behavior.isDraggable = true
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}

this.dialog = dialog

return dialog
}

private fun setupBottomSheet(dialogInterface: DialogInterface) {
val bottomSheetDialog = dialogInterface as BottomSheetDialog
val bottomSheet = bottomSheetDialog.findViewById<View>(MaterialR.id.design_bottom_sheet)
if (bottomSheet === null) return
bottomSheet.setBackgroundColor(Color.TRANSPARENT)
fun expand() {
this.dialog?.show()
}

fun init(config: ModalConfig) {
Expand Down
25 changes: 17 additions & 8 deletions library/src/main/java/com/paypal/messages/PayPalMessageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,9 @@ class PayPalMessageView @JvmOverloads constructor(
}

private fun showWebView(response: ActionResponse) {
// Does an instance of the modal exist already?
if (modal != null) {
modal!!.show((context as AppCompatActivity).supportFragmentManager, modal!!.tag)
}
else {
// if it doesn't, instantiate the modal
val modal = ModalFragment(clientId)
val fragmentManager = (context as AppCompatActivity).supportFragmentManager

val modal = modal ?: run {
val modal = ModalFragment(clientId)
// Build modal config
val modalConfig = ModalConfig(
amount = amount,
Expand All @@ -352,8 +346,23 @@ class PayPalMessageView @JvmOverloads constructor(

modal.init(modalConfig)
modal.show((context as AppCompatActivity).supportFragmentManager, modal.tag)

this.modal = modal

modal
}

// modal.show() above will display the modal on initial view, but if the user closes the modal
// it will become visually hidden and this method will re-display the modal without
// attempting to reattach it
modal.expand()
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
// The modal will not dismiss (destroy) itself, it will only hide/show when opening and closing
// so we need to cleanup the modal instance if the message is removed
this.modal?.dismiss()
}

fun refresh() {
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetModal</item>
</style>

<style name="BottomSheetModal" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
</resources>

0 comments on commit ae7e032

Please sign in to comment.