jsoncreator_app/JSONCreator_iOS/JSONCreator/MoleculeReplacementViewController.swift
Hedden, Kyle Matthew b9b2977fe5 Create a replacement VC for testing molecular replacement.
Add user defaults saving of progress.
2024-07-12 14:33:38 -04:00

138 lines
5.3 KiB
Swift

//
// MoleculeReplacementViewController.swift
// JSONCreator
//
// Created by Kyle Hedden on 7/9/24.
// Copyright © 2024 Verizon Wireless. All rights reserved.
//
import UIKit
import MVMCoreUI
class MoleculeReplacementViewController: UIViewController {
let textView = UITextView(frame: .zero)
var replaceCallback: (([AnyHashable : Any], MVMCoreLoadObject)->Void)?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
guard textView.superview == nil else {
return
}
modalPresentationStyle = .fullScreen
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
textView.autocapitalizationType = .none
textView.autocorrectionType = .no
if let data = UserDefaults.standard.data(forKey: "priorReplace"),
let prior = String(data: data, encoding: .utf8) {
textView.text = prior
} else {
textView.text = """
{
"ResponseInfo" : {
"code" : "00000",
"type" : "Success"
},
"ModuleMap": {
}
}
"""
}
let clearButton = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(clearButtonPressed))
let buildButton = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(play))
navigationItem.setRightBarButtonItems([clearButton, buildButton], animated: true)
navigationItem.title = "Trigger Update"
}
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 clearButtonPressed() {
textView.text = ""
UserDefaults.standard.removeObject(forKey: "priorReplace")
}
@objc func play() {
do {
if let data = textView.text.data(using: .utf8),
let rawServerResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable: Any],
let modulesJSON = rawServerResponse["ModuleMap"] as? [AnyHashable: Any] {
UserDefaults.standard.set(data, forKey: "priorReplace")
let loadObject = MVMCoreLoadObject(pageJSON: nil, modulesJSON: modulesJSON, requestParameters: nil, dataForPage: nil, delegateObject: nil)!
replaceCallback?(rawServerResponse, loadObject)
}
} catch {
showError(error as NSError)
}
}
@objc func close() {
Task { @MainActor in
await NavigationHandler.shared().popTopViewController()
}
}
}
extension MoleculeReplacementViewController: 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, .sortedKeys])
}
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, .sortedKeys])
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)
}
}
}