mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 23:47:02 +05:30
Separate out item loading to enable host previews again
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
778faf2950
commit
0c3b1d388f
1 changed files with 28 additions and 17 deletions
|
@ -10,8 +10,11 @@ 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
|
||||
|
@ -31,27 +34,31 @@ class MainActivity : AppCompatActivity() {
|
|||
super.onCreate(savedInstanceState)
|
||||
setContent {
|
||||
TodoTheme {
|
||||
TodoApp()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val itemsDao = Graph.database.todoItemsDao()
|
||||
TodoApp(
|
||||
itemsDao.getAllItems().collectAsState(initial = emptyList()),
|
||||
{ item -> coroutineScope.launch { itemsDao.insert(item) } },
|
||||
{ item -> coroutineScope.launch { itemsDao.delete(item) } },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TodoApp() {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val itemsDao = Graph.database.todoItemsDao()
|
||||
val items by itemsDao.getAllItems().collectAsState(initial = emptyList())
|
||||
fun TodoApp(
|
||||
items: State<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 = {
|
||||
coroutineScope.launch {
|
||||
itemsDao.insert(TodoItem("Item ${items.size + 1}"))
|
||||
}
|
||||
},
|
||||
onClick = { onAdd.invoke(TodoItem("Item ${realItems.size + 1}")) },
|
||||
elevation = 8.dp,
|
||||
modifier = Modifier.semantics { testTag = "fab" }
|
||||
) {
|
||||
|
@ -62,12 +69,11 @@ fun TodoApp() {
|
|||
}
|
||||
},
|
||||
bodyContent = {
|
||||
LazyColumnFor(items = items, modifier = Modifier.padding(horizontal = 16.dp)) { todoItem ->
|
||||
TodoRowItem(item = todoItem) {
|
||||
coroutineScope.launch {
|
||||
itemsDao.delete(todoItem)
|
||||
}
|
||||
}
|
||||
LazyColumnFor(
|
||||
items = realItems,
|
||||
modifier = Modifier.padding(horizontal = 16.dp)
|
||||
) { todoItem ->
|
||||
TodoRowItem(item = todoItem) { onDelete.invoke(todoItem) }
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -77,6 +83,11 @@ fun TodoApp() {
|
|||
@Composable
|
||||
fun PreviewApp() {
|
||||
TodoTheme {
|
||||
TodoApp()
|
||||
val items = remember { mutableStateOf(listOf(TodoItem("Item 1"))) }
|
||||
TodoApp(
|
||||
items,
|
||||
{},
|
||||
{},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue