Compose UI (recompose) with window Manager not working
Asked Answered
A

1

7

I'm working on floating window on android and i'm having a problem. So I have my activity that calls my floating class that creates a the view and adds it to the windowmanager with a timer that is displayed and counting down. When trying the same code in a normal activity it works find :S?

Here is my viewmodel holding my mutableStateOf value

class WicViewModel : ViewModel() {

private val TAG = "WicViewModel"

private val a = 10000L
private val b = 1000L

private var _input: Long by mutableStateOf(a)

var input: Long
    get() = _input
    set(value) {
        if (value == _input) return
        _input = value

    }


fun start() {
    val timer = object : CountDownTimer(a, b) {
        override fun onTick(millisUntilFinished: Long) {
            Log.d("MYLOG", "text updated programmatically")
            input = millisUntilFinished

        }

        override fun onFinish() {
            input = 0
        }
    }
    timer.start()

}
}

Here is my class with the compose into windowmanager

class WicController constructor(val context: Context?, val viewModel: WicViewModel) {

private val TAG = "WicController"
private var windowManager: WindowManager? = null
private var wicView: View? = null
private lateinit var lifecycleOwner: CDOLifecycleOwner


fun show() {
    if (context == null) return
    val paramFloat = WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        getWindowType(),
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT
    )
    paramFloat.gravity = Gravity.CENTER or Gravity.END

    windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager?


    val composeView = ComposeView(context.applicationContext)
    composeView.setContent {
        timerText(viewModel)
    }

    lifecycleOwner = CDOLifecycleOwner()
    lifecycleOwner.performRestore(null)
    lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
    ViewTreeLifecycleOwner.set(composeView, lifecycleOwner)
    ViewTreeSavedStateRegistryOwner.set(composeView, lifecycleOwner)
    windowManager?.addView(composeView, paramFloat)

}

fun hide() {
    windowManager?.removeView(wicView!!)
    context?.let { Demo.startCalldorado(it) }
}

private fun getWindowType(): Int {
    var windowType = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        windowType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
    }
    return windowType
}


@Composable
fun timerText(viewModel: WicViewModel){
    Text(
        text = (viewModel.input / 1000).toString(),
        modifier = Modifier
            .padding(bottom = 8.dp)
            .fillMaxWidth(),
        fontSize = 13.sp,
        color = Color.White,

        )
}}

I know that the value is being changed but the compose is not recomposing itself sometimes it may change but it very random.

I dont know if im doing it wrong or if compose is not made for windowmanager/floating window use, however I hope some of you guys on here can help me out since I cant find anything on the web about this.

Accede answered 17/8, 2021 at 11:55 Comment(1)
same situation here, did you have any luck?Blinders
L
1

Jetpack Compose, recomposition won't trigger when using custom Lifecycle/ViewModelStore/SavedStateRegistry Owner

this answer will help you.

val viewModelStore = ViewModelStore()
lifecycleOwner.performRestore(null)
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START)
ViewTreeLifecycleOwner.set(composeView, lifecycleOwner)
ViewTreeViewModelStoreOwner.set(composeView) { viewModelStore }
it.setViewTreeSavedStateRegistryOwner(lifecycleOwner)
Lidialidice answered 1/11, 2023 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.