This repository has been archived by the owner on Jan 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDZRootViewController.m
175 lines (136 loc) · 5.34 KB
/
CDZRootViewController.m
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
//
// CDZRootViewController.m
// QRScanner
//
// Created by Chris Dzombak on 10/27/13.
// Copyright (c) 2013 Chris Dzombak. All rights reserved.
//
#import "CDZRootViewController.h"
#import "CDZDataController.h"
#import "CDZScanAction.h"
#import "CDZQRScanningViewController.h"
#import "CDZScansListViewController.h"
#import "NSString+Trimming.h"
#import "UIWindow+Hairline.h"
@interface CDZRootViewController () <UIActionSheetDelegate>
@property (nonatomic, readonly) UIViewController *scannerVC;
@property (nonatomic, readonly) UIViewController *scansListVC;
@property (nonatomic, readonly) UIView *separatorView;
@property (nonatomic, weak) UIActionSheet *displayedActionSheet;
@property (nonatomic, strong) NSOrderedSet *availableActions;
@end
@implementation CDZRootViewController
@synthesize scannerVC = _scannerVC,
scansListVC = _scansListVC,
separatorView = _separatorView
;
- (instancetype)initWithDataController:(CDZDataController *)dataController {
self = [super init];
if (self) {
self.title = NSLocalizedString(@"QR Scanner", nil);
self.dataController = dataController;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
[self addChildViews];
}
- (void)addChildViews
{
[self addChildViewController:self.scannerVC];
[self.view addSubview:self.scannerVC.view];
[self.scannerVC didMoveToParentViewController:self];
[self addChildViewController:self.scansListVC];
[self.view addSubview:self.scansListVC.view];
[self.scansListVC didMoveToParentViewController:self];
[self.view addSubview:self.separatorView];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self layoutMyView];
}
- (void)layoutMyView
{
CGRect scannerFrame = (CGRect) {
.origin = CGPointZero,
.size = CGSizeMake(CGRectGetWidth(self.view.bounds), 0.55f*CGRectGetHeight(self.view.bounds))
};
CGRect listFrame = (CGRect) {
.origin = CGPointMake(0, CGRectGetMaxY(scannerFrame)),
.size = CGSizeMake(CGRectGetWidth(self.view.bounds), 0.45f*CGRectGetHeight(self.view.bounds))
};
CGRect separatorFrame = (CGRect) {
.origin = CGPointMake(0, CGRectGetMaxY(scannerFrame)),
.size = CGSizeMake(CGRectGetWidth(self.view.bounds), [self.view.window cdz_hairlineWidth])
};
self.scannerVC.view.frame = scannerFrame;
self.scansListVC.view.frame = listFrame;
self.separatorView.frame = separatorFrame;
}
- (void)didScanQRCodeWithResult:(NSString *)result {
[self.dataController addScanWithText:result];
[self didSelectScanWithText:result];
}
#pragma mark - Actions
- (void)didSelectScanWithText:(NSString *)text {
[CDZScanAction determineActionsForString:text result:^(NSOrderedSet *actions) {
self.availableActions = actions;
UIActionSheet *displayedActionSheet = self.displayedActionSheet;
[displayedActionSheet dismissWithClickedButtonIndex:displayedActionSheet.cancelButtonIndex animated:NO];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[text cdz_stringWithTrimmedNewlines]
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (CDZScanAction *action in self.availableActions) {
[actionSheet addButtonWithTitle:action.localizedActionName];
}
NSInteger cancelButtonIdx = [actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
actionSheet.cancelButtonIndex = cancelButtonIdx;
[actionSheet showInView:self.view];
self.displayedActionSheet = actionSheet;
}];
}
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) return;
[[self.availableActions objectAtIndex:(NSUInteger)buttonIndex] takeAction];
}
#pragma mark - Property Overrides
- (UIViewController *)scannerVC {
if (!_scannerVC) {
CDZQRScanningViewController *vc = [[CDZQRScanningViewController alloc] init];
vc.errorBlock = ^(NSError *error) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"QR Scanning Error", nil)
message:[error description]
delegate:nil
cancelButtonTitle:@"Okay."
otherButtonTitles:nil]
show];
};
vc.resultBlock = ^(NSString *result) {
[self didScanQRCodeWithResult:result];
};
_scannerVC = vc;
}
return _scannerVC;
}
- (UIViewController *)scansListVC {
if (!_scansListVC) {
CDZScansListViewController *vc = [[CDZScansListViewController alloc] initWithDataController:self.dataController];
_scansListVC = vc;
}
return _scansListVC;
}
- (UIView *)separatorView {
if (!_separatorView) {
_separatorView = [[UIView alloc] initWithFrame:CGRectZero];
_separatorView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.9];
}
return _separatorView;
}
@end