Skip to content

Commit b81f7c7

Browse files
committed
refactor: change how posts are parsed to avoid checking count on the cell
1 parent 387089c commit b81f7c7

3 files changed

Lines changed: 22 additions & 15 deletions

File tree

WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Cards/Posts/DashboardPostsCardCell.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class DashboardPostsCardCell: UICollectionViewCell, Reusable, BlogDashboardCardC
66
return
77
}
88

9-
let hasDrafts = (dataModel["draft"] as? Array<Any>)?.count ?? 0 > 0
10-
let hasScheduled = (dataModel["scheduled"] as? Array<Any>)?.count ?? 0 > 0
9+
let hasDrafts = dataModel["show_drafts"] as? Bool ?? false
10+
let hasScheduled = dataModel["show_scheduled"] as? Bool ?? false
1111

1212
let postsViewController = PostsCardViewController(blog: blog)
1313

WordPress/Classes/ViewRelated/Blog/Blog Dashboard/Service/BlogDashboardService.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,22 @@ private extension BlogDashboardService {
6363
var sections: [DashboardCardSection] = []
6464
var items: [DashboardCardModel] = []
6565

66-
let hasDrafts = (posts["draft"] as? Array<Any>)?.count ?? 0 > 0
67-
let hasScheduled = (posts["scheduled"] as? Array<Any>)?.count ?? 0 > 0
66+
let draftsCount = (posts["draft"] as? Array<Any>)?.count ?? 0
67+
let scheduledCount = (posts["scheduled"] as? Array<Any>)?.count ?? 0
68+
69+
let hasDrafts = draftsCount > 0
70+
let hasScheduled = scheduledCount > 0
6871

6972
if hasDrafts && hasScheduled {
70-
var drafts = posts.copy() as? [String: Any]
71-
drafts?["scheduled"] = []
73+
var draft = posts.copy() as? [String: Any]
74+
draft?["show_drafts"] = true
75+
draft?["show_scheduled"] = false
7276
sections.append(DashboardCardSection(id: "posts", subtype: "draft"))
73-
items.append(DashboardCardModel(id: .posts, cellViewModel: drafts as NSDictionary?))
77+
items.append(DashboardCardModel(id: .posts, cellViewModel: draft as NSDictionary?))
7478

7579
var scheduled = posts.copy() as? [String: Any]
76-
scheduled?["draft"] = []
80+
scheduled?["show_drafts"] = false
81+
scheduled?["show_scheduled"] = true
7782
sections.append(DashboardCardSection(id: "posts", subtype: "scheduled"))
7883
items.append(DashboardCardModel(id: .posts, cellViewModel: scheduled as NSDictionary?))
7984
} else {

WordPress/WordPressTest/Dashboard/BlogDashboardServiceTests.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ class BlogDashboardServiceTests: XCTestCase {
4040
// The id is posts
4141
XCTAssertEqual(snapshot.itemIdentifiers(inSection: draftsSection!).first?.id, .posts)
4242

43-
// For Drafts section, scheduled has 0 posts
44-
XCTAssertEqual((snapshot.itemIdentifiers(inSection: draftsSection!).first?.cellViewModel?["scheduled"] as? [Any])?.count, 0)
43+
// For Drafts section
44+
XCTAssertFalse(snapshot.itemIdentifiers(inSection: draftsSection!).first?.cellViewModel?["show_scheduled"] as! Bool)
45+
XCTAssertTrue(snapshot.itemIdentifiers(inSection: draftsSection!).first?.cellViewModel?["show_drafts"] as! Bool)
4546

46-
// For Scheduled, draft has 0 posts
47-
XCTAssertEqual((snapshot.itemIdentifiers(inSection: scheduledSection!).first?.cellViewModel?["draft"] as? [Any])?.count, 0)
47+
// For Scheduled section
48+
XCTAssertFalse(snapshot.itemIdentifiers(inSection: scheduledSection!).first?.cellViewModel?["show_drafts"] as! Bool)
49+
XCTAssertTrue(snapshot.itemIdentifiers(inSection: scheduledSection!).first?.cellViewModel?["show_scheduled"] as! Bool)
4850
expect.fulfill()
4951
}
5052

@@ -63,11 +65,11 @@ class BlogDashboardServiceTests: XCTestCase {
6365
// The item identifier id is posts
6466
XCTAssertEqual(snapshot.itemIdentifiers(inSection: draftsSection.first!).first?.id, .posts)
6567

66-
// For Drafts section, scheduled has 0 posts
67-
XCTAssertEqual((snapshot.itemIdentifiers(inSection: draftsSection.first!).first?.cellViewModel?["scheduled"] as? [Any])?.count, 0)
68+
// For Drafts section, showScheduled is nil
69+
XCTAssertFalse(snapshot.itemIdentifiers(inSection: draftsSection.first!).first?.cellViewModel?["show_scheduled"] as! Bool)
6870

6971
// For Drafts section, scheduled has 1 post
70-
XCTAssertEqual((snapshot.itemIdentifiers(inSection: draftsSection.first!).first?.cellViewModel?["draft"] as? [Any])?.count, 1)
72+
XCTAssertTrue(snapshot.itemIdentifiers(inSection: draftsSection.first!).first?.cellViewModel?["show_drafts"] as! Bool)
7173

7274
expect.fulfill()
7375
}

0 commit comments

Comments
 (0)