104 lines
4.2 KiB
Swift
104 lines
4.2 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: UITableViewController {
|
|
|
|
let folderPath = Bundle.main.resourcePath! + "/JSON"
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view.
|
|
|
|
tableView.estimatedRowHeight = 80
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
}
|
|
|
|
// MARK: - Table View
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
return try? FileManager.default.contentsOfDirectory(atPath: folderPath)[section]
|
|
}
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
return try! FileManager.default.contentsOfDirectory(atPath: folderPath).count
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
if section == 0 {
|
|
return try! FileManager.default.contentsOfDirectory(atPath: folderPath + "/" + FileManager.default.contentsOfDirectory(atPath: folderPath)[section]).count + 1
|
|
}
|
|
|
|
return try! FileManager.default.contentsOfDirectory(atPath: folderPath + "/" + FileManager.default.contentsOfDirectory(atPath: folderPath)[section]).count
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
|
if indexPath.section == 0 {
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? QueryServerCell else { return UITableViewCell() }
|
|
cell.requestServer = requestJSON
|
|
return cell
|
|
|
|
} else {
|
|
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
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
// Return false if you do not want the specified item to be editable.
|
|
return true
|
|
}
|
|
|
|
@objc func requestJSON() {
|
|
|
|
let requestParameters = MVMCoreRequestParameters(actionMap: [:])
|
|
let errorLocation = (MVMCoreLoadHandler.sharedGlobal()?.errorLocation(forRequest: MVMCoreSessionTimeHandler.sharedSession(),
|
|
pageType: "test",
|
|
modules: ""))!
|
|
|
|
MVMCoreDispatchUtility.performBlock(inBackground: {
|
|
|
|
MVMCoreLoadHandler.sharedGlobal()?.sendRequest(requestParameters!, locationForError: errorLocation, requestFinished: { json, error in
|
|
|
|
if error != nil {
|
|
// BAD
|
|
} else {
|
|
// GOOD
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
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.init(contentsOfFile: path)
|
|
let provider = NSItemProvider(object: content as NSString)
|
|
let item = UIDragItem(itemProvider: provider)
|
|
return [item]
|
|
}
|
|
}
|