@@ -16,7 +16,7 @@ struct TodosRepositoryImpl: TodosRepository {
1616 func addTodo( name: String , date: Date , note: String , category: TodoCategory ) -> Observable < Void > {
1717 Future < Void , Error > { promise in
1818 do {
19- let item = TodoItem ( name: name, note: note, category : category, date: date)
19+ let item = TodoItem ( name: name, note: note, categoryId : category. id , date: date)
2020 modelContext? . insert ( item)
2121 try modelContext? . save ( )
2222 promise ( . success( ( ) ) )
@@ -40,6 +40,21 @@ struct TodosRepositoryImpl: TodosRepository {
4040 . eraseToAnyPublisher ( )
4141 }
4242
43+ func getTodos( category: TodoCategory ) -> Observable < [ TodoItem ] > {
44+ Future < [ TodoItem ] , Error > { promise in
45+ do {
46+ let descriptor = FetchDescriptor < TodoItem > ( predicate: #Predicate {
47+ $0. categoryId == category. id
48+ } )
49+ let items = try modelContext? . fetch ( descriptor) ?? [ ]
50+ promise ( . success( items) )
51+ } catch {
52+ promise ( . failure( error) )
53+ }
54+ }
55+ . eraseToAnyPublisher ( )
56+ }
57+
4358 func deleteTodo( item: TodoItem ) -> Observable < Void > {
4459 Future < Void , Error > { promise in
4560 do {
@@ -53,11 +68,15 @@ struct TodosRepositoryImpl: TodosRepository {
5368 . eraseToAnyPublisher ( )
5469 }
5570
71+ func updateCompleted( item: TodoItem ) {
72+ item. isCompleted. toggle ( )
73+ }
74+
5675 private func groupTodoItemsToTodoLists( items: [ TodoItem ] ) -> [ TodoList ] {
5776 var dict : [ TodoCategory : TodoList ] = [ : ]
5877
5978 for todoItem in items {
60- dict [ todoItem. category , default: TodoList ( category: todoItem. category ) ] . items. append ( todoItem)
79+ dict [ TodoCategory . byId ( todoItem. categoryId ) , default: TodoList ( category: TodoCategory . byId ( todoItem. categoryId ) ) ] . items. append ( todoItem)
6180 }
6281
6382 return Array ( dict. values)
0 commit comments