Skip to content

Instantly share code, notes, and snippets.

@chiragthummar
Created December 25, 2023 06:27
Show Gist options
  • Save chiragthummar/0c6befd2378f3b3c037ed27f0c65c9b1 to your computer and use it in GitHub Desktop.
Save chiragthummar/0c6befd2378f3b3c037ed27f0c65c9b1 to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun HomeScreen(state: HomeScreenState, onEvent: (HomeScreenEvents) -> Unit) {
val keyboardManager = LocalSoftwareKeyboardController.current
Scaffold(topBar = {
TopAppBar(
title = {
Text(text = "MVI App", color = MaterialTheme.colorScheme.onPrimary)
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primary
)
)
}) { pv ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(pv),
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
placeholder = {
Text(text = "Enter image prompt")
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
value = state.text, onValueChange = {
onEvent(HomeScreenEvents.UpdateText(it))
},
keyboardActions = KeyboardActions(
onDone = {
keyboardManager?.hide()
onEvent(HomeScreenEvents.LoadImages(state.text))
}
),
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Done
),
trailingIcon = {
if(state.text.isNotEmpty()){
IconButton(onClick = {
onEvent(HomeScreenEvents.UpdateText(""))
}) {
Icon(imageVector = Icons.Filled.Clear, contentDescription = "Clear")
}
}
}
)
Box(
modifier = Modifier
.fillMaxSize()
.weight(1f),
contentAlignment = Alignment.Center
) {
if (state.isLoading) {
CircularProgressIndicator()
} else if (state.images.isEmpty()) {
Text(text = "No images found")
} else {
MyListView(images = state.images)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment