-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputViewController.m
204 lines (165 loc) · 6.93 KB
/
InputViewController.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
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
//
// InputViewController.m
// Morse Torch
//
// Created by Stevenson on 1/20/14.
// Copyright (c) 2014 Steven Stevenson. All rights reserved.
//
#import "InputViewController.h"
#import "NSString+MorseCode.h"
#import "UIColor+MorseTorch.h"
#import "SSProgressViewRingGradient.h"
#import "SSTorchAccess.h"
#import "SSMorseButton.h"
#import "SSResponsiveScrollView.h"
@import AVFoundation;
@interface InputViewController () <UITextFieldDelegate, UIScrollViewDelegate>
//the UILabel containing text to encode in flashes
@property (weak, nonatomic) IBOutlet UITextField *inputField;
//the UIButton to send the text
@property (weak, nonatomic) IBOutlet SSMorseButton *transmitButton;
//the UILabels to show the current letter and morse word being sent
@property (weak, nonatomic) IBOutlet UILabel *morseText;
@property (weak, nonatomic) IBOutlet UILabel *letterText;
//the background queue containing the flashes
@property (nonatomic) NSOperationQueue *torchQueue;
//the HUD that shows the current progress of the text being sent
@property (nonatomic) SSProgressViewRingGradient *hudProgress;
//UI objects for content wrapping
@property (weak, nonatomic) IBOutlet UIView *theInputView;
@property (weak, nonatomic) IBOutlet SSResponsiveScrollView *theScrollView;
@end
@implementation InputViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.inputField.delegate = self;
self.torchQueue = [[NSOperationQueue alloc] init];
[self.torchQueue setMaxConcurrentOperationCount:1];
[self setup];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
[self.theScrollView setTextFields:@[self.inputField]];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
self.theScrollView.contentOffset = CGPointMake(0, 0);
self.theScrollView.contentSize = CGSizeMake(320.f, 460.f);
}
#pragma mark Setup Views
-(void) setup {
self.hudProgress = [[SSProgressViewRingGradient alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.hudProgress.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMaxY(self.transmitButton.frame)+CGRectGetHeight(self.transmitButton.frame)+20);
self.hudProgress.showPercentage = NO;
[self.theInputView addSubview:self.hudProgress];
[self.hudProgress setProgress:0.0f animated:YES];
self.letterText.layer.cornerRadius = 5;
self.letterText.layer.masksToBounds = YES;
self.morseText.layer.cornerRadius = 5;
self.morseText.layer.masksToBounds = YES;
self.transmitButton.layer.cornerRadius = 5;
self.transmitButton.layer.masksToBounds = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Transmit using many operations
- (IBAction)transmitString:(id)sender {
if ([self.inputField isFirstResponder]) {
[self.inputField resignFirstResponder];
}
if (self.torchQueue.operationCount == 0) {
[[SSTorchAccess sharedManager] takeTorch];
[self.transmitButton setCancel];
NSString *inputText = [NSString validateString:self.inputField.text];
NSArray* morse = [NSString getSymbolsFromString:inputText];
CGFloat totalProcess = [morse count];
CGFloat segmentedProcess = (1.f/(float)totalProcess);
NSInteger counter = 0;
CGFloat ongoingSegment = 0;
for (NSString* code in morse) {
for (NSUInteger i=0;i<[code length];i++) {
NSString *dotOrDash = [code substringWithRange:NSMakeRange(i, 1)];
[self.torchQueue addOperationWithBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CGFloat thisProgress = (float)(i+1)/(float)[code length]*segmentedProcess+ongoingSegment;
[self.hudProgress setProgress:thisProgress animated:YES];
unichar charString = [inputText characterAtIndex:counter];
[self updateTextLabelText: [NSString stringWithFormat:@"%c",charString] andMorseLabelText: code];
}];
}];
if ([dotOrDash isEqualToString:@"."]) {
[self.torchQueue addOperationWithBlock:^{
[[SSTorchAccess sharedManager] engageTorch];
usleep(DOT_IN_MICROSEC);
[[SSTorchAccess sharedManager] disengageTorch];
}];
} else if ([dotOrDash isEqualToString:@"_"]) {
[self.torchQueue addOperationWithBlock:^{
[[SSTorchAccess sharedManager] engageTorch];
usleep(DASH_IN_MICROSEC);
[[SSTorchAccess sharedManager] disengageTorch];
}];
}
[self.torchQueue addOperationWithBlock:^{
usleep(INTER_FLASH_DELAY_IN_MICROSEC);
}];
}
[self.torchQueue addOperationWithBlock:^{
usleep(WORD_DELAY_IN_MICROSEC);
}];
counter++;
ongoingSegment += segmentedProcess;
}
[self.torchQueue addOperationWithBlock:^{
[[SSTorchAccess sharedManager] releaseTorch];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.hudProgress setAlpha:1];
[self.transmitButton setTransmit];
}];
}];
} else {
[[SSTorchAccess sharedManager] releaseTorch];
[self.hudProgress setAlpha:1];
[self.torchQueue cancelAllOperations];
[self.transmitButton setTransmit];
}
}
#pragma mark -
- (void)updateTextLabelText:(NSString*) text andMorseLabelText: (NSString*)morseChar {
self.letterText.text = text;
[self.letterText setNeedsDisplay];
self.morseText.text = morseChar;
[self.morseText setNeedsDisplay];
}
#pragma mark - UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField {
self.transmitButton.enabled = YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
self.transmitButton.enabled = NO;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
#pragma mark UITextFieldDidChange
-(void)textFieldDidChange: (NSNotification *) notification {
[self switchDisabledToTransmit];
}
#pragma mark switch button
-(void)switchDisabledToTransmit {
if(self.torchQueue.operationCount == 0) {
if ([self.inputField.text length] == 0) {
self.transmitButton.enabled = NO;
[self.transmitButton setDisabled];
[self.transmitButton setAlpha:.5f];
} else {
[self.transmitButton setTransmit];
self.transmitButton.enabled = YES;
[self.transmitButton setAlpha:1.f];
}
}
}
@end