Android Day 26 ToDoList App
package com.williamjiamin.supertodolistapp
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Card
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.williamjiamin.supertodolistapp.data.DummyToDo
import com.williamjiamin.supertodolistapp.data.ToDo
@Composable
fun HomeView(
navController: NavController,
viewModel: ToDoViewModel
) {
val context = LocalContext.current
Scaffold(
topBar = {
TopBarView(title = "Super To Do List App") {
// Toast.makeText(context, "You just clicked the back button!", Toast.LENGTH_LONG).show()
}
},
floatingActionButton = {
FloatingActionButton(
modifier = Modifier.padding(all = 20.dp),
contentColor = Color.Green,
backgroundColor = Color.DarkGray,
onClick = {
// Toast.makeText(context, "You just clicked the floating action button!", Toast.LENGTH_LONG).show()
navController.navigate(ScreenController.AddToDo.route)
}
) {
Icon(imageVector = Icons.Default.Add, contentDescription = null)
}
}
) {
LazyColumn(modifier = Modifier
.fillMaxSize()
.padding(it)
.background(color = Color.Black)
) {
items(DummyToDo.toDoList) { toDo ->
ToDoItems(toDo = toDo) {
// Handle ToDo item click here if needed
}
}
}
}
}
@Composable
fun ToDoItems(toDo: ToDo, onClick: () -> Unit = {}) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.clickable(onClick = onClick),
backgroundColor = Color.DarkGray ,
elevation = 8.dp
) {
Column(modifier = Modifier.padding(8.dp)) {
Text(text = toDo.title, fontSize = 24.sp, fontWeight = FontWeight.Bold, color = Color.Green)
Text(text = toDo.description, fontSize = 16.sp, fontWeight = FontWeight.Normal, color = Color.Green)
}
}
}
package com.williamjiamin.supertodolistapp
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.williamjiamin.supertodolistapp.ui.theme.SuperToDoListAppTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SuperToDoListAppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = Color.Black
) {
Navigation()
}
}
}
}
}
package com.williamjiamin.supertodolistapp
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
@Composable
fun ModifyView(
id: Long,
viewModel: ToDoViewModel,
navController: NavHostController
) {
Scaffold (topBar = {
TopBarView(
title =
if (id != 0L) "Update To Do Items" else "Add To Do Items"){
navController.navigateUp()
}
}
){
Column(modifier = Modifier
.padding(it)
.background(color = Color.Black)
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
InputTextField(
label = "Title",
value = viewModel.toDoTitleState,
onValueChange = { viewModel.onTitleChange(it) }
)
InputTextField(
label = "Description",
value = viewModel.toDoDescriptionState,
onValueChange = { viewModel.onDescriptionChange(it) }
)
// ImputTextField(
// label = "Priority",
// value = viewModel.priority,
// onValueChange = { viewModel.onPriorityChange(it) }
// )
// ImputTextField(
// label = "Date",
// value = viewModel.date,
// onValueChange = { viewModel.onDateChange(it) }
// )
// ImputTextField(
// label = "Time",
// value = viewModel.time,
// onValueChange = { viewModel.onTimeChange(it) }
// )
Button(onClick = {
if (viewModel.toDoTitleState.isNotEmpty() &&
viewModel.toDoDescriptionState.isNotEmpty()
) {
} else {
}
}) {
Text(
text = if (id != 0L) "Update" else "Add",
color = Color.Green,
style = TextStyle(fontSize = 20.sp)
)
}
}
}
}
@Composable
fun InputTextField(
label: String,
value: String,
onValueChange: (String) -> Unit
) {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
label = { Text(text = label, color = Color.Green) },
modifier = Modifier
.padding(8.dp)
.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = Color.Green,
unfocusedBorderColor = Color.Green
)
)
}
package com.williamjiamin.supertodolistapp
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.Navigation
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
@Composable
fun Navigation(viewModel: ToDoViewModel = ToDoViewModel(),
navController: NavHostController = rememberNavController()) {
NavHost(
navController = navController,
startDestination = ScreenController.Home.route
){
composable(ScreenController.Home.route){
HomeView(navController, viewModel)
}
composable(ScreenController.AddToDo.route){
ModifyView(
id = 0L,
viewModel = viewModel,
navController = navController
)
}
}
}
package com.williamjiamin.supertodolistapp
sealed class ScreenController(val route: String) {
object Home: ScreenController("home")
object AddToDo: ScreenController("add_todo")
}
package com.williamjiamin.supertodolistapp
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
class ToDoViewModel: ViewModel(){
var toDoTitleState by mutableStateOf("")
var toDoDescriptionState by mutableStateOf("")
fun onTitleChange(newTitle: String){
toDoTitleState = newTitle
}
fun onDescriptionChange(newDescription: String){
toDoDescriptionState = newDescription
}
}
package com.williamjiamin.supertodolistapp
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBarView(
title: String,
onBackNavClicked: () -> Unit= {}
) {
val navigationIcon : (@Composable () -> Unit)? =
if(!title.contains("Super To Do List App")){
{
IconButton(onClick = { onBackNavClicked() }) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
tint = Color.Green,
contentDescription = null
)
}
}
}else{
null
}
TopAppBar(
title = {
Text(text = title,
color = Color.Green,
modifier = Modifier
.padding(start = 4.dp)
.heightIn(max = 24.dp))
},
elevation = 3.dp,
backgroundColor = Color.Black,
navigationIcon = navigationIcon
)
}