Skip to content

Commit 80dc20b

Browse files
Shardul PatelShardul Patel
Shardul Patel
authored and
Shardul Patel
committed
1. CFAlertViewController class made open so that it devs can subclass
2. Init method support added
1 parent e18aea6 commit 80dc20b

File tree

2 files changed

+63
-45
lines changed

2 files changed

+63
-45
lines changed

CFAlertViewController/CFAlertViewController.swift

+63-45
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UIKit
1010

1111

1212
@objc(CFAlertViewController)
13-
public class CFAlertViewController: UIViewController {
13+
open class CFAlertViewController: UIViewController {
1414

1515
// MARK: - Declarations
1616
public typealias CFAlertViewControllerDismissBlock = (_ isBackgroundTapped: Bool) -> ()
@@ -24,13 +24,13 @@ public class CFAlertViewController: UIViewController {
2424
case plain = 0
2525
case blur
2626
}
27-
public static func CF_ALERT_DEFAULT_BACKGROUND_COLOR() -> UIColor {
27+
open static func CF_ALERT_DEFAULT_BACKGROUND_COLOR() -> UIColor {
2828
return UIColor(white: 0.0, alpha: 0.7)
2929
}
30-
public static func CF_ALERT_DEFAULT_TITLE_COLOR() -> UIColor {
30+
open static func CF_ALERT_DEFAULT_TITLE_COLOR() -> UIColor {
3131
return UIColor.init(red: 1.0/255.0, green: 51.0/255.0, blue: 86.0/255.0, alpha: 1.0)
3232
}
33-
public static func CF_ALERT_DEFAULT_MESSAGE_COLOR() -> UIColor {
33+
open static func CF_ALERT_DEFAULT_MESSAGE_COLOR() -> UIColor {
3434
return UIColor.init(red: 1.0/255.0, green: 51.0/255.0, blue: 86.0/255.0, alpha: 1.0)
3535
}
3636

@@ -155,25 +155,6 @@ public class CFAlertViewController: UIViewController {
155155
didDismissAlertHandler: dismiss)
156156
}
157157

158-
public class func alertController(title: String?,
159-
message: String?,
160-
textAlignment: NSTextAlignment,
161-
preferredStyle: CFAlertControllerStyle,
162-
headerView: UIView?,
163-
footerView: UIView?,
164-
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {
165-
166-
return CFAlertViewController.alertController(title: title,
167-
titleColor: nil,
168-
message: message,
169-
messageColor: nil,
170-
textAlignment: textAlignment,
171-
preferredStyle: preferredStyle,
172-
headerView: headerView,
173-
footerView: footerView,
174-
didDismissAlertHandler: dismiss)
175-
}
176-
177158
public class func alertController(title: String?,
178159
titleColor: UIColor?,
179160
message: String?,
@@ -184,47 +165,84 @@ public class CFAlertViewController: UIViewController {
184165
footerView: UIView?,
185166
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) -> CFAlertViewController {
186167

168+
// Create New Instance Of Alert Controller
169+
return CFAlertViewController.init(title: title,
170+
titleColor: titleColor,
171+
message: message,
172+
messageColor: messageColor,
173+
textAlignment: textAlignment,
174+
preferredStyle: preferredStyle,
175+
headerView: headerView,
176+
footerView: footerView,
177+
didDismissAlertHandler: dismiss)
178+
}
179+
180+
public convenience init(title: String?,
181+
message: String?,
182+
textAlignment: NSTextAlignment,
183+
preferredStyle: CFAlertControllerStyle,
184+
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) {
185+
186+
// Create New Instance Of Alert Controller
187+
self.init(title: title,
188+
titleColor: nil,
189+
message: message,
190+
messageColor: nil,
191+
textAlignment: textAlignment,
192+
preferredStyle: preferredStyle,
193+
headerView: nil,
194+
footerView: nil,
195+
didDismissAlertHandler: dismiss)
196+
}
197+
198+
public convenience init(title: String?,
199+
titleColor: UIColor?,
200+
message: String?,
201+
messageColor: UIColor?,
202+
textAlignment: NSTextAlignment,
203+
preferredStyle: CFAlertControllerStyle,
204+
headerView: UIView?,
205+
footerView: UIView?,
206+
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?) {
207+
187208
// Get Current Bundle
188209
let bundle = Bundle(for: CFAlertViewController.self)
189210

190211
// Create New Instance Of Alert Controller
191-
let alert = CFAlertViewController.init(nibName: "CFAlertViewController", bundle: bundle)
212+
self.init(nibName: "CFAlertViewController", bundle: bundle)
192213

193214
// Assign Properties
194-
alert.preferredStyle = preferredStyle
195-
alert.backgroundStyle = .plain
196-
alert.backgroundColor = CF_ALERT_DEFAULT_BACKGROUND_COLOR()
197-
alert.titleString = title
215+
self.preferredStyle = preferredStyle
216+
backgroundStyle = .plain
217+
backgroundColor = CFAlertViewController.CF_ALERT_DEFAULT_BACKGROUND_COLOR()
218+
titleString = title
198219
if let titleColor = titleColor {
199-
alert.titleColor = titleColor
220+
self.titleColor = titleColor
200221
}
201222

202-
alert.messageString = message
223+
messageString = message
203224
if let messageColor = messageColor {
204-
alert.messageColor = messageColor
225+
self.messageColor = messageColor
205226
}
206227

207-
alert.textAlignment = textAlignment
208-
alert.setHeaderView(headerView, shouldUpdateContainerFrame: false, withAnimation: false)
209-
alert.setFooterView(footerView, shouldUpdateContainerFrame: false, withAnimation: false)
210-
alert.dismissHandler = dismiss
228+
self.textAlignment = textAlignment
229+
setHeaderView(headerView, shouldUpdateContainerFrame: false, withAnimation: false)
230+
setFooterView(footerView, shouldUpdateContainerFrame: false, withAnimation: false)
231+
dismissHandler = dismiss
211232

212233
// Custom Presentation
213-
alert.modalPresentationStyle = .custom
214-
alert.transitioningDelegate = alert
234+
modalPresentationStyle = .custom
235+
transitioningDelegate = self
215236

216237
// Preload View
217238
if #available(iOS 9.0, *) {
218-
alert.loadViewIfNeeded()
239+
loadViewIfNeeded()
219240
} else {
220241
// Fallback on earlier versions
221242
}
222-
223-
return alert
224243
}
225244

226245

227-
228246
// MARK: - View Life Cycle Methods
229247
internal func loadVariables() {
230248

@@ -264,7 +282,7 @@ public class CFAlertViewController: UIViewController {
264282
view.addGestureRecognizer(self.tapGesture)
265283
}
266284

267-
public override func viewDidLoad() {
285+
open override func viewDidLoad() {
268286
super.viewDidLoad()
269287

270288
// Load Variables
@@ -274,7 +292,7 @@ public class CFAlertViewController: UIViewController {
274292
loadDisplayContent()
275293
}
276294

277-
public override func viewWillAppear(_ animated: Bool) {
295+
open override func viewWillAppear(_ animated: Bool) {
278296
super.viewWillAppear(animated)
279297

280298
// Update UI
@@ -481,7 +499,7 @@ public class CFAlertViewController: UIViewController {
481499

482500

483501
// MARK: - View Rotation / Size Change Method
484-
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
502+
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
485503
super.viewWillTransition(to: size, with: coordinator)
486504
// Code here will execute before the rotation begins.
487505
// Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]
@@ -498,7 +516,7 @@ public class CFAlertViewController: UIViewController {
498516

499517

500518
// MARK: - Key Value Observers
501-
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
519+
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
502520
if (keyPath == "contentSize") {
503521
// Update Container View Frame Without Animation
504522
updateContainerViewFrame(withAnimation: false)

0 commit comments

Comments
 (0)