This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun FormInputContainer( | |
onAddItem: (ShoppingItem) -> Unit | |
) { | |
var newItem by remember { mutableStateOf("") } | |
val focusManager = LocalFocusManager.current | |
Row( | |
modifier = Modifier | |
.fillMaxWidth(1f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ShoppingList( | |
shoppingItems: List<ShoppingItem>, | |
onToggleTickItem: (ShoppingItem) -> Unit, | |
onDeleteItem: (ShoppingItem) -> Unit, | |
modifier: Modifier = Modifier | |
) { | |
LazyColumn(modifier.padding(8.dp)) { | |
items( | |
items = shoppingItems, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun LoadingIndicator(modifier: Modifier = Modifier) { | |
Box(modifier = modifier.fillMaxWidth(), contentAlignment = Alignment.Center) { | |
CircularProgressIndicator(color = MaterialTheme.colors.primary) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Preview(showBackground = true) | |
@Composable | |
fun PreviewShoppingListScreen() { | |
val previewItems = List(10) { index -> | |
ShoppingItem(id = index, name = "Item Preview $index") | |
} | |
MaterialTheme { | |
ShoppingListScreen( | |
isLoading = true, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalMaterialApi::class) | |
@Composable | |
fun ShoppingItemCard( | |
shoppingItem: ShoppingItem, | |
onToggleTickItem: (ShoppingItem) -> Unit, | |
onDeleteItem: (ShoppingItem) -> Unit | |
) { | |
val swiperState = rememberDismissState( | |
confirmStateChange = { dismissValue -> | |
val isItemDismissed = dismissValue == DismissValue.DismissedToEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder