Skip to content

Instantly share code, notes, and snippets.

View widiarifki's full-sized avatar

Widia Rifkianti widiarifki

View GitHub Profile
@Composable
fun FormInputContainer(
onAddItem: (ShoppingItem) -> Unit
) {
var newItem by remember { mutableStateOf("") }
val focusManager = LocalFocusManager.current
Row(
modifier = Modifier
.fillMaxWidth(1f)
@Composable
fun ShoppingList(
shoppingItems: List<ShoppingItem>,
onToggleTickItem: (ShoppingItem) -> Unit,
onDeleteItem: (ShoppingItem) -> Unit,
modifier: Modifier = Modifier
) {
LazyColumn(modifier.padding(8.dp)) {
items(
items = shoppingItems,
@Composable
fun EmptyState(modifier: Modifier = Modifier) {
Box(modifier = modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
Text(
text = "Yah, belum ada item belanja!",
style = MaterialTheme.typography.h5,
textAlign = TextAlign.Center
)
}
}
@Composable
fun LoadingIndicator(modifier: Modifier = Modifier) {
Box(modifier = modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
CircularProgressIndicator(color = MaterialTheme.colors.primary)
}
}
buildscript {
ext {
compose_version = '1.0.1'
// Compose need to use Kotlin v 1.5.10 or more
kotlin_version = '1.5.21'
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
@Entity(tableName = "tbl_shopping_item")
data class ShoppingItem(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
@ColumnInfo(name = "name")
var name: String,
@ColumnInfo(name = "is_ticked")
var isTicked: Boolean = false
)
@Preview(showBackground = true)
@Composable
fun PreviewShoppingListScreen() {
val previewItems = List(10) { index ->
ShoppingItem(id = index, name = "Item Preview $index")
}
MaterialTheme {
ShoppingListScreen(
isLoading = true,
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ShoppingItemCard(
shoppingItem: ShoppingItem,
onToggleTickItem: (ShoppingItem) -> Unit,
onDeleteItem: (ShoppingItem) -> Unit
) {
val swiperState = rememberDismissState(
confirmStateChange = { dismissValue ->
val isItemDismissed = dismissValue == DismissValue.DismissedToEnd
android {
// your other configuration..
buildFeatures {
// enable jetpack compose in the module
compose true
}
// target Java compiler to Java 8
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
class ShoppingListViewModel(
private val repository: ShoppingItemRepository = ShoppingItemRepository()
) : ViewModel() {
// variables that hold state
var isLoading by mutableStateOf(true)
val shoppingItems: LiveData<List<ShoppingItem>> = getItems()
private fun getItems(): LiveData<List<ShoppingItem>> {
isLoading = true