diff --git a/WordPress/Classes/Services/AccountService.m b/WordPress/Classes/Services/AccountService.m index 2e284314af35..a80f7563a251 100644 --- a/WordPress/Classes/Services/AccountService.m +++ b/WordPress/Classes/Services/AccountService.m @@ -5,7 +5,6 @@ #import "Blog.h" #import "WPAnalyticsTrackerMixpanel.h" #import "BlogService.h" -#import "TodayExtensionService.h" #import "NSString+XMLExtensions.h" diff --git a/WordPress/Classes/Services/BlogService.m b/WordPress/Classes/Services/BlogService.m index 41258adc01e3..5a660693ee9b 100644 --- a/WordPress/Classes/Services/BlogService.m +++ b/WordPress/Classes/Services/BlogService.m @@ -18,6 +18,7 @@ #import "AccountServiceRemoteXMLRPC.h" #import "RemoteBlog.h" #import "NSString+XMLExtensions.h" +#import "TodayExtensionService.h" @interface BlogService () @@ -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); @@ -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];