From ec52278144e672965cfc1cb3f3049af0f992bc03 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Wed, 21 Aug 2019 14:56:18 -0400 Subject: [PATCH] gotit. --- .../JSONCreator.xcodeproj/project.pbxproj | 12 ++ JSONCreator_iOS/JSONCreator/AppDelegate.swift | 3 +- .../JSONCreator/Base.lproj/Main.storyboard | 21 ++- .../JSONCreator/DetailViewController.swift | 7 +- .../JSONCreator/MasterViewController.swift | 154 +++++++++++------- .../JSONCreator/Models/MolecularModel.swift | 16 ++ 6 files changed, 154 insertions(+), 59 deletions(-) create mode 100644 JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift diff --git a/JSONCreator_iOS/JSONCreator.xcodeproj/project.pbxproj b/JSONCreator_iOS/JSONCreator.xcodeproj/project.pbxproj index 8542629..b76b07e 100644 --- a/JSONCreator_iOS/JSONCreator.xcodeproj/project.pbxproj +++ b/JSONCreator_iOS/JSONCreator.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + 0AA7BDD2230DA06D00EED90B /* MolecularModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AA7BDD1230DA06D00EED90B /* MolecularModel.swift */; }; D2B1E3F322F4A68F0065F95C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2B1E3F222F4A68F0065F95C /* AppDelegate.swift */; }; D2B1E3F522F4A68F0065F95C /* MasterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2B1E3F422F4A68F0065F95C /* MasterViewController.swift */; }; D2B1E3F722F4A68F0065F95C /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2B1E3F622F4A68F0065F95C /* DetailViewController.swift */; }; @@ -53,6 +54,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 0AA7BDD1230DA06D00EED90B /* MolecularModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MolecularModel.swift; sourceTree = ""; }; D2B1E3EF22F4A68F0065F95C /* JSONCreator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JSONCreator.app; sourceTree = BUILT_PRODUCTS_DIR; }; D2B1E3F222F4A68F0065F95C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; D2B1E3F422F4A68F0065F95C /* MasterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterViewController.swift; sourceTree = ""; }; @@ -81,6 +83,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 0AA7BDD0230DA05600EED90B /* Models */ = { + isa = PBXGroup; + children = ( + 0AA7BDD1230DA06D00EED90B /* MolecularModel.swift */, + ); + path = Models; + sourceTree = ""; + }; D2B1E3E622F4A68F0065F95C = { isa = PBXGroup; children = ( @@ -101,6 +111,7 @@ D2B1E3F122F4A68F0065F95C /* JSONCreator */ = { isa = PBXGroup; children = ( + 0AA7BDD0230DA05600EED90B /* Models */, D2B1E3F222F4A68F0065F95C /* AppDelegate.swift */, D2B1E3F422F4A68F0065F95C /* MasterViewController.swift */, D2B1E3F622F4A68F0065F95C /* DetailViewController.swift */, @@ -222,6 +233,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 0AA7BDD2230DA06D00EED90B /* MolecularModel.swift in Sources */, D2B1E3F722F4A68F0065F95C /* DetailViewController.swift in Sources */, D2B1E3F522F4A68F0065F95C /* MasterViewController.swift in Sources */, D2B1E3F322F4A68F0065F95C /* AppDelegate.swift in Sources */, diff --git a/JSONCreator_iOS/JSONCreator/AppDelegate.swift b/JSONCreator_iOS/JSONCreator/AppDelegate.swift index 364d09f..d36ebb3 100644 --- a/JSONCreator_iOS/JSONCreator/AppDelegate.swift +++ b/JSONCreator_iOS/JSONCreator/AppDelegate.swift @@ -38,7 +38,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele splitViewController.delegate = self splitViewController.preferredDisplayMode = .allVisible - NotificationCenter.default.addObserver(forName: UIViewController.showDetailTargetDidChangeNotification, object: splitViewController, queue: nil) { [weak self] (notification) in + NotificationCenter.default.addObserver(forName: UIViewController.showDetailTargetDidChangeNotification, object: splitViewController, queue: nil) { [weak self] notification in if let strongSelf = self, let svc = notification.object as? UISplitViewController { if svc.isCollapsed { strongSelf.mvc?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Editor", style: .plain, target: self, action: #selector(self?.showEditor)) @@ -87,6 +87,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele } extension AppDelegate: UINavigationControllerDelegate { + func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { if mvc == viewController { dvcNav?.setViewControllers([dvc!], animated: false) diff --git a/JSONCreator_iOS/JSONCreator/Base.lproj/Main.storyboard b/JSONCreator_iOS/JSONCreator/Base.lproj/Main.storyboard index fb59f52..1db93f2 100644 --- a/JSONCreator_iOS/JSONCreator/Base.lproj/Main.storyboard +++ b/JSONCreator_iOS/JSONCreator/Base.lproj/Main.storyboard @@ -95,29 +95,46 @@ - + + - + + + + diff --git a/JSONCreator_iOS/JSONCreator/DetailViewController.swift b/JSONCreator_iOS/JSONCreator/DetailViewController.swift index 40c65db..f75cba8 100644 --- a/JSONCreator_iOS/JSONCreator/DetailViewController.swift +++ b/JSONCreator_iOS/JSONCreator/DetailViewController.swift @@ -46,7 +46,8 @@ class DetailViewController: UIViewController { let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareButtonPressed)) let buildButton = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(play)) - navigationItem.setRightBarButtonItems([shareButton, buildButton], animated: true) + let clearButton = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(clearTextView)) + navigationItem.setRightBarButtonItems([clearButton, shareButton, buildButton], animated: true) } //-------------------------------------------------- @@ -69,6 +70,10 @@ class DetailViewController: UIViewController { } } + @objc func clearTextView() { + textView.text = "" + } + @objc func play() { do { diff --git a/JSONCreator_iOS/JSONCreator/MasterViewController.swift b/JSONCreator_iOS/JSONCreator/MasterViewController.swift index 2cfded2..d5b5039 100644 --- a/JSONCreator_iOS/JSONCreator/MasterViewController.swift +++ b/JSONCreator_iOS/JSONCreator/MasterViewController.swift @@ -17,13 +17,13 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa @IBOutlet weak var tableView: UITableView! @IBOutlet weak var ipTextField: UITextField! + @IBOutlet weak var requestButton: UIButton! //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - let folderPath = Bundle.main.resourcePath! + "/JSON" - var sectionsMap: [AnyHashable: Any] = [:] + var molecularModels: [MolecularModel] = [] //-------------------------------------------------- // MARK: - Life Cycle @@ -32,36 +32,61 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa override func viewDidLoad() { super.viewDidLoad() - // setup the data - sectionsMap = [:] - let sections = try! FileManager.default.contentsOfDirectory(atPath: folderPath) - for section in sections { - let paths = try! FileManager.default.subpathsOfDirectory(atPath: "\(folderPath)/\(section)") - var array: [String] = [] - for path in paths { - if path.hasSuffix(".json") { - array.append(path) - } - } - sectionsMap[section] = array - } - tableView.delegate = self tableView.dataSource = self tableView.estimatedRowHeight = 80 tableView.rowHeight = UITableView.automaticDimension ipTextField.text = "127.0.0.1:8181" + requestButton.layer.cornerRadius = 15.0 + requestButton.layer.shadowColor = UIColor.black.cgColor + requestButton.layer.shadowOpacity = 0.5 + requestButton.layer.shadowOffset = CGSize(width: 1, height: 2) + requestButton.layer.shadowRadius = 1 + requestButton.layer.masksToBounds = false + requestButton.backgroundColor = UIColor.mfGet(forHex: "#d0261d") + + guard let ip = ipTextField.text else { return } + + let requestParameters = MVMCoreRequestParameters(actionMap: [:]) + requestParameters?.parameters = ["category": "Atoms", "filename": "Button"] + requestParameters?.alternateBaseURL = URL(string: "http://\(ip)/v1/fetchAllMolecules") + + let errorLocation = (MVMCoreLoadHandler.sharedGlobal()?.errorLocation(forRequest: MVMCoreSessionTimeHandler.sharedSession(), pageType: "", modules: ""))! + + MVMCoreDispatchUtility.performBlock(inBackground: { + 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) + } + }) + }) } func getData(for indexPath: IndexPath) -> String? { - let sectionName = self.tableView(tableView, titleForHeaderInSection: indexPath.section) - let array = sectionsMap[sectionName!]! as! Array - let subPath = (array[indexPath.row] as! String) - let path = "\(folderPath)/\(sectionName!)/\(subPath)" + let subDictionary = molecularModels[indexPath.section].molecules[indexPath.row] as! [String: Any] - return try! String.init(contentsOfFile: path) + let bb = subDictionary.keys.first! + let val = subDictionary[bb] + + var json: String = "" + do { + let data = try JSONSerialization.data(withJSONObject: val, options: .prettyPrinted) + json = String(data: data, encoding: .utf8)! + } catch { + + } + return json } //-------------------------------------------------- @@ -69,31 +94,30 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa //-------------------------------------------------- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - return (((sectionsMap as NSDictionary).allKeys[section]) as! String) + + return molecularModels[section].category } func numberOfSections(in tableView: UITableView) -> Int { - return sectionsMap.count + + return molecularModels.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - let sectionName = self.tableView(tableView, titleForHeaderInSection: section) - let array = sectionsMap.arrayForKey(sectionName!) - return array.count + return molecularModels[section].molecules.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) - let sectionName = self.tableView(tableView, titleForHeaderInSection: indexPath.section) - let array = sectionsMap[sectionName!]! as! Array - cell.textLabel?.text = (array[indexPath.row] as! String) - + cell.textLabel?.text = molecularModels[indexPath.section].molecules[indexPath.row].keys.first as? String ?? "no" if cell.interactions.first == nil { let dragInteraction = UIDragInteraction(delegate: self) cell.addInteraction(dragInteraction) } + return cell } @@ -102,20 +126,6 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa return true } - - func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { - - guard section == 0 else { return nil } - - let button = UIButton(frame: .zero) - button.setTitle((((sectionsMap as NSDictionary).allKeys[section]) as! String), for: .normal) - button.backgroundColor = .red - button.tag = section // why not... ``\__(~_~)__/`` - button.addTarget(self, action: #selector(requestJSON), for: .touchUpInside) - - return button - } - func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { UIPasteboard.general.string = getData(for: indexPath) @@ -124,7 +134,7 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa label.clipsToBounds = true label.backgroundColor = .gray label.text = " Copied " - label.font = UIFont.systemFont(ofSize: 40) + label.font = UIFont.systemFont(ofSize: 30) label.translatesAutoresizingMaskIntoConstraints = false label.layer.cornerRadius = 5 view.superview!.addSubview(label) @@ -142,14 +152,15 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa // MARK: - Methods //-------------------------------------------------- - @objc func requestJSON() { + @IBAction func requestMolecules(_ sender: UIButton) { guard let ip = ipTextField.text else { return } let requestParameters = MVMCoreRequestParameters(actionMap: [:]) - let errorLocation = (MVMCoreLoadHandler.sharedGlobal()?.errorLocation(forRequest: MVMCoreSessionTimeHandler.sharedSession(), pageType: "stackTest", modules: ""))! + requestParameters?.parameters = ["category": "Atoms", "filename": "Button"] + requestParameters?.alternateBaseURL = URL(string: "http://\(ip)/v1/fetchAllMolecules") - requestParameters?.alternateBaseURL = URL(string: "http://\(ip)/v1/molecularResponse") + let errorLocation = (MVMCoreLoadHandler.sharedGlobal()?.errorLocation(forRequest: MVMCoreSessionTimeHandler.sharedSession(), pageType: "", modules: ""))! MVMCoreDispatchUtility.performBlock(inBackground: { MVMCoreLoadHandler.sharedGlobal()?.sendRequest(requestParameters!, locationForError: errorLocation, requestFinished: { json, error in @@ -157,11 +168,10 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa guard error == nil else { return } do { - let data = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) - let convertedString = String(data: data, encoding: .utf8) + try self.fetchAllMolecules(from: json) MVMCoreDispatchUtility.performSyncBlock(onMainThread: { - ((self.splitViewController?.viewControllers[1] as? UINavigationController)?.topViewController as? DetailViewController)?.textView.text = convertedString + self.tableView.reloadData() }) } catch let myJSONError { print(myJSONError) @@ -169,19 +179,53 @@ class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDa }) }) } + + func setDetailPage() { + ((self.splitViewController?.viewControllers[1] as? UINavigationController)?.topViewController as? DetailViewController)?.textView.text = "" + } + + private func fetchAllMolecules(from json: Any?) throws { + + 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 } + + // Cycle through the categories passed from server. + for (categoryName, content) in dictionary { + + let molecularModel = MolecularModel() + molecularModel.category = categoryName as! String + + let jsonContent = (content as! String).data(using: .utf8) + guard let categoryContent = try JSONSerialization.jsonObject(with: jsonContent!, options: []) as? [AnyHashable: Any] else { return } + + // Cycle through the molecules held by a category. + for (moleculeName, moleculeValue) in categoryContent { + + let moleculeJSON = (moleculeValue as! String).data(using: .utf8) + guard let moldictionaryCat = try JSONSerialization.jsonObject(with: moleculeJSON!, options: []) as? [AnyHashable: Any] else { return } + + molecularModel.molecules.append([moleculeName as! String: moldictionaryCat]) + } + molecularModels.append(molecularModel) + } + } } - // MARK:- Drag Interaction Delegate extension MasterViewController: UIDragInteractionDelegate { func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] { let location = session.location(in: tableView) - let indexPath = tableView.indexPathForRow(at: location) - let content = getData(for: indexPath!) - let provider = NSItemProvider(object: content! as NSString) + + guard let indexPath = tableView.indexPathForRow(at: location), + let content = getData(for: indexPath) + else { return [] } + + let provider = NSItemProvider(object: content as NSString) let item = UIDragItem(itemProvider: provider) + return [item] } } diff --git a/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift b/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift new file mode 100644 index 0000000..d3d7c3c --- /dev/null +++ b/JSONCreator_iOS/JSONCreator/Models/MolecularModel.swift @@ -0,0 +1,16 @@ +// +// MolecularModel.swift +// JSONCreator +// +// Created by Kevin Christiano on 8/21/19. +// Copyright © 2019 Verizon Wireless. All rights reserved. +// + +import Foundation + + +class MolecularModel { + + var category: String = "" + var molecules: [[AnyHashable: Any]] = [] +}