Skip to content

Commit f9b6363

Browse files
committed
feat(add-new-user)
1 parent d6d8e92 commit f9b6363

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

feature-add/src/main/java/com/hoc/flowmvi/ui/add/AddNewUserScreen.kt

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import androidx.compose.material3.TextField
3232
import androidx.compose.material3.TopAppBarDefaults
3333
import androidx.compose.runtime.Composable
3434
import androidx.compose.runtime.LaunchedEffect
35+
import androidx.compose.runtime.State
3536
import androidx.compose.runtime.getValue
3637
import androidx.compose.runtime.remember
3738
import androidx.compose.runtime.rememberCoroutineScope
@@ -68,38 +69,16 @@ import kotlinx.coroutines.flow.onEach
6869
import kotlinx.coroutines.launch
6970
import kotlinx.coroutines.withContext
7071

71-
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLifecycleComposeApi::class)
72+
@OptIn(ExperimentalLifecycleComposeApi::class)
7273
@Composable
7374
internal fun AddNewUserRoute(
7475
configAppBar: ConfigAppBar,
7576
onBackClick: () -> Unit,
7677
modifier: Modifier = Modifier,
7778
viewModel: AddVM = hiltViewModel(),
7879
) {
79-
val currentOnBackClick by rememberUpdatedState(onBackClick)
80-
81-
val title = stringResource(id = R.string.add_new_user)
82-
val colors = TopAppBarDefaults.centerAlignedTopAppBarColors()
83-
val appBarState = remember(colors) {
84-
AppBarState(
85-
title = title,
86-
actions = {},
87-
navigationIcon = {
88-
IconButton(onClick = { currentOnBackClick() }) {
89-
Icon(
90-
imageVector = Icons.Filled.ArrowBack,
91-
contentDescription = "Back"
92-
)
93-
}
94-
},
95-
colors = colors
96-
)
97-
}
98-
OnLifecycleEvent(configAppBar, appBarState) { _, event ->
99-
if (event == Lifecycle.Event.ON_START) {
100-
configAppBar(appBarState)
101-
}
102-
}
80+
val currentOnBackClickState = rememberUpdatedState(onBackClick)
81+
ConfigAppBar(currentOnBackClickState, configAppBar)
10382

10483
val intentChannel = remember { Channel<ViewIntent>(Channel.UNLIMITED) }
10584
LaunchedEffect(Unit) {
@@ -133,7 +112,7 @@ internal fun AddNewUserRoute(
133112
}
134113
scope.launch {
135114
delay(200)
136-
currentOnBackClick()
115+
currentOnBackClickState.value()
137116
}
138117
}
139118
}
@@ -156,6 +135,36 @@ internal fun AddNewUserRoute(
156135
)
157136
}
158137

138+
@Composable
139+
@OptIn(ExperimentalMaterial3Api::class)
140+
private fun ConfigAppBar(
141+
currentOnBackClickState: State<() -> Unit>,
142+
configAppBar: ConfigAppBar
143+
) {
144+
val title = stringResource(id = R.string.add_new_user)
145+
val colors = TopAppBarDefaults.centerAlignedTopAppBarColors()
146+
val appBarState = remember(colors) {
147+
AppBarState(
148+
title = title,
149+
actions = {},
150+
navigationIcon = {
151+
IconButton(onClick = { currentOnBackClickState.value() }) {
152+
Icon(
153+
imageVector = Icons.Filled.ArrowBack,
154+
contentDescription = "Back"
155+
)
156+
}
157+
},
158+
colors = colors
159+
)
160+
}
161+
OnLifecycleEvent(configAppBar, appBarState) { _, event ->
162+
if (event == Lifecycle.Event.ON_START) {
163+
configAppBar(appBarState)
164+
}
165+
}
166+
}
167+
159168
@Composable
160169
private fun AddNewUserContent(
161170
viewState: ViewState,

feature-main/src/main/java/com/hoc/flowmvi/ui/main/UsersListScreen.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,7 @@ internal fun UsersListRoute(
6262
modifier: Modifier = Modifier,
6363
viewModel: MainVM = hiltViewModel(),
6464
) {
65-
val title = stringResource(id = R.string.app_name)
66-
val colors = TopAppBarDefaults.centerAlignedTopAppBarColors()
67-
val appBarState = remember(colors) {
68-
AppBarState(
69-
title = title,
70-
actions = {
71-
IconButton(onClick = navigateToAddUser) {
72-
Icon(
73-
imageVector = Icons.Default.Add,
74-
contentDescription = "Add new user",
75-
)
76-
}
77-
},
78-
navigationIcon = {},
79-
colors = colors,
80-
)
81-
}
82-
OnLifecycleEvent(configAppBar, appBarState) { _, event ->
83-
if (event == Lifecycle.Event.ON_START) {
84-
configAppBar(appBarState)
85-
}
86-
}
65+
ConfigAppBar(navigateToAddUser, configAppBar)
8766

8867
val intentChannel = remember { Channel<ViewIntent>(Channel.UNLIMITED) }
8968
LaunchedEffect(Unit) {
@@ -160,6 +139,36 @@ internal fun UsersListRoute(
160139
)
161140
}
162141

142+
@Composable
143+
@OptIn(ExperimentalMaterial3Api::class)
144+
private fun ConfigAppBar(
145+
navigateToAddUser: () -> Unit,
146+
configAppBar: ConfigAppBar
147+
) {
148+
val title = stringResource(id = R.string.app_name)
149+
val colors = TopAppBarDefaults.centerAlignedTopAppBarColors()
150+
val appBarState = remember(colors) {
151+
AppBarState(
152+
title = title,
153+
actions = {
154+
IconButton(onClick = navigateToAddUser) {
155+
Icon(
156+
imageVector = Icons.Default.Add,
157+
contentDescription = "Add new user",
158+
)
159+
}
160+
},
161+
navigationIcon = {},
162+
colors = colors,
163+
)
164+
}
165+
OnLifecycleEvent(configAppBar, appBarState) { _, event ->
166+
if (event == Lifecycle.Event.ON_START) {
167+
configAppBar(appBarState)
168+
}
169+
}
170+
}
171+
163172
@Composable
164173
private fun DeleteUserConfirmationDialog(
165174
shouldBeDeletedItem: UserItem,

0 commit comments

Comments
 (0)