Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Mar 26, 2024
1 parent 9f541cc commit 9510421
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerModule
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.views.view.ReactViewGroup
import kotlin.math.abs

/**
* TrueSheetRootViewGroup is the ViewGroup which contains all the children of a Modal. It gets all
Expand Down
107 changes: 24 additions & 83 deletions android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
package com.lodev09.truesheet

import android.content.Context
import android.content.DialogInterface.OnDismissListener
import android.content.DialogInterface.OnShowListener
import android.view.View
import android.view.ViewGroup
import android.view.ViewStructure
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import android.widget.FrameLayout
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.EventDispatcher
import com.google.android.material.bottomsheet.BottomSheetDialog

open class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventListener {
// This listener is called when the user presses KeyEvent.KEYCODE_BACK
// An event is then passed to JS which can either close or not close the Modal by setting the
// visible property

private val rootView: TrueSheetRootViewGroup?

private var sheetDialog: BottomSheetDialog? = null
private var onShowListener: OnShowListener? = null
private var onDismissListener: OnDismissListener? = null
class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventListener {
private val sheetRootView: TrueSheetRootViewGroup?
private var sheetDialog: BottomSheetDialog?

private val reactContext: ThemedReactContext
get() = context as ThemedReactContext

init {
reactContext.addLifecycleEventListener(this)
rootView = TrueSheetRootViewGroup(context)
}

private fun destroyDialog() {
sheetDialog = null

// We need to remove the mHostView from the parent
// It is possible we are dismissing this dialog and reattaching the hostView to another
val parent = rootView!!.parent as ViewGroup
parent.removeViewAt(0)
sheetRootView = TrueSheetRootViewGroup(context)
sheetDialog = BottomSheetDialog(context)
}

override fun dispatchProvideStructure(structure: ViewStructure) {
rootView!!.dispatchProvideStructure(structure)
sheetRootView?.dispatchProvideStructure(structure)
}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
Expand All @@ -60,39 +41,41 @@ open class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventL
// Hide this host view
visibility = GONE

UiThreadUtil.assertOnUiThread()
rootView!!.addView(child, index)
sheetRootView?.addView(child, index)

val frameLayout = FrameLayout(context)
frameLayout.addView(sheetRootView)

sheetDialog?.setContentView(frameLayout)
}

override fun getChildCount(): Int {
// This method may be called by the parent constructor
// before mHostView is initialized.
return rootView?.childCount ?: 0
// before rootView is initialized.
return sheetRootView?.childCount ?: 0
}

override fun getChildAt(index: Int): View {
return rootView!!.getChildAt(index)
return sheetRootView!!.getChildAt(index)
}

override fun removeView(child: View) {
UiThreadUtil.assertOnUiThread()
rootView!!.removeView(child)
sheetRootView?.removeView(child)
}

override fun removeViewAt(index: Int) {
UiThreadUtil.assertOnUiThread()
val child = getChildAt(index)
rootView!!.removeView(child)
sheetRootView?.removeView(child)
}

override fun addChildrenForAccessibility(outChildren: ArrayList<View>) {
// Explicitly override this to prevent accessibility events being passed down to children
// Those will be handled by the mHostView which lives in the dialog
// Those will be handled by the rootView which lives in the dialog
}

override fun dispatchPopulateAccessibilityEvent(event: AccessibilityEvent): Boolean {
// Explicitly override this to prevent accessibility events being passed down to children
// Those will be handled by the mHostView which lives in the dialog
// Those will be handled by the rootView which lives in the dialog
return false
}

Expand All @@ -101,16 +84,8 @@ open class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventL
dismiss()
}

protected fun setOnShowListener(listener: OnShowListener?) {
onShowListener = listener
}

protected fun setOnDismissListener(listener: OnDismissListener?) {
onDismissListener = listener
}

fun setEventDispatcher(eventDispatcher: EventDispatcher) {
rootView!!.setEventDispatcher(eventDispatcher)
sheetRootView?.setEventDispatcher(eventDispatcher)
}

override fun onHostResume() {
Expand All @@ -126,46 +101,12 @@ open class TrueSheetView(context: Context) : ViewGroup(context), LifecycleEventL
onDropInstance()
}

fun dismiss() {
UiThreadUtil.assertOnUiThread()

if (sheetDialog != null && sheetDialog!!.isShowing) {
sheetDialog!!.dismiss()
}

destroyDialog()
fun present() {
sheetDialog?.show()
}

fun present() {
UiThreadUtil.assertOnUiThread()
if (sheetDialog == null) {

/**
* View that will be the root view of the dialog. We are wrapping this in a
* FrameLayout because this is the system's way of notifying us that the dialog size has changed.
* This has the pleasant side-effect of us not having to preface all Modals with "top:
* statusBarHeight", since that margin will be included in the FrameLayout.
*/
val frameLayout = FrameLayout(context)
frameLayout.addView(rootView)
frameLayout.fitsSystemWindows = true

sheetDialog = BottomSheetDialog(context)

sheetDialog!!
.window
?.setFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
)

sheetDialog!!.setContentView(frameLayout)
sheetDialog!!.setOnShowListener(onShowListener)
sheetDialog!!.setOnDismissListener(onDismissListener)

sheetDialog!!.show()
sheetDialog!!.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
fun dismiss() {
sheetDialog?.dismiss()
}

companion object {
Expand Down

0 comments on commit 9510421

Please sign in to comment.