From 95104215d2727eb8013fde0554335a96dd57a413 Mon Sep 17 00:00:00 2001 From: lodev09 Date: Wed, 27 Mar 2024 05:41:30 +0800 Subject: [PATCH] refactor: cleanup --- .../truesheet/TrueSheetRootViewGroup.kt | 1 - .../com/lodev09/truesheet/TrueSheetView.kt | 107 ++++-------------- 2 files changed, 24 insertions(+), 84 deletions(-) diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetRootViewGroup.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetRootViewGroup.kt index dc3307b..ca8d767 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetRootViewGroup.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetRootViewGroup.kt @@ -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 diff --git a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt index 27515b0..e1bd1ff 100644 --- a/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt +++ b/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt @@ -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) { @@ -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) { // 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 } @@ -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() { @@ -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 {