Step #3: Adding the Constructor
Let’s add 5 properties to ToDoModel
, as constructor val
parameters:
- A unique ID
- A flag to indicate if the task is completed or not
- A description, which will appear in the list
- Some notes, in case there is more information
- The date/time that the model was created on
To that end, modify ToDoModel
to look like:
package com.commonsware.todo
import java.time.Instant
import java.util.*
data class ToDoModel(
val description: String,
val id: String = UUID.randomUUID().toString(),
val isCompleted: Boolean = false,
val notes: String = "",
val createdOn: Instant = Instant.now()
)
Here, we have added the five constructor parameters. Four of them — all but description
— provide default values, so we can supply values or not as we see fit when we create instances.
Of particular note:
- We use
UUID
to generate a unique identifier for our to-do item, held in theid
property - We use
Instant
for tracking the created-on time for this to-do item, held in thecreatedOn
property
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.