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

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