状态保留和永久性存储

Jetpack Compose

在 Jetpack Compose 中,界面状态通常使用 rememberrememberSaveable 进行管理。虽然 rememberSaveable 可在配置更改后自动保留状态,但其内置功能仅限于实现 ParcelableSerializable 的原始数据类型和对象。

对于可能包含复杂嵌套结构和属性的自定义对象(例如 Brush),必须使用显式序列化和反序列化机制。这时,自定义状态 Saver 就派上用场了。通过为 Brush 对象定义自定义 Saver(如提供的示例中使用示例 Converters 类的 brushStateSaver 所示),您可以确保即使发生配置更改,也能保留画笔的基本属性。

fun brushStateSaver(converters: Converters): Saver<MutableState<Brush>, String> = Saver(
    save
= { state ->
        converters
.brushToString(state.value)
   
},
    restore
= { jsonString ->
       
val brush = converters.stringToBrush(jsonString)
        mutableStateOf
(brush)
   
}
)

然后,您可以使用自定义 Saver 来保留用户选择的画笔状态,如下所示:

val converters = Converters()
val currentBrush = rememberSaveable(saver = brushStateSaver(converters)) { mutableStateOf(defaultBrush) }

永久性存储空间

如需启用文档保存、加载和潜在的实时协作等功能,请以序列化格式存储笔触和关联数据。对于 Ink API,必须手动进行序列化和反序列化。

如需准确恢复笔画,请保存其 Brush 和 [StrokeInputBatch]。

基本序列化

定义一个反映 Ink 库对象的序列化对象结构。

使用您的首选框架(例如 Gson、Moshi、Protobuf 等)对序列化数据进行编码,并使用压缩进行优化。

data class SerializedStroke(
 
val inputs: SerializedStrokeInputBatch,
 
val brush: SerializedBrush
)

data class SerializedBrush(
 
val size: Float,
 
val color: Long,
 
val epsilon: Float,
 
val stockBrush: SerializedStockBrush
)

enum class SerializedStockBrush {
  MARKER
_V1,
  PRESSURE
_PEN_V1,
  HIGHLIGHTER
_V1
}

data class SerializedStrokeInputBatch(
 
val toolType: SerializedToolType,
 
val strokeUnitLengthCm: Float,
 
val inputs: List<SerializedStrokeInput>
)

data class SerializedStrokeInput(
 
val x: Float,
 
val y: Float,
 
val timeMillis: Float,
 
val pressure: Float,
 
val tiltRadians: Float,
 
val orientationRadians: Float,
 
val strokeUnitLengthCm: Float
)

