Sorts skills specifically for the EditSkills view, but also more generally when creating a MonsterViewModel.

This commit is contained in:
2021-03-21 18:28:04 -07:00
parent 7073e3d952
commit 0299213dfa
3 changed files with 9 additions and 2 deletions

View File

@@ -138,7 +138,7 @@ class MonsterViewModel: ObservableObject {
self.charismaScore = monster.charismaScore
self.charismaSavingThrowAdvantage = monster.charismaSavingThrowAdvantageEnum
self.charismaSavingThrowProficiency = monster.charismaSavingThrowProficiencyEnum
self.skills = (monster.skills?.allObjects.map {SkillViewModel(($0 as! Skill))})!
self.skills = (monster.skills?.allObjects.map {SkillViewModel(($0 as! Skill))})!.sorted()
}
func copyToMonster(monster: Monster) {

View File

@@ -8,7 +8,11 @@
import Foundation
import CoreData
class SkillViewModel: ObservableObject, Hashable, Identifiable {
class SkillViewModel: ObservableObject, Comparable, Hashable, Identifiable {
static func < (lhs: SkillViewModel, rhs: SkillViewModel) -> Bool {
return lhs.name < rhs.name
}
static func == (lhs: SkillViewModel, rhs: SkillViewModel) -> Bool {
return lhs.abilityScore == rhs.abilityScore
&& lhs.advantage == rhs.advantage

View File

@@ -33,6 +33,9 @@ struct EditSkills: View {
}
)
})
.onAppear(perform: {
monsterViewModel.skills = monsterViewModel.skills.sorted()
})
}
}