diff --git a/JSONCreator_iOS/JSONCreator/MasterViewController.swift b/JSONCreator_iOS/JSONCreator/MasterViewController.swift index 4732728..d517e05 100644 --- a/JSONCreator_iOS/JSONCreator/MasterViewController.swift +++ b/JSONCreator_iOS/JSONCreator/MasterViewController.swift @@ -22,7 +22,7 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa // MARK: - Properties //-------------------------------------------------- - var molecularModels: [MolecularModel] = [] + var structureModels: [StructureCategoryModel] = [] //-------------------------------------------------- // MARK: - Life Cycle @@ -48,40 +48,23 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa let errorLocation = (MVMCoreLoadHandler.sharedGlobal()?.errorLocation(forRequest: MVMCoreSessionTimeHandler.sharedSession(), pageType: "", modules: ""))! - MVMCoreDispatchUtility.performBlock(inBackground: { + DispatchQueue.global(qos: .default).async { MVMCoreLoadHandler.sharedGlobal()?.sendRequest(requestParameters!, locationForError: errorLocation, requestFinished: { json, error in guard error == nil else { return } - do { - try self.fetchAllMolecules(from: json) - - MVMCoreDispatchUtility.performSyncBlock(onMainThread: { - self.tableView.reloadData() - }) - } catch let myJSONError { - print(myJSONError) + self.fetchAllMolecules(from: json) + + DispatchQueue.main.async { + self.tableView.reloadData() } }) - }) + } } func getData(for indexPath: IndexPath) -> String? { - guard let subDictionary = molecularModels[indexPath.section].molecules[indexPath.row] as? [String: Any], - let bb = subDictionary.keys.first, - let val = subDictionary[bb] - else { return nil } - - var json: String = "" - do { - let data = try JSONSerialization.data(withJSONObject: val, options: .prettyPrinted) - json = String(data: data, encoding: .utf8)! - } catch { - print(error.localizedDescription) - } - - return json + return structureModels[indexPath.section].structures[indexPath.row].value } //-------------------------------------------------- @@ -89,25 +72,23 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa //-------------------------------------------------- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - - return molecularModels[section].category.capitalized + return structureModels[section].category.capitalized } func numberOfSections(in tableView: UITableView) -> Int { - - return molecularModels.count + return structureModels.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - - return molecularModels[section].molecules.count + return structureModels[section].structures.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) - cell.textLabel?.text = molecularModels[indexPath.section].molecules[indexPath.row].keys.first as? String ?? "no" + cell.textLabel?.text = structureModels[indexPath.section].structures[indexPath.row].key + if cell.interactions.first == nil { let dragInteraction = UIDragInteraction(delegate: self) cell.addInteraction(dragInteraction) @@ -128,7 +109,7 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa let label = UILabel(frame: .zero) label.clipsToBounds = true label.backgroundColor = .gray - label.text = " Copied " + label.text = "\tCopied\t" label.font = UIFont.systemFont(ofSize: 30) label.translatesAutoresizingMaskIntoConstraints = false label.layer.cornerRadius = 5 @@ -179,31 +160,32 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa ((self.splitViewController?.viewControllers[1] as? UINavigationController)?.topViewController as? DetailViewController)?.textView.text = "" } - private func fetchAllMolecules(from json: Any?) throws { + private func fetchAllMolecules(from json: Any?) { - let data = try JSONSerialization.data(withJSONObject: json!, options: .prettyPrinted) - let convertedString = String(data: data, encoding: .utf8)?.data(using: .utf8) - guard let dictionary = try JSONSerialization.jsonObject(with: convertedString!, options: []) as? [AnyHashable: Any] else { return } + guard let serverJSON = json, + let data = try? JSONSerialization.data(withJSONObject: serverJSON, options: .prettyPrinted), + let convertedString = String(data: data, encoding: .utf8)?.data(using: .utf8), + let categoryDictionary = try? JSONSerialization.jsonObject(with: convertedString, options: []) as? [AnyHashable: Any] + else { return } // Cycle through the categories passed from server. - for (categoryName, content) in dictionary { + for (categoryName, content) in categoryDictionary { - let molecularModel = MolecularModel() - molecularModel.category = categoryName as! String + guard let category = categoryName as? String else { continue } - guard let jsonContent = (content as? String)?.data(using: .utf8), - let categoryContent = try JSONSerialization.jsonObject(with: jsonContent, options: []) as? [AnyHashable: Any] + let structureCategoryModel = StructureCategoryModel(category: category) + + guard let data = try? JSONSerialization.data(withJSONObject: content, options: .prettyPrinted), + let convertedString = String(data: data, encoding: .utf8)?.data(using: .utf8), + let categoryContent = try? JSONSerialization.jsonObject(with: convertedString, options: []) as? [String: String] else { continue } // Cycle through the molecules held by a category. - for (moleculeName, moleculeValue) in categoryContent { - - guard let moleculeJSON = (moleculeValue as? String)?.data(using: .utf8), - let moldictionaryCat = try JSONSerialization.jsonObject(with: moleculeJSON, options: []) as? [AnyHashable: Any] else { continue } - - molecularModel.molecules.append([moleculeName as! String: moldictionaryCat]) + for (structureName, structureValue) in categoryContent { + structureCategoryModel.structures.append((key: structureName, value: structureValue)) } - molecularModels.append(molecularModel) + + structureModels.append(structureCategoryModel) } } } diff --git a/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift b/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift index d3d7c3c..90c69f9 100644 --- a/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift +++ b/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift @@ -9,8 +9,13 @@ import Foundation -class MolecularModel { +class StructureCategoryModel { var category: String = "" - var molecules: [[AnyHashable: Any]] = [] + var structures: [(key: String, value: String)] = [] +// var jsonRepresentation = "" + + init(category: String) { + self.category = category + } }