From a682c12be7b4a59996f8a0c7d3b6292fc8e6596d Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Sun, 28 May 2023 23:44:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Graduator/Graduator.xcodeproj/project.pbxproj | 16 ++ Graduator/Graduator/MainView.swift | 8 +- .../View/Cells/SubjectViewCell.swift | 87 +++---- .../Graduator/View/Cells/UnitViewCell.swift | 8 +- .../View/Forms/SubjectFormView.swift | 40 ++++ .../Graduator/View/Utils/Formatters.swift | 9 + Graduator/Graduator/View/Views/UnitView.swift | 102 ++++++-- .../Graduator/ViewModel/SubjectFormVM.swift | 38 +++ Graduator/Graduator/ViewModel/SubjectVM.swift | 19 +- Graduator/Graduator/ViewModel/UnitVM.swift | 25 +- .../Graduator/ViewModel/UnitsManagerVM.swift | 4 +- README.md | 223 +++++++++++++++++- 12 files changed, 489 insertions(+), 90 deletions(-) create mode 100644 Graduator/Graduator/View/Forms/SubjectFormView.swift create mode 100644 Graduator/Graduator/ViewModel/SubjectFormVM.swift diff --git a/Graduator/Graduator.xcodeproj/project.pbxproj b/Graduator/Graduator.xcodeproj/project.pbxproj index 5701db1..3a41674 100644 --- a/Graduator/Graduator.xcodeproj/project.pbxproj +++ b/Graduator/Graduator.xcodeproj/project.pbxproj @@ -19,6 +19,8 @@ EC242B882A1FC605006FE760 /* NoGradesInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC242B872A1FC605006FE760 /* NoGradesInfo.swift */; }; EC242B8A2A1FCECA006FE760 /* AverageBlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC242B892A1FCECA006FE760 /* AverageBlockView.swift */; }; EC5FE5A52A20882F0028AA5F /* Formatters.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC5FE5A42A20882F0028AA5F /* Formatters.swift */; }; + ECB2FFCE2A23C4A700FF9F91 /* SubjectFormView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB2FFCD2A23C4A700FF9F91 /* SubjectFormView.swift */; }; + ECB2FFD02A23C4B700FF9F91 /* SubjectFormVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB2FFCF2A23C4B700FF9F91 /* SubjectFormVM.swift */; }; ECC581D22A1D085B006C55EF /* GraduatorApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECC581D12A1D085B006C55EF /* GraduatorApp.swift */; }; ECC581D62A1D085C006C55EF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = ECC581D52A1D085C006C55EF /* Assets.xcassets */; }; ECC581D92A1D085C006C55EF /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = ECC581D82A1D085C006C55EF /* Preview Assets.xcassets */; }; @@ -39,6 +41,8 @@ EC242B872A1FC605006FE760 /* NoGradesInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoGradesInfo.swift; sourceTree = ""; }; EC242B892A1FCECA006FE760 /* AverageBlockView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AverageBlockView.swift; sourceTree = ""; }; EC5FE5A42A20882F0028AA5F /* Formatters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Formatters.swift; sourceTree = ""; }; + ECB2FFCD2A23C4A700FF9F91 /* SubjectFormView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubjectFormView.swift; sourceTree = ""; }; + ECB2FFCF2A23C4B700FF9F91 /* SubjectFormVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubjectFormVM.swift; sourceTree = ""; }; ECC581CE2A1D085B006C55EF /* Graduator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Graduator.app; sourceTree = BUILT_PRODUCTS_DIR; }; ECC581D12A1D085B006C55EF /* GraduatorApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraduatorApp.swift; sourceTree = ""; }; ECC581D52A1D085C006C55EF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -61,6 +65,7 @@ EC242B6F2A1F8260006FE760 /* View */ = { isa = PBXGroup; children = ( + ECB2FFCC2A23C49500FF9F91 /* Forms */, EC5FE5A32A20881B0028AA5F /* Utils */, EC242B862A1FC5EC006FE760 /* Components */, EC242B772A1F834C006FE760 /* Cells */, @@ -103,6 +108,14 @@ path = Utils; sourceTree = ""; }; + ECB2FFCC2A23C49500FF9F91 /* Forms */ = { + isa = PBXGroup; + children = ( + ECB2FFCD2A23C4A700FF9F91 /* SubjectFormView.swift */, + ); + path = Forms; + sourceTree = ""; + }; ECC581C52A1D085B006C55EF = { isa = PBXGroup; children = ( @@ -148,6 +161,7 @@ ECC581E02A1D08DB006C55EF /* SubjectVM.swift */, ECC581E42A1D0C44006C55EF /* UnitVM.swift */, EC242B7E2A1F83BF006FE760 /* UnitsManagerVM.swift */, + ECB2FFCF2A23C4B700FF9F91 /* SubjectFormVM.swift */, ); path = ViewModel; sourceTree = ""; @@ -232,6 +246,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + ECB2FFCE2A23C4A700FF9F91 /* SubjectFormView.swift in Sources */, EC242B712A1F8283006FE760 /* MainView.swift in Sources */, EC242B6E2A1F81CC006FE760 /* UnitsManager.swift in Sources */, ECC581E52A1D0C44006C55EF /* UnitVM.swift in Sources */, @@ -240,6 +255,7 @@ EC242B7B2A1F838C006FE760 /* UnitViewCell.swift in Sources */, EC242B6C2A1F81AE006FE760 /* Subject.swift in Sources */, EC242B8A2A1FCECA006FE760 /* AverageBlockView.swift in Sources */, + ECB2FFD02A23C4B700FF9F91 /* SubjectFormVM.swift in Sources */, ECC581E12A1D08DB006C55EF /* SubjectVM.swift in Sources */, EC242B6A2A1F8189006FE760 /* Unit.swift in Sources */, EC242B752A1F8339006FE760 /* UnitView.swift in Sources */, diff --git a/Graduator/Graduator/MainView.swift b/Graduator/Graduator/MainView.swift index cb541b1..aa0b62f 100644 --- a/Graduator/Graduator/MainView.swift +++ b/Graduator/Graduator/MainView.swift @@ -49,10 +49,10 @@ struct MainView: View { unitVM: unitVM, unitsManagerVM: unitsManagerVM )) { - UnitViewCell( - unitVM: unitVM, - unitsManagerVM: unitsManagerVM - ) + UnitViewCell( + unitVM: unitVM, + unitsManagerVM: unitsManagerVM + ) } } } diff --git a/Graduator/Graduator/View/Cells/SubjectViewCell.swift b/Graduator/Graduator/View/Cells/SubjectViewCell.swift index 5d98061..137edb3 100644 --- a/Graduator/Graduator/View/Cells/SubjectViewCell.swift +++ b/Graduator/Graduator/View/Cells/SubjectViewCell.swift @@ -11,101 +11,78 @@ struct SubjectViewCell: View { @ObservedObject var subjectVM: SubjectVM @ObservedObject var unitVM: UnitVM @ObservedObject var unitsManagerVM: UnitsManagerVM - - //TODO also allow using the unitview's navigation bar item "Edit" (makes all subjects editable, and more) - @State private var isEditable = false - + + @State private var isGradeEditable = false + var body: some View { HStack { - if isEditable { - VStack { - Button(action: { - isEditable = false - subjectVM.onEdited() - unitVM.updateSubject(subjectVM) - unitsManagerVM.updateUnit(unitVM) - }) { - Image(systemName: "checkmark.square") - .foregroundColor(.green) - } - - Button(action: { - isEditable = false - subjectVM.onEdited(isCancelled: true) - }) { - Image(systemName: "nosign.app") - .padding(.top, 10.0) - .foregroundColor(.pink) - } - } - } else { - Button(action: { - isEditable = true - subjectVM.onEditing() - }) { - Image(systemName: "lock") - } - } - VStack { HStack { TextField("", text: $subjectVM.model.name) - .disabled(!isEditable) - + .disabled(!unitsManagerVM.isAllEditable) - TextField("", value: $subjectVM.model.weight, formatter: NumberFormatter()) - .frame(width: 20) - .disabled(!isEditable) + TextField("", value: $subjectVM.model.weight, formatter: Formatters.weightFormatter) + .frame(width: 40) + .disabled(!unitsManagerVM.isAllEditable) } HStack { if let grade = subjectVM.model.grade { + VStack { + Toggle("", isOn: $isGradeEditable) + .frame(width: 40) + .onChange(of: isGradeEditable) { value in + if !value { + subjectVM.onEdited() + unitVM.updateSubject(subjectVM) + unitsManagerVM.updateUnit(unitVM) + } + } + Image(systemName: isGradeEditable ? "checkmark" : "lock.open") + } Slider(value: Binding( get: { grade }, set: { newValue in - if isEditable { + if isGradeEditable { subjectVM.model.grade = newValue } } ), in: 0...1, step: 0.001) .accentColor(grade < 0.5 ? .red : .green) - .disabled(!isEditable || subjectVM.model.isCalled) + .disabled(!isGradeEditable || subjectVM.model.isCalled) TextField("", value: Binding( get: { grade * 20.0 }, set: { newValue in - if isEditable { + if isGradeEditable { subjectVM.model.grade = newValue / 20.0 } } ), formatter: Formatters.gradeFormatter) .frame(width: 50) - .disabled(!isEditable || subjectVM.model.isCalled) + .disabled(!isGradeEditable || subjectVM.model.isCalled) - Image(systemName: "snowflake.circle.fill") - .foregroundColor(subjectVM.model.isCalled ? .primary : .gray) - - Toggle("", isOn: $subjectVM.model.isCalled) - .frame(width: 50) - .disabled(!isEditable) + VStack { + Toggle("", isOn: $subjectVM.model.isCalled) + .frame(width: 40) + Image(systemName: "snowflake.circle.fill") + .foregroundColor(subjectVM.model.isCalled ? .primary : .gray) + } + } else { NoGradesInfo() Button(action: { subjectVM.model.grade = 0.0 + isGradeEditable = true }) { Image(systemName: "pencil.line") } - .disabled(!isEditable) - } } + .padding(.bottom) } } - .padding() - .background(Color.gray.opacity(0.2)) - .cornerRadius(12) - .padding(.horizontal) } } diff --git a/Graduator/Graduator/View/Cells/UnitViewCell.swift b/Graduator/Graduator/View/Cells/UnitViewCell.swift index eae57cc..b6c0f01 100644 --- a/Graduator/Graduator/View/Cells/UnitViewCell.swift +++ b/Graduator/Graduator/View/Cells/UnitViewCell.swift @@ -16,9 +16,13 @@ struct UnitViewCell: View { HStack { Text("UE " + String(unitVM.model.code)) - Text(unitVM.model.name) + .frame(width: 40) + TextField("", text: $unitVM.model.name) + .disabled(!unitsManagerVM.isAllEditable) Spacer() - Text(String(unitVM.model.weight)) + TextField("", value: $unitVM.model.weight, formatter: Formatters.weightFormatter) + .frame(width: 30) + .disabled(!unitsManagerVM.isAllEditable) } HStack { diff --git a/Graduator/Graduator/View/Forms/SubjectFormView.swift b/Graduator/Graduator/View/Forms/SubjectFormView.swift new file mode 100644 index 0000000..91e9ca4 --- /dev/null +++ b/Graduator/Graduator/View/Forms/SubjectFormView.swift @@ -0,0 +1,40 @@ +// +// SubjectForm.swift +// Graduator +// +// Created by etudiant on 2023-05-28. +// + +import SwiftUI + +struct SubjectFormView: View { + @ObservedObject var formVM: SubjectFormVM + + var body: some View { + Form { + HStack { + Text("Nom de la matière:") + Spacer() + TextField("Nom", text: $formVM.name) + } + HStack { + Text("Coefficient:") + Spacer() + TextField("Coefficient", value: $formVM.weight, formatter: NumberFormatter()) + } + HStack { + Text("Note sur 20.0:") + Spacer() + TextField("Note", text: $formVM.gradeString) + } + Toggle("Note définitive?", isOn: $formVM.isCalled) + } + } +} + + +struct SubjectFormView_Previews: PreviewProvider { + static var previews: some View { + SubjectFormView(formVM: SubjectFormVM()) + } +} diff --git a/Graduator/Graduator/View/Utils/Formatters.swift b/Graduator/Graduator/View/Utils/Formatters.swift index 726094e..927127a 100644 --- a/Graduator/Graduator/View/Utils/Formatters.swift +++ b/Graduator/Graduator/View/Utils/Formatters.swift @@ -12,6 +12,15 @@ class Formatters { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.maximumFractionDigits = 2 + formatter.minimum = 0 + formatter.maximum = 20 + return formatter + }() + + static let weightFormatter: NumberFormatter = { + let formatter = NumberFormatter() + formatter.numberStyle = .none + formatter.minimum = 1 return formatter }() } diff --git a/Graduator/Graduator/View/Views/UnitView.swift b/Graduator/Graduator/View/Views/UnitView.swift index 5446a8a..1605b8a 100644 --- a/Graduator/Graduator/View/Views/UnitView.swift +++ b/Graduator/Graduator/View/Views/UnitView.swift @@ -11,6 +11,17 @@ struct UnitView: View { @ObservedObject var unitVM: UnitVM @ObservedObject var unitsManagerVM: UnitsManagerVM + @State private var showAlert = false + @State private var showingForm: Bool = false + var formVM = SubjectFormVM() + + private func delete(at offsets: IndexSet) { + guard let index = offsets.first else { return } + let subjectVMToDelete = unitVM.SubjectsVM[index] + unitVM.deleteSubject(subjectVMToDelete) + unitsManagerVM.updateUnit(unitVM) + } + var body: some View { VStack(alignment: .leading) { HStack { @@ -39,27 +50,84 @@ struct UnitView: View { } .padding(.horizontal) - ScrollView { - ForEach(unitVM.SubjectsVM) { subjectVM in - SubjectViewCell( - subjectVM: subjectVM, - unitVM: unitVM, - unitsManagerVM: unitsManagerVM - ) + + List { + if unitsManagerVM.isAllEditable { + ForEach(unitVM.SubjectsVM) { subjectVM in + SubjectViewCell( + subjectVM: subjectVM, + unitVM: unitVM, + unitsManagerVM: unitsManagerVM + ) + } + .onDelete(perform: delete) + } else { + ForEach(unitVM.SubjectsVM) { subjectVM in + SubjectViewCell( + subjectVM: subjectVM, + unitVM: unitVM, + unitsManagerVM: unitsManagerVM + ) + } } } - .navigationBarItems(trailing: Button(action: { - // TODO Add action for button. Make editable - // * (LATER, in UnitViewCell) unit weight - // * (LATER, in UnitViewCell) unit description - // * subjects - // * make all fields editable (=> just toggle isEditable is the SubjectCellViews?) - // * create new subject (creation screen with simple form for name, weight, code, isCalled. Of course, will need to deal with adding it to the unitVM, updating the unitVM, and updating the unitsmanagerVM with the new unitVM. Check the result to make sure that the model does get updated by the VM in the end) - // * delete a subject (again, this has repercusions for the unit and the unitmanager, be careful) - }) { - Text("Edit") + .navigationBarItems(trailing: HStack { + if unitsManagerVM.isAllEditable { + Button(action: { showingForm = true }) { + Image(systemName: "plus") + } + Button(action: { + unitsManagerVM.isAllEditable.toggle() + unitVM.onEdited(isCancelled: true) + }) { + Text("Annuler") + } + Button(action: { + unitsManagerVM.isAllEditable.toggle() + unitVM.onEdited() + unitVM.updateAllSubjects() + unitsManagerVM.updateUnit(unitVM) + }) { + Text("OK") + } + } else { + Button(action: { + unitsManagerVM.isAllEditable.toggle() + unitVM.onEditing() + }) { + Text("Modifier") + } + } }) + .sheet(isPresented: $showingForm) { + NavigationView { + SubjectFormView(formVM: formVM) + .navigationTitle("Nouvelle matière") + .navigationBarItems( + leading: Button("Annuler") { showingForm = false }, + trailing: Button("Enregistrer") { + if let newSubject = formVM.createSubject() { + unitVM.addSubject(newSubject) + unitsManagerVM.updateUnit(unitVM) + showingForm = false + } else { + showAlert = true + } + }.alert(isPresented: $showAlert) { + Alert(title: Text("Annulé: matière invalide"), + message: Text("C'est un peu plus compliqué que ça..."), + dismissButton: .default(Text("OK"))) + }) + } + } } + // If user navigates back while editing but before clicking 'OK', the changes are cancelled + .onDisappear(perform: { + if unitsManagerVM.isAllEditable { + unitVM.onEdited(isCancelled: true) + unitsManagerVM.isAllEditable = false + } + }) } } diff --git a/Graduator/Graduator/ViewModel/SubjectFormVM.swift b/Graduator/Graduator/ViewModel/SubjectFormVM.swift new file mode 100644 index 0000000..db260a6 --- /dev/null +++ b/Graduator/Graduator/ViewModel/SubjectFormVM.swift @@ -0,0 +1,38 @@ +// +// SubjectFormVM.swift +// Graduator +// +// Created by etudiant on 2023-05-28. +// + +import Foundation + +class SubjectFormVM: ObservableObject { + @Published var name: String = "" + @Published var weight: Int = 1 + @Published var gradeString: String = "" + @Published var isCalled: Bool = false + + var grade: Double? { + if let gradeOverTwenty = Double(gradeString) { + return gradeOverTwenty / 20.0 + } else { + return nil + } + } + + func isValid(_ subject: Subject) -> Bool { + return !(name.isEmpty || weight <= 0 || !subject.gradeIsValid(grade) || (isCalled && grade == nil)) + } + + func createSubject() -> Subject? { + let subject = Subject( + id: UUID(), + name: name, + weight: weight, + grade: grade, + isCalled: isCalled) + guard isValid(subject) else { return nil } + return subject + } +} diff --git a/Graduator/Graduator/ViewModel/SubjectVM.swift b/Graduator/Graduator/ViewModel/SubjectVM.swift index 6587aef..50a8da5 100644 --- a/Graduator/Graduator/ViewModel/SubjectVM.swift +++ b/Graduator/Graduator/ViewModel/SubjectVM.swift @@ -29,16 +29,23 @@ extension Subject { mutating func update(from data: Data) { // papers please guard data.id == self.data.id else { return } - // can't update if this subject is called, unless the update is to 'un-call' the subject - guard !(self.isCalled && data.isCalled) else { return } + // can't update grade if this subject is called, unless the update is to 'un-call' the subject + guard !(self.isCalled && data.isCalled && self.grade != data.grade) else { return } // can't update a subject to become called and have a nil grade at the same time guard !(data.grade == nil && data.isCalled) else { return } - - self.name = data.name - self.weight = data.weight - self.grade = data.grade + + if (!data.name.isEmpty) { + self.name = data.name + } + self.weight = max(abs(data.weight), 1) + if let grade = data.grade { + self.grade = max(abs(grade), 0.0) + } else { + self.grade = nil + } self.isCalled = data.isCalled } + } class SubjectVM : ObservableObject, Identifiable { diff --git a/Graduator/Graduator/ViewModel/UnitVM.swift b/Graduator/Graduator/ViewModel/UnitVM.swift index e6f912f..9762baf 100644 --- a/Graduator/Graduator/ViewModel/UnitVM.swift +++ b/Graduator/Graduator/ViewModel/UnitVM.swift @@ -30,8 +30,10 @@ extension Unit { mutating func update(from data: Data) { guard self.id == data.id else {return} - self.name = data.name - self.weight = data.weight + if (!data.name.isEmpty) { + self.name = data.name + } + self.weight = max(abs(data.weight), 1) self.isProfessional = data.isProfessional self.code = data.code self.subjects = data.subjects.map { @@ -93,6 +95,25 @@ class UnitVM : ObservableObject, Identifiable { model = original.data } + func updateAllSubjects() { + for subjectVM in subjectsVM { + updateSubject(subjectVM) + } + } + + func deleteSubject(_ subjectVM: SubjectVM) { + guard let index = subjectsVM.firstIndex(where: { $0.id == subjectVM.id }) else { return } + subjectsVM.remove(at: index) + original.subjects.remove(at: index) + model = original.data + } + + func addSubject(_ subject: Subject) { + subjectsVM.append(SubjectVM(subject: subject)) + original.subjects.append(subject) + model = original.data + } + var Average: Double? { return original.getAverage() } diff --git a/Graduator/Graduator/ViewModel/UnitsManagerVM.swift b/Graduator/Graduator/ViewModel/UnitsManagerVM.swift index 988621e..fdfd6bc 100644 --- a/Graduator/Graduator/ViewModel/UnitsManagerVM.swift +++ b/Graduator/Graduator/ViewModel/UnitsManagerVM.swift @@ -46,6 +46,7 @@ class UnitsManagerVM : ObservableObject { private var original: UnitsManager @Published var model: UnitsManager.Data @Published var isEdited: Bool = false + @Published var isAllEditable: Bool = false private var unitsVM: [UnitVM] @@ -76,6 +77,3 @@ class UnitsManagerVM : ObservableObject { return original.getProfessionalAverage() } } - - - diff --git a/README.md b/README.md index 870d500..1b76c71 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,223 @@ -# Graduator salutes you +# Graduator +Graduator is an iOS application developed with SwiftUI that helps users manage their academic `units`, `subjects`, and grades. Users can add and delete `subjects`, edit the weight and name of `subjects` and `units`, and input grades. The app displays weighted averages and explains the conditions for graduating from the Clermont Auvergne Tech Institute's mobile development BSc in 2023. + +## Features + +Beyond those basic features, some details need to be specified here. + +### isCalled Feature + +The isCalled property, available for each subject, denotes whether the grade of a specific subject has been called or not -- i.e., whether it is a definitive grade or a temporary one. + +From the user's point of view, they can see an image of a snowflake that turns from gray to primary color when a subject grade has been called. A Toggle control allows them to set or unset the isCalled status. If they attempt to change the grade of a subject that has been called, they will not be able to -- the corresponding slider and textfield will remain disabled. + +In the model and view model, the `isCalled` property of `subjects` affects the state of their `unit`. If all of a `unit`'s `subject` grades are called, the `unit` is considered called too. Note that if a `subject`'s grade is not set (nil), the `subject` can't be called. + +### Deleting a Subject + +In the app, users can delete a subject by swiping it off the list, right-to-left. + +Note that when a subject is deleted, it is permanently removed from the system. If a user is in the process of editing and deletes a subject, the deletion occurs immediately upon swiping, not when they save (click 'OK'). If the user chooses to cancel their edits (click 'Annuler'), all other unsaved changes will be discarded, but the deletion of the subject remains. + +### Changing a grade + +Before a user changes a grade, they first need to activate the `lock.open` toggle. If the grade is *called*, they also need to use the `isCalled` toggle. + +After a grade was changed, in order to save the change and to see it reflected iun the weighted average, users need to use the (previously `lock.open`, now) `checkmark` toggle. + +## Architecture + +Graduator is based on the MVVM (Model-View-ViewModel) architectural pattern. The below UML class diagram details the structure of the models, viewmodels, and views for `UnitsManager`, `Unit`, and `Subject`. + +```mermaid +classDiagram + class Unit { + +name: String + +weight: Int + +isProfessional: Bool + +code: Int + +subjects: [Subject] + +getAverage(): Double? + +data: Data + +update(from: Data): Void + } + + class UnitVM { + -original: Unit + +model: Unit.Data + +isEdited: Bool + +SubjectsVM: [SubjectVM] + +onEditing(): Void + +onEdited(isCancelled: Bool): Void + +updateSubject(subjectVM: SubjectVM): Void + +updateAllSubjects(): Void + +deleteSubject(subjectVM: SubjectVM): Void + +addSubject(subject: Subject): Void + +Average: Double? + +IsCalled: Bool + } + + class UnitView { + -unitVM: UnitVM + -unitsManagerVM: UnitsManagerVM + +delete(at: IndexSet): Void + } + + class Subject { + +name: String + +weight: Int + +grade: Double? + +isCalled: Bool + +gradeIsValid(grade: Double?): Bool + +data: Data + +update(from: Data): Void + } + + class SubjectVM { + -original: Subject + +model: Subject.Data + +isEdited: Bool + +onEditing(): Void + +onEdited(isCancelled: Bool): Void + } + + class SubjectViewCell { + -subjectVM: SubjectVM + -unitVM: UnitVM + -unitsManagerVM: UnitsManagerVM + -isGradeEditable: Bool + } + + class UnitsManager { + +units: [Unit] + +getTotalAverage(): Double? + +getProfessionalAverage(): Double? + +getAverage(units: [Unit]): Double? + +data: Data + +update(from: Data): Void + } + + class UnitsManagerVM { + -original: UnitsManager + +model: UnitsManager.Data + +isEdited: Bool + +isAllEditable: Bool + +UnitsVM: [UnitVM] + +updateUnit(unitVM: UnitVM): Void + +TotalAverage: Double? + +ProfessionalAverage: Double? + } + + class MainView { + -unitsManagerVM: UnitsManagerVM + } + + UnitVM -- Unit : Uses + UnitView -- UnitVM : Observes + SubjectVM -- Subject : Uses + SubjectViewCell -- SubjectVM : Observes + UnitVM "1" o-- "*" SubjectVM : Contains + UnitsManagerVM -- UnitsManager : Uses + UnitsManagerVM "1" o-- "*" UnitVM : Contains + MainView -- UnitsManagerVM : Observes +``` + +It might be useful to note that, just like `UnitVM`s aggregate `SubjectVM`s, `Unit`s aggregate `Subject`s, but these relationship between `Model` entities were removed from the diagram above for clarity. + +Here is the diagram with those relationships depicted. + +```mermaid +classDiagram + class Unit { + +name: String + +weight: Int + +isProfessional: Bool + +code: Int + +subjects: [Subject] + +getAverage(): Double? + +data: Data + +update(from: Data): Void + } + + class UnitVM { + -original: Unit + +model: Unit.Data + +isEdited: Bool + +SubjectsVM: [SubjectVM] + +onEditing(): Void + +onEdited(isCancelled: Bool): Void + +updateSubject(subjectVM: SubjectVM): Void + +updateAllSubjects(): Void + +deleteSubject(subjectVM: SubjectVM): Void + +addSubject(subject: Subject): Void + +Average: Double? + +IsCalled: Bool + } + + class UnitView { + -unitVM: UnitVM + -unitsManagerVM: UnitsManagerVM + +delete(at: IndexSet): Void + } + + class Subject { + +name: String + +weight: Int + +grade: Double? + +isCalled: Bool + +gradeIsValid(grade: Double?): Bool + +data: Data + +update(from: Data): Void + } + + class SubjectVM { + -original: Subject + +model: Subject.Data + +isEdited: Bool + +onEditing(): Void + +onEdited(isCancelled: Bool): Void + } + + class SubjectViewCell { + -subjectVM: SubjectVM + -unitVM: UnitVM + -unitsManagerVM: UnitsManagerVM + -isGradeEditable: Bool + } + + class UnitsManager { + +units: [Unit] + +getTotalAverage(): Double? + +getProfessionalAverage(): Double? + +getAverage(units: [Unit]): Double? + +data: Data + +update(from: Data): Void + } + + class UnitsManagerVM { + -original: UnitsManager + +model: UnitsManager.Data + +isEdited: Bool + +isAllEditable: Bool + +UnitsVM: [UnitVM] + +updateUnit(unitVM: UnitVM): Void + +TotalAverage: Double? + +ProfessionalAverage: Double? + } + + class MainView { + -unitsManagerVM: UnitsManagerVM + } + + UnitVM -- Unit : Uses + UnitView -- UnitVM : Observes + SubjectVM -- Subject : Uses + SubjectViewCell -- SubjectVM : Observes + UnitVM "1" o-- "*" SubjectVM : Contains + Unit "1" o-- "*" Subject : Contains + UnitsManagerVM -- UnitsManager : Uses + UnitsManagerVM "1" o-- "*" UnitVM : Contains + UnitsManager "1" o-- "*" Unit : Contains + MainView -- UnitsManagerVM : Observes +```