-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17945 from wordpress-mobile/issue/17873-persist-d…
…ashboard-response My Site Dashboard: cache the response
- Loading branch information
Showing
7 changed files
with
200 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Service/BlogDashboardPersistence.swift
This file contains 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,29 @@ | ||
import Foundation | ||
|
||
class BlogDashboardPersistence { | ||
func persist(cards: NSDictionary, for wpComID: Int) { | ||
do { | ||
let directory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) | ||
let fileURL = directory.appendingPathComponent(filename(for: wpComID)) | ||
let data = try JSONSerialization.data(withJSONObject: cards, options: []) | ||
try data.write(to: fileURL, options: .atomic) | ||
} catch { | ||
// In case of an error, nothing is done | ||
} | ||
} | ||
|
||
func getCards(for wpComID: Int) -> NSDictionary? { | ||
do { | ||
let directory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) | ||
let fileURL = directory.appendingPathComponent(filename(for: wpComID)) | ||
let data = try Data(contentsOf: fileURL) | ||
return try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary | ||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
private func filename(for blogID: Int) -> String { | ||
"cards_\(blogID).json" | ||
} | ||
} |
This file contains 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 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 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
40 changes: 40 additions & 0 deletions
40
WordPress/WordPressTest/Dashboard/BlogDashboardPersistenceTests.swift
This file contains 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,40 @@ | ||
import XCTest | ||
|
||
@testable import WordPress | ||
|
||
class BlogDashboardPersistenceTests: XCTestCase { | ||
private var persistence: BlogDashboardPersistence! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
persistence = BlogDashboardPersistence() | ||
} | ||
|
||
func testSaveData() { | ||
persistence.persist(cards: cardsResponse, for: 1234) | ||
|
||
let directory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) | ||
let fileURL = directory.appendingPathComponent("cards_1234.json") | ||
let data: Data = try! Data(contentsOf: fileURL) | ||
let cardsDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary | ||
|
||
XCTAssertEqual(cardsDictionary, cardsResponse) | ||
} | ||
|
||
func testGetCards() { | ||
persistence.persist(cards: cardsResponse, for: 1235) | ||
|
||
let persistedCards = persistence.getCards(for: 1235) | ||
|
||
XCTAssertEqual(persistedCards, cardsResponse) | ||
} | ||
} | ||
|
||
private extension BlogDashboardPersistenceTests { | ||
var cardsResponse: NSDictionary { | ||
let fileURL: URL = Bundle(for: BlogDashboardPersistenceTests.self).url(forResource: "dashboard-200-with-drafts-and-scheduled.json", withExtension: nil)! | ||
let data: Data = try! Data(contentsOf: fileURL) | ||
return try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary | ||
} | ||
} |
This file contains 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