Skip to content

Commit

Permalink
[#28 feature] state로 관리
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahn-seokjoo committed Jun 27, 2024
1 parent 6f21bef commit 258c5ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constructor(
fun add(number: Int) =
intent {
reduce {
state.copy(state.number + number)
state.copy(number = state.number + number)
}
}

Expand All @@ -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)
}
}
}

0 comments on commit 258c5ce

Please sign in to comment.