Untitled

mail@pastecode.io avatar
unknown
kotlin
7 months ago
2.8 kB
1
Indexable
Never
data class Note(
    val id: String = UUID.randomUUID().toString(),
    val title: String,
    val desc: String,
    var isChecked: MutableState<Boolean> = mutableStateOf(false)
)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val list = remember {
                mutableStateListOf(Note(title = "Hello", desc = "World"),
                    Note(title = "World", desc="Destroyed"))
            }

            TodoDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    MainScreen(list = list)
                }
            }
        }
    }
}

@Composable
fun MainScreen(list: MutableList<Note>, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxSize()
    ) {
        TextInputView(list = list)
        ListView(list = list)
    }
}

 @Composable
 fun TextInputView(list: MutableList<Note>)
 {
     var title by rememberSaveable {
         mutableStateOf("")

     }
     var desc by rememberSaveable {
         mutableStateOf("")
     }
     Column (
         modifier = Modifier.fillMaxWidth(),
         verticalArrangement =  Arrangement.spacedBy(8.dp)
     ){
         OutlinedTextField(value = title,
             onValueChange = { title = it },
             placeholder = { Text("Enter Title")},
             modifier = Modifier.fillMaxWidth()
             )
         OutlinedTextField(value = desc,
             onValueChange = { desc = it },
             placeholder = {Text("Enter Desc")},
             modifier = Modifier.fillMaxWidth())
         Button(onClick = {
             list.add(Note(title = title, desc = desc))
             title = ""
             desc = ""
         }) {
             Text("Add Note")
         }
     }

 }

@Composable
fun ListView(list: List<Note>) {
    LazyColumn {
        items(list) { note ->
            RowView(note)
        }
    }
}



@Composable
fun RowView(note: Note) {
    Row(
        modifier = Modifier
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Checkbox(
            checked = note.isChecked.value,
            onCheckedChange = {
                note.isChecked.value = !note.isChecked.value
            }
        )
        Text(note.title)
    }
}

@Preview(showBackground = true)
@Composable
fun RowViewPreview() {
    TodoDemoTheme {
        RowView(Note(title = "Hello", desc = ""))
    }
}