summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLConsoleController.m
blob: fcbcd78a700ea5ade6f618b0d0e1e0d5dab556e3 (plain)
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
//  SDLConsoleController.m
//

#import "SDLConsoleController.h"

#import "SDLJsonEncoder.h"
#import "SDLRPCResponse.h"


@implementation SDLConsoleController

@synthesize messageList;

- (instancetype)initWithTableView:(UITableView *)tableView {
    if (self = [super initWithStyle:UITableViewStylePlain]) {
        self.tableView = tableView;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self viewDidLoad];
    }
    return self;
}


- (void)append:(id)toAppend {
    dispatch_async(dispatch_get_main_queue(), ^{
        //Insert the new data
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

        [dictionary setObject:toAppend forKey:@"object"];
        [dictionary setObject:[NSDate date] forKey:@"date"];

        [messageList addObject:dictionary];
        NSIndexPath *newIndex = [NSIndexPath indexPathForRow:(messageList.count - 1) inSection:0];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndex] withRowAnimation:UITableViewRowAnimationNone];

        //If we were at the bottom, scroll to the new bottom.
        if (atBottom) {
            [self.tableView scrollToRowAtIndexPath:newIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }

        [self.tableView reloadData];
    });
}

- (BOOL)isLastRowVisible {
    if (messageList.count == 0) {
        return YES;
    } else {
        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:(messageList.count - 1) inSection:0];

        NSArray *visibleRowIndexes = [self.tableView indexPathsForVisibleRows];
        for (NSIndexPath *aPath in visibleRowIndexes) {
            if ([aPath compare:lastIndex] == NSOrderedSame) {
                return YES;
            }
        }
    }
    return NO;
}


#pragma mark -
#pragma mark SDLDebugTool Console Delegate

- (void)logInfo:(NSString *)info {
    [self append:info];
}

- (void)logException:(NSException *)ex withMessage:(NSString *)message {
    [self append:message];
    [self append:[ex description]];
}

- (void)logMessage:(SDLRPCMessage *)message {
    [self append:message];
}


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [SDLDebugTool addConsole:self];

    [super viewDidLoad];

    atBottom = YES;

    messageList = [[NSMutableArray alloc] initWithCapacity:100];
    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"hh:mm:ss.SSS";
}

#pragma mark -
#pragma mark Scroll View Delegate

- (void)updateWhetherScrolledToBottom {
    if ([self isLastRowVisible]) {
        atBottom = YES;
    } else {
        atBottom = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self updateWhetherScrolledToBottom];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)willDecelerate {
    [self updateWhetherScrolledToBottom];
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return messageList.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *currentDictionary = [messageList objectAtIndex:indexPath.row];
    id msg = [currentDictionary objectForKey:@"object"];

    NSString *tempdetail = [@"Time: " stringByAppendingString:[dateFormatter stringFromDate:[currentDictionary objectForKey:@"date"]]];

    if ([msg isKindOfClass:SDLRPCMessage.class]) {
        SDLRPCMessage *rpc = msg;
        NSString *title = [NSString stringWithFormat:@"%@ (%@)", rpc.name, rpc.messageType];

        cell.textLabel.text = title;

        if ([rpc.messageType isEqualToString:@"response"]) {
            SDLRPCResponse *response = (SDLRPCResponse *)rpc;

            NSString *detail = [NSString stringWithFormat:@"%@ - %@", tempdetail, [response resultCode]];
            cell.detailTextLabel.text = detail;
        } else {
            cell.detailTextLabel.text = tempdetail;
        }

    } else {
        cell.textLabel.text = msg;
        cell.detailTextLabel.text = tempdetail;
    }

    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *currentDictionary = [messageList objectAtIndex:indexPath.row];
    id obj = [currentDictionary objectForKey:@"object"];

    NSString *alertText = nil;
    if (obj == nil || [obj isKindOfClass:SDLRPCMessage.class]) {
        SDLRPCMessage *rpc = obj;
        NSDictionary *dictionary = [rpc serializeAsDictionary:2];
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error];

        if (!jsonData) {
            alertText = @"Error parsing the JSON.";
        } else {
            alertText = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }

    } else {
        alertText = [NSString stringWithFormat:@"%@", [obj description]];
    }

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"RPCMessage", nil) message:alertText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    alertView = nil;

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end