Skip to content

Commit

Permalink
Merge pull request #2881 from wordpress-mobile/issue/2880-defaultblog…
Browse files Browse the repository at this point in the history
…-bad-merge

Pulled in mis-merged code from AccountService into BlogService
  • Loading branch information
sendhil committed Dec 4, 2014
2 parents 096eedd + 042306e commit 677fd0a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 0 additions & 1 deletion WordPress/Classes/Services/AccountService.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#import "Blog.h"
#import "WPAnalyticsTrackerMixpanel.h"
#import "BlogService.h"
#import "TodayExtensionService.h"

#import "NSString+XMLExtensions.h"

Expand Down
28 changes: 28 additions & 0 deletions WordPress/Classes/Services/BlogService.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "AccountServiceRemoteXMLRPC.h"
#import "RemoteBlog.h"
#import "NSString+XMLExtensions.h"
#import "TodayExtensionService.h"

@interface BlogService ()

Expand Down Expand Up @@ -170,6 +171,28 @@ - (void)syncBlogsForAccount:(WPAccount *)account success:(void (^)())success fai
[remote getBlogsWithSuccess:^(NSArray *blogs) {
[self.managedObjectContext performBlock:^{
[self mergeBlogs:blogs withAccount:account completion:success];

Blog *defaultBlog = account.defaultBlog;
TodayExtensionService *service = [TodayExtensionService new];
BOOL widgetIsConfigured = [service widgetIsConfigured];

if (WIDGETS_EXIST
&& !widgetIsConfigured
&& defaultBlog != nil
&& account.isWpcom) {
NSNumber *siteId = defaultBlog.blogID;
NSString *blogName = defaultBlog.blogName;
NSTimeZone *timeZone = [self timeZoneForBlog:defaultBlog];
NSString *oauth2Token = account.authToken;

dispatch_async(dispatch_get_main_queue(), ^{
TodayExtensionService *service = [TodayExtensionService new];
[service configureTodayWidgetWithSiteID:siteId
blogName:blogName
siteTimeZone:timeZone
andOAuth2Token:oauth2Token];
});
}
}];
} failure:^(NSError *error) {
DDLogError(@"Error syncing blogs: %@", error);
Expand Down Expand Up @@ -354,6 +377,11 @@ - (void)mergeBlogs:(NSArray *)blogs withAccount:(WPAccount *)account completion:
blog.url = remoteBlog.url;
blog.blogName = [remoteBlog.title stringByDecodingXMLCharacters];
blog.blogID = remoteBlog.ID;

// If non-WPcom then always default or if first from remote (assuming .com)
if (!account.isWpcom || [blogs indexOfObject:remoteBlog] == 0) {
account.defaultBlog = blog;
}
}

[[ContextManager sharedInstance] saveContext:self.managedObjectContext];
Expand Down

0 comments on commit 677fd0a

Please sign in to comment.