This commit is contained in:
Kevin G Christiano 2019-08-21 14:56:18 -04:00
parent fd060971e5
commit ec52278144
6 changed files with 154 additions and 59 deletions

View File

@ -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 = "<group>"; };
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 = "<group>"; };
D2B1E3F422F4A68F0065F95C /* MasterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterViewController.swift; sourceTree = "<group>"; };
@ -81,6 +83,14 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
0AA7BDD0230DA05600EED90B /* Models */ = {
isa = PBXGroup;
children = (
0AA7BDD1230DA06D00EED90B /* MolecularModel.swift */,
);
path = Models;
sourceTree = "<group>";
};
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 */,

View File

@ -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)

View File

@ -95,29 +95,46 @@
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Enter IP" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="8ES-JF-wQQ">
<rect key="frame" x="46.5" y="96" width="347.5" height="30"/>
<rect key="frame" x="46.5" y="96" width="301.5" height="30"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="numbersAndPunctuation" returnKeyType="done" smartDashesType="no" smartInsertDeleteType="no" smartQuotesType="no" textContentType="url"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="LWi-u3-TWQ">
<rect key="frame" x="364" y="96" width="30" height="30"/>
<color key="backgroundColor" red="1" green="0.14913141730000001" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" constant="30" id="SpC-pP-yWO"/>
<constraint firstAttribute="height" constant="30" id="j4C-NY-l84"/>
</constraints>
<state key="normal">
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
</state>
<connections>
<action selector="requestMolecules:" destination="d7m-B4-a2J" eventType="touchUpInside" id="Ub3-dN-BVq"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="4Uk-eQ-EQn" firstAttribute="bottom" secondItem="sWB-DD-mBr" secondAttribute="bottom" id="1EE-Zt-vT1"/>
<constraint firstItem="sWB-DD-mBr" firstAttribute="trailing" secondItem="8ES-JF-wQQ" secondAttribute="trailing" constant="20" id="5Nc-rO-Na3"/>
<constraint firstItem="4Uk-eQ-EQn" firstAttribute="leading" secondItem="sWB-DD-mBr" secondAttribute="leading" id="5h4-de-VEb"/>
<constraint firstItem="8ES-JF-wQQ" firstAttribute="centerY" secondItem="SXL-ds-O0O" secondAttribute="centerY" id="62j-61-bE9"/>
<constraint firstItem="4Uk-eQ-EQn" firstAttribute="top" secondItem="8ES-JF-wQQ" secondAttribute="bottom" constant="20" id="A4e-rm-tbe"/>
<constraint firstItem="8ES-JF-wQQ" firstAttribute="leading" secondItem="SXL-ds-O0O" secondAttribute="trailing" constant="8" id="AL1-Zm-i3c"/>
<constraint firstItem="sWB-DD-mBr" firstAttribute="trailing" secondItem="4Uk-eQ-EQn" secondAttribute="trailing" id="DhH-Gw-OdP"/>
<constraint firstItem="LWi-u3-TWQ" firstAttribute="leading" secondItem="8ES-JF-wQQ" secondAttribute="trailing" constant="16" id="Edc-z4-keA"/>
<constraint firstItem="SXL-ds-O0O" firstAttribute="leading" secondItem="sWB-DD-mBr" secondAttribute="leading" constant="20" id="FIo-Dp-pZs"/>
<constraint firstItem="sWB-DD-mBr" firstAttribute="trailing" secondItem="LWi-u3-TWQ" secondAttribute="trailing" constant="20" id="Ink-aD-8d1"/>
<constraint firstItem="8ES-JF-wQQ" firstAttribute="top" secondItem="sWB-DD-mBr" secondAttribute="top" constant="8" id="aPy-EJ-zp5"/>
<constraint firstItem="LWi-u3-TWQ" firstAttribute="top" secondItem="sWB-DD-mBr" secondAttribute="top" constant="8" id="jjz-l8-hnl"/>
</constraints>
<viewLayoutGuide key="safeArea" id="sWB-DD-mBr"/>
</view>
<navigationItem key="navigationItem" id="QQY-EX-HwK"/>
<connections>
<outlet property="ipTextField" destination="8ES-JF-wQQ" id="ngi-c0-NuP"/>
<outlet property="requestButton" destination="LWi-u3-TWQ" id="SxK-RZ-esy"/>
<outlet property="tableView" destination="4Uk-eQ-EQn" id="l5E-2O-dib"/>
</connections>
</viewController>

View File

@ -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 {

View File

@ -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<Any>
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<Any>
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]
}
}

View File

@ -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]] = []
}