-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigation.swift
More file actions
177 lines (150 loc) · 6.8 KB
/
Navigation.swift
File metadata and controls
177 lines (150 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Navigation.swift
// Navigation
//
// Created by Drouin on 02/04/2019.
// Copyright © 2019 VersusMind. All rights reserved.
//
import UIKit
// Alias
public let Navigation = NavigationSingleton.instance
public class NavigationSingleton {
// Singleton
internal static let instance = NavigationSingleton()
enum PlistKey: String {
case identifierNibName = "UIViewControllerIdentifiersToNibNames"
case entryPoint = "UIStoryboardDesignatedEntryPointIdentifier"
case externalReference = "UIViewControllerIdentifiersToExternalStoryboardReferences"
}
private var storyboards: [Storyboard]!
private init() {
storyboards = contructArrayStoryboard()
}
private func contructArrayStoryboard() -> [Storyboard] {
var storyboards = [Storyboard]()
var arrayName = [String]()
if let directory = Bundle.main.resourcePath {
// Storyboard file is in Base.lproj
let allResources = try! FileManager.default.contentsOfDirectory(atPath: directory)
// Filtered and add storyboard file
arrayName.append(contentsOf: allResources.filter({ $0.hasSuffix(".storyboardc" )}))
for name in arrayName {
let shortName = name.split(separator: ".")[0] // cut extension
storyboards += findAndParsePlist(in: "\(directory)/\(name)", shortName: String(shortName))
}
arrayName = [String]()
let lprojs = allResources.filter({ $0.hasSuffix(".lproj" )})
for lproj in lprojs {
let allResources = try! FileManager.default.contentsOfDirectory(atPath: "\(directory)/\(lproj)")
// Filtered and add storyboard file
arrayName.append(contentsOf: allResources.filter({ $0.hasSuffix(".storyboardc" )}))
for name in arrayName {
let shortName = name.split(separator: ".")[0]
storyboards += findAndParsePlist(in: "\(directory)/\(lproj)/\(name)", shortName: String(shortName))
}
}
}
return storyboards
}
// Search plist in "storyboardc", Parse and
// Return : [Storyboard]
private func findAndParsePlist(in storyboardName: String, shortName: String) -> [Storyboard] {
var storyboardArray = [Storyboard]()
let directory = "\(storyboardName)"
if let directory = URL(string: directory) {
guard let files = try? FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles) else {
return [Storyboard]()
}
let plists = files.filter({ $0.pathExtension == "plist"})
for plist in plists {
if let storyboard = parsePlist(file: plist.absoluteString, shortName: shortName) {
storyboardArray.append(storyboard)
}
}
}
return storyboardArray
}
private func parsePlist(file: String, shortName: String) -> Storyboard? {
var nsDictionary = NSDictionary()
if let url = URL(string: file) {
nsDictionary = NSDictionary(contentsOf: url) ?? NSDictionary()
let entryPoint: String = (nsDictionary.value(forKey: PlistKey.entryPoint.rawValue) as? String) ?? "none"
let identifiersNibName = (nsDictionary.value(forKey: PlistKey.identifierNibName.rawValue) as? Dictionary<String, String>) ?? Dictionary<String, String>()
let externalReference = (nsDictionary.value(forKey: PlistKey.externalReference.rawValue) as? Dictionary<String, Dictionary<String, String>>) ?? Dictionary<String, Dictionary<String, String>>()
var externalReferenceName = [(key: String, id: String)]()
for reference in externalReference {
for controller in reference.value {
externalReferenceName.append((key: reference.key, id: controller.value))
}
}
var arrayController = [(name: String, id: String)]()
for idDictionnary in identifiersNibName {
arrayController.append((name: idDictionnary.key, id: idDictionnary.key))
}
return Storyboard(fileName: shortName,
entryPoint: entryPoint,
arrayController: arrayController,
externalReferenceName: externalReferenceName)
}
return nil
}
// MARK : - Accessible method
// Return true if dictionnary contain controller Name
func controllerExist(_ id: String) -> Bool {
for storyboard in storyboards {
for controllersName in storyboard.arrayController where controllersName.id == id {
return true
}
}
return false
}
func getEntryPoint(inStoryboard name: String) -> String? {
for storyboard in storyboards where storyboard.fileName == name {
return storyboard.entryPoint
}
return nil
}
func getExternalStoryboardReference(inStoryboard name: String) -> [String] {
var arrayName = [String]()
for storyboard in storyboards where storyboard.fileName == name {
for reference in storyboard.externalReferenceName {
arrayName.append(reference.id)
}
}
return arrayName
}
func getViewController(_ id: String) -> UIViewController? {
for storyboard in storyboards {
for controller in storyboard.arrayController where controller.id == id {
let storyboard = UIStoryboard(name: storyboard.fileName, bundle: nil)
return storyboard.instantiateViewController(withIdentifier: controller.id)
}
}
return nil
}
func getAllStoryboardName() -> [String] {
var arrayNames = [String]()
for storyboard in storyboards {
arrayNames.append(storyboard.fileName)
}
return arrayNames
}
func getAllViewControllerId() -> [String] {
var arrayNames = [String]()
for storyboard in storyboards {
for controllerName in storyboard.arrayController {
arrayNames.append(controllerName.id)
}
}
return arrayNames
}
func getAllViewControllerId(inStoryboard name: String) -> [String] {
var arrayNames = [String]()
for storyboard in storyboards where storyboard.fileName == name {
for controllerName in storyboard.arrayController {
arrayNames.append(controllerName.id)
}
}
return arrayNames
}
}