-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHabitListViewModel.swift
51 lines (38 loc) · 1.25 KB
/
HabitListViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// HabitListViewModel.swift
// Productiv
//
// Created by Dev Patel on 10/14/21.
//
import Foundation
import Combine
class HabitListViewModel: ObservableObject{//list of all of the tasks on the screen
@Published var HabitCellViewModels = [HabitCellViewModel]()
@Published var habitRepository=HabitRepository()
private var cancellable = Set<AnyCancellable>()
init(){
habitRepository.$habits
.map{habits in//adding our tasks to TCVM
habits.map{habit in
HabitCellViewModel(habit: habit)
}
}
.assign(to: \.HabitCellViewModels,on:self)
.store(in: &cancellable)
}
func addHabit(habit: Habit){//adds a task to the ViewModel
print("in?")
habitRepository.addHabit(habit)
print("taskName: \(habit.habitName)")
let habitVM = HabitCellViewModel(habit: habit)
self.HabitCellViewModels.append(habitVM)
}
func updateHabit(habit: Habit){
habitRepository.updateHabit(habit)
}
func deleteHabit(habit: Habit){
habitRepository.delteHabit(habit)
let habitVM = HabitCellViewModel(habit: habit)
self.HabitCellViewModels.removeAll{$0.id==habitVM.id}
}
}