jsoncreator_app/JSONCreator_iOS/JSONCreator/AppDelegate.swift
Matt Bruce 26c2243d0d updated samples
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-01-12 10:07:11 -06:00

236 lines
10 KiB
Swift

//
// AppDelegate.swift
// JSONCreator
//
// Created by Scott Pfeil on 8/2/19.
// Copyright © 2019 Verizon Wireless. All rights reserved.
//
import UIKit
import MVMCoreUI
import VDS
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
var mvcNav: UINavigationController?
var dvcNav: UINavigationController?
var mvc: UIViewController?
var dvc: UIViewController?
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Setup our core object with the default implementation
CoreUIObject.sharedInstance()?.defaultInitialSetup()
CoreUIObject.sharedInstance()?.globalTopAlertDelegate = self
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = window!.rootViewController as! UISplitViewController
mvcNav = splitViewController.viewControllers[0] as? UINavigationController
mvcNav?.delegate = self
mvc = mvcNav?.topViewController
dvcNav = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as? UINavigationController
dvc = dvcNav?.topViewController
dvc?.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
dvc?.navigationItem.leftItemsSupplementBackButton = true
splitViewController.delegate = self
splitViewController.preferredDisplayMode = .allVisible
let gr = UILongPressGestureRecognizer(target: self, action: #selector(jsonPasteAndGo))
splitViewController.view.addGestureRecognizer(gr)
NotificationCenter.default.addObserver(forName: UIViewController.showDetailTargetDidChangeNotification, object: splitViewController, queue: nil) { [weak self] (notification) in
if let strongSelf = self, let svc = notification.object as? UISplitViewController {
if svc.isCollapsed {
strongSelf.mvc?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Editor", style: .plain, target: self, action: #selector(self?.showEditor))
} else {
strongSelf.mvcNav?.setViewControllers([strongSelf.mvc!], animated: false)
strongSelf.mvc?.navigationItem.rightBarButtonItem = nil
}
}
}
self.register()
return true
}
@objc func jsonPasteAndGo(_ sender: UILongPressGestureRecognizer) {
if sender.state == .ended {
(dvc as? DetailViewController)?.textView.text = UIPasteboard.general.string
(dvc as? DetailViewController)?.play()
}
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// MARK: - Split view
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
//guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
//guard let _ = secondaryAsNavController.topViewController as? DetailViewController else { return false }
return true
}
@objc func showEditor() {
mvcNav?.pushViewController(dvc!, animated: true)
}
}
extension AppDelegate: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if mvc == viewController {
dvcNav?.setViewControllers([dvc!], animated: false)
}
}
}
extension AppDelegate: MVMCoreGlobalTopAlertDelegateProtocol {
func getTopAlertView() -> UIView & MVMCoreTopAlertViewProtocol {
guard let topAlertView = MVMCoreUISplitViewController.main()?.topAlertView else {
return MVMCoreUITopAlertView()
}
return topAlertView;
}
func priorityForTopAlert(by object: MVMCoreTopAlertObject) -> Operation.QueuePriority {
return object.queuePriority
}
}
extension AppDelegate {
func register(){
ModelRegistry.register(handler: TestToggle.self, for: TestToggleModel.self)
ModelRegistry.register(handler: DynamicHeadlineBodyToggle.self, for: DynamicHeadlineBodyToggleModel.self)
ModelRegistry.register(handler: BiometricLabelToggle.self, for: BiometricLabelToggleModel.self)
}
}
public class DynamicHeadlineBodyToggleModel: MoleculeModelProtocol {
public static var identifier: String = "dynamicHeadlineBodyToggle"
open var backgroundColor: Color?
public var moleculeName: String?
open var headlineBody: HeadlineBodyModel
open var toggle: ToggleModel?
public var switchOnText: LabelModel?
public var switchOffText: LabelModel?
}
@objcMembers public class DynamicHeadlineBodyToggle: HeadlineBodyToggle {
//MARK:Declaration
var delegate: MVMCoreUIDelegateObject?
//override the toggles.valueChangedCancellable
public override func setupView() {
super.setupView()
toggle.valueChangedSubscription = toggle.publisher(for: .valueChanged)
.sink { [weak self] changed in
guard let self = self,
let model = self.model as? DynamicHeadlineBodyToggleModel else { return }
self.updateDynamicHeadLine(model)
self.toggle.executeDefaultAction()
}
}
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
guard let dynamicHeadlineBodyToggleModel = model as? DynamicHeadlineBodyToggleModel else { return }
delegate = delegateObject
self.model = model
toggle.setOptional(with: dynamicHeadlineBodyToggleModel.toggle, delegateObject, additionalData)
updateDynamicHeadLine(dynamicHeadlineBodyToggleModel)
}
func updateDynamicHeadLine(_ model: DynamicHeadlineBodyToggleModel) {
let headLineBodyModel: HeadlineBodyModel? = model.headlineBody
// check if toogle model present
// otherwise toggle should be hidden for member
if let _ = model.toggle {
if toggle.isOn, let switchOnText = model.switchOnText {
headLineBodyModel?.headline = switchOnText
} else if let switchOffText = model.switchOffText {
headLineBodyModel?.headline = switchOffText
}
} else {
toggle.isHidden = true
}
headlineBody.setOptional(with: headLineBodyModel, delegate, nil)
layoutIfNeeded()
}
}
public class BiometricLabelToggleModel: MoleculeModelProtocol {
public static var identifier: String = "biometricLabelToggle"
public var backgroundColor: Color?
public var touchId: LabelToggleModel
public var faceId: LabelToggleModel
}
@objc public protocol BiometricLabelToggleDelegate: NSObjectProtocol {
@objc func processBiometricID(_ toggle: MVMCoreUI.Toggle?)
@objc func setupBiometricID(_ toggle: MVMCoreUI.Toggle?)
}
@objcMembers public class BiometricLabelToggle: LabelToggle {
var biometricDelegate: BiometricLabelToggleDelegate?
// MARK: - MVMCoreViewProtocol
public override func setupView() {
super.setupView()
// alignment fix
label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
trailingAnchor.constraint(equalTo: toggle.trailingAnchor).isActive = true
//overwrite the default logic for value changed
toggle.valueChangedSubscription = toggle
.publisher(for: .valueChanged)
.sink { [weak self] changed in
self?.biometricDelegate?.processBiometricID(changed)
}
}
// MARK:- MoleculeViewProtocol
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
guard let model = model as? BiometricLabelToggleModel else { return }
biometricDelegate = delegateObject?.actionDelegate as? BiometricLabelToggleDelegate
// check device/server supports face id
if false {
// Load the face id label json
super.set(with: model.faceId, delegateObject, additionalData)
} else {
// if device/server supports touch Id
super.set(with: model.touchId, delegateObject, additionalData)
}
biometricDelegate?.setupBiometricID(toggle)
}
public class override func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
guard let model = model as? BiometricLabelToggleModel else { return nil }
return LabelToggle.estimatedHeight(with: model.faceId, delegateObject)
}
}