Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions OneNotePicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@
COMBINE_HIDPI_IMAGES = YES;
DEAD_CODE_STRIPPING = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "OneNotePicker/OneNotePicker-Prefix.pch";
GCC_PRECOMPILE_PREFIX_HEADER = NO;
GCC_PREFIX_HEADER = "";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
Expand All @@ -381,8 +381,8 @@
DEAD_CODE_STRIPPING = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "OneNotePicker/OneNotePicker-Prefix.pch";
GCC_PRECOMPILE_PREFIX_HEADER = NO;
GCC_PREFIX_HEADER = "";
INFOPLIST_FILE = "OneNotePicker/OneNotePicker-Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Bundles";
LINK_WITH_STANDARD_LIBRARIES = NO;
Expand Down
9 changes: 1 addition & 8 deletions OneNotePicker/OneNotePickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ - (id)init

- (void)loadNextNavItem
{
if (self.navItemsToLoad.count == 0 && self.nextNavItemsToLoad != 0) {
[self.navItemsToLoad addObjectsFromArray:self.nextNavItemsToLoad];
[self.nextNavItemsToLoad removeAllObjects];
}
if (self.navItemsToLoad.count) {
OneNotePickerNavItem *item = [self.navItemsToLoad firstObject];
[self.navItemsToLoad removeObjectAtIndex:0];
[item loadChildrenWithToken:self.accessToken completionBlock:^(NSDictionary *errorInfo) {
[item getOneNoteEntitiesWithToken:self.accessToken completionBlock:^(NSDictionary *errorInfo) {
if (errorInfo) {
void (^notifyDelegateBlock) () = ^{
if ([self.delegate respondsToSelector:@selector(oneNotePickerController:didErrorWithInfo:)]) {
Expand All @@ -70,9 +66,6 @@ - (void)loadNextNavItem
} else {
self.onAppearHandler = notifyDelegateBlock;
}
} else {
[self.nextNavItemsToLoad addObjectsFromArray:item.sectionGroups];
[self loadNextNavItem];
}
}];
}
Expand Down
2 changes: 1 addition & 1 deletion OneNotePicker/OneNotePickerNavItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern NSString * const kOneNotePickerNavItemLoadedDataNotification;
@property (readonly, nonatomic) BOOL isLoaded;

- (id)initWithDictionary:(NSDictionary *)dictionary;
- (void)loadChildrenWithToken:(NSString *)token completionBlock:(void(^)(NSDictionary *errorInfo))completionBlock;
- (void)getOneNoteEntitiesWithToken:(NSString *)token completionBlock:(void(^)(NSDictionary *errorInfo))completionBlock;
- (NSDictionary *)resultDictionary; // Only relevant for sections
- (void)reset; // Delete all data and stop connections; ONLY call this on the root nav item

Expand Down
150 changes: 68 additions & 82 deletions OneNotePicker/OneNotePickerNavItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@

@interface OneNotePickerNavItem () <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

@property (strong, nonatomic) NSURLConnection *sectionsConnection;
@property (strong, nonatomic) NSURLConnection *sectionGroupsConnection;
@property (copy, nonatomic) void (^loadCompletionBlock)(NSDictionary *);
@property (strong, nonatomic) NSMutableData *data; // For loading URLs into

@end

@implementation OneNotePickerNavItem

- (id)initWithDictionary:(NSDictionary *)dictionary
- (id)initWithDictionary:(NSDictionary *)dictionary;
{
if (self = [self init]) {
jsonData_ = dictionary;
Expand All @@ -36,70 +34,38 @@ - (id)initWithDictionary:(NSDictionary *)dictionary
return self;
}

- (BOOL)isLoaded
{
return self.sections != nil || (self.sectionGroups != nil && self.type == kOneNotePickerNavItemTypeRoot);
}

- (NSDictionary *)resultDictionary
{
return nil;
}

- (void)loadChildrenWithToken:(NSString *)token completionBlock:(void (^)(NSDictionary *))completionBlock
{
if (self.type != kOneNotePickerNavItemTypeSection && !self.sectionGroupsConnection) {
BOOL iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
NSURL *URL = [self URLForSectionGroups];
if (self.type == kOneNotePickerNavItemTypeRoot) {
URL = [self URLForNotebooks];
}
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"];
self.sectionGroupsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

if (self.type != kOneNotePickerNavItemTypeRoot) {
URL = [self URLForSections];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request addValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
self.sectionsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
} else {
[self.sectionGroupsConnection start];
}
}
}

- (NSURL *)URLForSections
{
return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", [self baseAPIString], @"Sections"]];
}

- (NSURL *)URLForSectionGroups
- (BOOL)isLoaded
{
return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", [self baseAPIString], @"SectionGroups"]];
return self.sections != nil || (self.sectionGroups != nil && self.type == kOneNotePickerNavItemTypeRoot);
}

- (NSURL *)URLForNotebooks
- (void)getOneNoteEntitiesWithToken:(NSString *)token completionBlock:(void (^)(NSDictionary *))completionBlock
{
return [NSURL URLWithString:[self baseAPIString]];
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];
}

- (NSString *)baseAPIString
- (NSURL *)URLForOneNoteHierarchy
{
NSString *groupType = self.type == kOneNotePickerNavItemTypeSectionGroup ? @"SectionGroups" : @"Notebooks";
NSString *base = [NSString stringWithFormat:@"%@/%@",
return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",
kOneNotePickerRootAPIURL,
groupType];
if (self.type == kOneNotePickerNavItemTypeRoot) {
return base;
} else {
return [NSString stringWithFormat:@"%@/%@", base, self.ID];
}
@"notebooks?$expand=sections,sectionGroups($expand=sections,sectionGroups($expand=sections;$levels=max))"]];
}

- (OneNotePickerNavItemType)type
Expand All @@ -110,10 +76,6 @@ - (OneNotePickerNavItemType)type
- (void)reset
{
jsonData_ = nil;
[self.sectionsConnection cancel];
[self.sectionGroupsConnection cancel];
self.sectionsConnection = nil;
self.sectionGroupsConnection = nil;
self.sections = nil;
self.sectionGroups = nil;
self.ID = nil;
Expand All @@ -139,6 +101,8 @@ - (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),
Expand All @@ -147,6 +111,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
return;
}

// The OneNote API might return an error - handle this case
if (json[@"error"]) {
self.loadCompletionBlock(@{
OneNotePickerControllerIsAPIError: @(YES),
Expand All @@ -156,31 +121,52 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
});
}

NSMutableArray *array = [NSMutableArray array];
for (NSDictionary *item in json[@"value"]) {
OneNotePickerNavItem *navItem = nil;
if (connection == self.sectionGroupsConnection) {
if (self.type == kOneNotePickerNavItemTypeRoot) {
navItem = [[OneNotePickerNotebook alloc] initWithDictionary:item];
} else {
navItem = [[OneNotePickerSectionGroup alloc] initWithDictionary:item];
}
} else {
navItem = [[OneNotePickerSection alloc] initWithDictionary:item];
}
[array addObject:navItem];
}
if (connection == self.sectionGroupsConnection) {
self.sectionGroups = array;
if (self.loadCompletionBlock) {
self.loadCompletionBlock(nil);
self.loadCompletionBlock = nil;
}
[[NSNotificationCenter defaultCenter] postNotificationName:kOneNotePickerNavItemLoadedDataNotification object:self];
} else {
self.sections = array;
[self.sectionGroupsConnection start];
// 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
Expand Down
7 changes: 6 additions & 1 deletion OneNotePicker/OneNotePickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ - (id)initWithNavItem:(OneNotePickerNavItem *)navItem
target:self
action:@selector(cancelButtonClicked:)];
self.navigationItem.rightBarButtonItem = rightButton;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:kOneNotePickerNavItemLoadedDataNotification object:self.navItem];

if (navItem.type == kOneNotePickerNavItemTypeRoot){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:kOneNotePickerNavItemLoadedDataNotification object:self.navItem];
} else {
[self reloadData];
}
}
return self;
}
Expand Down