Android Weekly Notes #489 Android Weekly Issue #489

Android Weekly Issue #489

Roadmap for Jetpack Compose

这个总结还挺好的, Compose的方方面面, 可以作为一个checklist.

Jetpack Compose: filling max width or height

Compose只measure child一次, 第二次会抛出异常. 官方文档写的.

这样写:

@Composable
fun content() {
    return Row {
        Box(
            modifier = Modifier
                .width(8.dp)
                .fillMaxHeight()
                .background(Color.Red)
        )
        Column {
            Text("Hello")
            Text("World")
        }
    }
}

会显示左边的Box很长.

改成这样就好了:

@Composable
fun content() {
    return Row(
        modifier = Modifier
            .height(IntrinsicSize.Min)
    ) {
        Box(
            modifier = Modifier
                .width(8.dp)
                .fillMaxHeight()
                .background(Color.Red)
        )
        Column {
            Text("Hello")
            Text("World")
        }
    }
}

这样会强迫Row的Child使用它们的最小尺寸, 这样Box才能算出自己的最大高度.

Always provide a Modifier parameter

Modifier: 组合而不是继承的概念.

官方文档的modifiers-list

不带Modifier的代码, 比如:

@Composable
private fun HeaderText(text: String) {
    Text(
        text = text,
        color = ...,
        style = ...,
        maxLines = ...,
        overflow = ...,
        modifier = Modifier.fillMaxWidth(),
    )
}

有如下的问题:

  • 调用者无法影响和控制它. 也许调用者想改它的大小, 对齐方式, 点击事件呢.
  • 隐式的layout行为.
    即便是Surface, 也不会提供layout和尺寸相关的modifier:
@Composable
fun Surface(
    // other parameters
    modifier: Modifier,
) {
    Box(
        modifier
            .shadow(...)
            .background(...)
            .clip(...),
    ) {
        content()
    }
}

  • 破坏了parent/child的关系. child应该只关注自己的内容, 而由parent layout来决定自己的位置, 这才是一个良好的parent/child关系.

Notification trampoline restrictions-Android12

target Android 12以后, 用户点击notification之后, 不能再通过broadcast/service启动Activity的方式了.

LogCat里会打出:

Indirect notification activity start (trampoline) from PACKAGE_NAME, \
this should be avoided for performance reasons.

Android: Apollo3 and GraphQL

apollo要出3.0了.

这篇文章讨论了实现中的:

  • 错误处理.
  • 部分成功.
  • 测试.
  • log.

Android Data Serialization Tutorial with the Kotlin Serialization Library

如何使用Kotlin serialization.

比起Moshi和Gson, 它的优势是:

  • runtime性能.
  • 更快的build速度. compiler plugin比annotation processor更快.
  • 比Gson对Kotlin的类型支持更好. 包括可空和默认.
  • 支持非常多的编码格式.

Incident Review and Postmortem Best Practices

Incident处理的最佳实践.

Assisted Inject for less boilerplate?

  • @AssistedInject types(dependencies) cannot be injected directly, only the @AssistedFactory can be injected
  • @AssistedInject types(dependencies) cannot be scoped.

Code

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章