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 bad8a374af
commit eb7199d5a9
3 changed files with 9 additions and 2 deletions

View File

@@ -138,7 +138,7 @@ class MonsterViewModel: ObservableObject {
self.charismaScore = monster.charismaScore self.charismaScore = monster.charismaScore
self.charismaSavingThrowAdvantage = monster.charismaSavingThrowAdvantageEnum self.charismaSavingThrowAdvantage = monster.charismaSavingThrowAdvantageEnum
self.charismaSavingThrowProficiency = monster.charismaSavingThrowProficiencyEnum 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) { func copyToMonster(monster: Monster) {

View File

@@ -8,7 +8,11 @@
import Foundation import Foundation
import CoreData 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 { static func == (lhs: SkillViewModel, rhs: SkillViewModel) -> Bool {
return lhs.abilityScore == rhs.abilityScore return lhs.abilityScore == rhs.abilityScore
&& lhs.advantage == rhs.advantage && lhs.advantage == rhs.advantage

View File

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