-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelpfulExtensions.swift
466 lines (404 loc) · 18.8 KB
/
HelpfulExtensions.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// HelpfulExtensions.swift
// Created by Mark McCorkle
// Copyright ® 2017. All rights reserved.
//
import UIKit
import CoreData
//MARK: - String
extension String {
// Various String helpers and conversions
func length() -> Int {
return self.characters.count
}
func trim() -> String {
return self.trimmingCharacters(in: NSMutableCharacterSet.whitespaceAndNewline() as CharacterSet)
}
func substring(_ location:Int, length:Int) -> String! {
return (self as NSString).substring(with: NSMakeRange(location, length))
}
subscript(index: Int) -> String! {
get {
return self.substring(index, length: 1)
}
}
func location(_ other: String) -> Int {
return (self as NSString).range(of: other).location
}
func contains(_ other: String) -> Bool {
return (self as NSString).contains(other)
}
func isNumeric() -> Bool {
return (self as NSString).rangeOfCharacter(from: CharacterSet.decimalDigits.inverted).location == NSNotFound
}
var numbersOnly:String {
return self.components(separatedBy: CharacterSet(charactersIn: "1234567890").inverted).joined(separator: "")
}
var numbersWithDecimalOnly:String {
return self.components(separatedBy: CharacterSet(charactersIn: "1234567890.").inverted).joined(separator: "")
}
var decimalNumberValue:NSDecimalNumber {
if NSString(string:self).doubleValue > 0 {
return NSDecimalNumber(value: NSString(string:self).doubleValue as Double).rounding(accordingToBehavior: NSDecimalNumberHandler(roundingMode: NSDecimalNumber.RoundingMode.bankers, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false))
} else {
return NSDecimalNumber.zero
}
}
var currencyValue:String {
var number = self.numbersWithDecimalOnly
if number.isEmpty {
number = "0"
}
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.currency
formatter.locale = Locale.current
let newValue = formatter.string(from: NSDecimalNumber(string: number)) ?? ""
return newValue
}
var numbersExempt:String {
return self.components(separatedBy: CharacterSet(charactersIn: "1234567890")).joined(separator: "")
}
var charactersOnly:String {
return self.components(separatedBy: CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").inverted).joined(separator: "")
}
var charactersExempt:String {
return self.components(separatedBy: CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")).joined(separator: "")
}
func keep(_ keepIt: String) -> String {
return self.components(separatedBy: CharacterSet(charactersIn: keepIt).inverted).joined(separator: "")
}
func exclude(_ excludeIt: String) -> String {
return self.components(separatedBy: CharacterSet(charactersIn: excludeIt)).joined(separator: "")
}
var removeSpecialCharsFromString:String {
let okayChars : Set<Character> = Set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890+-*=(),.:!_".characters)
return String(self.characters.filter {okayChars.contains($0) })
}
var unescaped: String {
let entities = ["\0", "\t", "\n", "\r", "\"", "\'", "\\"]
var current = self
for entity in entities {
let descriptionCharacters = entity.debugDescription.characters.dropFirst().dropLast()
let description = String(descriptionCharacters)
current = current.replacingOccurrences(of: description, with: entity)
}
return current
}
}
// MARK: - Core Data
extension NSManagedObject {
// Used for prior versions of swift which did not authomatically create these relationships
func addObject(_ value: NSManagedObject, forKey: String) {
let items = self.mutableSetValue(forKey: forKey);
items.add(value)
}
}
//MARK: - UIViewController
extension UIViewController {
// Delay a block of code for a given amount of time
func delay(_ delay:Double, closure:@escaping ()->()) {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}
// Display a quick simple alert even if another is already being displayed
func displaySimpleAlert(_ title:String,message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil))
self.presentViewControllerWithoutConflict(alert, animated: true, completion: { (result) in
//
})
}
// Present a view regardless of whats beinh displayed currently
func presentViewControllerWithoutConflict(_ alert:UIViewController, animated:Bool, completion: @escaping (_ result: Bool)->()) {
DispatchQueue.main.async { () -> Void in
if let currentVisibleController = UIApplication.topViewController() {
print("Current visibleViewControler = \(currentVisibleController.classForCoder)")
if currentVisibleController.isKind(of: UIAlertController.self) {
print("Current viewcontroller is type UIAlertController")
let presentingViewController = currentVisibleController.presentingViewController
presentingViewController?.dismiss(animated: true, completion: { () -> Void in
presentingViewController?.present(alert, animated: animated, completion: { () -> Void in
completion(true)
})
})
} else if currentVisibleController.tabBarController != nil {
currentVisibleController.tabBarController?.present(alert, animated: animated, completion: { () -> Void in
completion(true)
})
} else {
currentVisibleController.present(alert, animated: animated, completion: { () -> Void in
completion(true)
})
}
} else {
print("PROBLEM!!!")
completion(true)
}
}
}
}
// MARK: - UITableView
extension UITableView {
// Get the indexPath for a given view
func indexPathForView (_ view : UIView) -> IndexPath? {
let location = view.convert(CGPoint.zero, to:self)
return indexPathForRow(at: location)
}
}
// MARK: - NSMutableURLRequest
extension NSMutableURLRequest {
// Set a nicely formatted body to an existed request
func setBodyContent(contentMap: Dictionary<String, String>) {
var firstOneAdded = false
let contentKeys:Array<String> = Array(contentMap.keys)
var contentBodyAsString = ""
for contentKey in contentKeys {
if(!firstOneAdded) {
contentBodyAsString += contentKey + "=" + contentMap[contentKey]!
firstOneAdded = true
}
else {
contentBodyAsString += "&" + contentKey + "=" + contentMap[contentKey]!
}
}
contentBodyAsString = contentBodyAsString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let postData = contentBodyAsString.data(using: String.Encoding.ascii)!
let postLength = "\(postData.count)"
self.setValue(postLength, forHTTPHeaderField: "Content-Length")
self.httpBody = postData
}
}
//MARK: - Date
// Compare dates with < or ==
public func <(a: Date, b: Date) -> Bool {
return a.compare(b) == ComparisonResult.orderedAscending
}
public func ==(a: Date, b: Date) -> Bool {
return a.compare(b) == ComparisonResult.orderedSame
}
//MARK: - NSDate
extension NSDate {
// Format existing date to expected postgres date format
func convertToPostgresDate() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateString = formatter.string(from: self as Date)
return dateString
}
}
extension NSDecimalNumber {
// Return value in US currency format
func currencyStringValue() -> String {
if self > 0 {
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.currency
formatter.locale = Locale.current
let newValue = formatter.string(from: self) ?? ""
return newValue
}
return "$0.00"
}
}
//MARK: - UIImage
extension UIImage {
// Resizing images
func resizeWith(percentage: CGFloat) -> UIImage? {
let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage)))
imageView.contentMode = .scaleAspectFit
imageView.image = self
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
imageView.layer.render(in: context)
guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
UIGraphicsEndImageContext()
return result
}
func resizeWith(width: CGFloat, height: CGFloat) -> UIImage? {
let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: width, height: height)))
imageView.image = self
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
imageView.layer.render(in: context)
guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
UIGraphicsEndImageContext()
return result
}
func resize(size:CGSize, completionHandler:@escaping (UIImage, Foundation.Data)->()) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async(execute: { () -> Void in
let newSize:CGSize = size
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageData = UIImageJPEGRepresentation(newImage!, 0.5)
DispatchQueue.main.async(execute: { () -> Void in
completionHandler(newImage!, imageData!)
})
})
}
// A quick grayscale conversion
func convertToGrayScale() -> UIImage {
let imageRect:CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let width = self.size.width
let height = self.size.height
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
context?.draw(self.cgImage!, in: imageRect)
let imageRef = context?.makeImage()
let newImage = UIImage(cgImage: imageRef!)
return newImage
}
// Convert the image to 1bit black and white
func blackAndWhiteImage() -> UIImage {
let context = CIContext(options: nil)
let ciImage = CoreImage.CIImage(image: self)!
// Set image color to b/w
let bwFilter = CIFilter(name: "CIColorControls")!
bwFilter.setValuesForKeys([kCIInputImageKey:ciImage, kCIInputBrightnessKey:NSNumber(value: 0.0 as Float), kCIInputContrastKey:NSNumber(value: 1.1 as Float), kCIInputSaturationKey:NSNumber(value: 0.0 as Float)])
let bwFilterOutput = (bwFilter.outputImage)!
// Adjust exposure
let exposureFilter = CIFilter(name: "CIExposureAdjust")!
exposureFilter.setValuesForKeys([kCIInputImageKey:bwFilterOutput, kCIInputEVKey:NSNumber(value: 0.7 as Float)])
let exposureFilterOutput = (exposureFilter.outputImage)!
// Create UIImage from context
let bwCGIImage = context.createCGImage(exposureFilterOutput, from: ciImage.extent)
let resultImage = UIImage(cgImage: bwCGIImage!, scale: 1.0, orientation: self.imageOrientation)
return resultImage
}
}
//MARK: - UIButton
extension UIButton {
// Set the background color of a UIButton easily use a calculated background image
fileprivate func imageWithColor(_ color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
func setBackgroundColor(_ color: UIColor, forUIControlState state: UIControlState) {
self.setBackgroundImage(imageWithColor(color), for: state)
}
//
}
//MARK: - UIDevice
public extension UIDevice {
// Get readable device names
var modelName: String {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8 , value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
switch identifier {
case "iPod5,1": return "iPod Touch 5"
case "iPod7,1": return "iPod Touch 6"
case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
case "iPhone4,1": return "iPhone 4s"
case "iPhone5,1", "iPhone5,2": return "iPhone 5"
case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
case "iPhone7,2": return "iPhone 6"
case "iPhone7,1": return "iPhone 6 Plus"
case "iPhone8,1": return "iPhone 6s"
case "iPhone8,2": return "iPhone 6s Plus"
case "iPhone9,1", "iPhone9,3": return "iPhone 7"
case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
case "iPhone8,4": return "iPhone SE"
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
case "iPad5,3", "iPad5,4": return "iPad Air 2"
case "iPad6,11", "iPad6,12": return "iPad 5"
case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
case "iPad5,1", "iPad5,2": return "iPad Mini 4"
case "iPad6,3", "iPad6,4": return "iPad Pro 9.7 Inch"
case "iPad6,7", "iPad6,8": return "iPad Pro 12.9 Inch"
case "iPad7,1", "iPad7,2": return "iPad Pro 12.9 Inch 2. Generation"
case "iPad7,3", "iPad7,4": return "iPad Pro 10.5 Inch"
case "AppleTV5,3": return "Apple TV"
case "i386", "x86_64": return "Simulator"
default: return identifier
}
}
}
//MARK: - Dictionary
extension UIApplication {
// Check if location serveices are enabled
func locationServicesEnabled() -> Bool {
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
print("No access")
return false
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
return true
}
} else {
print("Location services are not enabled")
return false
}
}
// Returns the actual top view controller useful for determining if alerts and such are displayed
class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
//MARK: - NSDecimalNumber
extension NSDecimalNumber: Comparable {}
public func ==(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .orderedSame
}
public func <(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .orderedAscending
}
public func >(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .orderedDescending
}
public func <=(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .orderedAscending || lhs.compare(rhs) == .orderedSame
}
public func >=(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .orderedDescending || lhs.compare(rhs) == .orderedSame
}
// MARK: - Arithmetic Operators
public prefix func -(value: NSDecimalNumber) -> NSDecimalNumber {
return value.multiplying(by: NSDecimalNumber(mantissa: 1, exponent: 0, isNegative: true))
}
public func +(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> NSDecimalNumber {
return lhs.adding(rhs)
}
public func -(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> NSDecimalNumber {
return lhs.subtracting(rhs)
}
public func *(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> NSDecimalNumber {
return lhs.multiplying(by: rhs)
}
public func /(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> NSDecimalNumber {
return lhs.dividing(by: rhs)
}
public func ^(lhs: NSDecimalNumber, rhs: Int) -> NSDecimalNumber {
return lhs.raising(toPower: rhs)
}