Skip to content

Commit f1aad93

Browse files
committed
Add common UIKit and Foundation extensions
1 parent 526a9c6 commit f1aad93

File tree

9 files changed

+247
-43
lines changed

9 files changed

+247
-43
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// String+Localized.swift
3+
// ExamEGERussian
4+
//
5+
// Created by Ivan Magda on 19/07/2018.
6+
// Copyright © 2018 Alex Karpov. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension String {
12+
var localized: String {
13+
return NSLocalizedString(self, comment: "")
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// UITableView+Registration.swift
3+
// ExamEGERussian
4+
//
5+
// Created by Ivan Magda on 19/07/2018.
6+
// Copyright © 2018 Alex Karpov. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UITableView {
12+
func registerNib(for cellClass: UITableViewCell.Type) {
13+
register(cellClass.nib, forCellReuseIdentifier: cellClass.identifier)
14+
}
15+
16+
func registerHeaderNib(for headerClass: UITableViewHeaderFooterView.Type) {
17+
register(headerClass.nib, forHeaderFooterViewReuseIdentifier: headerClass.identifier)
18+
}
19+
20+
func registerClass(for cellClass: UITableViewCell.Type) {
21+
register(cellClass, forCellReuseIdentifier: cellClass.identifier)
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// UITableView+Reuse.swift
3+
// ExamEGERussian
4+
//
5+
// Created by Ivan Magda on 19/07/2018.
6+
// Copyright © 2018 Alex Karpov. All rights reserved.
7+
//
8+
9+
import UIKit.UITableView
10+
11+
extension UITableView {
12+
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
13+
let identifier = T.identifier
14+
return dequeueReusableCell(withIdentifier: identifier,
15+
for: indexPath) as! T // swiftlint:disable:this force_cast
16+
}
17+
18+
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>() -> T {
19+
let identifier = T.identifier
20+
return dequeueReusableHeaderFooterView(withIdentifier: identifier) as! T // swiftlint:disable:this force_cast
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// UITableViewCell+ReusableCell.swift
3+
// ExamEGERussian
4+
//
5+
// Created by Ivan Magda on 19/07/2018.
6+
// Copyright © 2018 Alex Karpov. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
import UIKit
12+
13+
protocol ReusableCell {
14+
static var identifier: String { get }
15+
static var nib: UINib { get }
16+
}
17+
18+
extension UITableViewCell: ReusableCell {
19+
static var identifier: String {
20+
return String(describing: self)
21+
}
22+
23+
static var nib: UINib {
24+
return UINib(nibName: identifier, bundle: nil)
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// UITableViewHeaderFooterView+ReusableCell.swift
3+
// ExamEGERussian
4+
//
5+
// Created by Ivan Magda on 19/07/2018.
6+
// Copyright © 2018 Alex Karpov. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension UITableViewHeaderFooterView: ReusableCell {
12+
static var identifier: String {
13+
return String(describing: self)
14+
}
15+
16+
static var nib: UINib {
17+
return UINib(nibName: identifier, bundle: nil)
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// UIViewController+Alert.swift
3+
// ExamEGERussian
4+
//
5+
// Created by Ivan Magda on 19/07/2018.
6+
// Copyright © 2018 Alex Karpov. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIViewController {
12+
13+
func presentAlert(withTitle title: String?, message: String? = nil,
14+
actionTitle: String? = "Ок".localized,
15+
action actionCallback: (() -> Void)? = nil) {
16+
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
17+
alert.addAction(UIAlertAction(title: actionTitle, style: .cancel) { _ in
18+
actionCallback?()
19+
})
20+
present(alert, animated: true, completion: nil)
21+
}
22+
23+
func presentConfirmationAlert(withTitle title: String?, message: String? = nil,
24+
buttonFirstTitle: String? = "Ок".localized,
25+
buttonSecondTitle: String? = "Cancel".localized,
26+
firstAction: (() -> Void)? = nil, secondAction: (() -> Void)? = nil) {
27+
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
28+
alert.addAction(UIAlertAction(title: buttonFirstTitle, style: .default) { _ in
29+
firstAction?()
30+
})
31+
alert.addAction(UIAlertAction(title: buttonSecondTitle, style: .default) { _ in
32+
secondAction?()
33+
})
34+
present(alert, animated: true, completion: nil)
35+
}
36+
37+
}

ExamEGERussian/Sources/Helpers/RandomCredentialsGenerator/RandomCredentialsGenerator.swift renamed to ExamEGERussian/Sources/Common/Helpers/RandomCredentialsGenerator/RandomCredentialsGenerator.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
import Foundation
1010

1111
protocol RandomCredentialsGenerator {
12-
1312
var firstname: String { get }
14-
1513
var lastname: String { get }
16-
1714
var email: String { get }
18-
1915
var password: String { get }
20-
2116
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct RandomCredentialsGeneratorImplementation {
1212
}
1313

1414
extension RandomCredentialsGeneratorImplementation: RandomCredentialsGenerator {
15-
1615
var firstname: String {
1716
return StringHelper.generateRandomString(of: 6)
1817
}
@@ -28,5 +27,4 @@ extension RandomCredentialsGeneratorImplementation: RandomCredentialsGenerator {
2827
var password: String {
2928
return StringHelper.generateRandomString(of: 16)
3029
}
31-
3230
}

0 commit comments

Comments
 (0)