🚧 Implement MVVM for Units

pull/1/head
Alexis Drai 2 years ago
parent 0e4beec348
commit 3c3e30eb7d

@ -18,6 +18,7 @@
EC242B832A1FAA9B006FE760 /* Stub.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC242B822A1FAA9B006FE760 /* Stub.swift */; };
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 */; };
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 */; };
@ -37,6 +38,7 @@
EC242B822A1FAA9B006FE760 /* Stub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stub.swift; sourceTree = "<group>"; };
EC242B872A1FC605006FE760 /* NoGradesInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoGradesInfo.swift; sourceTree = "<group>"; };
EC242B892A1FCECA006FE760 /* AverageBlockView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AverageBlockView.swift; sourceTree = "<group>"; };
EC5FE5A42A20882F0028AA5F /* Formatters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Formatters.swift; sourceTree = "<group>"; };
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 = "<group>"; };
ECC581D52A1D085C006C55EF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -59,7 +61,8 @@
EC242B6F2A1F8260006FE760 /* View */ = {
isa = PBXGroup;
children = (
EC242B862A1FC5EC006FE760 /* Bits */,
EC5FE5A32A20881B0028AA5F /* Utils */,
EC242B862A1FC5EC006FE760 /* Components */,
EC242B772A1F834C006FE760 /* Cells */,
EC242B762A1F8345006FE760 /* Views */,
);
@ -83,13 +86,21 @@
path = Cells;
sourceTree = "<group>";
};
EC242B862A1FC5EC006FE760 /* Bits */ = {
EC242B862A1FC5EC006FE760 /* Components */ = {
isa = PBXGroup;
children = (
EC242B872A1FC605006FE760 /* NoGradesInfo.swift */,
EC242B892A1FCECA006FE760 /* AverageBlockView.swift */,
);
path = Bits;
path = Components;
sourceTree = "<group>";
};
EC5FE5A32A20881B0028AA5F /* Utils */ = {
isa = PBXGroup;
children = (
EC5FE5A42A20882F0028AA5F /* Formatters.swift */,
);
path = Utils;
sourceTree = "<group>";
};
ECC581C52A1D085B006C55EF = {
@ -224,6 +235,7 @@
EC242B712A1F8283006FE760 /* MainView.swift in Sources */,
EC242B6E2A1F81CC006FE760 /* UnitsManager.swift in Sources */,
ECC581E52A1D0C44006C55EF /* UnitVM.swift in Sources */,
EC5FE5A52A20882F0028AA5F /* Formatters.swift in Sources */,
EC242B7F2A1F83BF006FE760 /* UnitsManagerVM.swift in Sources */,
EC242B7B2A1F838C006FE760 /* UnitViewCell.swift in Sources */,
EC242B6C2A1F81AE006FE760 /* Subject.swift in Sources */,

@ -44,7 +44,7 @@ struct MainView: View {
.padding()
VStack(alignment: .leading) {
ForEach(unitsManagerVM.UnitsVM, id: \.model.id) { unitVM in
ForEach(unitsManagerVM.UnitsVM) { unitVM in
NavigationLink(
destination: UnitView(unitVM: unitVM)) {
UnitViewCell(unitVM: unitVM)

@ -13,4 +13,8 @@ struct Subject : Identifiable {
var weight: Int
var grade: Double?
var isCalled: Bool
func gradeIsValid(_ grade: Double?) -> Bool {
return grade == nil || (grade! >= 0 && grade! <= 1)
}
}

@ -14,4 +14,20 @@ struct Unit : Identifiable {
var isProfessional: Bool
var code: Int
var subjects: [Subject]
func getAverage() -> Double? {
var totalWeight = 0
var weightedSum = 0.0
for subject in subjects {
if let grade = subject.grade {
totalWeight += subject.weight
weightedSum += grade * Double(subject.weight)
}
}
guard totalWeight > 0 else { return nil }
return weightedSum / Double(totalWeight)
}
}

@ -15,39 +15,27 @@ struct UnitsManager {
self.units = units
}
func getUnits() -> [Unit] {
return units
func getTotalAverage() -> Double? {
return getAverage(units: units)
}
func getUnit(id: UUID) -> Unit? {
if let index = getIndex(id: id) {
return units[index]
} else {
return nil
}
}
mutating func addUnit(unit: Unit) -> Unit {
units.append(unit)
return unit
}
mutating func updateUnit(id: UUID, unit: Unit) -> Unit? {
if let index = getIndex(id: id) {
units[index] = unit
return unit
} else {
return nil
}
func getProfessionalAverage() -> Double? {
return getAverage(units: units.filter { $0.isProfessional })
}
private func getAverage(units: [Unit]) -> Double? {
var totalWeight = 0
var weightedSum = 0.0
mutating func removeUnit(id: UUID) {
if let index = getIndex(id: id) {
units.remove(at: index)
for unit in units {
if let grade = unit.getAverage() {
totalWeight += unit.weight
weightedSum += grade * Double(unit.weight)
}
}
}
private func getIndex(id: UUID) -> Int? {
return units.firstIndex(where: { $0.id == id })
guard totalWeight > 0 else { return nil }
return weightedSum / Double(totalWeight)
}
}

@ -11,21 +11,20 @@ struct SubjectViewCell: View {
@ObservedObject var subjectVM: SubjectVM
//TODO also allow using the unitview's navigation bar item "Edit" (makes all subjects editable, and more)
@State private var isEditable = false
private let gradeFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
return formatter
}()
@State private var isEditable = false
var body: some View {
HStack {
if isEditable {
VStack {
Image(systemName: "checkmark.square")
Button(action: {
isEditable = false
subjectVM.onEdited()
}) {
Image(systemName: "checkmark.square")
.foregroundColor(.green)
}
Button(action: {
isEditable = false
subjectVM.onEdited(isCancelled: true)
@ -75,9 +74,9 @@ struct SubjectViewCell: View {
subjectVM.model.grade = newValue / 20.0
}
}
), formatter: gradeFormatter)
), formatter: Formatters.gradeFormatter)
.frame(width: 50)
.disabled(!isEditable)
.disabled(!isEditable || subjectVM.model.isCalled)
Image(systemName: "snowflake.circle.fill")
.foregroundColor(subjectVM.model.isCalled ? .primary : .gray)

@ -20,17 +20,23 @@ struct UnitViewCell: View {
Text(String(unitVM.model.weight))
}
if let average = unitVM.Average {
HStack {
// TODO add slider linked to "grade" value
// TODO link slider color to the average. If below 10.0, red, else green.
Text("Sliiiiiiiiiiiiider")
.background(Color.red)
Text(String(format: "%.2f", average * 20))
HStack {
if let average = unitVM.Average {
ProgressView(value: average, total: 1.0)
.accentColor(average < 0.5 ? .red : .green)
.scaleEffect(x: 1, y: 4, anchor: .center)
Text(String(format: "%.2f", average * 20.0))
Spacer()
Image(systemName: "snowflake.circle.fill")
.foregroundColor(unitVM.IsCalled ? .primary : .gray)
} else {
NoGradesInfo()
}
} else {
NoGradesInfo()
}
}

@ -15,7 +15,7 @@ struct AverageBlockView: View {
HStack {
Text(title)
Spacer()
Text(String(format: "%.2f", average * 20))
Text(String(format: "%.2f", average * 20.0))
Image(systemName: average >= 0.5 ? "graduationcap.fill" : "exclamationmark.bubble.fill")
}
}

@ -11,7 +11,6 @@ struct NoGradesInfo: View {
var body: some View {
HStack {
Text("Aucune note enregistrée")
.font(.caption)
Spacer()
}
}

@ -0,0 +1,17 @@
//
// Formatters.swift
// Graduator
//
// Created by etudiant on 2023-05-26.
//
import Foundation
class Formatters {
static let gradeFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
return formatter
}()
}

@ -21,30 +21,28 @@ struct UnitView: View {
Divider()
HStack {
// TODO later add cross image (multiply)
Text("coefficient : " + "weight")
Image(systemName: "multiply.circle.fill")
Text(String(format: "coefficient : %d", unitVM.model.weight))
}
.padding(.horizontal)
HStack {
// TODO later add page with scribbling image
Image(systemName: "magnifyingglass.circle")
Text("Détail des notes")
}
.padding(.horizontal)
ScrollView {
ForEach(unitVM.model.subjects) { subjectData in
// You need to convert subjectData into SubjectVM, then use it to create SubjectViewCell
let subjectVM = SubjectVM(subjectData: subjectData)
ForEach(unitVM.subjectVMs) { subjectVM in
SubjectViewCell(subjectVM: subjectVM)
}
}
.navigationBarItems(trailing: Button(action: {
// TODO later: Add action for button. Make editable
// * unit weight
// * unit description
// TODO Add action for button. Make editable
// * (LATER) unit weight
// * (LATER) unit description
// * subjects
// * make all fields editable (just toggle isEditable is the SubjectCellView?)
// * 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)
}) {
@ -56,6 +54,6 @@ struct UnitView: View {
struct UnitView_Previews: PreviewProvider {
static var previews: some View {
UnitView(unitVM: UnitVM(unit: Stub.units[5]))
UnitView(unitVM: UnitVM(unit: Stub.units[0]))
}
}

@ -35,11 +35,12 @@ extension Subject {
}
mutating func update(from data: Data) {
// FIXME improve the guard
guard (data.id == self.data.id
&& (self.isCalled == false || data.isCalled == false)
&& (data.grade != nil || data.isCalled == false))
else { return }
// 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 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
@ -48,8 +49,10 @@ extension Subject {
}
}
class SubjectVM : ObservableObject {
var original: Subject
class SubjectVM : ObservableObject, Identifiable {
private var original: Subject
var id: UUID { original.id }
weak var unitVM: UnitVM?
@Published var model: Subject.Data
@Published var isEdited: Bool = false
@ -68,7 +71,6 @@ class SubjectVM : ObservableObject {
id: UUID(),
name: "",
weight: 1,
grade: 10.0,
isCalled: false
))
}
@ -78,15 +80,16 @@ class SubjectVM : ObservableObject {
isEdited = true
}
// TODO add suitable error handling for cases where the user enters an invalid number. (negative numbers are forbidden... Do we need to guard against NaN and nil as well?)
func onEdited(isCancelled: Bool = false) {
if(!isCancelled && isEdited){
original.update(from: model)
if(!isCancelled && original.gradeIsValid(model.grade)){
if (isEdited) {
original.update(from: model)
unitVM?.updateSubjects()
}
}
else {
model = original.data
}
isEdited = false
}
var HasGrade: Bool {
return model.grade != nil
}
}

@ -28,8 +28,6 @@ extension Unit {
)
}
// TODO ? Maybe check that we're not setting isCalled to true on a subject with a nil grade. Keep in mind that's what we already did in SubjectVM's update function
mutating func update(from data: Data) {
guard self.id == data.id else {return}
self.name = data.name
@ -48,15 +46,20 @@ extension Unit {
}
}
class UnitVM : ObservableObject {
var original: Unit
class UnitVM : ObservableObject, Identifiable {
private var original: Unit
var id: UUID { original.id }
@Published var model: Unit.Data
@Published var isEdited: Bool = false
@Published var subjectVMs: [SubjectVM]
init(unit: Unit) {
original = unit
model = original.data
subjectVMs = unit.subjects.map { SubjectVM(subject: $0) }
for subjectVM in subjectVMs {
subjectVM.unitVM = self
}
}
convenience init() {
@ -76,32 +79,29 @@ class UnitVM : ObservableObject {
}
func onEdited(isCancelled: Bool = false) {
if(!isCancelled && isEdited){
original.update(from: model)
if(!isCancelled){
if (isEdited) {
original.update(from: model)
// TODO unitsManagerVM?.updateUnits()
}
}
else {
model = original.data
}
isEdited = false
}
// TODO Maybe move this to the model?
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()
model = original.data
}
var Average: Double? {
var totalWeight = 0
var weightedSum = 0.0
for subject in model.subjects {
if let grade = subject.grade {
totalWeight += subject.weight
weightedSum += grade * Double(subject.weight)
}
}
guard totalWeight > 0 else { return nil }
return weightedSum / Double(totalWeight)
return original.getAverage()
}
var isCalled: Bool {
// FIXME this is false if any suject therein has a nil grade. This is true if all subjects therein are locked
// also check if this can stay a function, or if it would be better as a calculated property
return false
var IsCalled: Bool {
return model.subjects.allSatisfy { $0.isCalled }
}
}

@ -14,7 +14,7 @@ extension UnitsManager {
var data: Data {
Data(
units: self.getUnits().map{ $0.data }
units: self.units.map{ $0.data }
)
}
@ -43,7 +43,7 @@ extension UnitsManager {
class UnitsManagerVM : ObservableObject {
var original: UnitsManager
private var original: UnitsManager
@Published var model: UnitsManager.Data
@Published var isEdited: Bool = false
@ -52,17 +52,17 @@ class UnitsManagerVM : ObservableObject {
public var UnitsVM: [UnitVM] {
unitsVM
}
init(unitsManager: UnitsManager) {
original = unitsManager
model = original.data
unitsVM = unitsManager.getUnits().map {
unitsVM = unitsManager.units.map {
UnitVM(unit: $0)
}
}
convenience init() {
self.init(unitsManager: UnitsManager(units: Stub.units))
self.init(unitsManager: UnitsManager(units: []))
}
func onEditing() {
@ -78,40 +78,12 @@ class UnitsManagerVM : ObservableObject {
}
var TotalAverage: Double? {
return getAverage(unitsVM: self.unitsVM)
return original.getTotalAverage()
}
var ProfessionalAverage: Double? {
return getAverage(unitsVM: self.unitsVM.filter { $0.model.isProfessional })
}
private func getAverage(unitsVM: [UnitVM]) -> Double? {
var totalWeight = 0
var weightedSum = 0.0
for unitVM in unitsVM {
if let grade = unitVM.Average {
totalWeight += unitVM.model.weight
weightedSum += grade * Double(unitVM.model.weight)
}
}
guard totalWeight > 0 else { return nil }
return weightedSum / Double(totalWeight)
}
// FIXME fix this nightmare after resting and doing Subject first
/*
func updateUnit(id: UUID, unitVM: UnitVM) -> UnitVM? {
if let index = unitsVM.firstIndex(where: { $0.model.id == id }) {
original.updateUnit(id: id, unit: Unit(unitsVM[index].original))
return unitVM
} else {
return nil
}
return original.getProfessionalAverage()
}
*/
}

Loading…
Cancel
Save