Skip to content

Commit 14bef73

Browse files
committed
Change TodoModel to data class
1 parent 66b6937 commit 14bef73

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

src/model/TodoModel.kt

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
11
package model
22

3-
class TodoModel(val id: String, var title: String, var completed: Boolean = false) {
4-
fun toggle(completed: Boolean): TodoModel {
5-
return TodoModel(
6-
id = id,
7-
title = title,
8-
completed = completed
9-
)
10-
}
11-
12-
fun changeTitle(title: String): TodoModel {
13-
return TodoModel(
14-
id = id,
15-
title = title,
16-
completed = completed
17-
)
18-
}
19-
}
3+
data class TodoModel(val id: String, val title: String, val completed: Boolean = false)

src/model/TodosModel.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class TodosModel(val key: String = "todomvc-react-kotlin") {
2424

2525
fun toggleAll(completed: Boolean) {
2626
todos = todos.map {
27-
it.toggle(completed)
27+
it.copy(completed = completed)
2828
}
2929
inform()
3030
}
3131

3232
fun toggle(id: String) {
3333
todos = todos.map {
3434
when (it.id) {
35-
id -> it.toggle(!it.completed)
35+
id -> it.copy(completed = !it.completed)
3636
else -> it
3737
}
3838
}
@@ -47,7 +47,7 @@ class TodosModel(val key: String = "todomvc-react-kotlin") {
4747
fun save(todo: TodoModel, text: String) {
4848
todos = todos.map {
4949
when (it) {
50-
todo -> todo.changeTitle(text)
50+
todo -> todo.copy(title = text)
5151
else -> it
5252
}
5353
}

0 commit comments

Comments
 (0)