Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
72125ae
Get authToken from account
koke Dec 5, 2013
5083e71
Migrated some WordPressComApi calls to WPAccount
koke Dec 5, 2013
13b3625
Don't sync notification settings on app active
koke Dec 5, 2013
3c2ad67
Remove calls to [WordPressComApi sharedApi]
Dec 9, 2013
e48aa6d
Merge branch 'feature/22-accounts' of github.com:wordpress-mobile/Wor…
Dec 9, 2013
ac0710b
Merge pull request #750 from wordpress-mobile/issue/22-remove-comapi-…
Dec 9, 2013
7120497
Remove method that was set for removal in future
Dec 9, 2013
3e61881
Collapse WPComApi and WPAccount notifications
Dec 9, 2013
d41eb25
Use WPOAuthClient for sign in
Dec 9, 2013
199a772
Make WPAccount handle keychain and sign out tasks
Dec 9, 2013
0a9b1a2
Move notifications constants, add convenience
Dec 10, 2013
2cd2325
Decouple syncPushNotificationInfo
Dec 10, 2013
e90f493
Decouple saveNotificationSettings
Dec 10, 2013
4b1fd64
Decouple fetchNotificationSettings
Dec 10, 2013
e29fcb9
Decouple notifications unregistration
Dec 10, 2013
a8d6f01
Remove notification from WordPressComApi
Dec 10, 2013
9564c57
Decouple numerous headers from WordPressComApi
Dec 10, 2013
5072ed0
Decouple Note from WordPressComApi
Dec 10, 2013
d870a6e
Update Note with renames and API category
Dec 10, 2013
24567ee
Method name change
Dec 10, 2013
8b98e72
Add refresh single note to Note
Dec 10, 2013
aba24a9
Use Note to keep API calls organized
Dec 10, 2013
2a8b142
Merge branch 'develop' of github.com:wordpress-mobile/WordPress-iOS i…
h4xnoodle Dec 12, 2013
6a671d9
Unregister for push before account is deleted
h4xnoodle Dec 12, 2013
56b5e40
Don't listen for account changes on blogs list
koke Jan 3, 2014
a1cf6d2
Merge branch 'develop' into feature/22-accounts
koke Jan 3, 2014
2456386
Fix build errors after merging develop
koke Jan 6, 2014
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
3 changes: 2 additions & 1 deletion WordPress/Classes/Blog+Jetpack.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#import "Blog+Jetpack.h"
#import "WPAccount.h"
#import "Note.h"
#import "WordPressAppDelegate.h"
#import "ContextManager.h"
#import "WordPressComOAuthClient.h"
Expand Down Expand Up @@ -147,7 +148,7 @@ - (void)saveJetpackUsername:(NSString *)username andPassword:(NSString *)passwor

// Sadly we don't care if this succeeds or not
[account syncBlogsWithSuccess:nil failure:nil];
[account.restApi getNotificationsSince:nil success:nil failure:nil];
[Note fetchNewNotificationsWithSuccess:nil failure:nil];
}
} failure:^(NSError *error) {
DDLogError(@"Error while obtaining OAuth2 token after enabling JetPack: %@", error);
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/Blog.m
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ - (WPXMLRPCClient *)api {
// Enable compression for wp.com only, as some self hosted have connection issues
if (self.isWPcom) {
[_api setDefaultHeader:@"gzip, deflate" value:@"Accept-Encoding"];
[_api setAuthorizationHeaderWithToken:[WordPressComApi sharedApi].authToken];
[_api setAuthorizationHeaderWithToken:self.account.authToken];
}
}
return _api;
Expand Down
3 changes: 0 additions & 3 deletions WordPress/Classes/BlogListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ - (void)viewDidLoad {
target:self
action:@selector(showSettings:)];
self.navigationItem.rightBarButtonItem = self.settingsButton;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordPressComApiDidLogin:) name:WordPressComApiDidLoginNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordPressComApiDidLogout:) name:WordPressComApiDidLogoutNotification object:nil];

