-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.swift
1203 lines (1007 loc) · 44.1 KB
/
Extension.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Extension.swift
// MovieApp_Ritesh
//
// Created by Susheel on 09/09/19.
// Copyright © 2019 openxcell. All rights reserved.
//
import UIKit
import AVKit
import Alamofire
import Foundation
import SwiftyJSON
import NVActivityIndicatorView
import Kingfisher
typealias ServiceResponse = (JSON, Error?) -> Void
// AppDelegate Shared Instance
var appDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
// Check Network Rechable
var isReachable: Bool {
return NetworkReachabilityManager()!.isReachable
}
/// App's name (if applicable).
public var appDisplayName: String? {
// http://stackoverflow.com/questions/28254377/get-app-name-in-swift
return Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String
}
/// Link of current app in appstore
public var appStoreLink: String {
return "https://itunes.apple.com/us/app/app-name/id1302095954?ls=1&mt=8"
}
/// Shared instance of current device.
public var currentDevice: UIDevice {
return UIDevice.current
}
// Current orientation of device.
public var deviceOrientation: UIDeviceOrientation {
return currentDevice.orientation
}
/// Screen width.
public var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}
/// Screen height.
public var screenHeight: CGFloat {
return UIScreen.main.bounds.height
}
/// App's bundle ID (if applicable).
public var appBundleID: String? {
return Bundle.main.bundleIdentifier
}
/// Application icon badge current number.
public var applicationIconBadgeNumber: Int {
get {
return UIApplication.shared.applicationIconBadgeNumber
}
set {
UIApplication.shared.applicationIconBadgeNumber = newValue
}
}
/// App's current version (if applicable).
public var appVersion: String {
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
}
/// Check if device is iPad.
public var isPad: Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}
/// Check if device is iPhone.
public var isPhone: Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}
/// Check if application is running on simulator (read-only).
public var isRunningOnSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
///Document directory
public var doumentDirectory:String {
return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}
/// Shared instance UIApplication.
public var sharedApplication: UIApplication {
return UIApplication.shared
}
func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel://\(phoneNumber)") {
if (sharedApplication.canOpenURL(phoneCallURL)) {
sharedApplication.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
// Get Latitude
var token:String {
return UserDefaults.standard.value(forKey: "token") as? String ?? ""
}
// Return TimeZone
var timezone: String {
return TimeZone.current.identifier
}
// User Default
var userPref: UserDefaults {
return UserDefaults.standard
}
var mainStoryboard: UIStoryboard {
return UIStoryboard(name: "Main", bundle: nil)
}
func getWidth(_ text: String, _ fontSize: CGFloat) -> CGFloat {
let label = UILabel()
label.numberOfLines = 0
label.font = UIFont(name: "Roboto-BoldCondensed", size: fontSize)
label.text = text
label.sizeToFit()
return label.frame.size.width
}
func getHeight(_ text: String, _ font: UIFont = UIFont(name: "Futura-Medium", size: 16)!) -> CGFloat {
let label = UILabel(frame: CGRect(x: 15, y: 0, width: screenWidth - 45, height: .greatestFiniteMagnitude))
label.font = font
label.text = text
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.sizeToFit()
return ceil(label.frame.size.height)
}
// Definition:
extension Notification.Name {
static let homePostNotification = Notification.Name("HomeReferesh")
static let buyPostNotification = Notification.Name("BuyReferesh")
}
extension Date {
func today() -> Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: Calendar.current.startOfDay(for: self)))!
}
/// Convert Date To String
public func toString(formates formate: String = "yyyy-MM-dd") -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = formate
return dateFormatter.string(from: self)
}
}
// Return LoggedIn userId
var isLoggedIn: Bool {
return userPref.object(forKey: "UserInfo") != nil
}
public func Log<T>(_ object: T?, filename: String = #file, line: Int = #line, funcname: String = #function) {
#if DEBUG
guard let object = object else { return }
print("***** \(Date()) \(filename.components(separatedBy: "/").last ?? "") (line: \(line)) :: \(funcname) :: \(object)")
#endif
}
extension UIViewController: NVActivityIndicatorViewable /*,MFMailComposeViewControllerDelegate */ {
//MARK:- Send Email
/* func sendEmail(_ subject:String,_ email:String) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients([email])
mail.setSubject(subject)
present(mail, animated: true)
} else {
// show failure alert
}
}
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
} */
func removeAllDeliverNotification() {
UIApplication.shared.applicationIconBadgeNumber = 1
UIApplication.shared.applicationIconBadgeNumber = 0
}
// Go Back Action
@IBAction func goBack(_ sender: UIButton) {
self.navigationController!.popViewController(animated: true)
}
@IBAction func showMenu(_ sender: UIButton) {
let sidemenu = mainStoryboard.instantiateViewController(withIdentifier: "LeftMenuNavigationController")
self.navigationController?.present(sidemenu, animated: true)
}
// Return AuthToken
func getToken() -> String {
let authToken = UserDefaults.standard.object(forKey: "token")
return (authToken == nil ? "" : authToken as! String)
}
// Return DeviceToken
func getDeviceToken() -> String {
let deviceToken = UserDefaults.standard.object(forKey: "DeviceToken")
return (deviceToken == nil ? "" : deviceToken as! String)
}
func showAlert(message: String){
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.self.present(alert, animated: true, completion: nil)
}
// Showing Toast Message
/* func showTostMessage(message: String, isSuccess:Bool = false) {
self.view.endEditing(true)
if message != "" {
ToastView.appearance().backgroundColor = isSuccess ? #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) : #colorLiteral(red: 0.8588235294, green: 0.3137254902, blue: 0.2901960784, alpha: 1)
ToastView.appearance().textColor = isSuccess ? #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) : #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
ToastView.appearance().font = UIFont.init(name: "SFProText-Regular", size: 17.0)
DispatchQueue.main.async {
Toast(text: message).show()
}
print("Working")
}
} */
func showConfirmation(title:String = "App Name",message:String,compleion:(() -> Void)?) {
if message != "" {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "YES", style: .default, handler: { (alert) in
guard compleion != nil else {
return
}
compleion!()
}))
alertController.addAction(UIAlertAction(title: "CANCEL", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
func randomString() -> String {
let len: Int = 3
let needle : NSString = "0123456789"
let randomString : NSMutableString = NSMutableString(capacity: len)
for _ in 0..<len {
let length = UInt32 (needle.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", needle.character(at: Int(rand)))
}
return randomString as String
}
// Show LoadingView When API is called
func showLoading(_ color: UIColor = #colorLiteral(red: 0.163174212, green: 0.2325206101, blue: 0.3331266046, alpha: 1)) {
let size = CGSize(width: 40, height:40)
startAnimating(size, message: nil, type: .ballClipRotate, color: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), backgroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0))
}
// Hide LoadingView
func hideLoading() {
stopAnimating()
}
func hideKeyboard() {
DispatchQueue.main.async {
self.view.endEditing(true)
}
}
// Listing of All Font Installed/Supported by System
func fontName() {
for family in UIFont.familyNames {
print("\(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print(" \(name)")
}
}
}
// Give Alpha Animation to the Selected View
func setAlphaAnimation(selectedView: UIView, alpha: CGFloat) {
if alpha == 1 {
selectedView.isHidden = false
}
UIView.animate(withDuration: 0.3, animations: { () -> Void in
selectedView.alpha = alpha
}) {
(complete) -> Void in
if alpha == 0 {
selectedView.isHidden = true
}
}
}
/// Check if device is registered for remote notifications for current app (read-only).
public static var isRegisteredForRemoteNotifications: Bool {
return UIApplication.shared.isRegisteredForRemoteNotifications
}
// Check Location is Allowed or Not
/* func isAllowLocation() -> Bool {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
return false
case .authorizedAlways, .authorizedWhenInUse:
return true
}
} */
//MARK: - WebService Call
func webServiceCall(_ url: String, param:[String:Any] = [String: Any](), isWithLoading: Bool = true, loaderColor: UIColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), imageKey: [String] = ["image"], imageData: [Data] = [],imageName:[String] = [], videoKey: [String] = ["video"], videoData: [Data] = [Data](), audioKey: [String] = ["audio"], audioData: [Data] = [Data](), isNeedToken: Bool = false, methods: HTTPMethod = .post, completionHandler:@escaping ServiceResponse) {
print("URL :- \(url)")
print("Parameter :- \(param)")
self.view.endEditing(true)
if isReachable {
if isWithLoading {
showLoading(loaderColor)
}
var headers = HTTPHeaders()
/* headers = [
"Content-Type": "application/json"
] */
if isNeedToken {
headers["Authorization"] = "Bearer \(token)"
}
print("HTTPHeaders :- \(headers) ")
if imageData.count > 0 || videoData.count > 0 || audioData.count > 0 {
Alamofire.upload (
multipartFormData: { multipartFormData in
for (key, value) in param {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
}
for i in 0..<imageData.count {
if imageData[i].count > 0 {
let fileName = imageName.count > i ? imageName[i]:"file[\(i)]"
multipartFormData.append(imageData[i], withName: imageKey[i], fileName: "\(fileName).jpg", mimeType: "image/jpeg")
}
}
for i in 0..<videoData.count {
if videoData[i].count > 0 {
multipartFormData.append(videoData[i], withName: videoKey[i], fileName: "file.mp4", mimeType: "video/mp4")
}
}
for i in 0..<audioData.count {
if audioData[i].count > 0 {
multipartFormData.append(audioData[i], withName: audioKey[i], fileName: "file.m4a", mimeType: "audio/m4a")
}
}
},
to: url,
headers : headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { result in
/*
print(result)
print(result.result)
*/
if let httpError = result.result.error {
print(NSString(data: result.data!, encoding: String.Encoding.utf8.rawValue)!)
print(httpError._code)
let response: [String: Any] = [
"errorCode": httpError._code,
"status": false,
"message": ValidationMessage.somethingwrong
]
let json = JSON(response)
completionHandler(json, nil)
print("JSON: - \(json)")
}
if result.result.isSuccess {
if let response = result.result.value {
let json = JSON(response)
completionHandler(json, nil)
print("JSON: - \(json)")
}
}
if isWithLoading {
self.hideLoading()
}
}
case .failure(let encodingError):
print(encodingError)
}
})
}
else
{
Alamofire.request(url, method: methods ,parameters: param,headers: headers)
.responseJSON { result in
/*
print(result)
print(result.result)
*/
if let httpError = result.result.error {
print(NSString(data: result.data!, encoding: String.Encoding.utf8.rawValue)!)
print(httpError._code)
let response: [String: Any] = [
"errorCode": httpError._code,
"status": false,
"message": ValidationMessage.somethingwrong
]
let json = JSON(response)
completionHandler(json, nil)
print("JSON: - \(json)")
}
if result.result.isSuccess {
if let response = result.result.value {
let json = JSON(response)
completionHandler(json, nil)
print("JSON: - \(json)")
}
}
if isWithLoading {
self.hideLoading()
}
}
}
}
else {
let response: [String: Any] = [
"errorCode": "",
"status": false,
"message": ValidationMessage.internetUnavailable
]
let json = JSON(response)
completionHandler(json, nil)
}
}
func webServiceRawDataCall(_ url: String, parameter:[String:Any] = [String: Any](),headers : HTTPHeaders = HTTPHeaders(), isWithLoading: Bool = true, imageKey: [String] = ["image"], imageData: [Data] = [Data](), videoKey: [String] = ["video"], videoData: [Data] = [Data](), audioKey: [String] = ["audio"], audioData: [Data] = [Data](), isNeedToken: Bool = true, methods: HTTPMethod = .post, completionHandler:@escaping ServiceResponse) {
let param = parameter
//let paramArray : [String] = []
print("URL :- \(url)")
print("Parameter :- \(param)")
print("Headers :- \(headers)")
hideKeyboard()
if isReachable {
if isWithLoading {
showLoading()
}
var headers = HTTPHeaders()
/* headers = [
"Content-Type": "application/json"
] */
if isNeedToken {
headers["Authorization"] = "Bearer \(token)" //(UserDefaults.standard.value(forKey: "access_token")!)"
}
print("HTTPHeaders :- \(headers) ")
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = HTTPMethod.post.rawValue
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let jsonData = try? JSONSerialization.data(withJSONObject:param, options: .prettyPrinted)
let json = NSString(data: jsonData!, encoding: String.Encoding.utf8.rawValue)
if let json = json {
print(json)
}
request.httpBody = jsonData//json!.data(using: String.Encoding.utf8.rawValue)
SessionManager.default.session.configuration.timeoutIntervalForRequest = 120
//Alamofire.sharedInstance.session.configuration.timeoutIntervalForRequest = 120
Alamofire.request(request)//(url, method: methods, parameters: param, encoding: JSONEncoding.default, headers: headers)
.responseJSON { result in
/*
print(result)
print(result.result)
*/
if isWithLoading {
self.hideLoading()
}
if let httpError = result.result.error {
print(NSString(data: result.data!, encoding: String.Encoding.utf8.rawValue)!)
print(httpError._code)
let response: [String: Any] = [
"errorCode": httpError._code,
"status": false,
"error_description": ValidationMessage.somethingwrong
]
let json = JSON(response)
completionHandler(json, nil)
//print("JSON: - \(json)")
}else if result.result.isSuccess {
if let response = result.result.value {
let json = JSON(response)
completionHandler(json, nil)
//print("JSON: - \(json)")
}
}
}
}
}
}
extension UITextView {
func padding() {
self.contentInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
}
}
extension UITextField {
func padding(width:Int = 12) {
let padding = UIView(frame: CGRect(x: 0, y: 5, width: 12, height: 12))
self.rightView = padding
self.rightViewMode = UITextField.ViewMode.always
self.leftView = padding
self.leftViewMode = UITextField.ViewMode.always
}
func setPlaceHolderTextColor(_ color: UIColor) {
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: color])
}
func placeholder(text value: String, color: UIColor = .red) {
self.attributedPlaceholder = NSAttributedString(string: value, attributes: [ NSAttributedString.Key.foregroundColor : color])
}
}
extension UISearchBar {
var textField: UITextField? {
return value(forKey: "searchField") as? UITextField
}
func setSearchIcon(image: UIImage) {
setImage(image, for: .search, state: .normal)
}
func setClearIcon(image: UIImage) {
setImage(image, for: .clear, state: .normal)
}
}
extension UIColor {
func toImage(size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
let rect:CGRect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
self.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image! // was image
}
convenience init(hex: String) {
let scanner = Scanner(string: hex)
scanner.scanLocation = 0
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(
red: CGFloat(r) / 0xff,
green: CGFloat(g) / 0xff,
blue: CGFloat(b) / 0xff, alpha: 1
)
}
}
extension UILabel {
var isTruncated: Bool {
guard let labelText = text else {
return false
}
let labelTextSize = (labelText as NSString).boundingRect(
with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [NSAttributedString.Key.font: font],
context: nil).size
return labelTextSize.height > bounds.size.height
}
func calculateMaxLines() -> Int {
let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity))
let charSize = font.lineHeight
let text = (self.text ?? "") as NSString
let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
let linesRoundedUp = Int(ceil(textSize.height/charSize))
return linesRoundedUp
}
func indexOfAttributedTextCharacterAtPoint(point: CGPoint) -> Int {
let textStorage = NSTextStorage(attributedString: self.attributedText!)
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: self.frame.size)
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = self.numberOfLines
textContainer.lineBreakMode = self.lineBreakMode
layoutManager.addTextContainer(textContainer)
let index = layoutManager.characterIndex(for: point, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return index
}
func getHeight(width:CGFloat) -> CGFloat {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = self.font
label.text = text
label.attributedText = attributedText
label.sizeToFit()
return label.frame.height
}
}
extension Float {
func makeCommaSeprator() -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSize = 3
return formatter.string(from: NSNumber(value: self)) ?? ""//NumberFormatter.localizedString(from: NSNumber(value: self), number: .decimal)
}
func makeAroundPointFifty() -> Float {
var amount = Float(Int(self))
if self > amount {
let valueAbove = self - amount
if valueAbove == 0.5 {
return self
} else if valueAbove > 0.5 {
amount += 1
} else {
amount += 0.50
}
}
return amount
}
func makeAroundPointFiftyLess() -> Float {
var amount = Float(Int(self))
if self > amount {
let valueAbove = self - amount
if valueAbove == 0.5 {
return self
} else if valueAbove > 0.5 {
amount += 0.5
}
}
return amount
}
}
extension String {
func containsOnlyDigits() -> Bool {
let notDigits = NSCharacterSet.decimalDigits.inverted
if rangeOfCharacter(from: notDigits, options: String.CompareOptions.literal, range: nil) == nil {
return true
}
return false
}
// Check for Password Validation
func isValidPassword() -> Bool {
let passwordRegEx = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$&*]).{8,}$"
let passwordValid = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
if passwordValid.evaluate(with: self) {
return true
}
return false
}
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
return ceil(boundingBox.height)
}
func isValidCapitalPassword() -> Bool {
let passwordRegEx = ".*[A-Z]+.*"
let passwordValid = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
return passwordValid.evaluate(with: self)
}
func isValidLowerPassword() -> Bool {
let passwordRegEx = ".*[A-Z]+.*"
let passwordValid = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
return passwordValid.evaluate(with: self)
}
func isValidNumberPassword() -> Bool {
let passwordRegEx = ".*[0-9]+.*"
let passwordValid = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
return passwordValid.evaluate(with: self)
}
func isValidSpecialCharPassword() -> Bool {
let passwordRegEx = ".*[!&^%$#@()/]+.*"
let passwordValid = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
return passwordValid.evaluate(with: self)
}
// Check for Valid Email Address
func isValidEmail() -> Bool {
let emailRegEx = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: self.trimming())
}
// Check for String is Empty
func isEmpty() -> Bool {
return self.trimming().isEmpty
}
// Return the string after trimming
func trimming() -> String {
let strText = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return strText
}
func toDate(_ format:String = "yyyy-MM-dd") -> Date {
let dateformat = DateFormatter()
dateformat.dateFormat = format
return dateformat.date(from: self) ?? Date()
}
var encodeEmoji: String? {
let encodedStr = NSString(cString: self.cString(using: String.Encoding.nonLossyASCII)!, encoding: String.Encoding.utf8.rawValue)
return encodedStr as String?
}
var decodeEmoji: String {
let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false)
if data != nil {
let valueUniCode = NSString(data: data!, encoding: String.Encoding.nonLossyASCII.rawValue) as String?
if valueUniCode != nil {
return valueUniCode!
} else {
return self
}
} else {
return self
}
}
func convertTimeStampToDate() -> Date {
let jsonDate = "/Date(\(self))/"
let prefix = "/Date("
let suffix = ")/"
let scanner = Scanner(string: jsonDate)
// Check prefix:
guard scanner.scanString(prefix, into: nil) else { return Date() }
// Read milliseconds part:
var milliseconds : Int64 = 0
guard scanner.scanInt64(&milliseconds) else { return Date() }
// Milliseconds to seconds:
var timeStamp = TimeInterval(milliseconds)/1000.0
// Read optional timezone part:
var timeZoneOffset : Int = 0
if scanner.scanInt(&timeZoneOffset) {
let hours = timeZoneOffset / 100
let minutes = timeZoneOffset % 100
// Adjust timestamp according to timezone:
timeStamp += TimeInterval(3600 * hours + 60 * minutes)
}
// Check suffix:
guard scanner.scanString(suffix, into: nil) else { return Date() }
// Success! Create NSDate and return.
return Date(timeIntervalSince1970: timeStamp)
}
}
extension UITableView {
// Set Text when no any Data found for TableView
func setTextForBlankTableview(message : String, color: UIColor = #colorLiteral(red: 0.2901960784, green: 0.2901960784, blue: 0.2901960784, alpha: 1),font:String = "Futura-Medium") -> Void {
let viewBg = UIView(frame: self.frame)
let messageLabel: UILabel = UILabel(frame: CGRect(x: 17, y: 0, width: self.frame.size.width-34, height: self.frame.size.height))
messageLabel.text = message
messageLabel.textColor = color
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFont.init(name: font, size: 17.0)
viewBg.addSubview(messageLabel)
self.backgroundView = viewBg
}
func setTextForBlankTableFooter(message : String, color: UIColor = #colorLiteral(red: 0.1294117647, green: 0.1294117647, blue: 0.1294117647, alpha: 1),height:CGFloat = 50) -> Void {
let messageLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: height))
messageLabel.text = message
messageLabel.textColor = color
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFont.init(name: "Futura-Medium", size: 17.0)
self.tableFooterView = messageLabel
}
// Set Loader in FooterView When pagination is enable
func makeFooterView(color: UIColor = #colorLiteral(red: 0.1098039216, green: 0.8078431373, blue: 0.8078431373, alpha: 1)) {
let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
let act = NVActivityIndicatorView(frame: CGRect(x: UIScreen.main.bounds.width / 2 - 15, y: 10, width: 30, height: 30))
act.color = color
act.type = .ballClipRotate
view.addSubview(act)
act.startAnimating()
self.tableFooterView = view
}
func makeHeaderView(color: UIColor = #colorLiteral(red: 0.1098039216, green: 0.8078431373, blue: 0.8078431373, alpha: 1)) {
let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
let act = NVActivityIndicatorView(frame: CGRect(x: UIScreen.main.bounds.width / 2 - 15, y: 10, width: 30, height: 30))
act.color = color
act.type = .ballClipRotate
view.addSubview(act)
act.startAnimating()
self.tableHeaderView = view
}
// Remove Footer View From Tableview
func removeFooterView() {
self.tableFooterView = UITableViewHeaderFooterView.init()
}
func removeHeaderView() {
self.tableHeaderView = UITableViewHeaderFooterView.init()
}
// Add Pull to Refresh
func addPullToRefresh(color: UIColor = #colorLiteral(red: 0.137254902, green: 0.6705882353, blue: 0.6431372549, alpha: 1)) -> UIRefreshControl {
let view = UIRefreshControl()
view.tintColor = color
self.addSubview(view)
return view
}
func extraOperation(_ msg : String? = "NO RECORD(S) FOUND",_ count:Int,_ isWhiteColor: Bool = false) {
removeFooterView()
if count == 0 {
if isWhiteColor {
setTextForBlankTableview(message: msg!,color: UIColor.white)
} else {
setTextForBlankTableview(message: msg!)
}
} else {
backgroundView = nil
}
reloadData()
}
func extraMessageOperation(_ count:Int) {
removeFooterView()
if count == 0 {
setTextForBlankTableview(message: "NO RECORD FOUND")
} else {
backgroundView = nil
}
reloadData()
}
}
extension UIImageView {
// Download image and set into given imageview
func setImage(image:String) {
if image != "" {
// let url = URL(string: "\(BasePath.ImagePath)\(image)")
let url = URL(string: image)
self.image = nil
self.kf.indicatorType = .activity
self.kf.setImage(with: url,placeholder: UIImage(named: "PlaceHolderImage"))
} else {
self.image = UIImage(named: "PlaceHolderImage")
}
}
}
extension UICollectionView {
func setTextForBlankCollectionView(message : String, color: UIColor = #colorLiteral(red: 0.1529411765, green: 0.2588235294, blue: 0.3529411765, alpha: 1)) -> Void {
let messageLabel: UILabel = UILabel(frame: CGRect(x: 17, y: 0, width: self.frame.size.width, height: self.frame.size.height))
messageLabel.text = message
messageLabel.textColor = color
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFont.init(name: "Futura-Medium", size: 17.0)
messageLabel.sizeToFit()
self.backgroundView = messageLabel
}
}
extension UIImage {
// Rotate Image by given Degree
public func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
//Calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let t: CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi / 180)
rotatedViewBox.transform = t
let rotatedSize: CGSize = rotatedViewBox.frame.size
//Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: rotatedSize.width / 2, y: rotatedSize.height / 2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat.pi / 180))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
bitmap.draw(self.cgImage!, in: CGRect(x: -self.size.width / 2, y: -self.size.height / 2, width: self.size.width, height: self.size.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()