-
Notifications
You must be signed in to change notification settings - Fork 30
/
AppDelegate.swift
159 lines (132 loc) · 5.27 KB
/
AppDelegate.swift
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
//
// AppDelegate.swift
// iOSStoryBookDemo
//
// Created by Jayakrishnan u on 9/3/20.
// Copyright © 2020 Jayakrishnan u. All rights reserved.
//
import UIKit
public enum StoryBoardType: String {
case Main
}
public enum DemoPageType: String {
case main,
label_control,
button,
floatingbutton,
label_inputs,
textfield,
slider,
label_communications,
spinner
}
public let menuItems: [String: String] = [
DemoPageType.main.rawValue: StoryBoardType.Main.rawValue
]
let isAppetize = "isAppetize"
let isDeveloper = "isDeveloper"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
static let bundleID = "com.test.iOSStoryBookDemo"
static let launch_option = "launch_option"
static let idsSwitch = "switch"
static let textArea = "text_area"
static let navigation = "navigation"
enum DemoAppType {
case developer, gallery
}
var window: UIWindow?
static func fromAppetize() -> Bool {
return UserDefaults.standard.bool(forKey: isAppetize)
}
static func fromDeveloper() -> Bool {
return UserDefaults.standard.bool(forKey: isDeveloper)
}
static func setGalleryDemoAppUserDefaults() {
UserDefaults.standard.set(true, forKey: isAppetize)
UserDefaults.standard.set(DemoPageType.main.rawValue, forKey: launch_option)
}
static func resetGalleryDemoApppUserDefaults() {
UserDefaults.standard.removeObject(forKey: isAppetize)
UserDefaults.standard.removeObject(forKey: launch_option)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let launchOption: String = UserDefaults.standard.string(forKey: AppDelegate.launch_option) ?? ""
if launchOption.lowercased() == DemoPageType.main.rawValue {
UserDefaults.standard.set(true, forKey: isDeveloper)
launchDemoPage( DemoAppType.developer, DemoPageType.main, [:])
} else if AppDelegate.fromAppetize() {
launchGalleryFromAppetize(launchOption, [:])
} else {
UserDefaults.standard.set(true, forKey: isDeveloper)
}
if #available(iOS 13.0, *) {
UINavigationBar.appearance().barTintColor = .systemBackground
}
return true
}
func application(_ app: UIApplication, open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if let scheme = url.scheme,
scheme.localizedCaseInsensitiveCompare("sb-native") == .orderedSame,
let view = url.host {
var parameters: [String: String] = [:]
URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
parameters[$0.name] = $0.value
}
launchGalleryFromAppetize(parameters["component"]!, parameters)
}
return true
}
func launchGalleryFromAppetize(_ launchOption: String, _ params: [String: String]) {
var launchOption = launchOption
if launchOption.isEmpty {
launchOption = DemoPageType.main.rawValue
}
if let pageType = DemoPageType(rawValue: launchOption.lowercased()) {
launchDemoPage( DemoAppType.gallery, pageType, params)
} else {
launchDemoPage( DemoAppType.gallery, DemoPageType.main, params)
}
}
func launchDemoAppMenu(_ demoAppType: DemoAppType) {
switch demoAppType {
case .gallery:
AppDelegate.setGalleryDemoAppUserDefaults()
launchDemoPage(DemoAppType.gallery, DemoPageType.main, [:])
default:
AppDelegate.resetGalleryDemoApppUserDefaults()
launchDemoPage( DemoAppType.developer, DemoPageType.main, [:])
}
}
func launchDemoPage(_ demoAppType: DemoAppType, _ demoPage: DemoPageType, _ params: [String: String]) {
let nvc = navigationController(demoAppType)
let menuVC = viewController(DemoPageType.main)
let VCToLaunch = viewController(demoPage)
if let controlsProtocol = VCToLaunch as? StorybookControlsProtocol {
controlsProtocol.loadControls(params)
}
switch demoAppType {
default:
if demoPage != DemoPageType.main {
nvc.pushViewController(menuVC, animated: false)
}
}
nvc.pushViewController(VCToLaunch, animated: false)
window?.rootViewController = nvc
}
func storyboardName(_ demoAppType: DemoAppType) -> String {
switch demoAppType {
default:
return StoryBoardType.Main.rawValue
}
}
func navigationController(_ demoAppType: DemoAppType) -> UINavigationController {
let storyboard = UIStoryboard(name: storyboardName(demoAppType), bundle: nil)
return storyboard.instantiateViewController(withIdentifier: AppDelegate.navigation) as! UINavigationController
}
func viewController( _ demoPage: DemoPageType) -> UIViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: demoPage.rawValue)
}
}