|
1 | 1 | package utils
|
2 | 2 |
|
| 3 | +import model.TodoModel |
| 4 | +import model.TodosModel |
3 | 5 | import kotlin.browser.localStorage
|
4 |
| -import kotlin.js.Json |
5 | 6 | import kotlin.js.Math.random
|
6 | 7 | import kotlin.math.truncate
|
7 | 8 |
|
8 | 9 | external class Number(value: kotlin.Number) {
|
9 | 10 | fun toString(radix: Int): String
|
10 | 11 | }
|
11 | 12 |
|
| 13 | +external class Array<T>() { |
| 14 | + fun concat(toConcat: Any?): Array<T> |
| 15 | + fun forEach(fn: (element: T, index: Int, array: Array<T>) -> Unit): Unit |
| 16 | +} |
| 17 | + |
| 18 | +fun Array<TodoModel>.asList(): List<TodoModel> { |
| 19 | + val list = mutableListOf<TodoModel>() |
| 20 | + this.forEach { todo, index, _ -> |
| 21 | + list.add(index, TodoModel(todo.id, todo.title, todo.completed)) } |
| 22 | + return list.toList() |
| 23 | +} |
| 24 | + |
12 | 25 | fun uuid(): String {
|
13 | 26 | var uuid = "";
|
14 | 27 |
|
@@ -36,16 +49,16 @@ fun pluralize(count: Int, word: String): String {
|
36 | 49 | }
|
37 | 50 | }
|
38 | 51 |
|
39 |
| -fun store(namespace: String): Json { |
| 52 | +fun store(namespace: String): List<TodoModel> { |
40 | 53 | val store = localStorage.getItem(namespace)
|
41 | 54 | return when (store) {
|
42 |
| - null -> JSON.parse("[]") |
43 |
| - else -> JSON.parse(store) |
| 55 | + null -> emptyList() |
| 56 | + else -> Array<TodoModel>().concat(JSON.parse(store)).asList() |
44 | 57 | }
|
45 | 58 | }
|
46 | 59 |
|
47 |
| -fun store(namespace: String, data: Json) { |
48 |
| - localStorage.setItem(namespace, JSON.stringify(data)) |
| 60 | +fun store(namespace: String, model: TodosModel) { |
| 61 | + localStorage.setItem(namespace, model.toString()) |
49 | 62 | }
|
50 | 63 |
|
51 | 64 | fun extend() {
|
|
0 commit comments