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/domain/host_info.h" | |
10 | |
11 @implementation HostInfo | |
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<HostInfo*>*)parseListFromJSON:(NSMutableData*)data { | |
31 NSError* error; | |
32 | |
33 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data | |
34 options:kNilOptions | |
35 error:&error]; | |
36 NSDictionary* dataDict = [json objectForKey:@"data"]; | |
37 NSArray* availableHosts = [dataDict objectForKey:@"items"]; | |
38 NSMutableArray* hostList = [[NSMutableArray alloc] init]; | |
39 NSUInteger idx = 0; | |
40 NSDictionary* svr; | |
41 NSUInteger count = [availableHosts count]; | |
42 | |
43 while (idx < count) { | |
44 svr = [availableHosts objectAtIndex:idx++]; | |
45 HostInfo* host = [[HostInfo alloc] init]; | |
46 host.createdTime = [svr objectForKey:@"createdTime"]; | |
47 host.hostId = [svr objectForKey:@"hostId"]; | |
48 host.hostName = [svr objectForKey:@"hostName"]; | |
49 host.hostVersion = [svr objectForKey:@"hostVersion"]; | |
50 host.jabberId = [svr objectForKey:@"jabberId"]; | |
51 host.kind = [svr objectForKey:@"kind"]; | |
52 host.publicKey = [svr objectForKey:@"publicKey"]; | |
53 host.status = [svr objectForKey:@"status"]; | |
54 | |
55 NSString* ISO8601DateString = [svr objectForKey:@"updatedTime"]; | |
56 if (ISO8601DateString != nil) { | |
57 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; | |
58 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSz"]; | |
59 host.updatedTime = [dateFormatter dateFromString:ISO8601DateString]; | |
60 } | |
61 | |
62 [hostList addObject:host]; | |
63 } | |
64 | |
65 return hostList; | |
66 } | |
67 | |
68 - (NSComparisonResult)compare:(HostInfo*)host { | |
69 if (self.isOnline != host.isOnline) { | |
70 return self.isOnline ? NSOrderedAscending : NSOrderedDescending; | |
71 } else { | |
72 return [self.hostName localizedCaseInsensitiveCompare:host.hostName]; | |
73 } | |
74 } | |
75 | |
76 - (NSString*)description { | |
77 return | |
78 [NSString stringWithFormat:@"HostInfo: name=%@ status=%@ updatedTime= %@", | |
79 _hostName, _status, _updatedTime]; | |
80 } | |
81 | |
82 @end | |
OLD | NEW |