feat: add State

This commit is contained in:
2025-05-06 16:52:05 +08:00
parent 7c8c0256e8
commit 036017a804
3 changed files with 293 additions and 0 deletions

View File

@@ -444,6 +444,66 @@ val subLayout = Hikageable<LinearLayout.LayoutParams> {
}
```
### State Management
Hikage has a similar state management workaround to Jetpack Compose, which makes it easy to set up state listening for layout components.
Hikage provides two states, `NonNullState` and `NullableState`, which are divided into two states: holding non-null and nullable.
Unlike the recompose of Jetpack Compose, Hikage will not be recomposed, and the states takes effect through listening and callbacks.
You can use both states in the following scenarios.
> The following example
```kotlin
val myLayout = Hikageable {
// Declare a non-null variable state.
val mTextState = mutableStateOf("Hello, World!")
// Declare a nullable and variable state.
val mDrawState = mutableStateOfNull<Drawable>()
// You can delegate the state to a variable.
var mText by mTextState
var mDraw by mDrawState
LinearLayout(
lparams = LayoutParams(matchParent = true),
init = {
orientation = LinearLayout.VERTICAL
}
) {
TextView {
textSize = 16f
gravity = Gravity.CENTER
// Set (binding) state to text.
setState(mTextState) {
text = it
}
}
ImageView {
// Set (binding) state to Drawable.
setState(mDrawState) {
setImageDrawable(it)
}
}
Button {
text = "Click Me!"
setOnClickListener {
// Modify the value of non-null state.
mText = "Hello, Hikage!"
// Modify the value of the nullable state.
mDraw = drawableResource(R.drawable.ic_my_drawable)
}
}
}
}
```
In the example above, we declare a non-null state `mTextState` with `"Hello, World!"` with `mutableStateOf`
Then continue to declare a nullable state `mDrawState` with `null` using `mutableStateOfNull`.
When clicking the button, we modify the value of `mTextState` to `"Hello, Hikage!"` and the value of `mDrawState` is the property resource `R.drawable.ic_my_drawable`.
At this time, the text and images of `TextView` and `ImageView` will be automatically updated.
### Custom Layout Factory
Hikage supports custom layout factories and is compatible with `LayoutInflater.Factory2`.

View File

@@ -425,6 +425,66 @@ val subLayout = Hikageable<LinearLayout.LayoutParams> {
}
```
### 状态管理
Hikage 拥有与 Jetpack Compose 类似的状态管理解决方法,它可以轻松地设置布局组件的状态监听。
Hikage 提供了两种状态,`NonNullState``NullableState`,分为持有非空和可空两种状态。
不同于 Jetpack Compose 的重组 (Recompose)Hikage 不会重组,状态通过监听与回调生效。
你可以在如下场景中使用这两种状态。
> 示例如下
```kotlin
val myLayout = Hikageable {
// 声明一个非空可变状态
val mTextState = mutableStateOf("Hello, World!")
// 声明一个可空可变状态
val mDrawState = mutableStateOfNull<Drawable>()
// 你可以将状态委托给一个变量
var mText by mTextState
var mDraw by mDrawState
LinearLayout(
lparams = LayoutParams(matchParent = true),
init = {
orientation = LinearLayout.VERTICAL
}
) {
TextView {
textSize = 16f
gravity = Gravity.CENTER
// 设置 (绑定) 状态到文本
setState(mTextState) {
text = it
}
}
ImageView {
// 设置 (绑定) 状态到 Drawable
setState(mDrawState) {
setImageDrawable(it)
}
}
Button {
text = "Click Me!"
setOnClickListener {
// 修改非空状态的值
mText = "Hello, Hikage!"
// 修改可空状态的值
mDraw = drawableResource(R.drawable.ic_my_drawable)
}
}
}
}
```
在上面的示例中,我们使用 `mutableStateOf` 声明了一个非空状态 `mTextState`,它的初始值为 `"Hello, World!"`
然后继续使用 `mutableStateOfNull` 声明了一个可空状态 `mDrawState`,它的初始值为 `null`
在点击按钮时,我们修改 `mTextState` 的值为 `"Hello, Hikage!"``mDrawState` 的值为属性资源 `R.drawable.ic_my_drawable`
这时 `TextView``ImageView` 的文本和图片将会自动更新。
### 自定义布局装载器
Hikage 支持自定义布局装载器并同时兼容 `LayoutInflater.Factory2`,你可以通过以下方式自定义在 Hikage 布局装载过程中的事件和监听。