jsoncreator_app/JSONCreator_iOS/JSONCreator/DetailViewController.swift
Kevin G Christiano e1c4fdda63 Merge branch 'develop' into feature/molecular
# Conflicts:
#	JSONCreator_iOS/JSONCreator/AppDelegate.swift
#	JSONCreator_iOS/JSONCreator/DetailViewController.swift
#	JSONCreator_iOS/JSONCreator/MasterViewController.swift
2019-08-15 09:38:57 -04:00

147 lines
6.3 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// DetailViewController.swift
// JSONCreator
//
// Created by Scott Pfeil on 8/2/19.
// Copyright © 2019 Verizon Wireless. All rights reserved.
//
import UIKit
import MVMCoreUI
class DetailViewController: UIViewController {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
let textView = UITextView(frame: .zero)
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
guard textView.superview == nil else { return }
textView.textDropDelegate = self
view.addSubview(textView)
if UIDevice.current.userInterfaceIdiom == .pad {
textView.font = UIFont.systemFont(ofSize: 40)
} else {
textView.font = UIFont.systemFont(ofSize: 14)
}
textView.translatesAutoresizingMaskIntoConstraints = false
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
view.trailingAnchor.constraint(equalTo: textView.trailingAnchor).isActive = true
view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: textView.bottomAnchor).isActive = true
textView.delegate = self
textView.smartDashesType = .no
textView.smartQuotesType = .no
textView.smartInsertDeleteType = .no
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)
}
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
func showError(_ error: NSError) {
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
@objc func shareButtonPressed() {
if let text = textView.text {
let activityController = UIActivityViewController(activityItems: [text], applicationActivities: nil)
activityController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(activityController, animated: true, completion: nil)
}
}
@objc func play() {
do {
if let data = textView.text.data(using: .utf8), let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable: Any] {
let page = jsonObject.optionalDictionaryForKey(KeyPage)
let pageType = page?.optionalStringForKey(KeyPageType)
let template = page?.optionalStringForKey("template")
var errorObject = MVMCoreErrorObject(title: nil, message: "No Template Found", code: ErrorCode.initViewController.rawValue, domain: ErrorDomainNative, location: nil)
     
if let viewController = MVMCoreUIViewControllerMappingObject.shared()?.createMFViewController(ofTemplate: template, pageType: pageType), let loadObject = MVMCoreLoadObject(pageJSON: page, modulesJSON: nil, requestParameters: nil, dataForPage: nil, delegateObject: nil), viewController.shouldFinishProcessingLoad(loadObject, error: &errorObject!) {
let navigation = UINavigationController(rootViewController: viewController)
viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(close))
MVMCoreNavigationHandler.shared()?.present(navigation, animated: true)
} else if let errorObject = errorObject {
let error = NSError(domain: ErrorDomainNative, code: ErrorCode.initViewController.rawValue, userInfo: [NSLocalizedDescriptionKey: errorObject.messageToDisplay!])
showError(error)
}
}
} catch {
showError(error as NSError)
}
}
@objc func close() {
MVMCoreNavigationHandler.shared()?.dismissTopViewController(animated: true)
}
}
// MARK:- TextView Delegate
extension DetailViewController: UITextViewDelegate {
func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
guard textView.text.count > 0 else { return true }
do {
if let data = textView.text.data(using: .utf8) {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
_ = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
}
return true
} catch {
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
return false
}
}
func textViewDidEndEditing(_ textView: UITextView) {
guard textView.text.count > 0 else { return }
do {
if let data = textView.text.data(using: .utf8) {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
let prettyData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
let prettyString = String.init(data: prettyData, encoding: .utf8)
textView.text = prettyString
}
} catch {
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}
}
extension DetailViewController: UITextDropDelegate {
func textDroppableView(_ textDroppableView: UIView & UITextDroppable, willPerformDrop drop: UITextDropRequest) {
let hi = "d"
}
}