Skip to content

Commit 35e33b7

Browse files
authored
Merge pull request #85 from hyperoslo/improve/child-view-controllers
Refactoring: introduce header view controller
2 parents 3617d6c + c172bee commit 35e33b7

File tree

11 files changed

+668
-14
lines changed

11 files changed

+668
-14
lines changed

BarcodeScanner.xcodeproj/project.pbxproj

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
D50BE3E91C9FE7A80000A34C /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D50BE3E51C9FE7A80000A34C /* [email protected] */; };
1313
D50BE3EA1C9FE7A80000A34C /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D50BE3E61C9FE7A80000A34C /* [email protected] */; };
1414
D50BE3EB1C9FE7A80000A34C /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D50BE3E71C9FE7A80000A34C /* [email protected] */; };
15+
D55281B62016758F00FF3CDD /* HeaderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55281B52016758F00FF3CDD /* HeaderViewController.swift */; };
16+
D55281B8201675D500FF3CDD /* MessageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55281B7201675D500FF3CDD /* MessageViewController.swift */; };
17+
D55281BA2016770800FF3CDD /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55281B92016770800FF3CDD /* SettingsViewController.swift */; };
18+
D55281BC2016782C00FF3CDD /* ScannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55281BB2016782C00FF3CDD /* ScannerViewController.swift */; };
19+
D55281BF20167DB400FF3CDD /* UIViewController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55281BE20167DB400FF3CDD /* UIViewController+Extensions.swift */; };
1520
D5C4E08E1CA0BFB9008D9269 /* InfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */; };
1621
D5C4E08F1CA0BFB9008D9269 /* TorchMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */; };
1722
D5F1C1C91C9C5113001E17A6 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F1C1C51C9C5113001E17A6 /* Config.swift */; };
@@ -26,6 +31,11 @@
2631
D50BE3E51C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
2732
D50BE3E61C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
2833
D50BE3E71C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
34+
D55281B52016758F00FF3CDD /* HeaderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeaderViewController.swift; sourceTree = "<group>"; };
35+
D55281B7201675D500FF3CDD /* MessageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageViewController.swift; sourceTree = "<group>"; };
36+
D55281B92016770800FF3CDD /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
37+
D55281BB2016782C00FF3CDD /* ScannerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScannerViewController.swift; sourceTree = "<group>"; };
38+
D55281BE20167DB400FF3CDD /* UIViewController+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Extensions.swift"; sourceTree = "<group>"; };
2939
D5B2E89F1C3A780C00C0327D /* BarcodeScanner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BarcodeScanner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3040
D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; tabWidth = 2; };
3141
D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = TorchMode.swift; sourceTree = "<group>"; tabWidth = 2; };
@@ -57,6 +67,27 @@
5767
path = Images;
5868
sourceTree = "<group>";
5969
};
70+
D55281B42016758000FF3CDD /* Controllers */ = {
71+
isa = PBXGroup;
72+
children = (
73+
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */,
74+
D55281B52016758F00FF3CDD /* HeaderViewController.swift */,
75+
D55281B7201675D500FF3CDD /* MessageViewController.swift */,
76+
D55281B92016770800FF3CDD /* SettingsViewController.swift */,
77+
D55281BB2016782C00FF3CDD /* ScannerViewController.swift */,
78+
);
79+
path = Controllers;
80+
sourceTree = "<group>";
81+
};
82+
D55281BD20167D9F00FF3CDD /* Extensions */ = {
83+
isa = PBXGroup;
84+
children = (
85+
D504555E1FD8714700E46826 /* UIView+Extensions.swift */,
86+
D55281BE20167DB400FF3CDD /* UIViewController+Extensions.swift */,
87+
);
88+
path = Extensions;
89+
sourceTree = "<group>";
90+
};
6091
D5B2E8951C3A780C00C0327D = {
6192
isa = PBXGroup;
6293
children = (
@@ -86,14 +117,14 @@
86117
D5C629691C3A809D007F7B7C /* Sources */ = {
87118
isa = PBXGroup;
88119
children = (
120+
D55281BD20167D9F00FF3CDD /* Extensions */,
121+
D55281B42016758000FF3CDD /* Controllers */,
89122
D5FC8AD61D252A12004BED88 /* State.swift */,
90123
D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */,
91124
D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */,
92125
2DBF9E0D1F169DEF006B5AA8 /* FocusViewType.swift */,
93126
D5F1C1C51C9C5113001E17A6 /* Config.swift */,
94-
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */,
95127
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */,
96-
D504555E1FD8714700E46826 /* UIView+Extensions.swift */,
97128
);
98129
path = Sources;
99130
sourceTree = "<group>";
@@ -181,12 +212,17 @@
181212
files = (
182213
D5C4E08F1CA0BFB9008D9269 /* TorchMode.swift in Sources */,
183214
D5C4E08E1CA0BFB9008D9269 /* InfoView.swift in Sources */,
215+
D55281BF20167DB400FF3CDD /* UIViewController+Extensions.swift in Sources */,
216+
D55281BA2016770800FF3CDD /* SettingsViewController.swift in Sources */,
184217
D5F1C1D31C9C5809001E17A6 /* HeaderView.swift in Sources */,
185218
D5F1C1C91C9C5113001E17A6 /* Config.swift in Sources */,
219+
D55281B8201675D500FF3CDD /* MessageViewController.swift in Sources */,
186220
D5F1C1CA1C9C5113001E17A6 /* BarcodeScannerController.swift in Sources */,
221+
D55281BC2016782C00FF3CDD /* ScannerViewController.swift in Sources */,
187222
2DBF9E0E1F169DEF006B5AA8 /* FocusViewType.swift in Sources */,
188223
D504555F1FD8714700E46826 /* UIView+Extensions.swift in Sources */,
189224
D5FC8AD71D252A12004BED88 /* State.swift in Sources */,
225+
D55281B62016758F00FF3CDD /* HeaderViewController.swift in Sources */,
190226
);
191227
runOnlyForDeploymentPostprocessing = 0;
192228
};

Example/BarcodeScannerExample/BarcodeScannerExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
TargetAttributes = {
127127
D50BE3F41C9FE8870000A34C = {
128128
CreatedOnToolsVersion = 7.2.1;
129+
DevelopmentTeam = XVAH95CHDC;
129130
LastSwiftMigration = 0800;
130131
ProvisioningStyle = Manual;
131132
};
@@ -404,14 +405,14 @@
404405
CODE_SIGN_IDENTITY = "";
405406
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
406407
CODE_SIGN_STYLE = Manual;
407-
DEVELOPMENT_TEAM = "";
408+
DEVELOPMENT_TEAM = XVAH95CHDC;
408409
INFOPLIST_FILE = BarcodeScannerExample/Info.plist;
409410
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
410411
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
411412
PRODUCT_BUNDLE_IDENTIFIER = no.hyper.BarcodeScannerExample2;
412413
PRODUCT_NAME = "$(TARGET_NAME)";
413-
PROVISIONING_PROFILE = "";
414-
PROVISIONING_PROFILE_SPECIFIER = "";
414+
PROVISIONING_PROFILE = "5429b4c3-def8-4c81-a541-9333e6f69af9";
415+
PROVISIONING_PROFILE_SPECIFIER = "Wildcard Development";
415416
SWIFT_VERSION = 4.0;
416417
TARGETED_DEVICE_FAMILY = "1,2";
417418
};

Example/BarcodeScannerExample/BarcodeScannerExample/ViewController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ViewController: UIViewController {
66
@IBOutlet var presentScannerButton: UIButton!
77
@IBOutlet var pushScannerButton: UIButton!
88

9-
private let controller = BarcodeScannerController()
9+
private let controller = ScannerController()
1010

1111
override func viewDidLoad() {
1212
super.viewDidLoad()
@@ -27,9 +27,9 @@ class ViewController: UIViewController {
2727
}
2828
}
2929

30-
extension ViewController: BarcodeScannerCodeDelegate {
30+
extension ViewController: ScannerCodeDelegate {
3131

32-
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
32+
func scanner(_ controller: ScannerController, didCaptureCode code: String, type: String) {
3333
print("Barcode Data: \(code)")
3434
print("Symbology Type: \(type)")
3535

@@ -40,16 +40,16 @@ extension ViewController: BarcodeScannerCodeDelegate {
4040
}
4141
}
4242

43-
extension ViewController: BarcodeScannerErrorDelegate {
43+
extension ViewController: ScannerErrorDelegate {
4444

45-
func barcodeScanner(_ controller: BarcodeScannerController, didReceiveError error: Error) {
45+
func scanner(_ controller: ScannerController, didReceiveError error: Error) {
4646
print(error)
4747
}
4848
}
4949

50-
extension ViewController: BarcodeScannerDismissalDelegate {
50+
extension ViewController: ScannerDismissalDelegate {
5151

52-
func barcodeScannerDidDismiss(_ controller: BarcodeScannerController) {
52+
func scannerDidDismiss(_ controller: ScannerController) {
5353
controller.dismiss(animated: true, completion: nil)
5454
}
5555
}

Example/BarcodeScannerExample/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- BarcodeScanner (3.0.1)
2+
- BarcodeScanner (3.0.3)
33

44
DEPENDENCIES:
55
- BarcodeScanner (from `../../`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: ../../
1010

1111
SPEC CHECKSUMS:
12-
BarcodeScanner: f884e5a1e0e8e6c95eb7d0c4b00d69b72fd2be83
12+
BarcodeScanner: d46e21e2c193b2a5501d66924c10c264352e5f21
1313

1414
PODFILE CHECKSUM: eae1a5fa9feaba2427db94d37a48c50c283939b8
1515

File renamed without changes.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import UIKit
2+
3+
/// Delegate to handle touch event of the close button.
4+
public protocol HeaderViewControllerDelegate: class {
5+
func headerViewControllerDidTapCloseButton(_ controller: HeaderViewController)
6+
}
7+
8+
/// Controller with title label and close button.
9+
/// It will be added as a child view controller if `BarcodeScannerController` is being presented.
10+
public final class HeaderViewController: UIViewController {
11+
public weak var delegate: HeaderViewControllerDelegate?
12+
13+
/// Header view with title label and close button.
14+
public private(set) lazy var navigationBar: UINavigationBar = self.makeNavigationBar()
15+
/// Title view of the navigation bar
16+
public private(set) lazy var titleLabel: UILabel = self.makeTitleLabel()
17+
/// Left bar button item of the navigation bar.
18+
public private(set) lazy var closeButton: UIButton = self.makeCloseButton()
19+
20+
// MARK: - View lifecycle
21+
22+
public override func viewDidLoad() {
23+
super.viewDidLoad()
24+
25+
navigationBar.delegate = self
26+
closeButton.addTarget(self, action: #selector(handleCloseButtonTap), for: .touchUpInside)
27+
28+
view.addSubview(navigationBar)
29+
setupConstraints()
30+
}
31+
32+
// MARK: - Actions
33+
34+
@objc private func handleCloseButtonTap() {
35+
delegate?.headerViewControllerDidTapCloseButton(self)
36+
}
37+
38+
// MARK: - Layout
39+
40+
private func setupConstraints() {
41+
navigationBar.translatesAutoresizingMaskIntoConstraints = false
42+
navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
43+
navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
44+
45+
if #available(iOS 11, *) {
46+
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
47+
} else {
48+
navigationBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
49+
}
50+
}
51+
}
52+
53+
// MARK: - Subviews
54+
55+
private extension HeaderViewController {
56+
func makeNavigationBar() -> UINavigationBar {
57+
let navigationBar = UINavigationBar()
58+
navigationBar.isTranslucent = false
59+
navigationBar.backgroundColor = Title.backgroundColor
60+
navigationBar.items = [makeNavigationItem()]
61+
return navigationBar
62+
}
63+
64+
func makeNavigationItem() -> UINavigationItem {
65+
let navigationItem = UINavigationItem()
66+
closeButton.sizeToFit()
67+
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: closeButton)
68+
titleLabel.sizeToFit()
69+
navigationItem.titleView = titleLabel
70+
return navigationItem
71+
}
72+
73+
func makeTitleLabel() -> UILabel {
74+
let label = UILabel()
75+
label.text = Title.text
76+
label.font = Title.font
77+
label.textColor = Title.color
78+
label.numberOfLines = 1
79+
label.textAlignment = .center
80+
return label
81+
}
82+
83+
func makeCloseButton() -> UIButton {
84+
let button = UIButton(type: .system)
85+
button.setTitle(CloseButton.text, for: UIControlState())
86+
button.titleLabel?.font = CloseButton.font
87+
button.tintColor = CloseButton.color
88+
return button
89+
}
90+
}
91+
92+
// MARK: - UINavigationBarDelegate
93+
94+
extension HeaderViewController: UINavigationBarDelegate {
95+
public func position(for bar: UIBarPositioning) -> UIBarPosition {
96+
return .topAttached
97+
}
98+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import UIKit
2+
3+
final class MessageViewController: UIViewController {
4+
5+
}

0 commit comments

Comments
 (0)