// Remove one-pixel gap resulting from a top-aligned grouped table view
if (IS_IPHONE) {
Expand Down
10 changes: 2 additions & 8 deletions WordPress/Classes/BlogSelectorViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ - (void)viewDidLoad {

self.navigationItem.leftBarButtonItem = cancelButtonItem;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordPressComApiDidLogin:) name:WordPressComApiDidLoginNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordPressComApiDidLogout:) name:WordPressComApiDidLogoutNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordPressComAccountChanged) name:WPAccountDefaultWordPressComAccountChangedNotification object:nil];

// Remove one-pixel gap resulting from a top-aligned grouped table view
if (IS_IPHONE) {
Expand Down Expand Up @@ -100,15 +99,10 @@ - (BOOL)hasDotComAndSelfHosted {

#pragma mark - Notifications

- (void)wordPressComApiDidLogin:(NSNotification *)notification {
- (void)wordPressComAccountChanged {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)wordPressComApiDidLogout:(NSNotification *)notification {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}


#pragma mark - Actions

- (IBAction)cancelButtonTapped:(id)sender {
Expand Down
1 change: 1 addition & 0 deletions WordPress/Classes/BlogToAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "WPAccount.h"

static NSString * const DefaultDotcomAccountDefaultsKey = @"AccountDefaultDotcom";
static NSString * const WPComXMLRPCUrl = @"https://wordpress.com/xmlrpc.php";

@implementation BlogToAccount {
NSString *_defaultWpcomUsername;
Expand Down
1 change: 1 addition & 0 deletions WordPress/Classes/BlogToJetpackAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "WPAccount.h"

static NSString * const BlogJetpackKeychainPrefix = @"jetpackblog-";
static NSString * const WPComXMLRPCUrl = @"https://wordpress.com/xmlrpc.php";

@implementation BlogToJetpackAccount

Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ static NSString *const kMobileReaderURL = @"https://en.wordpress.com/reader/mobi
static NSString *const kMobileReaderDetailURL = @"https://en.wordpress.com/reader/mobile/v2/?template=details";
static NSString *const kMobileReaderFFURL = @"https://en.wordpress.com/reader/mobile/v2/?template=friendfinder";

static NSString *const kXML_RPC_ERROR_OCCURS = @"kXML_RPC_ERROR_OCCURS";
static NSString *const kXML_RPC_ERROR_OCCURS = @"kXML_RPC_ERROR_OCCURS";
9 changes: 5 additions & 4 deletions WordPress/Classes/EditSiteViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "ReachabilityUtils.h"
#import "WPAccount.h"
#import "WPTableViewSectionHeaderView.h"
#import "NotificationsManager.h"
#import <WPXMLRPC/WPXMLRPC.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import <NSDictionary+SafeExpectations.h>
Expand Down Expand Up @@ -69,7 +70,7 @@ - (void)viewDidLoad {

_notificationPreferences = [[[NSUserDefaults standardUserDefaults] objectForKey:@"notification_preferences"] mutableCopy];
if (!_notificationPreferences) {
[[WordPressComApi sharedApi] fetchNotificationSettings:^{
[NotificationsManager fetchNotificationSettingsWithSuccess:^{
[self reloadNotificationSettings];
} failure:^(NSError *error) {
[WPError showAlertWithTitle:NSLocalizedString(@"Error", @"") message:error.localizedDescription];
Expand Down Expand Up @@ -375,8 +376,8 @@ - (void)configureTextField:(UITextField *)textField asPassword:(BOOL)asPassword
- (BOOL)canTogglePushNotifications {
return self.blog &&
([self.blog isWPcom] || [self.blog hasJetpack]) &&
[[WordPressComApi sharedApi] hasCredentials] &&
[[NSUserDefaults standardUserDefaults] objectForKey:NotificationsDeviceToken] != nil;
[[[WPAccount defaultWordPressComAccount] restApi] hasCredentials] &&
[NotificationsManager deviceRegisteredForPushNotifications];
}

- (void)toggleGeolocation:(id)sender {
Expand Down Expand Up @@ -408,7 +409,7 @@ - (void)togglePushNotifications:(id)sender {
[[NSUserDefaults standardUserDefaults] setValue:_notificationPreferences forKey:@"notification_preferences"];

// Send these settings optimistically since they're low-impact (not ideal but works for now)
[[WordPressComApi sharedApi] saveNotificationSettings:nil failure:nil];
[NotificationsManager saveNotificationSettings];
return;
}
}
Expand Down
6 changes: 6 additions & 0 deletions WordPress/Classes/FollowButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "FollowButton.h"
#import "NSString+XMLExtensions.h"
#import "WPToast.h"

@interface FollowButton ()
@property (nonatomic, strong) UIButton *button;
Expand Down Expand Up @@ -82,6 +83,11 @@ - (void)toggleFollowState:(id)sender {
[self.user followBlog:[self.siteID intValue] isFollowing:isFollowing success:^(AFHTTPRequestOperation *operation, id followResponse){
BOOL following = [[followResponse objectForKey:@"is_following"] intValue] == 1;
self.followState = following ? FollowButtonStateFollowing : FollowButtonStateNotFollowing;

NSString *message = following ? NSLocalizedString(@"Unfollowed", @"User unfollowed a blog") : NSLocalizedString(@"Followed", @"User followed a blog");
NSString *imageName = [NSString stringWithFormat:@"action_icon_%@", (following) ? @"unfollowed" : @"followed"];
[WPToast showToastWithMessage:message andImage:[UIImage imageNamed:imageName]];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unrelated to accounts and I think we're in string freeze now. Should we move this to a separate ticket/branch? @sendhil @xtreme-rebecca-putinski

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koke - we are in a string freeze, but I see both "Unfollowed" and "Followed" are already localized.

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// flip the button back since it failed
self.followState = isFollowing ? FollowButtonStateNotFollowing : FollowButtonStateFollowing;
Expand Down
9 changes: 9 additions & 0 deletions WordPress/Classes/JetpackSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "WPWalkthroughTextField.h"
#import "WPNUXSecondaryButton.h"
#import "UILabel+SuggestSize.h"
#import "WordPressComOAuthClient.h"

CGFloat const JetpackiOS7StatusBarOffset = 20.0;
CGFloat const JetpackStandardOffset = 16;
Expand Down Expand Up @@ -305,6 +306,14 @@ - (void)saveAction:(id)sender {
[_blog validateJetpackUsername:_usernameField.text
password:_passwordField.text
success:^{
if (![[[WPAccount defaultWordPressComAccount] restApi] hasCredentials]) {
[[WordPressComOAuthClient client] authenticateWithUsername:_usernameField.text password:_passwordField.text success:^(NSString *authToken) {
WPAccount *account = [WPAccount createOrUpdateWordPressComAccountWithUsername:_usernameField.text password:_passwordField.text authToken:authToken];
[WPAccount setDefaultWordPressComAccount:account];
} failure:^(NSError *error) {
DDLogWarn(@"Unabled to obtain OAuth token for account credentials provided for Jetpack blog. %@", error);
}];
}
[self setAuthenticating:NO];
if (self.completionBlock) {
self.completionBlock(YES);
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/LoginViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ - (void)createWordPressComAccountForUsername:(NSString *)username password:(NSSt
[self setAuthenticating:NO withStatusMessage:nil];
[self displayRemoteError:error];
}];
[account.restApi getNotificationsSince:nil success:nil failure:nil];
[Note fetchNewNotificationsWithSuccess:nil failure:nil];
}

- (void)createSelfHostedAccountAndBlogWithUsername:(NSString *)username password:(NSString *)password xmlrpc:(NSString *)xmlrpc options:(NSDictionary *)options
Expand Down
16 changes: 12 additions & 4 deletions WordPress/Classes/Note.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
- (BOOL)isUnread;

- (void)syncAttributes:(NSDictionary *)data;
- (NSDictionary *)getNoteData;

+ (void)syncNotesWithResponse:(NSArray *)notesData;
+ (void)refreshUnreadNotesWithContext:(NSManagedObjectContext *)context;
+ (void)mergeNewNotes:(NSArray *)notesData;

/**
Remove old notes from Core Data storage
Expand All @@ -43,6 +41,16 @@
@param context The context which contains the notes to delete.
*/
+ (void)pruneOldNotesBefore:(NSNumber *)timestamp withContext:(NSManagedObjectContext *)context;
+ (void)getNewNotificationswithContext:(NSManagedObjectContext *)context success:(void (^)(BOOL hasNewNotes))success failure:(void (^)(NSError *error))failure;

@end

@interface Note (WordPressComApi)

+ (void)fetchNewNotificationsWithSuccess:(void (^)(BOOL hasNewNotes))success failure:(void (^)(NSError *error))failure;
+ (void)refreshUnreadNotesWithContext:(NSManagedObjectContext *)context;
+ (void)fetchNotificationsSince:(NSNumber *)timestamp success:(void (^)())success failure:(void (^)(NSError *error))failure;
+ (void)fetchNotificationsBefore:(NSNumber *)timestamp success:(void (^)())success failure:(void (^)(NSError *error))failure;
- (void)refreshNoteDataWithSuccess:(void (^)())success failure:(void (^)(NSError *error))failure;
- (void)markAsReadWithSuccess:(void (^)())success failure:(void (^)(NSError *error))failure;

@end
112 changes: 83 additions & 29 deletions WordPress/Classes/Note.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ @implementation Note
@synthesize commentText = _commentText, noteData = _noteData;


+ (void)syncNotesWithResponse:(NSArray *)notesData {
+ (void)mergeNewNotes:(NSArray *)notesData {
NSManagedObjectContext *context = [[ContextManager sharedInstance] backgroundContext];
[context performBlock:^{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Note"];
Expand Down Expand Up @@ -81,15 +81,6 @@ + (void)syncNotesWithResponse:(NSArray *)notesData {
}];
}

+ (void)refreshUnreadNotesWithContext:(NSManagedObjectContext *)context {
NSFetchRequest *request = [[ContextManager sharedInstance].managedObjectModel fetchRequestTemplateForName:@"UnreadNotes"];
NSError *error = nil;
NSArray *notes = [context executeFetchRequest:request error:&error];
if ([notes count] > 0) {
[[WordPressComApi sharedApi] refreshNotifications:notes fields:@"id,unread" success:nil failure:nil];
}
}

+ (void)pruneOldNotesBefore:(NSNumber *)timestamp withContext:(NSManagedObjectContext *)context {
NSError *error;

Expand Down Expand Up @@ -128,21 +119,6 @@ + (void)pruneOldNotesBefore:(NSNumber *)timestamp withContext:(NSManagedObjectCo
[context save:&error];
}

+ (void)getNewNotificationswithContext:(NSManagedObjectContext *)context success:(void (^)(BOOL hasNewNotes))success failure:(void (^)(NSError *error))failure {
NSNumber *timestamp = [self lastNoteTimestampWithContext:context];

[[WordPressComApi sharedApi] getNotificationsSince:timestamp success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *notes = [responseObject arrayForKey:@"notes"];
if (success) {
success([notes count] > 0);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}

+ (NSNumber *)lastNoteTimestampWithContext:(NSManagedObjectContext *)context {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Note"];
request.resultType = NSDictionaryResultType;
Expand All @@ -158,10 +134,6 @@ + (NSNumber *)lastNoteTimestampWithContext:(NSManagedObjectContext *)context {
return timestamp;
}

- (NSDictionary *)getNoteData {
return self.noteData;
}

- (void)syncAttributes:(NSDictionary *)noteData {
self.payload = [NSJSONSerialization dataWithJSONObject:noteData options:0 error:nil];
self.noteData = [NSJSONSerialization JSONObjectWithData:self.payload options:NSJSONReadingMutableContainers error:nil];
Expand Down Expand Up @@ -251,3 +223,85 @@ - (void)parseComment {
}

@end

@implementation Note (WordPressComApi)

+ (void)fetchNewNotificationsWithSuccess:(void (^)(BOOL hasNewNotes))success failure:(void (^)(NSError *error))failure {
NSNumber *timestamp = [self lastNoteTimestampWithContext:[ContextManager sharedInstance].backgroundContext];

[[[WPAccount defaultWordPressComAccount] restApi] fetchNotificationsSince:timestamp success:^(NSArray *notes) {
[Note mergeNewNotes:notes];
if (success) {
success([notes count] > 0);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}

+ (void)refreshUnreadNotesWithContext:(NSManagedObjectContext *)context {
NSFetchRequest *request = [[ContextManager sharedInstance].managedObjectModel fetchRequestTemplateForName:@"UnreadNotes"];
NSError *error = nil;
NSArray *notes = [context executeFetchRequest:request error:&error];
if ([notes count] > 0) {
[[[WPAccount defaultWordPressComAccount] restApi] refreshNotifications:notes fields:@"id,unread" success:nil failure:nil];
}
}

+ (void)fetchNotificationsBefore:(NSNumber *)timestamp success:(void (^)())success failure:(void (^)(NSError *))failure {
[[[WPAccount defaultWordPressComAccount] restApi] fetchNotificationsBefore:timestamp success:^(NSArray *notes) {
[self mergeNewNotes:notes];
if (success) {
success();
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}

+ (void)fetchNotificationsSince:(NSNumber *)timestamp success:(void (^)())success failure:(void (^)(NSError *))failure {
[[[WPAccount defaultWordPressComAccount] restApi] fetchNotificationsSince:timestamp success:^(NSArray *notes) {
[self mergeNewNotes:notes];
if (success) {
success();
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}

- (void)refreshNoteDataWithSuccess:(void (^)())success failure:(void (^)(NSError *))failure {
[[[WPAccount defaultWordPressComAccount] restApi] refreshNotifications:@[self.noteID] fields:nil success:^(NSArray *updatedNotes){
if ([updatedNotes count] > 0 && ![self isDeleted] && self.managedObjectContext) {
[self syncAttributes:updatedNotes[0]];
}
[[ContextManager sharedInstance] saveContext:self.managedObjectContext];
if (success) {
success();
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}

- (void)markAsReadWithSuccess:(void (^)())success failure:(void (^)(NSError *))failure {
[[[WPAccount defaultWordPressComAccount] restApi] markNoteAsRead:self.noteID success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success();
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}

@end
13 changes: 8 additions & 5 deletions WordPress/Classes/NotificationSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#import "NSString+XMLExtensions.h"
#import "DateUtils.h"
#import "WPTableViewSectionHeaderView.h"
#import "WPAccount.h"
#import "NotificationsManager.h"

@interface NotificationSettingsViewController () <EGORefreshTableHeaderDelegate, UIActionSheetDelegate>

Expand Down Expand Up @@ -74,10 +76,10 @@ - (void)viewWillAppear:(BOOL)animated {
}

- (void)getNotificationSettings {
[[WordPressComApi sharedApi] fetchNotificationSettings:^{
[self notificationsDidFinishRefreshingWithError: nil];
[NotificationsManager fetchNotificationSettingsWithSuccess:^{
[self notificationsDidFinishRefreshingWithError:nil];
} failure:^(NSError *error) {
[self notificationsDidFinishRefreshingWithError: error];
[self notificationsDidFinishRefreshingWithError:error];
}];
}

Expand Down Expand Up @@ -251,8 +253,9 @@ - (void)dismiss {

- (void)viewWillDisappear:(BOOL)animated {
self.navigationController.toolbarHidden = YES;
if (hasChanges)
[[WordPressComApi sharedApi] saveNotificationSettings:nil failure:nil];
if (hasChanges){
[NotificationsManager saveNotificationSettings];
}
[super viewWillDisappear:animated];
}

Expand Down
Loading