-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Make MeViewControllers observe changes to account #4660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
daec0d9
Adds RxSwift
koke 2e39138
Add defaultWordPressComAccountInMainContext helper
koke e23dfcc
Make Me observe changes to the default account
koke 6d34ce1
Extract WPAccount directly from notifications
koke 2f92107
Set initial value for signal when account is set
koke 70dbc6c
NSUpdatedObjects is a Set not an Array
koke bb545d6
Don't reload Me model on service callback
koke eb03c9a
Make AccountService observables testable
koke ccc3496
Update RxSwift to 2.1 and add it to test target
koke 6bd8b9f
Map account using object ID
koke 38bc6cf
Adds unit tests for AccountService observables
koke 2f9a210
Moved Rx tests to a separate test case
koke 39bec54
Adds some more comments to tests
koke 56288c6
Adds documentation about thread safety
koke c7da5ea
Remove unused defaultWordPressComAccountInMainContext
koke c989d44
Post account changed notification synchronously if possible
koke e2a33db
Improved Rx tests
koke 70a31b8
Comment about the AccountService threading change
koke fc41c0c
Merge branch 'develop' into issue/me-observe-changes-to-profile
koke a78902e
Merge branch 'develop' into issue/me-observe-changes-to-profile
koke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import Foundation | ||
| import RxSwift | ||
| import RxCocoa | ||
|
|
||
| extension AccountService { | ||
| /// Observable that emits new values when the default account is set | ||
| /// | ||
| /// - warning: This should only be observed from the main thread, otherwise behavior is undefined | ||
| var defaultAccountObjectID: Observable<NSManagedObjectID?> { | ||
| return NSNotificationCenter.defaultCenter() | ||
| .rx_notification(WPAccountDefaultWordPressComAccountChangedNotification) | ||
| .map({ ($0.object as! WPAccount?)?.objectID }) | ||
| .startWith(defaultWordPressComAccount()?.objectID) | ||
| } | ||
|
|
||
| /// Observable that emits values when there is a change in the default account. | ||
| /// This can be that the default account is set or removed, or one of its properties changes. | ||
| /// | ||
| /// - warning: This should only be observed from the main thread, otherwise behavior is undefined | ||
| var defaultAccountChanged: Observable<WPAccount?> { | ||
| // Keep a reference to the context to avoid having to reference self | ||
| // within the closure | ||
| let context = managedObjectContext | ||
|
|
||
| return defaultAccountObjectID | ||
| // When the default account is set return the values of this new signal | ||
| .flatMapLatest({ (objectID) -> Observable<WPAccount?> in | ||
| if let objectID = objectID, | ||
| let account = try? context.existingObjectWithID(objectID) as? WPAccount { | ||
|
|
||
| return NSNotificationCenter.defaultCenter() | ||
| .rx_notification(NSManagedObjectContextObjectsDidChangeNotification, object: context) | ||
| // Transform the notifications into the changed account if it changed | ||
| .map({ (note) -> WPAccount? in | ||
| guard let updatedObjects = note.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObject> else { | ||
| return nil | ||
| } | ||
|
|
||
| let matchingObject = updatedObjects.filter({ $0.objectID == objectID }).first | ||
| return matchingObject as? WPAccount | ||
| }) | ||
| // A nil value here means the change didn't affect the current account | ||
| .filter({ $0 != nil }) | ||
| .startWith(account) | ||
| } else { | ||
| // If the default account was removed, just send a nil value | ||
| return Observable.just(nil) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