better API contact

This commit is contained in:
Kevin G Christiano 2020-01-28 14:52:25 -05:00
parent 9c8a55cc82
commit 93e11b7de8
2 changed files with 38 additions and 51 deletions

View File

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

View File

@ -9,8 +9,13 @@
import Foundation import Foundation
class MolecularModel { class StructureCategoryModel {
var category: String = "" var category: String = ""
var molecules: [[AnyHashable: Any]] = [] var structures: [(key: String, value: String)] = []
// var jsonRepresentation = ""
init(category: String) {
self.category = category
}
} }