mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-18 00:57:02 +05:30
Borrow some improvements from my previous attempt
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
0c3b1d388f
commit
bd6f7583c8
1 changed files with 8 additions and 11 deletions
|
@ -10,11 +10,8 @@ import androidx.compose.material.MaterialTheme
|
|||
import androidx.compose.material.Scaffold
|
||||
import androidx.compose.material.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.setContent
|
||||
|
@ -36,8 +33,9 @@ class MainActivity : AppCompatActivity() {
|
|||
TodoTheme {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val itemsDao = Graph.database.todoItemsDao()
|
||||
val items by itemsDao.getAllItems().collectAsState(initial = emptyList())
|
||||
TodoApp(
|
||||
itemsDao.getAllItems().collectAsState(initial = emptyList()),
|
||||
items,
|
||||
{ item -> coroutineScope.launch { itemsDao.insert(item) } },
|
||||
{ item -> coroutineScope.launch { itemsDao.delete(item) } },
|
||||
)
|
||||
|
@ -48,17 +46,16 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
@Composable
|
||||
fun TodoApp(
|
||||
items: State<List<TodoItem>>,
|
||||
items: List<TodoItem>,
|
||||
onAdd: (item: TodoItem) -> Unit,
|
||||
onDelete: (item: TodoItem) -> Unit,
|
||||
) {
|
||||
val realItems by items
|
||||
|
||||
Scaffold(
|
||||
topBar = { TopAppBar({ Text(text = "I can Compose?") }) },
|
||||
floatingActionButton = {
|
||||
FloatingActionButton(
|
||||
onClick = { onAdd.invoke(TodoItem("Item ${realItems.size + 1}")) },
|
||||
onClick = { onAdd.invoke(TodoItem("Item ${items.size + 1}")) },
|
||||
elevation = 8.dp,
|
||||
modifier = Modifier.semantics { testTag = "fab" }
|
||||
) {
|
||||
|
@ -70,7 +67,7 @@ fun TodoApp(
|
|||
},
|
||||
bodyContent = {
|
||||
LazyColumnFor(
|
||||
items = realItems,
|
||||
items = items,
|
||||
modifier = Modifier.padding(horizontal = 16.dp)
|
||||
) { todoItem ->
|
||||
TodoRowItem(item = todoItem) { onDelete.invoke(todoItem) }
|
||||
|
@ -83,11 +80,11 @@ fun TodoApp(
|
|||
@Composable
|
||||
fun PreviewApp() {
|
||||
TodoTheme {
|
||||
val items = remember { mutableStateOf(listOf(TodoItem("Item 1"))) }
|
||||
val items = arrayListOf(TodoItem("Item 1"))
|
||||
TodoApp(
|
||||
items,
|
||||
{},
|
||||
{},
|
||||
items::add,
|
||||
items::remove,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue