mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-18 07:57:03 +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.Scaffold
|
||||||
import androidx.compose.material.TopAppBar
|
import androidx.compose.material.TopAppBar
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.State
|
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.setContent
|
import androidx.compose.ui.platform.setContent
|
||||||
|
@ -36,8 +33,9 @@ class MainActivity : AppCompatActivity() {
|
||||||
TodoTheme {
|
TodoTheme {
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val itemsDao = Graph.database.todoItemsDao()
|
val itemsDao = Graph.database.todoItemsDao()
|
||||||
|
val items by itemsDao.getAllItems().collectAsState(initial = emptyList())
|
||||||
TodoApp(
|
TodoApp(
|
||||||
itemsDao.getAllItems().collectAsState(initial = emptyList()),
|
items,
|
||||||
{ item -> coroutineScope.launch { itemsDao.insert(item) } },
|
{ item -> coroutineScope.launch { itemsDao.insert(item) } },
|
||||||
{ item -> coroutineScope.launch { itemsDao.delete(item) } },
|
{ item -> coroutineScope.launch { itemsDao.delete(item) } },
|
||||||
)
|
)
|
||||||
|
@ -48,17 +46,16 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TodoApp(
|
fun TodoApp(
|
||||||
items: State<List<TodoItem>>,
|
items: List<TodoItem>,
|
||||||
onAdd: (item: TodoItem) -> Unit,
|
onAdd: (item: TodoItem) -> Unit,
|
||||||
onDelete: (item: TodoItem) -> Unit,
|
onDelete: (item: TodoItem) -> Unit,
|
||||||
) {
|
) {
|
||||||
val realItems by items
|
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = { TopAppBar({ Text(text = "I can Compose?") }) },
|
topBar = { TopAppBar({ Text(text = "I can Compose?") }) },
|
||||||
floatingActionButton = {
|
floatingActionButton = {
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
onClick = { onAdd.invoke(TodoItem("Item ${realItems.size + 1}")) },
|
onClick = { onAdd.invoke(TodoItem("Item ${items.size + 1}")) },
|
||||||
elevation = 8.dp,
|
elevation = 8.dp,
|
||||||
modifier = Modifier.semantics { testTag = "fab" }
|
modifier = Modifier.semantics { testTag = "fab" }
|
||||||
) {
|
) {
|
||||||
|
@ -70,7 +67,7 @@ fun TodoApp(
|
||||||
},
|
},
|
||||||
bodyContent = {
|
bodyContent = {
|
||||||
LazyColumnFor(
|
LazyColumnFor(
|
||||||
items = realItems,
|
items = items,
|
||||||
modifier = Modifier.padding(horizontal = 16.dp)
|
modifier = Modifier.padding(horizontal = 16.dp)
|
||||||
) { todoItem ->
|
) { todoItem ->
|
||||||
TodoRowItem(item = todoItem) { onDelete.invoke(todoItem) }
|
TodoRowItem(item = todoItem) { onDelete.invoke(todoItem) }
|
||||||
|
@ -83,11 +80,11 @@ fun TodoApp(
|
||||||
@Composable
|
@Composable
|
||||||
fun PreviewApp() {
|
fun PreviewApp() {
|
||||||
TodoTheme {
|
TodoTheme {
|
||||||
val items = remember { mutableStateOf(listOf(TodoItem("Item 1"))) }
|
val items = arrayListOf(TodoItem("Item 1"))
|
||||||
TodoApp(
|
TodoApp(
|
||||||
items,
|
items,
|
||||||
{},
|
items::add,
|
||||||
{},
|
items::remove,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue