This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOneNotePickerNavItem.m
More file actions
193 lines (157 loc) · 6.58 KB
/
OneNotePickerNavItem.m
File metadata and controls
193 lines (157 loc) · 6.58 KB
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
//
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
//
#import "OneNotePickerController.h"
#import "OneNotePickerNavItem.h"
#import "OneNotePickerSection.h"
#import "OneNotePickerSectionGroup.h"
#import "OneNotePickerNotebook.h"
NSString * const kOneNotePickerNavItemLoadedDataNotification = @"OneNotePickerNavItemLoadedDataNotification";
@interface OneNotePickerNavItem () <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property (copy, nonatomic) void (^loadCompletionBlock)(NSDictionary *);
@property (strong, nonatomic) NSMutableData *data; // For loading URLs into
@end
@implementation OneNotePickerNavItem
- (id)initWithDictionary:(NSDictionary *)dictionary;
{
if (self = [self init]) {
jsonData_ = dictionary;
self.ID = dictionary[@"id"];
self.name = dictionary[@"name"];
self.data = [NSMutableData data];
if (self.type == kOneNotePickerNavItemTypeRoot) {
self.name = @"OneNote";
}
}
return self;
}
- (NSDictionary *)resultDictionary
{
return nil;
}
- (BOOL)isLoaded
{
return self.sections != nil || (self.sectionGroups != nil && self.type == kOneNotePickerNavItemTypeRoot);
}
- (void)getOneNoteEntitiesWithToken:(NSString *)token completionBlock:(void (^)(NSDictionary *))completionBlock
{
BOOL iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
NSURL *URL = [self URLForOneNoteHierarchy];
self.loadCompletionBlock = completionBlock;
NSString *userAgent = iPad ? @"iPad OneNotePicker" : @"iPhone OneNotePicker";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request addValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[connection start];
}
- (NSURL *)URLForOneNoteHierarchy
{
return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",
kOneNotePickerRootAPIURL,
@"notebooks?$expand=sections,sectionGroups($expand=sections,sectionGroups($expand=sections;$levels=max))"]];
}
- (OneNotePickerNavItemType)type
{
return kOneNotePickerNavItemTypeRoot;
}
- (void)reset
{
jsonData_ = nil;
self.sections = nil;
self.sectionGroups = nil;
self.ID = nil;
[self.data setLength:0];
}
#pragma mark - NSURLConnectionDelegate / NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self.data setLength:0];
if (self.loadCompletionBlock) {
self.loadCompletionBlock(@{
OneNotePickerControllerIsAPIError: @(NO),
OneNotePickerControllerSystemError: error
});
self.loadCompletionBlock = nil;
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&error];
[self.data setLength:0];
// Handle connection errors
if (error) {
self.loadCompletionBlock(@{
OneNotePickerControllerIsAPIError: @(NO),
OneNotePickerControllerSystemError: error
});
return;
}
// The OneNote API might return an error - handle this case
if (json[@"error"]) {
self.loadCompletionBlock(@{
OneNotePickerControllerIsAPIError: @(YES),
OneNotePickerControllerAPIErrorCode: json[@"error"][@"code"] ?: [NSNull null],
OneNotePickerControllerAPIErrorString: json[@"error"][@"message"] ?: [NSNull null],
OneNotePickerControllerAPIErrorURL: json[@"error"][@"@api.url"] ?: [NSNull null]
});
}
// No error in the OneNote API - Parse out the returned JSON response
NSMutableArray *notebooksArray = [NSMutableArray array];
for (NSDictionary *notebookItem in json[@"value"]) {
// Build all notebooks
OneNotePickerNotebook *notebookNavItem = nil;
notebookNavItem = [[OneNotePickerNotebook alloc] initWithDictionary:notebookItem];
// Get the notebooks' sections
notebookNavItem.sections = [self getSectionsNavItemArrayFromParent:notebookItem];
// Get the notebooks' sectionGroups
notebookNavItem.sectionGroups = [self getSectionGroupsNavItemArrayFromParent:notebookItem];;
[notebooksArray addObject:notebookNavItem];
}
self.sectionGroups = notebooksArray;
[[NSNotificationCenter defaultCenter] postNotificationName:kOneNotePickerNavItemLoadedDataNotification object:self];
}
-(NSArray *)getSectionGroupsNavItemArrayFromParent:(NSDictionary *)parentItem{
NSMutableArray *sectionGroupsArray = [NSMutableArray array];
for (NSDictionary *sectionGroupItem in parentItem[@"sectionGroups"]) {
OneNotePickerSectionGroup *sectionGroupNavItem = nil;
sectionGroupNavItem = [[OneNotePickerSectionGroup alloc] initWithDictionary:sectionGroupItem];
// Get the notebooks' sections
sectionGroupNavItem.sections = [self getSectionsNavItemArrayFromParent:sectionGroupItem];
// Get the notebooks' sectionGroups
sectionGroupNavItem.sectionGroups = [self getSectionGroupsNavItemArrayFromParent:sectionGroupItem];;
[sectionGroupsArray addObject:sectionGroupNavItem];
}
return sectionGroupsArray;
}
-(NSArray *)getSectionsNavItemArrayFromParent:(NSDictionary *)parentItem{
NSMutableArray *sectionsArray = [NSMutableArray array];
for (NSDictionary *sectionItem in parentItem[@"sections"]) {
OneNotePickerSection *sectionNavItem = nil;
sectionNavItem = [[OneNotePickerSection alloc] initWithDictionary:sectionItem];
[sectionsArray addObject:sectionNavItem];
}
return sectionsArray;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
[self.data setLength:0];
if (httpResponse.statusCode >= 400 && httpResponse.statusCode < 599) {
[connection cancel];
self.loadCompletionBlock(@{
OneNotePickerControllerIsAPIError: @(YES),
OneNotePickerControllerAPIErrorCode: [NSNull null],
OneNotePickerControllerAPIErrorString: [NSString stringWithFormat: @"Failed to load API request with response code: %d", (int)httpResponse.statusCode],
OneNotePickerControllerAPIErrorURL: [NSNull null]
});
self.loadCompletionBlock = nil;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
@end