OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
6 #error "This file requires ARC support." | |
7 #endif | |
8 | |
9 #import "remoting/client/ios/host.h" | |
10 | |
11 @implementation Host | |
12 | |
13 @synthesize createdTime = _createdTime; | |
14 @synthesize hostId = _hostId; | |
15 @synthesize hostName = _hostName; | |
16 @synthesize hostVersion = _hostVersion; | |
17 @synthesize jabberId = _jabberId; | |
18 @synthesize kind = _kind; | |
19 @synthesize publicKey = _publicKey; | |
20 @synthesize status = _status; | |
21 @synthesize updatedTime = _updatedTime; | |
22 @synthesize isOnline = _isOnline; | |
23 | |
24 - (bool)isOnline { | |
25 _isOnline = (self.status && [self.status isEqualToString:@"ONLINE"]); | |
26 return _isOnline; | |
27 } | |
28 | |
29 // Parse jsonData into Host list. | |
30 + (NSMutableArray*)parseListFromJSON:(NSMutableData*)data { | |
31 NSError* error; | |
32 | |
33 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data | |
34 options:kNilOptions | |
35 error:&error]; | |
36 | |
37 NSDictionary* dataDict = [json objectForKey:@"data"]; | |
38 | |
39 NSArray* availableHosts = [dataDict objectForKey:@"items"]; | |
40 | |
41 NSMutableArray* hostList = [[NSMutableArray alloc] init]; | |
42 | |
43 NSUInteger idx = 0; | |
44 NSDictionary* svr; | |
45 NSUInteger count = [availableHosts count]; | |
46 | |
47 while (idx < count) { | |
48 svr = [availableHosts objectAtIndex:idx++]; | |
49 Host* host = [[Host alloc] init]; | |
50 host.createdTime = [svr objectForKey:@"createdTime"]; | |
51 host.hostId = [svr objectForKey:@"hostId"]; | |
52 host.hostName = [svr objectForKey:@"hostName"]; | |
53 host.hostVersion = [svr objectForKey:@"hostVersion"]; | |
54 host.jabberId = [svr objectForKey:@"jabberId"]; | |
55 host.kind = [svr objectForKey:@"kind"]; | |
56 host.publicKey = [svr objectForKey:@"publicKey"]; | |
57 host.status = [svr objectForKey:@"status"]; | |
58 | |
59 NSString* ISO8601DateString = [svr objectForKey:@"updatedTime"]; | |
60 if (ISO8601DateString != nil) { | |
61 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; | |
62 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSz"]; | |
63 host.updatedTime = [dateFormatter dateFromString:ISO8601DateString]; | |
64 } | |
65 | |
66 [hostList addObject:host]; | |
67 } | |
68 | |
69 return hostList; | |
70 } | |
71 | |
72 - (NSComparisonResult)compare:(Host*)host { | |
73 if (self.isOnline != host.isOnline) { | |
74 return self.isOnline ? NSOrderedAscending : NSOrderedDescending; | |
75 } else { | |
76 return [self.hostName localizedCaseInsensitiveCompare:host.hostName]; | |
77 } | |
78 } | |
79 | |
80 @end | |
OLD | NEW |