-
Notifications
You must be signed in to change notification settings - Fork 3
/
DMRViewController.m
420 lines (347 loc) · 17.3 KB
/
DMRViewController.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//
// DMRViewController.m
// Domainr
//
// Created by Connor Montgomery on 02/04/2014.
// Copyright (c) 2014 Domainr. All rights reserved.
//
#import "DMRViewController.h"
#import "DMRTableRowView.h"
#import "DMRTextFieldView.h"
#import "DMRResultsTableView.h"
#import "DMRSettingsWindowController.h"
#import <QuartzCore/QuartzCore.h>
#import <SVHTTPRequest.h>
#define kSpacer ((int) 16)
#define kAvailabilityImageDimension ((int) 16)
#define kResultCellHeight ((int) 30)
@interface DMRViewController ()
@property(nonatomic) NSProgressIndicator *spinner;
@property(nonatomic) NSMutableArray *domains;
@property(nonatomic) NSString *query;
@property(nonatomic) DMRResultsTableView *tableView;
@property(nonatomic) BOOL shouldOpenBrowser;
@property (strong, nonatomic) DMRSettingsWindowController *prefs;
@end
@implementation DMRViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_shouldOpenBrowser = NO;
_searchBox = [[DMRTextFieldView alloc] initWithFrame:CGRectMake(
kSpacer,
self.view.frame.size.height - 40,
self.view.frame.size.width - (kSpacer * 2),
30)];
[_searchBox.cell setPlaceholderString:@"search"];
_searchBox.extendedDelegate = self;
[_searchBox.cell setFocusRingType:NSFocusRingTypeNone];
_searchBox.textColor = [NSColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0];
[_searchBox setFont:[NSFont systemFontOfSize:13.0f]];
_searchBox.target = self;
_searchBox.action = @selector(searchBoxAction:);
_searchBox.bezelStyle = NSTextFieldRoundedBezel;
[_searchBox becomeFirstResponder];
[self.view addSubview:_searchBox];
_spinner = [[NSProgressIndicator alloc] initWithFrame:CGRectMake(
_searchBox.bounds.size.width - 10, self.view.frame.size.height - 33,
16,
16)];
[self.view addSubview:_spinner];
[_spinner setHidden:YES];
_spinner.style = NSProgressIndicatorSpinningStyle;
NSScrollView *tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(2,
kSpacer + 23,
self.view.frame.size.width - 4,
self.view.frame.size.height - 40 - _searchBox.frame.size.height - kSpacer)];
tableContainer.wantsLayer = YES;
_tableView = [[DMRResultsTableView alloc] initWithFrame:NSMakeRect(0, 0, self.view.frame.size.width, tableContainer.frame.size.height)];
NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:@"Col1"];
_tableView.backgroundColor = [NSColor whiteColor];
[column1 setWidth:self.view.frame.size.width];
[_tableView addTableColumn:column1];
_tableView.extendedDelegate = self;
[_tableView setDelegate:self];
[_tableView setBackgroundColor:[NSColor clearColor]];
[_tableView setDataSource:self];
[_tableView reloadData];
[tableContainer setDocumentView:_tableView];
_tableView.headerView = nil;
[tableContainer setHasVerticalScroller:YES];
[self.view addSubview:tableContainer];
NSButton *poweredByDomainr = [[NSButton alloc] initWithFrame:NSMakeRect(kSpacer,
self.view.frame.size.height - self.view.frame.size.height + (kSpacer) - 2,
self.view.frame.size.width - (kSpacer * 2),
14)];
[poweredByDomainr setBordered:NO];
poweredByDomainr.title = @"Powered by Domainr";
[poweredByDomainr setAction:@selector(didClickPoweredBy:)];
poweredByDomainr.target = self;
poweredByDomainr.font = [NSFont fontWithName:poweredByDomainr.font.fontName size:11.0f];
[poweredByDomainr sizeToFit];
NSMutableAttributedString *labelTitle =
[[NSMutableAttributedString alloc] initWithAttributedString:[poweredByDomainr attributedTitle]];
[poweredByDomainr setAlignment:NSLeftTextAlignment];
NSRange titleRange = NSMakeRange(0, [labelTitle length]);
[labelTitle addAttribute:NSForegroundColorAttributeName
value:[NSColor colorWithRed:180/255.0f green:189/255.0f blue:175/255.0f alpha:1.0]
range:titleRange];
[poweredByDomainr setAttributedTitle:labelTitle];
[self.view addSubview:poweredByDomainr positioned:NSWindowAbove relativeTo:nil];
NSInteger popupButtonSize = 44;
NSBox *box = [[NSBox alloc] initWithFrame:NSMakeRect(0, 38, self.view.frame.size.width, 3)];
[self.view addSubview:box];
NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(self.view.frame.size.width - kSpacer - popupButtonSize,
0,
popupButtonSize,
popupButtonSize)];
NSMenu *menu = [[NSMenu alloc] init];
popupButton.pullsDown = YES;
popupButton.bordered = NO;
[self.view addSubview:popupButton];
NSMenuItem *dummy = [[NSMenuItem alloc] init];
dummy.title = @"";
dummy.image = [NSImage imageNamed:NSImageNameActionTemplate];
[menu addItem:dummy];
NSMenuItem *fooMenuItem = [[NSMenuItem alloc] initWithTitle:@"Preferences" action:@selector(didClickPreferences) keyEquivalent:@","];
NSMenuItem *barMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(didClickQuit) keyEquivalent:@"q"];
[menu addItem:fooMenuItem];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:barMenuItem];
popupButton.menu = menu;
}
return self;
}
- (void)didClickPreferences {
_prefs = [[DMRSettingsWindowController alloc] initWithWindowNibName:@"DMRSettingsWindowController"];
[_prefs showWindow:self];
[NSApp activateIgnoringOtherApps:YES];
[_prefs.window makeKeyAndOrderFront:NSApp];
_prefs.opener = self;
[self.statusItemPopup hidePopover];
}
- (void)didClickQuit {
[NSApp terminate:self];
}
- (void)didClickPoweredBy: (id)sender {
[_tracker trackEvent:@"macAppOpenURLFromPoweredByLink"
action:@"clickPoweredByLink"
label:nil
value:-1
withError:nil];
NSString *url = @"https://domainr.com/";
url = [self urlWithMacID:url];
[self openUrl:url];
}
- (void)tableView:(NSTableView *)tableView didPressEnter:(NSEvent *)theEvent {
NSDictionary *domainObject = _domains[[_tableView selectedRow]];
NSString *url = [self urlForDomainObject:domainObject];
url = [self urlWithMacID:url];
[self openUrl:url];
}
- (NSString *)urlWithMacID:(NSString *)url {
NSString *newUrl = [NSString stringWithFormat:@"%@?client_id={your-mashape-key-or-client_id}", url];
return newUrl;
}
- (void)openUrl:(NSString *)url {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
[_statusItemPopup hidePopover];
}
- (NSString *)urlForDomainObject:(NSDictionary *)domainObject {
NSString *domain = domainObject[@"domain"];
NSString *safeQuery = [_query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *URL = [NSString stringWithFormat:@"https://domainr.com/%@/with/%@", safeQuery, domain];
return URL;
}
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row {
NSString *url = [self urlForDomainObject:_domains[row]];
url = [self urlWithMacID:url];
[self openUrl:url];
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
return kResultCellHeight;
}
- (void)timerDidExpire:(NSTimer *)timer {
[self search:nil];
}
- (void)didKeyUp:(NSEvent *)theEvent {
int keycode = theEvent.keyCode;
NSInteger currentIndex = [_tableView selectedRow];
NSInteger desiredIndex;
if (keycode == 126 || keycode == 125) {
_shouldOpenBrowser = YES;
} else {
_shouldOpenBrowser = NO;
}
if (keycode == 126) {
if (currentIndex == 0) {
desiredIndex = [_domains count] - 1;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:desiredIndex];
[_tableView selectRowIndexes:indexSet byExtendingSelection:NO];
} else {
desiredIndex = currentIndex - 1;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:desiredIndex];
[_tableView selectRowIndexes:indexSet byExtendingSelection:NO];
}
} else if (keycode == 125) {
if (currentIndex == [_domains count] - 1) {
desiredIndex = 0;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:desiredIndex];
[_tableView selectRowIndexes:indexSet byExtendingSelection:NO];
} else {
desiredIndex = currentIndex + 1;
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:currentIndex + 1];
[_tableView selectRowIndexes:indexSet byExtendingSelection:NO];
}
}
[_tableView scrollRowToVisible:desiredIndex];
}
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row {
return YES;
}
- (void)search:(id)selector {
[_spinner setHidden:NO];
[_spinner startAnimation:self];
[_tracker trackEvent:@"macAppSearch"
action:@"searchBegin"
label:nil
value:-1
withError:nil];
[SVHTTPRequest GET:@"https://domainr.com/api/json/search"
parameters:@{
@"q": _searchBox.stringValue,
@"client_id": @"{your-mashape-key}"
}
completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error != nil) {
[_tracker trackEvent:@"macAppSearch"
action:@"searchError"
label:nil
value:-1
withError:nil];
} else {
[_tracker trackEvent:@"macAppSearch"
action:@"searchSuccess"
label:nil
value:-1
withError:nil];
_domains = response[@"results"];
_query = response[@"query"];
[_tableView reloadData];
[_spinner stopAnimation:self];
[_spinner setHidden:YES];
}
}
];
}
- (void)searchBoxAction:(id)selector {
if (_shouldOpenBrowser) {
NSDictionary *domainObject = _domains[[_tableView selectedRow]];
NSString *url = [self urlForDomainObject:domainObject];
url = [self urlWithMacID:url];
[self openUrl:url];
[_tracker trackEvent:@"macAppOpenURLFromResult"
action:@"clickResult"
label:nil
value:-1
withError:nil];
} else {
[self search:selector];
}
}
-(NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row{
static NSString *cellID = @"cell_identifier";
//use this if you want to reuse the cells
DMRTableRowView *result = [tableView makeViewWithIdentifier:cellID owner:self];
result.domainData = _domains[row];
if (result == nil) {
result = [[DMRTableRowView alloc] initWithFrame:NSMakeRect(0, 0, self.view.frame.size.width, kResultCellHeight)];
result.identifier = cellID;
}
// Return the result
return result;
}
-(void)tableViewSelectionIsChanging:(NSNotification *)notification {
[self updateSelectedRowColor];
}
-(void)tableViewSelectionDidChange:(NSNotification *)notification {
[self updateSelectedRowColor];
}
- (void)updateSelectedRowColor {
[_tableView enumerateAvailableRowViewsUsingBlock:^(NSTableRowView *rowView, NSInteger row){
NSTableCellView *cellView = [rowView viewAtColumn:0];
if (rowView.selected){
cellView.textField.textColor = [NSColor whiteColor];
} else {
cellView.textField.textColor = [NSColor colorWithRed:40/255.0f green:112/255.0f blue:176/255.0f alpha:1.0];
NSString *availability = _domains[row][@"availability"];
NSString *domain = _domains[row][@"domain"];
if ([availability isEqualToString:@"unavailable"]) {
cellView.textField.textColor = [NSColor colorWithRed:65/255.0f green:66/255.0f blue:64/255.0f alpha:1.0];
}
if ([availability isEqualToString:@"tld"]) {
cellView.textField.textColor = [NSColor colorWithRed:65/255.0f green:66/255.0f blue:64/255.0f alpha:1.0];
cellView.textField.stringValue = [NSString stringWithFormat:@".%@", domain];
cellView.textField.font = [NSFont fontWithName:@"HelveticaNeue-Medium" size:18];
}
}
}];
}
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTableCellView *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];
if (result == nil) {
result = [[NSTableCellView alloc] initWithFrame:CGRectMake(kSpacer,
kSpacer,
self.view.frame.size.width - (2 * kSpacer),
kResultCellHeight)];
}
if (result.textField == nil) {
NSTextField *textField = [[NSTextField alloc] initWithFrame:CGRectMake(
kSpacer + kAvailabilityImageDimension,
3,
self.view.frame.size.width - (2 * kSpacer) - kAvailabilityImageDimension,
kResultCellHeight - 6)];
textField.font = [NSFont fontWithName:@"HelveticaNeue" size:15.0f];
textField.textColor = [NSColor colorWithRed:40/255.0f green:112/255.0f blue:176/255.0f alpha:1.0];
textField.backgroundColor = [NSColor clearColor];
[textField setBordered:NO];
[textField setEditable:NO];
[result addSubview:textField];
result.textField = textField;
}
if (result.imageView == nil) {
NSImageView *imageView = [[NSImageView alloc] initWithFrame:CGRectMake(
0,
-6,
40,
40)];
[result addSubview:imageView];
result.imageView = imageView;
}
result.textField.font = [NSFont fontWithName:@"HelveticaNeue" size:18];
NSString *domain = _domains[row][@"domain"];
result.textField.stringValue = domain;
NSString *availability = _domains[row][@"availability"];
result.textField.textColor = [NSColor colorWithRed:40/255.0f green:112/255.0f blue:176/255.0f alpha:1.0];
if ([availability isEqualToString:@"unavailable"]) {
result.textField.textColor = [NSColor colorWithRed:65/255.0f green:66/255.0f blue:64/255.0f alpha:1.0];
}
if ([availability isEqualToString:@"tld"]) {
result.textField.textColor = [NSColor colorWithRed:65/255.0f green:66/255.0f blue:64/255.0f alpha:1.0];
result.textField.stringValue = [NSString stringWithFormat:@".%@", domain];
result.textField.font = [NSFont fontWithName:@"HelveticaNeue-Medium" size:18];
}
NSLog(@"%@ => %@", _domains[row][@"domain"], _domains[row][@"availability"]);
NSImage *image = [NSImage imageNamed:_domains[row][@"availability"]];
[image setSize:NSSizeFromCGSize(CGSizeMake(10.0f, 10.0f))];
[result.imageView setImage:image];
result.identifier = @"MyView";
return result;
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [_domains count];
}
@end