Skip to content

Commit 7bb67b4

Browse files
committed
AccountStore: Sync Sites Action
1 parent 6526d1a commit 7bb67b4

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

Yosemite/Yosemite/Stores/AccountStore.swift

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,23 @@ private extension AccountStore {
4747
return
4848
}
4949

50-
self?.upsertStoredAccount(remote: account)
50+
self?.upsertStoredAccount(readOnlyAccount: account)
5151
onCompletion(account, nil)
5252
}
5353
}
5454

55-
///
55+
/// Synchronizes the WordPress.com sites associated with the Network's Auth Token.
5656
///
5757
func synchronizeSites(onCompletion: @escaping (Error?) -> Void) {
5858
let remote = AccountRemote(network: network)
5959

60-
remote.loadSites { (sites, error) in
61-
guard error == nil else {
60+
remote.loadSites { [weak self] (sites, error) in
61+
guard let sites = sites else {
6262
onCompletion(error)
6363
return
6464
}
6565

66+
self?.upsertStoredSites(readOnlySites: sites)
6667
onCompletion(nil)
6768
}
6869
}
@@ -73,26 +74,50 @@ private extension AccountStore {
7374
//
7475
private extension AccountStore {
7576

76-
/// Updates (OR Inserts) the Storage's Account with the specified (Networking) Account entity.
77+
/// Updates (OR Inserts) the specified ReadOnly Account Entity into the Storage Layer.
7778
///
78-
func upsertStoredAccount(remote: Networking.Account) {
79+
func upsertStoredAccount(readOnlyAccount: Networking.Account) {
7980
assert(Thread.isMainThread)
8081

8182
let storage = storageManager.viewStorage
82-
let account = loadStoredAccount(userId: remote.userID) ?? storage.insertNewObject(ofType: Storage.Account.self)
83+
let storageAccount = storage.loadAccount(userId: readOnlyAccount.userID) ?? storage.insertNewObject(ofType: Storage.Account.self)
8384

84-
account.update(with: remote)
85+
storageAccount.update(with: readOnlyAccount)
8586
storage.saveIfNeeded()
8687
}
8788

88-
/// Retrieves the Stored Account.
89+
/// Updates (OR Inserts) the specified ReadOnly Site Entities into the Storage Layer.
8990
///
90-
func loadStoredAccount(userId: Int) -> Storage.Account? {
91+
func upsertStoredSites(readOnlySites: [Networking.Site]) {
9192
assert(Thread.isMainThread)
9293

93-
let predicate = NSPredicate(format: "userID = %ld", userId)
9494
let storage = storageManager.viewStorage
9595

96-
return storage.firstObject(ofType: Storage.Account.self, matching: predicate)
96+
for readOnlySite in readOnlySites {
97+
let storageSite = storage.loadSite(siteID: readOnlySite.siteID) ?? storage.insertNewObject(ofType: Storage.Site.self)
98+
storageSite.update(with: readOnlySite)
99+
}
100+
101+
storage.saveIfNeeded()
102+
}
103+
}
104+
105+
106+
// MARK: - StorageType (AccountStore) private methods.
107+
//
108+
private extension StorageType {
109+
110+
/// Retrieves the Stored Account.
111+
///
112+
func loadAccount(userId: Int) -> Storage.Account? {
113+
let predicate = NSPredicate(format: "userID = %ld", userId)
114+
return firstObject(ofType: Storage.Account.self, matching: predicate)
115+
}
116+
117+
/// Retrieves the Stored Site.
118+
///
119+
func loadSite(siteID: Int) -> Storage.Site? {
120+
let predicate = NSPredicate(format: "siteID = %ld", siteID)
121+
return firstObject(ofType: Storage.Site.self, matching: predicate)
97122
}
98123
}

0 commit comments

Comments
 (0)