[TIL] ๐ŸŒผ24/05/16๐ŸŒผ#CompositionLocal

0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
100/104
post-thumbnail
post-custom-banner

[TIL] ๐ŸŒผ24/05/16๐ŸŒผ#CompositionLocal

CompositionLocal

๐Ÿ“Œ์ฐธ๊ณ ์ž๋ฃŒ

  • ์ผ๋ฐ˜์ ์œผ๋กœ UI Tree์˜ ์ƒ์œ„ Composable์—์„œ ํ•˜์œ„ Composable๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๋ช…์‹œ์ ์œผ๋กœ ์ „๋‹ฌ
    -> ์ƒ์œ„ Composable๊ณผ ํ•˜์œ„ Composable์˜ UI Tree์—์„œ์˜ ๊นŠ์ด ์ฐจ์ด๊ฐ€ ํฐ ๊ฒฝ์šฐ, ๋ฒˆ๊ฑฐ๋กœ์›€!
  • CompositionLocal?
    Composition ์•„๋ž˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์•”์‹œ์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋„๊ตฌ
  • MaterialTheme์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์—์„œ๋„ CompositionLocal ์‚ฌ์šฉ
    • MaterialTheme๋Š” 3๊ฐœ์˜ CompositionLocal ์ธ์Šคํ„ด์Šค ์ œ๊ณต
    • MaterialTheme.colors ํ”„๋กœํผํ‹ฐ๋กœ LocalColors ์ธ์Šคํ„ด์Šค ์ ‘๊ทผ
    • MaterialTheme.typography ํ”„๋กœํผํ‹ฐ๋กœ LocalTypography ์ธ์Šคํ„ด์Šค ์ ‘๊ทผ
    • MaterialTheme.shapes ํ”„๋กœํผํ‹ฐ๋กœ LocalShape ์ธ์Šคํ„ด์Šค ์ ‘๊ทผ
  • Composition ๋‚ด๋ถ€์—์„œ context ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋„ CompositionLocal ์‚ฌ์šฉ
    val resources = LocalContext.current.resources
  • CompositionLocal instance is scoped to a part of the Composition
    • UI tree์—์„œ์˜ ๊นŠ์ด ๋ณ„๋กœ ๋‹ค๋ฅธ CompositionLocal ์ œ๊ณต ๊ฐ€๋Šฅ
    • CompositionLocal.current: ํ•ด๋‹น Composition์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์กฐ์ƒ์˜ CompositionLocal ์ธ์Šคํ„ด์Šค ๊ฐ’
  • CompositionLocal ์ƒ์„ฑ ๋ฐฉ๋ฒ•
    • compositionLocalOf
      • current ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ ‘๊ทผํ•˜๋Š” Composition๋“ค์„ Compose๊ฐ€ ํŠธ๋ž˜ํ‚นํ•จ
        -> ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ, ํ•ด๋‹น Composition๋“ค๋งŒ recompose๋จ
    • staticCompositionLocalOf
      • Compose๊ฐ€ ํŠธ๋ž˜ํ‚นํ•˜์ง€ X
        -> ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ, content ๋žŒ๋‹ค ๋‚ด๋ถ€์˜ ๋ชจ๋“  Compositione๋“ค recompose๋จ
      • ๊ฑฐ์˜/์ ˆ๋Œ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์€ ๊ฐ’์„ ์ œ๊ณตํ•  ๋•Œ ์‚ฌ์šฉ -> performance benefits
// LocalElevations.kt

data class Elevations(val card: Dp = 0.dp, val default: Dp = 0.dp)

// Define a CompositionLocal global object with a default
// This instance can be accessed by all composables in the app
val LocalElevations = compositionLocalOf { Elevations() }
  • CompositionLocalProvider๋ฅผ ํ†ตํ•ด CompositionLocal ์ƒˆ๋กœ์šด ๊ฐ’ ์ œ๊ณตํ•˜๊ธฐ
    -> content ๋žŒ๋‹ค ๋‚ด๋ถ€์˜, ์ƒˆ๋กญ๊ฒŒ ์ œ๊ณต๋ฐ›์€ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” composition๋“ค recompose๋จ
    @Composable
    @NonSkippableComposable
    fun CompositionLocalProvider(
       vararg values: ProvidedValue<*>,
       content: @Composable () -> Unit
    ): Unit
// MyActivity.kt
class MyActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            // Calculate elevations based on the system theme
            val elevations = if (isSystemInDarkTheme()) {
                Elevations(card = 1.dp, default = 1.dp)
            } else {
                Elevations(card = 0.dp, default = 0.dp)
            }

            // Bind elevation as the value for LocalElevations
            CompositionLocalProvider(LocalElevations provides elevations) {
                // ... Content goes here ...
                // This part of Composition will see the `elevations` instance
                // when accessing LocalElevations.current
            }
        }
    }
}
@Composable
fun SomeComposable() {
    // Access the globally defined LocalElevations variable to get the
    // current Elevations in this part of the Composition
    Card(elevation = LocalElevations.current.card) {
        // Content
    }
}
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€