2022年4月30日土曜日

LazyColumn/LazyRow で content types を使う

LazyColumn( modifier = Modifier.fillMaxSize(), state = rememberLazyListState() ) { item( contentType = "header" ) { Text( text = "Header", modifier = Modifier .padding(16.dp) .fillParentMaxWidth() ) } items( count = 100, contentType = { "item" } ) { Text( text = "Item : $it", modifier = Modifier .padding(16.dp) .fillParentMaxWidth() ) } }

2022年4月14日木曜日

WindowInsetsControllerCompat を使って status bar と navigation bar の light mode を切り替える

Material Catalog アプリのコードを読んでいて見つけたんですが、
WindowCompat.getInsetsController() で取得した WindowInsetsControllerCompat の setAppearanceLightStatusBars() と setAppearanceLightNavigationBars() を使うことで、status bar と navigation bar の light mode(light mode だとアイコンがグレーになり、dark だと白になる)をコードから切り替えることができます。

このようにアプリ用の MaterialTheme のところで SideEffect を使って切り替え処理をすると、Theme の xml で頑張らなくて良くなるので便利です。 @Composable fun MyAppTheme( darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit ) { val view = LocalView.current val context = LocalContext.current SideEffect { val controller = WindowCompat.getInsetsController(context.findActivity().window, view) controller?.isAppearanceLightStatusBars = !darkTheme controller?.isAppearanceLightNavigationBars = !darkTheme } MaterialTheme( colors = if (!darkTheme) LightColorPalette else DarkColorPalette,, typography = Typography, shapes = Shapes, content = content ) } private tailrec fun Context.findActivity(): Activity = when (this) { is Activity -> this is ContextWrapper -> this.baseContext.findActivity() else -> throw IllegalArgumentException("Could not find activity!") }

2022年3月27日日曜日

CameraX で動画撮影できるようになったので Compose で実装してみた。

公式のサンプル https://github.com/android/camera-samples/tree/main/CameraXVideo を参考に、Compose で実装してみました。

https://github.com/yanzm/CameraXComposeSample



最初は逐次的に Compose に置き換えていたのですが、Recording の状態を処理するあたりで難しくなって、declarative UI の意識で状態を一から考えないと無理だなってなりました。
なので公式のサンプルがやっている処理を理解したうえで一から状態を考えた結果、公式のサンプルとはかなり違うコードになっています。

既存の imperative UI なコードを declarative UI に移行するのは結構難しいなと思いました。