enum class SerializedToolType {
  STYLUS
,
  TOUCH
,
  MOUSE
,
  UNKNOWN
}
class Converters {

 
private val gson: Gson = GsonBuilder().create()

 
companion object {
   
private val stockBrushToEnumValues =
      mapOf
(
       
StockBrushes.markerV1 to SerializedStockBrush.MARKER_V1,
       
StockBrushes.pressurePenV1 to SerializedStockBrush.PRESSURE_PEN_V1,
       
StockBrushes.highlighterV1 to SerializedStockBrush.HIGHLIGHTER_V1,
     
)

   
private val enumToStockBrush =
      stockBrushToEnumValues
.entries.associate { (key, value) -> value to key }
 
}

 
private fun serializeBrush(brush: Brush): SerializedBrush {
   
return SerializedBrush(
      size
= brush.size,
      color
= brush.colorLong,
      epsilon
= brush.epsilon,
      stockBrush
= stockBrushToEnumValues[brush.family] ?: SerializedStockBrush.MARKER_V1,
   
)
 
}

 
private fun serializeStrokeInputBatch(inputs: StrokeInputBatch): SerializedStrokeInputBatch {
   
val serializedInputs = mutableListOf<SerializedStrokeInput>()
   
val scratchInput = StrokeInput()

   
for (i in 0 until inputs.size) {
      inputs
.populate(i, scratchInput)
      serializedInputs
.add(
       
SerializedStrokeInput(
          x
= scratchInput.x,
          y
= scratchInput.y,
          timeMillis
= scratchInput.elapsedTimeMillis.toFloat(),
          pressure
= scratchInput.pressure,
          tiltRadians
= scratchInput.tiltRadians,
          orientationRadians
= scratchInput.orientationRadians,
          strokeUnitLengthCm
= scratchInput.strokeUnitLengthCm,
       
)
     
)
   
}

   
val toolType =
     
when (inputs.getToolType()) {
       
InputToolType.STYLUS -> SerializedToolType.STYLUS
       
InputToolType.TOUCH -> SerializedToolType.TOUCH
       
InputToolType.MOUSE -> SerializedToolType.MOUSE
       
else -> SerializedToolType.UNKNOWN
     
}

   
return SerializedStrokeInputBatch(
      toolType
= toolType,
      strokeUnitLengthCm
= inputs.getStrokeUnitLengthCm(),
      inputs
= serializedInputs,
   
)
 
}

 
private fun deserializeStroke(serializedStroke: SerializedStroke): Stroke? {
   
val inputs = deserializeStrokeInputBatch(serializedStroke.inputs) ?: return null
   
val brush = deserializeBrush(serializedStroke.brush) ?: return null
   
return Stroke(brush = brush, inputs = inputs)
 
}

 
private fun deserializeBrush(serializedBrush: SerializedBrush): Brush {
   
val stockBrushFamily = enumToStockBrush[serializedBrush.stockBrush] ?: StockBrushes.markerV1

   
return Brush.createWithColorLong(
      family
= stockBrushFamily,
      colorLong
= serializedBrush.color,
      size
= serializedBrush.size,
      epsilon
= serializedBrush.epsilon,
   
)
 
}

 
private fun deserializeStrokeInputBatch(
    serializedBatch
: SerializedStrokeInputBatch
 
): StrokeInputBatch {
   
val toolType =
     
when (serializedBatch.toolType) {
       
SerializedToolType.STYLUS -> InputToolType.STYLUS
       
SerializedToolType.TOUCH -> InputToolType.TOUCH
       
SerializedToolType.MOUSE -> InputToolType.MOUSE
       
else -> InputToolType.UNKNOWN
     
}

   
val batch = MutableStrokeInputBatch()

    serializedBatch
.inputs.forEach { input ->
      batch
.addOrThrow(
        type
= toolType,
        x
= input.x,
        y
= input.y,
        elapsedTimeMillis
= input.timeMillis.toLong(),
        pressure
= input.pressure,
        tiltRadians
= input.tiltRadians,
        orientationRadians
= input.orientationRadians,
     
)
   
}

   
return batch
 
}

 
fun serializeStrokeToEntity(stroke: Stroke): StrokeEntity {
   
val serializedBrush = serializeBrush(stroke.brush)
   
val serializedInputs = serializeStrokeInputBatch(stroke.inputs)
   
return StrokeEntity(
      brushSize
= serializedBrush.size,
      brushColor
= serializedBrush.color,
      brushEpsilon
= serializedBrush.epsilon,
      stockBrush
= serializedBrush.stockBrush,
      strokeInputs
= gson.toJson(serializedInputs),
   
)
 
}

 
fun deserializeEntityToStroke(entity: StrokeEntity): Stroke {
   
val serializedBrush =
     
SerializedBrush(
        size
= entity.brushSize,
        color
= entity.brushColor,
        epsilon
= entity.brushEpsilon,
        stockBrush
= entity.stockBrush,
     
)

   
val serializedInputs =
      gson
.fromJson(entity.strokeInputs, SerializedStrokeInputBatch::class.java)

   
val brush = deserializeBrush(serializedBrush)
   
val inputs = deserializeStrokeInputBatch(serializedInputs)

   
return Stroke(brush = brush, inputs = inputs)
 
}

 
fun brushToString(brush: Brush): String {
       
val serializedBrush = serializeBrush(brush)
       
return gson.toJson(serializedBrush)
   
}

 
fun stringToBrush(jsonString: String): Brush {
       
val serializedBrush = gson.fromJson(jsonString, SerializedBrush::class.java)
       
return deserializeBrush(serializedBrush)
   
}

}