139 lines
5.5 KiB
Swift
139 lines
5.5 KiB
Swift
//
|
|
// MasterViewController.swift
|
|
// JSONCreator
|
|
//
|
|
// Created by Scott Pfeil on 8/2/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import MVMCore
|
|
|
|
|
|
class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
|
//--------------------------------------------------
|
|
// MARK: - Outlets
|
|
//--------------------------------------------------
|
|
|
|
@IBOutlet weak var tableView: UITableView!
|
|
@IBOutlet weak var ipTextField: UITextField!
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
let folderPath = Bundle.main.resourcePath! + "/JSON"
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Life Cycle
|
|
//--------------------------------------------------
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
tableView.delegate = self
|
|
tableView.dataSource = self
|
|
tableView.estimatedRowHeight = 80
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
ipTextField.text = "127.0.0.1:8181"
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Table View
|
|
//--------------------------------------------------
|
|
|
|
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
return try? FileManager.default.contentsOfDirectory(atPath: folderPath)[section]
|
|
}
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
return try! FileManager.default.contentsOfDirectory(atPath: folderPath).count
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
return try! FileManager.default.contentsOfDirectory(atPath: folderPath + "/" + FileManager.default.contentsOfDirectory(atPath: folderPath)[section]).count
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
|
|
|
|
let name = try? FileManager.default.contentsOfDirectory(atPath: folderPath + "/" + FileManager.default.contentsOfDirectory(atPath: folderPath)[indexPath.section])[indexPath.row]
|
|
cell.textLabel?.text = name
|
|
|
|
if cell.interactions.first == nil {
|
|
let dragInteraction = UIDragInteraction(delegate: self)
|
|
cell.addInteraction(dragInteraction)
|
|
}
|
|
|
|
return cell
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
|
|
let button = UIButton(frame: .zero)
|
|
button.setTitle("RequestView", for: .normal)
|
|
button.backgroundColor = .red
|
|
button.tag = section // why not... ``\__(~_~)__/``
|
|
button.addTarget(self, action: #selector(requestJSON), for: .touchUpInside)
|
|
|
|
return button
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
// Return false if you do not want the specified item to be editable.
|
|
return true
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Methods
|
|
//--------------------------------------------------
|
|
|
|
@objc func requestJSON() {
|
|
|
|
guard let ip = ipTextField.text else { return }
|
|
|
|
let requestParameters = MVMCoreRequestParameters(actionMap: [:])
|
|
let errorLocation = (MVMCoreLoadHandler.sharedGlobal()?.errorLocation(forRequest: MVMCoreSessionTimeHandler.sharedSession(), pageType: "stackTest", modules: ""))!
|
|
|
|
requestParameters?.alternateBaseURL = URL(string: "http://\(ip)/v1/molecularResponse")
|
|
|
|
MVMCoreDispatchUtility.performBlock(inBackground: {
|
|
MVMCoreLoadHandler.sharedGlobal()?.sendRequest(requestParameters!, locationForError: errorLocation, requestFinished: { json, error in
|
|
|
|
guard error == nil else { return }
|
|
|
|
do {
|
|
let data = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
|
|
let convertedString = String(data: data, encoding: .utf8)
|
|
|
|
MVMCoreDispatchUtility.performSyncBlock(onMainThread: {
|
|
((self.splitViewController?.viewControllers[1] as? UINavigationController)?.topViewController as? DetailViewController)?.textView.text = convertedString
|
|
})
|
|
} catch let myJSONError {
|
|
print(myJSONError)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
// 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 sectionName = try! FileManager.default.contentsOfDirectory(atPath: folderPath)[indexPath!.section]
|
|
let path = try! folderPath + "/" + sectionName + "/" + FileManager.default.contentsOfDirectory(atPath: folderPath + "/" + sectionName)[indexPath!.row]
|
|
let content = try! String(contentsOfFile: path)
|
|
let provider = NSItemProvider(object: content as NSString)
|
|
let item = UIDragItem(itemProvider: provider)
|
|
|
|
return [item]
|
|
}
|
|
}
|