-
Notifications
You must be signed in to change notification settings - Fork 11
/
ViewController.m
343 lines (263 loc) · 11.1 KB
/
ViewController.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
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
//
// ViewController.m
// javascriptIPad
//
// Created by Matt Schmulen on 10/2/13.
// Copyright (c) 2013 Matt Schmulen. All rights reserved.
//
#import "ViewController.h"
#import "JavaScriptCodeBlock.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableViewJavaScriptCodeBlocks;
@property (weak, nonatomic) IBOutlet UITextView *textViewJavaScriptCode;
@property (weak, nonatomic) IBOutlet UITextView *textViewJavascriptConsoleOut;
@property ( strong, nonatomic) NSMutableArray *tableData;
//Current Context
@property (strong, nonatomic) JSContext *context;
//Current javaScriptCode
@property (strong, nonatomic) JavaScriptCodeBlock *currentJavaScriptCode;
@property (weak, nonatomic) IBOutlet UITextField *textFieldCommandTerminal;
//Core BlueTooth properties
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
@property (strong, nonatomic) CBMutableCharacteristic *transferCharacteristic;
#define ADVERTISEMENT_NAME @"JavaScriptActivatedIBeacon"
#define SERVICE_ID @"21451111-CC21-49C1-BCC1-B51A70111130"
#define CHARASTERISTIC_ID @"B1DA1111-EE1D-1D11-1331-1B11E111D1C0"
#define MAX_MTU 20
@end
@implementation ViewController
- (NSMutableArray *) tableData
{
if ( !_tableData){
_tableData = [[NSMutableArray alloc] init];
JavaScriptCodeBlock *blockIntro = [JavaScriptCodeBlock alloc];
blockIntro.name = @"intro";
blockIntro.javaScriptSourceFile = @"intro";
[_tableData addObject:blockIntro];
JavaScriptCodeBlock *blockFilter = [JavaScriptCodeBlock alloc];
blockFilter.name = @"filter";
blockFilter.javaScriptSourceFile = @"filter";
[_tableData addObject:blockFilter];
JavaScriptCodeBlock *blockMap = [JavaScriptCodeBlock alloc];
blockMap.name = @"map";
blockMap.javaScriptSourceFile = @"map";
[_tableData addObject:blockMap];
JavaScriptCodeBlock *blogFilterMap = [JavaScriptCodeBlock alloc];
blogFilterMap.name = @"filter + map";
blogFilterMap.javaScriptSourceFile = @"filterMap";
[_tableData addObject:blogFilterMap];
JavaScriptCodeBlock *blockReduce = [JavaScriptCodeBlock alloc];
blockReduce.name = @"reduce";
blockReduce.javaScriptSourceFile = @"reduce";
[_tableData addObject:blockReduce];
JavaScriptCodeBlock *blockFilterMapReduce = [JavaScriptCodeBlock alloc];
blockFilterMapReduce.name = @"filter + map + reduce";
blockFilterMapReduce.javaScriptSourceFile = @"filterMapReduce";
[_tableData addObject:blockFilterMapReduce];
/*
JavaScriptCodeBlock *block2 = [JavaScriptCodeBlock alloc];
block2.name = @"sortBubble";
block2.javaScriptSourceFile = @"sortBubble";
[_tableData addObject:block2];
JavaScriptCodeBlock *block3 = [JavaScriptCodeBlock alloc];
block3.name = @"sortMerge";
block3.javaScriptSourceFile = @"sortMerge";
[_tableData addObject:block3];
JavaScriptCodeBlock *block4 = [JavaScriptCodeBlock alloc];
block3.name = @"insertion sort";
block3.javaScriptSourceFile = @"sortInsertion";
[_tableData addObject:block4];
JavaScriptCodeBlock *block5 = [JavaScriptCodeBlock alloc];
block3.name = @"selection sort";
block3.javaScriptSourceFile = @"sortSelection";
[_tableData addObject:block5];
JavaScriptCodeBlock *block6 = [JavaScriptCodeBlock alloc];
block3.name = @"binary tree";
block3.javaScriptSourceFile = @"dataStructureBinaryTree";
[_tableData addObject:block6];
JavaScriptCodeBlock *block7 = [JavaScriptCodeBlock alloc];
block3.name = @"heap";
block3.javaScriptSourceFile = @"dataStructureHeap";
[_tableData addObject:block7];
JavaScriptCodeBlock *blockλ = [JavaScriptCodeBlock alloc];
block.name = @"λ";
block3.javaScriptSourceFile = @"λ";
[_tableData addObject:block];
*/
}
return _tableData;
};
- (JSContext *) context
{
if ( !_context) _context = [[JSContext alloc] init];
return _context;
};
- (void) logger:(NSString *)logMessage
{
//NSLog(logMessage);
[self.textViewJavascriptConsoleOut setText: [NSString stringWithFormat: @"%@\n%@", self.textViewJavascriptConsoleOut.text, logMessage] ];
//set the console out to the bottom of the view
//CGRect caretRect = [self.textViewJavascriptConsoleOut caretRectForPosition:self.textViewJavascriptConsoleOut.endOfDocument];
//[self.textViewJavascriptConsoleOut scrollRectToVisible:caretRect animated:YES];
}
- (NSString *) loadCurrentJavaScriptCodeFromFile
{
NSString *path = [[NSBundle mainBundle] pathForResource:self.currentJavaScriptCode.javaScriptSourceFile ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//set the text in the view
[[self textViewJavaScriptCode] setText: jsCode ];
//clear the consoleOut
self.textViewJavascriptConsoleOut.text = @"";
return jsCode;
}
- ( void) preLoadJavaScriptGlobalScopeVariablesAndFunctions
{
//Preload Global JavaScript Variables and Functions
//global variables
self.context[@"version"] = @1;
//global functions
[self.context evaluateScript:@"var square = function(x) {return x*x;}"];
};
- ( void) preLoadObjectiveCBlockFunctions
{
//consoleLog
self.context[@"consoleLog"] = ^(NSString *message) {
[self logger: message];
};
//execute factorial in native Obj-C , Logger(@"5! = %@", factorial(5) );
self.context[@"factorial"] = ^(int x) {
int factorial = 1;
for (; x > 1; x--) {
factorial *= x;
}
return factorial;
};
//UI stuff
self.context[@"setBackgroundColor"] = ^() // Logger(@"sum2 = %@", sum2(4,5)
{
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *newColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
[self.view setBackgroundColor: newColor ];
};
}
- ( void) evaluateJavaScriptInCodeWindow
{
JSValue * value = [self.context evaluateScript: [[self textViewJavaScriptCode] text]];
//[self logger: [ NSString stringWithFormat:@"JavaScript context return value : %@", value] ];
//set the console out to the bottom of the view
CGRect caretRect = [self.textViewJavascriptConsoleOut caretRectForPosition:self.textViewJavascriptConsoleOut.endOfDocument];
[self.textViewJavascriptConsoleOut scrollRectToVisible:caretRect animated:YES];
}
- ( void ) resetScriptContext
{
NSLog(@"resetScriptContext");
[self preLoadJavaScriptGlobalScopeVariablesAndFunctions];
[self preLoadObjectiveCBlockFunctions];
}
//ACTIONS
- (IBAction)actionRefreshScriptList:(id)sender {
NSLog(@"Referesh the scripts");
}
- (IBAction)actionCommandTerminal:(id)sender {
if ( self.textFieldCommandTerminal != nil )
{
//NSLog(@"Executing Command Terminal %@", self.textFieldCommandTerminal.text);
JSValue * value = [self.context evaluateScript:self.textFieldCommandTerminal.text];
if ( value )
[self logger: [ NSString stringWithFormat:@"%@", value] ];
self.textFieldCommandTerminal.text = nil;
}
}
- (IBAction)actionClearConsoleOut:(id)sender {
self.textViewJavascriptConsoleOut.text = @"";
}
- (IBAction)actionExecute:(id)sender {
[self evaluateJavaScriptInCodeWindow];
}
// BLE Stuff
- (void)peripheralManager:(CBPeripheralManager *)peripheral
didAddService:(CBService *)service
error:(NSError *)error
{
if (error != nil) {
return;
}
NSLog(@"Start advertising.");
[self.peripheralManager startAdvertising:@{CBAdvertisementDataLocalNameKey: ADVERTISEMENT_NAME,
CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:SERVICE_ID]]}];
}
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if(peripheral.state == CBPeripheralManagerStatePoweredOn)
{
CBMutableService *service = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:SERVICE_ID]
primary:YES];
self.transferCharacteristic =
[[CBMutableCharacteristic alloc]
initWithType:[CBUUID UUIDWithString:CHARASTERISTIC_ID]
properties:CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable];
service.characteristics = @[self.transferCharacteristic];
[self.peripheralManager addService:service];
}
else
{
NSLog(@"Peripheral Manager did change state");
}
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableData count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( [[ [self.tableData objectAtIndex:indexPath.row] class] isSubclassOfClass:[JavaScriptCodeBlock class]])
{
JavaScriptCodeBlock *model = (JavaScriptCodeBlock *)[self.tableData objectAtIndex:indexPath.row];
self.currentJavaScriptCode = model;
[self loadCurrentJavaScriptCodeFromFile];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if ( [[ [self.tableData objectAtIndex:indexPath.row] class] isSubclassOfClass:[JavaScriptCodeBlock class]])
{
JavaScriptCodeBlock *model = (JavaScriptCodeBlock *)[self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = model.name;
}
return cell;
}
#pragma mark - prepare for segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//showIntro
if ( [segue.identifier isEqualToString:@"showIntro"]) {
NSLog(@" Prparing for Intro");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self resetScriptContext];
self.currentJavaScriptCode = [self.tableData objectAtIndex:0];
//turn of Auto Correct
[[self textViewJavaScriptCode] setAutocorrectionType:UITextAutocorrectionTypeNo];
[self loadCurrentJavaScriptCodeFromFile];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end