forked from SwiftKickMobile/SwiftMessages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
221 lines (191 loc) · 9.09 KB
/
ViewController.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// ViewController.swift
// Demo
//
// Created by Tim Moose on 8/11/16.
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
//
import UIKit
import SwiftMessages
class ViewController: UITableViewController {
var items: [Item] = [
.titleBody(title: "MESSAGE VIEW", body: "SwiftMessages provides a standard message view along with a number of layouts, themes and presentation options.", function: ViewController.demoBasics),
.titleBody(title: "ANY VIEW", body: "Any view, no matter how cute, can be displayed as a message.", function: ViewController.demoAnyView),
.titleBody(title: "CUSTOMIZE", body: "Easily customize by copying one of the SwiftMessages nib files into your project as a starting point. Then order some tacos.", function: ViewController.demoCustomNib),
.explore,
.titleBody(title: "CENTERED", body: "Show cenetered messages with a fun, physics-based dismissal gesture.", function: ViewController.demoCentered),
.viewController,
//.counted,
]
/*
MARK: - UITableViewDataSource
*/
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[(indexPath as NSIndexPath).row]
return item.dequeueCell(tableView)
}
/*
MARK: - UITableViewDelegate
*/
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let item = items[(indexPath as NSIndexPath).row]
item.performDemo()
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 50.0
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
/*
MARK: - Demos
*/
static func demoBasics() -> Void {
let error = MessageView.viewFromNib(layout: .tabView)
error.configureTheme(.error)
error.configureContent(title: "Error", body: "Something is horribly wrong!")
error.button?.setTitle("Stop", for: .normal)
let warning = MessageView.viewFromNib(layout: .cardView)
warning.configureTheme(.warning)
warning.configureDropShadow()
let iconText = ["🤔", "😳", "🙄", "😶"].randomElement()!
warning.configureContent(title: "Warning", body: "Consider yourself warned.", iconText: iconText)
warning.button?.isHidden = true
var warningConfig = SwiftMessages.defaultConfig
warningConfig.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
let success = MessageView.viewFromNib(layout: .cardView)
success.configureTheme(.success)
success.configureDropShadow()
success.configureContent(title: "Success", body: "Something good happened!")
success.button?.isHidden = true
var successConfig = SwiftMessages.defaultConfig
successConfig.presentationStyle = .center
successConfig.presentationContext = .window(windowLevel: UIWindow.Level.normal)
let info = MessageView.viewFromNib(layout: .messageView)
info.configureTheme(.info)
info.button?.isHidden = true
info.configureContent(title: "Info", body: "This is a very lengthy and informative info message that wraps across multiple lines and grows in height as needed.")
var infoConfig = SwiftMessages.defaultConfig
infoConfig.presentationStyle = .bottom
infoConfig.duration = .seconds(seconds: 0.25)
let status = MessageView.viewFromNib(layout: .statusLine)
status.backgroundView.backgroundColor = UIColor.purple
status.bodyLabel?.textColor = UIColor.white
status.configureContent(body: "A tiny line of text covering the status bar.")
var statusConfig = SwiftMessages.defaultConfig
statusConfig.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
let status2 = MessageView.viewFromNib(layout: .statusLine)
status2.backgroundView.backgroundColor = UIColor.orange
status2.bodyLabel?.textColor = UIColor.white
status2.configureContent(body: "Switched to light status bar!")
var status2Config = SwiftMessages.defaultConfig
status2Config.presentationContext = .window(windowLevel: UIWindow.Level.normal)
status2Config.preferredStatusBarStyle = .lightContent
SwiftMessages.show(view: error)
SwiftMessages.show(config: warningConfig, view: warning)
SwiftMessages.show(config: successConfig, view: success)
SwiftMessages.show(config: infoConfig, view: info)
SwiftMessages.show(config: statusConfig, view: status)
SwiftMessages.show(config: status2Config, view: status2)
}
static func demoAnyView() -> Void {
let imageView = UIImageView()
imageView.image = UIImage(named: "puppies")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
let messageView = BaseView(frame: .zero)
messageView.layoutMargins = .zero
messageView.backgroundHeight = 120.0
do {
let backgroundView = CornerRoundingView()
backgroundView.cornerRadius = 15
backgroundView.layer.masksToBounds = true
messageView.installBackgroundView(backgroundView)
messageView.installContentView(imageView)
messageView.layoutMarginAdditions = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
messageView.configureDropShadow()
var config = SwiftMessages.defaultConfig
config.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
SwiftMessages.show(config: config, view: messageView)
}
static func demoCustomNib() {
let view: TacoDialogView = try! SwiftMessages.viewFromNib()
view.configureDropShadow()
view.getTacosAction = { _ in SwiftMessages.hide() }
view.cancelAction = { SwiftMessages.hide() }
var config = SwiftMessages.defaultConfig
config.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
config.duration = .forever
config.presentationStyle = .bottom
config.dimMode = .gray(interactive: true)
SwiftMessages.show(config: config, view: view)
}
static func demoCentered() {
let messageView: MessageView = MessageView.viewFromNib(layout: .centeredView)
messageView.configureBackgroundView(width: 250)
messageView.configureContent(title: "Hey There!", body: "Please try swiping to dismiss this message.", iconImage: nil, iconText: "🦄", buttonImage: nil, buttonTitle: "No Thanks") { _ in
SwiftMessages.hide()
}
messageView.backgroundView.backgroundColor = UIColor.init(white: 0.97, alpha: 1)
messageView.backgroundView.layer.cornerRadius = 10
var config = SwiftMessages.defaultConfig
config.presentationStyle = .center
config.duration = .forever
config.dimMode = .blur(style: .dark, alpha: 1, interactive: true)
config.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
SwiftMessages.show(config: config, view: messageView)
}
}
typealias Function = () -> Void
enum Item {
case titleBody(title: String, body: String, function: Function)
case explore
case counted
case viewController
func dequeueCell(_ tableView: UITableView) -> UITableViewCell {
switch self {
case .titleBody(let title, let body, _):
let cell = tableView.dequeueReusableCell(withIdentifier: "TitleBody") as! TitleBodyCell
cell.titleLabel.text = title
cell.bodyLabel.text = body
cell.configureBodyTextStyle()
return cell
case .explore:
let cell = tableView.dequeueReusableCell(withIdentifier: "Explore") as! TitleBodyCell
cell.configureBodyTextStyle()
return cell
case .counted:
let cell = tableView.dequeueReusableCell(withIdentifier: "Counted") as! TitleBodyCell
cell.configureBodyTextStyle()
cell.bodyLabel.configureCodeStyle(on: "show()")
cell.bodyLabel.configureCodeStyle(on: "hideCounted(id:)")
return cell
case .viewController:
let cell = tableView.dequeueReusableCell(withIdentifier: "ViewController") as! TitleBodyCell
cell.configureBodyTextStyle()
return cell
}
}
func performDemo() {
switch self {
case .titleBody(_, _, let function):
function()
default:
break
}
}
}
class TitleBodyCell: UITableViewCell {
@IBOutlet var titleLabel: UILabel!
@IBOutlet var bodyLabel: UILabel!
func configureBodyTextStyle() {
let bodyStyle = NSMutableParagraphStyle()
bodyStyle.lineSpacing = 5.0
bodyLabel.configureBodyTextStyle()
}
}