mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-18 13: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.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
|
||||||
|
@ -31,27 +34,31 @@ class MainActivity : AppCompatActivity() {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContent {
|
setContent {
|
||||||
TodoTheme {
|
TodoTheme {
|
||||||
TodoApp()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun TodoApp() {
|
|
||||||
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(
|
||||||
|
itemsDao.getAllItems().collectAsState(initial = emptyList()),
|
||||||
|
{ item -> coroutineScope.launch { itemsDao.insert(item) } },
|
||||||
|
{ item -> coroutineScope.launch { itemsDao.delete(item) } },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun TodoApp(
|
||||||
|
items: State<List<TodoItem>>,
|
||||||
|
onAdd: (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 = {
|
onClick = { onAdd.invoke(TodoItem("Item ${realItems.size + 1}")) },
|
||||||
coroutineScope.launch {
|
|
||||||
itemsDao.insert(TodoItem("Item ${items.size + 1}"))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
elevation = 8.dp,
|
elevation = 8.dp,
|
||||||
modifier = Modifier.semantics { testTag = "fab" }
|
modifier = Modifier.semantics { testTag = "fab" }
|
||||||
) {
|
) {
|
||||||
|
@ -62,12 +69,11 @@ fun TodoApp() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
bodyContent = {
|
bodyContent = {
|
||||||
LazyColumnFor(items = items, modifier = Modifier.padding(horizontal = 16.dp)) { todoItem ->
|
LazyColumnFor(
|
||||||
TodoRowItem(item = todoItem) {
|
items = realItems,
|
||||||
coroutineScope.launch {
|
modifier = Modifier.padding(horizontal = 16.dp)
|
||||||
itemsDao.delete(todoItem)
|
) { todoItem ->
|
||||||
}
|
TodoRowItem(item = todoItem) { onDelete.invoke(todoItem) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -77,6 +83,11 @@ fun TodoApp() {
|
||||||
@Composable
|
@Composable
|
||||||
fun PreviewApp() {
|
fun PreviewApp() {
|
||||||
TodoTheme {
|
TodoTheme {
|
||||||
TodoApp()
|
val items = remember { mutableStateOf(listOf(TodoItem("Item 1"))) }
|
||||||
|
TodoApp(
|
||||||
|
items,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue