From 0b150e894543fa1a93bd15ea829a81e7478413e7 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Tue, 13 Aug 2019 15:38:59 -0400 Subject: [PATCH 1/2] Make it more user friendly --- JSONCreator_iOS/JSONCreator/AppDelegate.swift | 21 ++---- .../JSONCreator/DetailViewController.swift | 6 +- JSONCreator_iOS/JSONCreator/Info.plist | 1 + .../JSONCreator/MasterViewController.swift | 64 ++++++++++++++++--- 4 files changed, 66 insertions(+), 26 deletions(-) diff --git a/JSONCreator_iOS/JSONCreator/AppDelegate.swift b/JSONCreator_iOS/JSONCreator/AppDelegate.swift index ee7469b..9591a37 100644 --- a/JSONCreator_iOS/JSONCreator/AppDelegate.swift +++ b/JSONCreator_iOS/JSONCreator/AppDelegate.swift @@ -13,15 +13,6 @@ import MVMCoreUI class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate { var window: UIWindow? - - func itera(_ path: String) { - let fileManager = FileManager.default - let enumerator = fileManager.enumerator(atPath: path)! - for element in enumerator { - //do something - print(element) - } - } func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { // Setup our core object with the default implementation @@ -30,7 +21,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele } func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - itera(Bundle.main.resourcePath! + "/JSON") // Override point for customization after application launch. let splitViewController = window!.rootViewController as! UISplitViewController @@ -65,11 +55,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele // 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 false - } + /*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 false + return true + }*/ } diff --git a/JSONCreator_iOS/JSONCreator/DetailViewController.swift b/JSONCreator_iOS/JSONCreator/DetailViewController.swift index da7b9a3..09f818b 100644 --- a/JSONCreator_iOS/JSONCreator/DetailViewController.swift +++ b/JSONCreator_iOS/JSONCreator/DetailViewController.swift @@ -20,7 +20,11 @@ class DetailViewController: UIViewController { return } view.addSubview(textView) - textView.font = UIFont.systemFont(ofSize: 40) + 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 diff --git a/JSONCreator_iOS/JSONCreator/Info.plist b/JSONCreator_iOS/JSONCreator/Info.plist index 6873106..879dddf 100644 --- a/JSONCreator_iOS/JSONCreator/Info.plist +++ b/JSONCreator_iOS/JSONCreator/Info.plist @@ -43,6 +43,7 @@ UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + UIInterfaceOrientationPortraitUpsideDown UISupportedInterfaceOrientations~ipad diff --git a/JSONCreator_iOS/JSONCreator/MasterViewController.swift b/JSONCreator_iOS/JSONCreator/MasterViewController.swift index b3350e7..082a9db 100644 --- a/JSONCreator_iOS/JSONCreator/MasterViewController.swift +++ b/JSONCreator_iOS/JSONCreator/MasterViewController.swift @@ -11,31 +11,57 @@ import UIKit class MasterViewController: UITableViewController { let folderPath = Bundle.main.resourcePath! + "/JSON" - + var sectionsMap: [AnyHashable: Any] = [:] + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. + + // setup the data + sectionsMap = [:] + let sections = try! FileManager.default.contentsOfDirectory(atPath: folderPath) + for section in sections { + let paths = try! FileManager.default.subpathsOfDirectory(atPath: "\(folderPath)/\(section)") + var array: [String] = [] + for path in paths { + if path.hasSuffix(".json") { + array.append(path) + } + } + sectionsMap[section] = array + } + } + + func getData(for indexPath: IndexPath) -> String? { + let sectionName = self.tableView(tableView, titleForHeaderInSection: indexPath.section) + let array = sectionsMap[sectionName!]! as! Array + let subPath = (array[indexPath.row] as! String) + let path = "\(folderPath)/\(sectionName!)/\(subPath)" + return try! String.init(contentsOfFile: path) } // MARK: - Table View override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - return try! FileManager.default.contentsOfDirectory(atPath: folderPath)[section] + return (((sectionsMap as NSDictionary).allKeys[section]) as! String) } override func numberOfSections(in tableView: UITableView) -> Int { - return try! FileManager.default.contentsOfDirectory(atPath: folderPath).count + return sectionsMap.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return try! FileManager.default.contentsOfDirectory(atPath: folderPath + "/" + FileManager.default.contentsOfDirectory(atPath: folderPath)[section]).count + let sectionName = self.tableView(tableView, titleForHeaderInSection: section) + let array = sectionsMap.arrayForKey(sectionName!) + return array.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 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 + let sectionName = self.tableView(tableView, titleForHeaderInSection: indexPath.section) + let array = sectionsMap[sectionName!]! as! Array + cell.textLabel?.text = (array[indexPath.row] as! String) if cell.interactions.first == nil { let dragInteraction = UIDragInteraction(delegate: self) @@ -48,16 +74,34 @@ class MasterViewController: UITableViewController { // Return false if you do not want the specified item to be editable. return true } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + UIPasteboard.general.string = getData(for: indexPath) + + let label = UILabel(frame: .zero) + label.clipsToBounds = true + label.backgroundColor = .gray + label.text = " Copied " + label.font = UIFont.systemFont(ofSize: 40) + label.translatesAutoresizingMaskIntoConstraints = false + label.layer.cornerRadius = 5 + view.superview!.addSubview(label) + label.centerXAnchor.constraint(equalTo: view.superview!.centerXAnchor).isActive = true + label.centerYAnchor.constraint(equalTo: view.superview!.centerYAnchor).isActive = true + UIView.animate(withDuration: 2, animations: { + label.alpha = 0 + }) { (completed) in + label.removeFromSuperview() + } + } } 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 content = getData(for: indexPath!) + let provider = NSItemProvider(object: content! as NSString) let item = UIDragItem(itemProvider: provider) return [item] } From 5c2901fbfa7330e7c9ade6ddabc848765fdb67dd Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 14 Aug 2019 11:45:19 -0400 Subject: [PATCH 2/2] logic to work on small phones --- JSONCreator_iOS/JSONCreator/AppDelegate.swift | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/JSONCreator_iOS/JSONCreator/AppDelegate.swift b/JSONCreator_iOS/JSONCreator/AppDelegate.swift index 9591a37..364d09f 100644 --- a/JSONCreator_iOS/JSONCreator/AppDelegate.swift +++ b/JSONCreator_iOS/JSONCreator/AppDelegate.swift @@ -13,6 +13,10 @@ import MVMCoreUI 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 @@ -24,10 +28,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele // Override point for customization after application launch. let splitViewController = window!.rootViewController as! UISplitViewController - let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController - navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem + 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 + + 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 + } + } + } return true } @@ -55,12 +75,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele // 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 false + 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) + } + } }