Meshtastic-Android/app/src/main/java/com/geeksville/mesh/ui/ComposeFragment.kt

40 lines
1.1 KiB
Kotlin
Raw Normal View History

2020-04-07 11:27:51 -07:00
package com.geeksville.mesh.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.compose.Composable
import androidx.ui.core.setContent
fun androidx.fragment.app.Fragment.setComposable(
id: Int,
content: @Composable() () -> Unit
): View? =
context?.let {
FrameLayout(it).apply {
this.id =
id // Compose requires a unique ID for the containing view to make savedInstanceState work
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
setContent(content)
}
}
2020-04-07 12:13:50 -07:00
open class ComposeFragment(
screenName: String,
id: Int,
private val content: @Composable() () -> Unit
) :
ScreenFragment(screenName) {
2020-04-07 11:27:51 -07:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
setComposable(id, content)
}