Adds condition immunities to the monster editor.

This commit is contained in:
2021-03-22 18:00:05 -07:00
parent e23b35f75e
commit c8f18a00dd
5 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
//
// DamageTypesViewModel.swift
// MonsterCards
//
// Created by Tom Hicks on 3/22/21.
//
import Foundation
class DamageTypeViewModel: ObservableObject, Comparable, Identifiable {
static func < (lhs: DamageTypeViewModel, rhs: DamageTypeViewModel) -> Bool {
lhs.name < rhs.name
}
static func == (lhs: DamageTypeViewModel, rhs: DamageTypeViewModel) -> Bool {
lhs.name == rhs.name
}
@Published var name: String
init(_ name: String) {
self.name = name
}
}

View File

@@ -51,6 +51,7 @@ class MonsterViewModel: ObservableObject {
@Published var charismaSavingThrowProficiency: ProficiencyType
@Published var charismaSavingThrowAdvantage: AdvantageType
@Published var skills: [SkillViewModel]
@Published var conditionImmunities: [DamageTypeViewModel]
init(_ rawMonster: Monster?) {
self.name = ""
@@ -92,7 +93,8 @@ class MonsterViewModel: ObservableObject {
self.charismaSavingThrowAdvantage = .none
self.charismaSavingThrowProficiency = .none
self.skills = []
self.conditionImmunities = []
if (rawMonster != nil) {
self.rawMonster = rawMonster
self.copyFromMonster(monster: rawMonster!)
@@ -139,6 +141,10 @@ class MonsterViewModel: ObservableObject {
self.charismaSavingThrowAdvantage = monster.charismaSavingThrowAdvantageEnum
self.charismaSavingThrowProficiency = monster.charismaSavingThrowProficiencyEnum
self.skills = (monster.skills?.allObjects.map {SkillViewModel(($0 as! Skill))})!.sorted()
self.conditionImmunities = monster.conditionImmunitiesArray
.map {DamageTypeViewModel($0)}
.sorted()
}
func copyToMonster(monster: Monster) {

View File

@@ -0,0 +1,54 @@
//
// EditDamageTypes.swift
// MonsterCards
//
// Created by Tom Hicks on 3/22/21.
//
import SwiftUI
struct EditDamageTypes: View {
@ObservedObject var viewModel: MonsterViewModel
var path: ReferenceWritableKeyPath<MonsterViewModel, [DamageTypeViewModel]>
var body: some View {
List {
ForEach(viewModel[keyPath: path]) { damageType in
TextField(
"",
text: Binding<String>(
get: {damageType.name},
set: {damageType.name = $0}
)
)
}
.onDelete(perform: { indexSet in
for index in indexSet {
viewModel[keyPath: path].remove(at: index)
}
})
}
.toolbar(content: {
Button(
action: {
let newDamageType = DamageTypeViewModel("New Type")
viewModel[keyPath: path].append(newDamageType)
viewModel[keyPath: path] = viewModel[keyPath: path].sorted()
},
label: {
Image(systemName: "plus")
}
)
})
.onAppear(perform: {
viewModel[keyPath: path] = viewModel[keyPath: path].sorted()
})
}
}
struct EditDamageTypes_Previews: PreviewProvider {
static var previews: some View {
let viewModel = MonsterViewModel(nil)
EditDamageTypes(viewModel: viewModel, path: \.damageImmunities)
}
}

View File

@@ -43,6 +43,10 @@ struct EditMonster: View {
NavigationLink(
"Skills",
destination: EditSkills(monsterViewModel: monsterViewModel))
NavigationLink(
"Condition Immunities",
destination: EditDamageTypes(viewModel: monsterViewModel, path: \.conditionImmunities))
}
.onAppear(perform: copyMonsterToLocal)
.toolbar(content: {