From 258c5ceddc035417b0287c0ba2be51bbc8e7a831 Mon Sep 17 00:00:00 2001 From: Ahn-seokjoo Date: Thu, 27 Jun 2024 16:57:44 +0900 Subject: [PATCH] =?UTF-8?q?[#28=20feature]=20state=EB=A1=9C=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dorabangs/feature/home/HomeContract.kt | 2 ++ .../dorabangs/feature/home/HomeScreen.kt | 23 ++++++++----------- .../dorabangs/feature/home/HomeViewModel.kt | 15 +++++++++++- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeContract.kt b/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeContract.kt index 14e1816d..e6137201 100644 --- a/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeContract.kt +++ b/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeContract.kt @@ -2,6 +2,8 @@ package com.mashup.dorabangs.feature.home data class HomeState( val number: Int = 0, + val copiedText: String = "", + val shouldSnackBarShown: Boolean = false, ) sealed class HomeSideEffect diff --git a/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeScreen.kt b/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeScreen.kt index 646a7f7f..5df8979c 100644 --- a/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeScreen.kt +++ b/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeScreen.kt @@ -8,10 +8,6 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.ClipboardManager @@ -33,37 +29,38 @@ fun HomeRoute( viewModel: HomeViewModel = hiltViewModel(), actionSnackBar: () -> Unit = {}, ) { - var copiedText by remember { mutableStateOf("") } - var shouldSnackBarShown by remember { mutableStateOf(false) } + val state = viewModel.collectAsState().value LifecycleResumeEffect(key1 = clipboardManager) { runCatching { view.post { - copiedText = clipboardManager.getText()?.text.orEmpty() - shouldSnackBarShown = copiedText.isNotBlank() && copiedText.isValidUrl() + val clipboardText = clipboardManager.getText()?.text.orEmpty() + if (clipboardText.isNotBlank() && clipboardText.isValidUrl()) { + viewModel.showSnackBar(clipboardText) + } } } onPauseOrDispose { - shouldSnackBarShown = false + viewModel.hideSnackBar() } } Box { HomeScreen( - state = viewModel.collectAsState().value, + state = state, modifier = modifier, onClickAddButton = { viewModel.add(1) }, onClickTestButton = { viewModel.test() }, ) - if (shouldSnackBarShown) { + if (state.shouldSnackBarShown) { DoraSnackBar( modifier = Modifier .align(Alignment.BottomCenter), - text = copiedText, + text = state.copiedText, action = actionSnackBar, dismissAction = { clipboardManager.setText(AnnotatedString("")) - shouldSnackBarShown = false + viewModel.hideSnackBar() }, ) } diff --git a/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeViewModel.kt b/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeViewModel.kt index 8ff56631..fd6a18c3 100644 --- a/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeViewModel.kt +++ b/feature/home/src/main/java/com/mashup/dorabangs/feature/home/HomeViewModel.kt @@ -23,7 +23,7 @@ constructor( fun add(number: Int) = intent { reduce { - state.copy(state.number + number) + state.copy(number = state.number + number) } } @@ -32,4 +32,17 @@ constructor( delay(1000L) println("tjrwn 현재 쓰레드 name ${Thread.currentThread().name}") } + + fun hideSnackBar() = + intent { + reduce { + state.copy(shouldSnackBarShown = false) + } + } + + fun showSnackBar(clipboardText: String) = intent { + reduce { + state.copy(copiedText = clipboardText, shouldSnackBarShown = true) + } + } }