Adds importing a character from a share. You can now save the monster as previewed.
This commit is contained in:
@@ -42,8 +42,8 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
.onOpenURL(perform: beginImportingMonster)
|
||||
.sheet(isPresented: self.$isShowingImportDialog) {
|
||||
ImportMonster(monster: $importInfo.monster)
|
||||
.sheet(isPresented: $isShowingImportDialog) {
|
||||
ImportMonster(monster: $importInfo.monster, isOpen: $isShowingImportDialog)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ struct ContentView: View {
|
||||
do {
|
||||
let data = try Data(contentsOf: url)
|
||||
let monsterDTO = try decoder.decode(MonsterDTO.self, from: data)
|
||||
print(String(format: "Loaded monster: %@", monsterDTO.name))
|
||||
// TODO: check for some minimal set of properties to ensure this is the expected json schema
|
||||
self.importInfo.monster = MonsterImportHelper.import5ESBMonster(monsterDTO)
|
||||
// TODO: throw or set an err here and don't set isShowingImportDialog to true if the file didn't match any of our supported monster schemas.
|
||||
|
||||
@@ -9,16 +9,72 @@ import SwiftUI
|
||||
|
||||
struct ImportMonster: View {
|
||||
@Binding var monster: MonsterViewModel
|
||||
@Environment(\.managedObjectContext) private var viewContext
|
||||
@Binding var isOpen: Bool
|
||||
|
||||
var body: some View {
|
||||
MonsterDetailView(viewModel: monster)
|
||||
VStack{
|
||||
HStack {
|
||||
Button("Cancel", action: cancelImport)
|
||||
Spacer()
|
||||
Button("Add to Library", action: saveNewMonster)
|
||||
}
|
||||
MonsterDetailView(viewModel: monster)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
func cancelImport() {
|
||||
isOpen = false
|
||||
}
|
||||
|
||||
func saveNewMonster() {
|
||||
print("Saving monster: \(monster.name)")
|
||||
withAnimation {
|
||||
let newMonster = Monster(context: viewContext)
|
||||
monster.copyToMonster(monster: newMonster)
|
||||
|
||||
do {
|
||||
try viewContext.save()
|
||||
isOpen = false
|
||||
} catch {
|
||||
// Replace this implementation with code to handle the error appropriately.
|
||||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||
let nsError = error as NSError
|
||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ImportMonster_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ImportMonster(
|
||||
monster: .constant(MonsterViewModel())
|
||||
)
|
||||
let monster = MonsterViewModel()
|
||||
monster.name = "Steve"
|
||||
monster.size = "Medium"
|
||||
monster.type = "dwarf"
|
||||
monster.alignment = "chaotic good"
|
||||
monster.armorType = .none
|
||||
monster.hasShield = true
|
||||
monster.hitDice = 4
|
||||
monster.strengthScore = 20
|
||||
monster.dexterityScore = 14
|
||||
monster.constitutionScore = 18
|
||||
monster.intelligenceScore = 8
|
||||
monster.wisdomScore = 8
|
||||
monster.charismaScore = 15
|
||||
monster.walkSpeed = 40
|
||||
monster.challengeRating = .four
|
||||
monster.languages = [LanguageViewModel("Common", true), LanguageViewModel("Giant", true)]
|
||||
monster.actions = [AbilityViewModel("Greataxe, +3", "__Badass Attack:___ Hits the other dude on a _3_ or above and does a ton of damage")]
|
||||
|
||||
return
|
||||
VStack{
|
||||
Text("Hello, World!")
|
||||
}
|
||||
.sheet(isPresented: .constant(true)) {
|
||||
ImportMonster(monster: .constant(monster), isOpen: .constant(true))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user