154 lines
6.6 KiB
Swift
154 lines
6.6 KiB
Swift
//
|
|
// 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: 20)
|
|
} 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))
|
|
let clearButton = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(clearTextView))
|
|
navigationItem.setRightBarButtonItems([clearButton, 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 clearTextView() {
|
|
textView.text = ""
|
|
}
|
|
|
|
@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) {
|
|
// Nothing for now....
|
|
}
|
|
}
|