|
|
|
@ -14,7 +14,7 @@ extension Unit {
|
|
|
|
|
var weight: Int
|
|
|
|
|
var isProfessional: Bool
|
|
|
|
|
var code: Int
|
|
|
|
|
public var subjects: [Subject.Data] = []
|
|
|
|
|
var subjects: [Subject.Data] = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data: Data {
|
|
|
|
@ -24,7 +24,7 @@ extension Unit {
|
|
|
|
|
weight: self.weight,
|
|
|
|
|
isProfessional: self.isProfessional,
|
|
|
|
|
code: self.code,
|
|
|
|
|
subjects: self.subjects.map{ $0.data }
|
|
|
|
|
subjects: self.subjects.map { $0.data }
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -51,15 +51,15 @@ class UnitVM : ObservableObject, Identifiable {
|
|
|
|
|
var id: UUID { original.id }
|
|
|
|
|
@Published var model: Unit.Data
|
|
|
|
|
@Published var isEdited: Bool = false
|
|
|
|
|
@Published var subjectVMs: [SubjectVM]
|
|
|
|
|
|
|
|
|
|
private var subjectsVM: [SubjectVM]
|
|
|
|
|
|
|
|
|
|
public var SubjectsVM: [SubjectVM] { subjectsVM }
|
|
|
|
|
|
|
|
|
|
init(unit: Unit) {
|
|
|
|
|
original = unit
|
|
|
|
|
model = original.data
|
|
|
|
|
subjectVMs = unit.subjects.map { SubjectVM(subject: $0) }
|
|
|
|
|
for subjectVM in subjectVMs {
|
|
|
|
|
subjectVM.unitVM = self
|
|
|
|
|
}
|
|
|
|
|
subjectsVM = unit.subjects.map { SubjectVM(subject: $0) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
@ -80,20 +80,16 @@ class UnitVM : ObservableObject, Identifiable {
|
|
|
|
|
|
|
|
|
|
func onEdited(isCancelled: Bool = false) {
|
|
|
|
|
if(!isCancelled){
|
|
|
|
|
if (isEdited) {
|
|
|
|
|
original.update(from: model)
|
|
|
|
|
// TODO unitsManagerVM?.updateUnits()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
model = original.data
|
|
|
|
|
original.update(from: model)
|
|
|
|
|
}
|
|
|
|
|
model = original.data
|
|
|
|
|
isEdited = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateSubjects() {
|
|
|
|
|
// FIXME neither instruction seems to update the model. At least the unitViewCell wtill displays the old average after we update a grade inside
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
func updateSubject(_ subjectVM: SubjectVM) {
|
|
|
|
|
guard let index = subjectsVM.firstIndex(where: { $0.id == subjectVM.id }) else { return }
|
|
|
|
|
let updatedSubject = subjectsVM[index].model
|
|
|
|
|
original.subjects[index].update(from: updatedSubject)
|
|
|
|
|
model = original.data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,6 +98,6 @@ class UnitVM : ObservableObject, Identifiable {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var IsCalled: Bool {
|
|
|
|
|
return model.subjects.allSatisfy { $0.isCalled }
|
|
|
|
|
return original.subjects.allSatisfy { $0.isCalled }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|