Skip to content

Commit

Permalink
Merge pull request #27 from TorryDo/composeFix-refactorCode
Browse files Browse the repository at this point in the history
Compose fix refactor code
  • Loading branch information
TorryDo committed Apr 6, 2023
2 parents db910c3 + 42df04f commit 599b231
Show file tree
Hide file tree
Showing 20 changed files with 696 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal open class BaseFloatingViewBinding<T : ViewBinding>(
if(binding.root.windowToken != null) return
super.show(binding.root)
}catch (e: Exception){
Log.d("<>", "show: ${e.stackTraceToString()}");
// Log.d("<>", "show: ${e.stackTraceToString()}"); this line show error in some cases
}
}

Expand All @@ -37,7 +37,7 @@ internal open class BaseFloatingViewBinding<T : ViewBinding>(
try {
super.remove(binding.root)
}catch (e: Exception){
e.printStackTrace()
Log.e("<>", e.stackTraceToString())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.torrydo.floatingbubbleview

import android.view.KeyEvent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect

/**
* override "dispatchKeyEvent" of the floating composable
*
* */
@Composable
fun OverrideDispatchKeyEvent(handler: (KeyEvent?) -> Boolean?) {
DisposableEffect(Unit){
FloatingComposeView.keyEventHandler = handler
onDispose {
// FloatingComposeView.keyEventHandler = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ComposeLifecycleOwner : LifecycleOwner, SavedStateRegistryOwner {

ViewTreeLifecycleOwner.set(decorView, this)
decorView.setViewTreeSavedStateRegistryOwner(this)

}

// LifecycleOwner methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ package com.torrydo.floatingbubbleview

class PermissionDeniedException : Exception() {
override val message: String
get() = "display-over-other-app permission IS NOT granted!"
}

class NullViewException(private val inputMessage: String?) : Exception() {
override val message: String?
get() = inputMessage
get() = "\"display over other app\" permission IS NOT granted!"
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import android.view.View
import android.view.WindowManager
import androidx.annotation.StyleRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ComposeView

class ExpandableView(
private val builder: Builder,
) : BaseFloatingView(builder.context) {

private var isComposeInitialized = false

init {
setupLayoutParams()
}
Expand All @@ -20,7 +21,6 @@ class ExpandableView(

interface Action {
fun popToBubble() {}

}

interface Listener {
Expand All @@ -30,8 +30,6 @@ class ExpandableView(

// public --------------------------------------------------------------------------------------


private var isInitialized = false
fun show() {

if (builder.view != null) {
Expand All @@ -44,18 +42,14 @@ class ExpandableView(
throw Exception("You doesn't specify compose or view, please review your code!")
}

if (isInitialized) {
// ContextCompat.getMainExecutor(builder.context).execute {}
builder.composeLifecycleOwner?.onStart()
builder.composeLifecycleOwner?.onResume()

} else {
if (isComposeInitialized.not()) {
builder.composeLifecycleOwner?.onCreate()
builder.composeLifecycleOwner?.onStart()

isInitialized = true
isComposeInitialized = true
}

builder.composeLifecycleOwner?.onStart()
builder.composeLifecycleOwner?.onResume()


super.show(builder.composeView!!)

Expand All @@ -64,23 +58,31 @@ class ExpandableView(


fun remove() {
builder.view?.let {
super.remove(it)
if(builder.view != null){
super.remove(builder.view!!)
builder.listener.onCloseExpandableView()
return
}

// it's important to check if the expandable-view is initialized or not,
// if you blindly end the compose's lifecycle, error appear
if (isComposeInitialized.not()) {
return
}

builder.composeView?.also {
super.remove(it)
builder.composeLifecycleOwner?.onPause()
builder.composeLifecycleOwner?.onStop()

builder.composeLifecycleOwner?.onDestroy()
super.remove(it)
}
builder.listener.onCloseExpandableView()
}

override fun destroy() {
super.destroy()
builder.composeLifecycleOwner?.onStop()
builder.composeLifecycleOwner?.onDestroy()

super.destroy()
}


Expand Down Expand Up @@ -112,7 +114,7 @@ class ExpandableView(
internal var viewStyle: Int? = R.style.default_bubble_style

internal var composeLifecycleOwner: ComposeLifecycleOwner? = null
internal var composeView: ComposeView? = null
internal var composeView: FloatingComposeView? = null

internal var listener = object : Listener {}

Expand All @@ -130,7 +132,7 @@ class ExpandableView(
if (view != null) {
throw IllegalStateException("Cannot pass composable after setting view")
}
composeView = ComposeView(context).apply {
composeView = FloatingComposeView(context).apply {
setContent { content() }
}
composeLifecycleOwner = ComposeLifecycleOwner()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.Bitmap
import android.graphics.Point
import android.util.Size
import android.view.View
import androidx.annotation.Discouraged
import androidx.annotation.DrawableRes
import androidx.annotation.StyleRes
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -72,8 +73,10 @@ internal constructor(

fun onClick() {}

fun onDestroy() {}
}

internal interface ServiceInteractor{
fun requestStop()
}

// internal func ---------------------------------------------------------------------------------
Expand All @@ -92,8 +95,8 @@ internal constructor(
}

internal fun tryRemoveCloseBubbleAndBackground() {
bottomBackground?.remove()
closeBubbleView?.remove()
bottomBackground?.remove()
}

// private func --------------------------------------------------------------------------------
Expand Down Expand Up @@ -152,7 +155,7 @@ internal constructor(
}

if (shouldDestroy) {
builder.listener?.onDestroy()
builder.serviceInteractor?.requestStop()
} else {
if (builder.isAnimateToEdgeEnabled) {
bubbleView.animateIconToEdge()
Expand Down Expand Up @@ -207,6 +210,7 @@ internal constructor(
internal var closablePerimeterDp = 100

internal var listener: Listener? = null
internal var serviceInteractor: ServiceInteractor? = null

internal var behavior: BubbleBehavior = BubbleBehavior.FIXED_CLOSE_BUBBLE

Expand Down Expand Up @@ -366,15 +370,15 @@ internal constructor(
listener.onUp(x, y)
}

override fun onDestroy() {
tempListener?.onDestroy()
listener.onDestroy()
}

}
return this
}

internal fun addServiceInteractor(interactor: ServiceInteractor): Builder{
this.serviceInteractor = interactor
return this
}

/**
* examples: x=0, y=0 show bubble on the top-left corner of the screen.
*
Expand Down
Loading

0 comments on commit 599b231

Please sign in to comment.