From 2e6b8bb3772fe6479451e2e4cb51949cbc0989c4 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sun, 4 Apr 2021 01:46:58 -0700 Subject: [PATCH] Loads the imported monster from file and sets the new monster's name from the loaded data. --- iOS/MonsterCards/Views/ContentView.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/iOS/MonsterCards/Views/ContentView.swift b/iOS/MonsterCards/Views/ContentView.swift index e1a6d99..335e61e 100644 --- a/iOS/MonsterCards/Views/ContentView.swift +++ b/iOS/MonsterCards/Views/ContentView.swift @@ -48,9 +48,24 @@ struct ContentView: View { } func beginImportingMonster(url: URL) { - self.importInfo.monster.name = url.absoluteString - self.isShowingImportDialog = true + + // TOOD: only do this if the file name ends in .json or .monster + + let decoder = JSONDecoder() + 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. + self.isShowingImportDialog = true + } catch let error as NSError { + // TODO: handle this better + print(error) + } } + } struct ContentView_Previews: PreviewProvider {