Share
Embed
선언형 UI를 학습할 때 알아둬야하는 키워드들
Devfest Android in Korea 2024에서 진행한 선언형 UI를 학습할 때 알아둬야하는 키워드들의 Speaker Deck입니다.
HyunWoo Lee
September 28, 2024
Transcript
67 slides
-
Essesntial concepts to know when learning Declarative UI Korea Android
HyunWoo Lee Android/React Native Developer, Viva Republica(Toss) Organizer, Kotlin User Groups Seoul/GDG Songdo -
ࢶഋ UIܳ णೡ ٸ ঌইىঠೞח ఃਕٜ٘ Korea Android അ Android/React
Native Developer, Viva Republica(Toss) Organizer, Kotlin User Groups Seoul/GDG Songdo -
ࢶഋ UIח ޖੋоਃ? ݾର ࢶഋ UI ਃ ਃٜࣗ ࢚కҙܻ ې
- ࠺زӝ ࢚క ҙܻ -
ߣ ߊח DroidKaigi 2024ীࢲ ೯ೠ ߊܳ ೠҴয۽ द Recapೠ Ѫਸ
ঌ۰٘݀פ https://www.youtube.com/watch?v=txaf9F0d-aE -
fun sum(): Int { var result = 0 for(i in
1 . . 10) { sum += i } return result } -
ח ࠗ࠙ • ೞա ചݶ, ೞա ஹನքח ஹನ ք
ઑ. • Custom View ٜ݅ӝ ਤ೧ ࢚ࣘ ೞ؍ җѢ दрٜ ઁ Ӓ݅ -
fun TimeTableItem( tags: List<String>, title: String, isLike: Boolean ) {
Column() { Row() { Tags(tags = tags) Like(isLike = isLike) } SectionTitle(title = title) } } -
struct TimeTableItem: View { var tags: [String] var title: String
var isLike: Bool var body: some View { VStack { HStack { Tags(tags: tags) Like(isLike: isLike) } SectionTitle(title: title) } } } -
function TimeTableItem({ tags, title, isLike }: Props) { return (
<View> <View style= { { flexDirection: ‘row’ } } > <Tags tags={tags} / > <Like isLike={isLike} / > < / View> <SectionTitle title=title / > < / View> ) } -
ۖಬ ߹ ର • ೣࣻ ஹನք vs ېझ ஹನք •
ೣࣻ ஹನքח ಁ۞ఠ ೣࣻ ղ ߸ࣻ۽ UI ҙܻ • Jetpack Compose, React • ېझ ஹನքח ࢤࢿ, ݯߡ ߸ࣻ۽ UI ҙܻ • SwiftUI, Flutter, (җѢ) React -
Flex(FlexBox) • CSSীࢲ ࢎਊೞח ۨইਓ ߓ ӝߨ ೞա •
ࢶഋ UI ۖಬ ࣻо ܳ ఖೣ • ConstraintLayoutҗח ܲ ߑधਵ۽ UIܳ ࢿ೧ঠ ೞפ CSS Flex ѐ֛ਸ ܻ ೠߣঀ ঌҊ оݶ ઑӘ ؊ औѱ UI ࢿਸ ೡ ࣻ णפ. -
State৬ UI • Stateܳ ഝਊೞৈ UIܳ ߸҃೧ࠁ • Stateח ചݶਸ
߸҃दఆ ࣻ ח ؘఠ. • Stateо ߸҃ غݶ ചݶ ߸҃ػ. • ߸҃ਸ ۖಬ݃ ࢚ೞѱ ࠗܰ݅ ѐ ܻ۪؊݂(re- rendering)ۄ ೠ. -
fun TimeTableItem( tags: List<String>, title: String, isLike: Boolean ) {
Column() { Row() { Tags(tags = tags) Like(isLike = isLike) } SectionTitle(title = title) } } -
fun TimeTableItem( tags: List<String>, title: String, isLike: Boolean ) {
var isLike by remember { mutableStateOf(false) } Column() { Row() { Tags(tags = tags) Like( isLike = isLike, modifier = Modifier.clickable { isLike = !isLike } ) } SectionTitle(title = title) } } -
fun TimeTableItem( tags: List<String>, title: String, isLike: Boolean ) {
var (isLike, setIsLike) = remember { mutableStateOf(false) } Column() { Row() { Tags(tags = tags) Like( isLike = isLike, modifier = Modifier.clickable { setIsLike(!isLike) } ) } SectionTitle(title = title) } } -
function TimeTableItem({ tags, title, isLike }: Props) { const [isLike,
setIsLike] = useState(false); return ( <View> <View style= { { flexDirection: ‘row’ } } > <Tags tags={tags} / > <Like isLike={isLike} onLikeChange={() = > setIsLike(!isLike)} / > < / View> <SectionTitle title=title / > < / View> ) } -
struct TimeTableItem: View { var tags: [String] var title: String
@State var isLike: Bool var body: some View { VStack { HStack { Tags(tags: tags) Like(isLike: isLike) } SectionTitle(title: title) } } } struct Like: View { @Binding var isLike: Bool var body: some View { if isLike { Image(.icFavoriteFill) } else { Image( .icFavoriteOutline ) } } } -
Re-rendering • ࢶഋ UI ۨਕীࢲח ࢚కܳ ߄Բח о ѐߊীࢲ दझ
మ ۖಬী ҳগ߉ ঋҊ ࠺तೠ ߑधਸ ഝਊ • п ۖಬ݃ ܻ۪؊݂ ݫழפ્ ܴ • উ٘۽٘: (Smart) Recomposition • React(React Native): Re-rendering/Virtual • SwiftUI: ViewGroup idী ೠ সؘ -
UI সؘ ৻ ӝמٜ? • Әө ܘ Ѫٜ UI ۪؊݂ী
ҙ۲ػ ӝמٜ • ݅ডী ചݶী ੑೞ݃ ࢲߡాनਸ ೧ঠೠѢա ӂೠ ਃਸ ೧ঠೠݶ? -
Side Effect ചݶਸ Ӓܻ ঋח ӝמ ৻ী ݽٚ ӝמٜਸ Side
Effectۄ ೠ. -
fun TimeTableItem( tags: List<String>, title: String, isLike: Boolean ) {
var isLike by remember { mutableStateOf(false) } Column() { Row() { Tags(tags = tags) Like( isLike = isLike, modifier = Modifier.clickable { isLike = !isLike } ) } SectionTitle(title = title) } } -
fun TimeTableItem( tags: List<String>, title: String, isLike: Boolean ) {
var isLike by remember { mutableStateOf(false) } LaunchedEffect(isLike) { toast(if (isLike) “೮णפ!” else “द ೞ۰ݶ ߡౡਸ ׂ۞ࣁਃ!”) } Column() { Row() { Tags(tags = tags) Like( isLike = isLike, modifier = Modifier.clickable { isLike = !isLike } ) } SectionTitle(title = title) -
function TimeTableItem({ tags, title, isLike }: Props) { const [isLike,
setIsLike] = useState(false); return ( <View> <View style= { { flexDirection: ‘row’ } } > <Tags tags={tags} / > <Like isLike={isLike} onLikeChange={() = > setIsLike(!isLike)} / > < / View> <SectionTitle title=title / > < / View> ) } -
function TimeTableItem({ tags, title, isLike }: Props) { const [isLike,
setIsLike] = useState(false); useEffect(() = > { alert(`${isLike ? ‘೮णפ’ : ‘द ೞ۰ݶ ߡౡਸ ׂ۞ࣁਃ!’) }, [isLike]) return ( <View> <View style= { { flexDirection: ‘row’ } } > <Tags tags={tags} / > <Like isLike={isLike} onLikeChange={() = > setIsLike(!isLike)} / > < / View> <SectionTitle title=title / > < / View> -
function TimeTableItem({ tags, title, isLike }: Props) { useEffect( ()
= > { / / ਗೞח Side Effect ܻ }, [isLike] ) } Dependency Array • ࠼ ߓৌ: ചݶী ੑೠ റ ࢎ٘ ಖ ܻ • ࠺য ঋ ߓৌ(ਃࣗ A, B, etc): Aա B ч ߸҃غח ҃ী Side E ff ect ܻ • ߓৌਸ য֍ ঋ ҃: ݒ Re-rendering݃ ࢎ٘ ಖ ܻ -
function TimeTableItem({ tags, title, isLike }: Props) { useEffect( ()
= > { () = > { / * Clean Up Function * / } }, [isLike] ) } Clean Up Function • ചݶ ӝӝীࢲ ߩযզ ٸ ೧ ߔ উী ٜযח ۽ प೯ؽ -
LaunchedEffect(key1, key2) { / / Side Effect } LaunchedE ff
ect • (key1, key2, …etc):useE ff ect Dependency Array৬ э ೡ • key1, key2о ߸҃غݶ ೧ Side E ff ect प೯ • ݅ড ചݶী ੑೞ݃ Side E ff ectܳ प ೯ೞҊ रݶ? • keyী ࠛ߸чਸ ֍ח • true, false, Unit, etc • trailing lambdaח suspend function -
DisposableEffect(key1, key2) { / / Side Effect onDispose { }
} DisposableE ff ect • onDispose: useE ff ect Clean Up Functionҗ زੌೠ ೡ • trailing lambdaо non-suspend function -
SideEffect { / / Side Effect } SideE ff ect
• SideE ff ectо ݒ Recomposition݃ प೯ؽ -
VStack { Text(text).padding() .onAppear { } .onDisappear { } .onChange(of:
text) { newValue in print("Text changed to: \(newValue)”) } Button("Change Text") { text = "Text changed \(count + 1) times" } } -
࢚క ҕਬ ߑߨۿ • ध ҅க Өযࣻ۾ ࠗݽ ࢚కܳ ध
҅கীѱ ׳ೞҊ ҕ ਬೞӝо ҭ য۰ਕ • ध ஹನքٜীѱ ؊ ਬ܂ѱ ࢚కܳ ҕਬೡ ࣻ ח ߑध হ ਸө? -
Props Drilling • ध ஹನքীѱ ࢚కܳ ׳ೞӝ ਤ೧ ࢤࢿա ೣࣻ
ಁ۞ఠ ܳ ഝਊೞৈ ׳ೞח Ѫ • Composeীࢲ ӂೞח ߑध • https://developer.android.com/develop/ui/compose/ state-hoisting -
Local Scoped Data • ࠗݽ ஹನքীࢲ ध҅கীѱ ࢚కܳ ׳ೡ ࣻ
ח Provider ܳ ࢸೞҊ ध ஹನքীࢲח ܳ ࢎਊೡ ࣻ ب۾ ೞח ߑߨ • Reactীࢲ ۽ ࢎਊೞח ߑߨۿ • https://react.dev/learn/passing-data-deeply-with- context#replace-prop-drilling-with-context -
Local Scoped Data • Android - CompositionLocal • iOS -
EnvironmentValues • React(React Native) - Context -
/ / Contextܳ ೠ. const ThemeContext = createContext() / /
Context Providerܳ ೠ. const ThemeProvider = ({ children }) = > { const [theme, setTheme] = useState(‘light’) return ( <ThemeContext.Provider value= { { theme } } > {children} < / ThemeContext.Provider> ) } -
/ / Contextܳ ࢎਊೞח ஹನքীࢲ useContext ೣࣻܳ ഝਊೠ. function Component()
{ const { theme } = useContext() return <Text>{`Current Theme is ${theme}`} < / Text> } / / Contextܳ ࢎਊೞҊ ೞח ஹನքܳ Provider۽ хऴ. function Component() { return <ThemeProvider><Component / > < / ThemeProvider> } -
/ / CompositionLocal val LocalColor = staticCompositionLocalOf<Colors> { Error(“No
Colors”) } / / LocalColorܳ ઁҕೞח CompositionLocalProviderܳ ࢸೠ. CompositionLocalProvider( LocalColor provides appColors() ) { / / The components within this block can use LocalColors } -
CompositionLocalProvider( LocalColor provides appColors() ) { DroidKnightsRow() } fun DroidKnightsRow()
{ val colors = LocalColors.current Row(modifier = Modifier.background(color.background)) { } } -
CompositionLocal • staticCompositionLocalOf • CompositionLocal۽ ઁҕೞח ч ࠛ߸ೠҊ оೞҊ CompositionLocal
чਸ ઁҕೞח җਸ ୭ച • ч ߸҃ ػݶ ചݶী recomposition ੌযթ • compositionLocalOf • CompositionLocal ߸҃غݶ чਸ ੍ח ஹನք݅ Recomposition ੌযթ -
/ / EnvironmentValues۽ ղ۰ࠁյ ч keyܳ struct ThemeKey: EnvironmentKey
{ static let isDarkMode: Bool = false } / / EnvironmentValues ഛೣࣻܳ ೧ࢲ / / ਗೞח EnvironmentValuesܳ ࡳইৢ ࣻ ѱ ࢸ(setter, getter) extension EnvironmentValues { var isDarkMode: Bool { get { self[ThemeKey.self] } set { self[ThemeKey.self] = newValue } } } -
/ / EnvironmentValuesܳ .environment۽ ઁҕ struct ContentView: View { var
body: some View { VStack { Child() .environment(\.isDarkMode, true) } } } / / EnvironmentValuesܳ ࢎਊೞח ध ஹನքח ݯߡ߸ࣻ۽ struct Child: View { @Environment(\.isDarkMode) var isDarkMode } -
Single Store ӝ߈ Architecture ਬ೯ • Android, iOS ন ۖಬ
ݽف അ Single Store ӝ߈ Architecureо ൜೯ ೞҊ • Androidীࢲח MVI • iOSীࢲח TCA(The Composable Architecture) • ۠ ইఃఫо ਬ೯ ػѱ োੌөਃ? -
Single Store ӝ߈ Architecture ਬ೯ • Javascript ࢤక҅ীࢲח 2014֙ী
दೠ Cycle.jsܳ ӝ߈ ਵ۽ MVI ѐ֛ فؽ -
Reduxח ৵ ۨѢद۽ ੋधೞҊ חо? • ੋ दр ݆ լ
(Initiial Commit 2015֙) • Boilerplate ٘ ন ࢚ ݆ • API ਃ чী ೠ ࢚క о ࠗ -> ӏѺചػ ߑध হ • ࠂೠ ਬ दաܻয়ী ೞӝ ਤ೧ࢲ ݆ࣻ Ҋҗ ٘ ࢿ -
࠺زӝ ࢚క ҙܻ - SWR • stale-while-revalidate • ߔӒۄ٘ীࢲ
࢜۽ ؘఠܳ ߉ইয়ח زউ நदػ ؘఠܳ ࢎਊೡ ࣻ ח ػ ӝрਸ • HTTP ؊ীࢲ ۽ಌ౭о غয݅ ѐ Cached ؘ ఠ৬ ࢜۽ ؘఠ ਃਸ ҙܻೞח ߑߨۿਸ ೣ -
࠺زӝ ࢚క ҙܻ • ࠺زӝ ࢚కҙܻח ࢲߡীࢲ ߉ইয়ח “࠺زӝ” ؘఠܳ
ҙܻೞח ߑߨ ۿ • ݒߣ ചݶী ੑೡ ٸ݃ ࢜۽ ؘఠܳ ߉ই৬ঠೡө? • ੌ ӝр ղী ചݶী ੑೠݶ ӝઓ ؘఠܳ ഝਊ೧ب ରо হ • நदػ ؘఠܳ ࢎਊೞח ҃ ҭ ࡅܰѱ ചݶਸ ҳࢿೡ ࣻ -
࠺زӝ ࢚క ҙܻ • द: Optimistic Update • ࢲߡо ࢿҕਵ۽
ೡ Ѫۄ оೞҊ UIܳ ܻ ߸҃ೞҊ पಁೞݶ ܀ߔਸ ೞח ӝߨ -
React Query • SWR ଼ਸ ӝ߈ਵ۽ ࠺زӝ ࢚కܳ ҙܻೡ ࣻ
ח ۄ࠳۞ܻ • ࠺زӝ ࢚క ҙܻܳ ਊೞѱ ೞח ӝמٜ ઁҕؽ • ۽٬/ࢿҕ/पಁ ١ ࠺زӝ ࢚క ӏѺ ળച • ࠺زӝ ؘఠ நय • stale(ؘఠо য়ېؼ ٸ) زਵ۽ நद ؘఠ Ү • ъઁ நद ޖബച -
٘ द function Example() { const {data, isLoading, isError, refetch}
= useQuery(‘index’, fetchData, { staleTime: 5000 }); if (isLoading) return <Text>Loading… < / Text> if (isError) return <View><ErrorIamge / > < / View> const invalidate = () = > { queryClient.invalidateQueries(‘index’) } return <View>{data}<View onClick={invalidate}>Invalidate < / View> < / View> } -
٘ द function Example() { const {data, isLoading, isError, refetch}
= useQuery(‘index’, fetchData, { staleTime: 5000 }); if (isLoading) return <Text>Loading… < / Text> if (isError) return <View><ErrorIamge / > < / View> const invalidate = () = > { queryClient.invalidateQueries(‘index’) } return <View>{data}<View onClick={invalidate}>Invalidate < / View> < / View> } -
٘ द function Example() { const {data, isLoading, isError, refetch}
= useQuery(‘index’, fetchData, { staleTime: 5000 }); if (isLoading) return <Text>Loading… < / Text> if (isError) return <View><ErrorIamge / > < / View> const invalidate = () = > { queryClient.invalidateQueries(‘index’) } return <View>{data}<View onClick={invalidate}>Invalidate < / View> < / View> } -
٘ द function Example() { const {data, isLoading, isError, refetch}
= useQuery(‘index’, fetchData, { staleTime: 5000 }); if (isLoading) return <Text>Loading… < / Text> if (isError) return <View><ErrorIamge / > < / View> const invalidate = () = > { queryClient.invalidateQueries(‘index’) } return <View>{data}<View onClick={invalidate}>Invalidate < / View> < / View> }