Oops, sorry! Busy couple days.
//
// CharacterEditView.swift
// CPR Health Tracker
//
// View for creating or editing a character
// Originally based off npcCreationView.swift
import SwiftUI
struct CharacterEditView: View {
@Binding var character: Entity
// number display width constant
static let widthConstant: Double = 25
var body: some View {
Form {
Section(header: Text("Character Edit")) {
VStack {
HStack {
Text("Name:")
TextField("Character Name", text: $character.name)
}
// initiative hstack
HStack {
Image(systemName: "figure.run")
.foregroundColor(.orange)
Text("Initiative:")
TextField("#", text: $character.initiativeInString)
}
HStack {
Image(systemName: "shield.fill")
.foregroundColor(.green)
VStack {
HStack {
Text("Head SP:")
TextField("Head SP", text: $character.headArmorInString)
}
HStack {
Text("Body SP:")
TextField("Body SP", text: $character.bodyArmorInString)
}
}
}
// health hstack
// HStack {
// Slider(value: $character.healthInDouble, in: 0...50, step: 5) {
// Text("Health")
// }
// Spacer()
// Image(systemName: "heart.fill")
// .foregroundColor(.red)
// TextField("#", text: $character.healthInString)
// .frame(width: CharacterEditView.widthConstant)
// Text("HP")
// }
HStack {
Image(systemName: "heart.fill")
.foregroundColor(.red)
Text("HP:")
TextField("HP", text: $character.healthInString)
}
}
}
}
}
}
struct CharacterEditView_Preview: PreviewProvider {
static var previews: some View {
CharacterEditView(character: .constant(Entity.sampleData[0]))
}
}
//
// ListView.swift
// CPR Health Tracker
//
// Displays the list of entity cards
import SwiftUI
struct ListView: View {
@State var cardList: [Entity]
@State private var isPresentingNewCharacterView = false
var body: some View {
NavigationStack {
List($cardList) { $i in
NavigationLink(destination: DetailView(character: $i, editingChar: Entity.emptyEntity, cardList: $cardList)) {
CardView(character: i)
}
}
.navigationTitle("Characters")
.toolbar {
Button(action: {
isPresentingNewCharacterView = true
} ) {
Image(systemName: "plus")
}
}
}
.sheet(isPresented: $isPresentingNewCharacterView) {
NewCharacterSheet(cardList: $cardList, isPresentingNewCharacterView: $isPresentingNewCharacterView)
}
// ERROR BELOW
.onChange(of: cardList) { newList in
cardList = newList.sorted(by: >)
}
.onAppear {
cardList.sort(by: >)
}
// ERROR ABOVE
}
}
struct ListView_Preview: PreviewProvider {
static var previews: some View {
ListView(cardList: Entity.sampleData)
}
}
//
// Entity.swift
// CPR Health Tracker
//
// Represents either an Entity or PC
// Originally based off the same code as npc.swift
import Foundation
struct Entity: Identifiable {
let id: UUID
var name: String
var initiative: Int
var initiativeInString: String {
get {
String(initiative)
}
set {
initiative = Int(newValue) ?? 0
}
}
var bodyArmor: Int
var bodyArmorInDouble: Double {
get {
Double(bodyArmor)
}
set {
bodyArmor = Int(newValue)
}
}
var bodyArmorInString: String {
get {
String(bodyArmor)
}
set {
bodyArmor = Int(newValue) ?? 0
}
}
var headArmor: Int
var headArmorInDouble: Double {
get {
Double(headArmor)
}
set {
headArmor = Int(newValue)
}
}
var headArmorInString: String {
get {
String(headArmor)
}
set {
headArmor = Int(newValue) ?? 0
}
}
var health: Int
var healthInDouble: Double {
get {
Double(health)
}
set {
health = Int(newValue)
}
}
var healthInString: String {
get {
String(health)
}
set {
health = Int(newValue) ?? 0
}
}
init(id: UUID = UUID(), name: String, initiative: Int, bodyArmor: Int, headArmor: Int, health: Int) {
self.id = id
self.name = name
self.initiative = initiative
self.bodyArmor = bodyArmor
self.headArmor = headArmor
self.health = health
}
}
// empty Entity
extension Entity {
static var emptyEntity: Entity {
Entity(name: "", initiative: 0, bodyArmor: 0, headArmor: 0, health: 0)
}
}
// sample entities
extension Entity {
static var sampleData: [Entity] {
[
Entity(name: "Easy Goon", initiative: 4, bodyArmor: 4, headArmor: 0, health: 15),
Entity(name: "Average Goon", initiative: 6, bodyArmor: 7, headArmor: 4, health: 25),
Entity(name: "Elite Goon", initiative: 8, bodyArmor: 11, headArmor: 11, health: 35)
]
}
}
// comparable extension
// ERROR BELOW
extension Entity:Comparable {
static func == (lhs: Entity, rhs: Entity) -> Bool {
return lhs.initiative == rhs.initiative
}
static func < (lhs: Entity, rhs: Entity) -> Bool {
return lhs.initiative < rhs.initiative
}
}
// ERROR ABOVE
It seems the comparable extension causes the weird textfield problems. The stuff I added are labeled with ERROR BELOW.