Skip to content

Commit b5199f3

Browse files
authored
Merge pull request #21976 from wordpress-mobile/tonyli-use-post-repository-post-search
Use PostRepository.search in PostSearchService
2 parents 75f4d22 + b4777e6 commit b5199f3

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
23.7
22
-----
33
* [*] Bug fix: Reader now scrolls to the top when tapping the status bar. [#21914]
4+
* [*] [internal] Refactor sending the API requests for searching posts and pages. [#21976]
45
* [*] Fix an issue in Menu screen where it fails to create default menu items. [#21949]
56

67
23.6

WordPress/Classes/Services/PostRepository.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ extension PostRepository {
343343
/// - type: `Post.self` and `Page.self` are the only acceptable types.
344344
/// - input: The text input from user. Or `nil` for searching all posts or pages.
345345
/// - statuses: Filter posts or pages with given status.
346+
/// - tag: Filter posts or pages with given tag.
346347
/// - authorUserID: Filter posts or pages that are authored by given user.
348+
/// - offset: The position of the paginated request. Pass 0 for the first page and count of already fetched results for following pages.
347349
/// - limit: Number of posts or pages should be fetched.
348350
/// - orderBy: The property by which to sort posts or pages.
349351
/// - descending: Whether to sort the results in descending order.
@@ -354,7 +356,9 @@ extension PostRepository {
354356
type: P.Type = P.self,
355357
input: String?,
356358
statuses: [BasePost.Status],
359+
tag: String?,
357360
authorUserID: NSNumber? = nil,
361+
offset: Int,
358362
limit: Int,
359363
orderBy: PostServiceResultsOrdering,
360364
descending: Bool,
@@ -364,8 +368,9 @@ extension PostRepository {
364368
type: type,
365369
searchInput: input,
366370
statuses: statuses,
371+
tag: tag,
367372
authorUserID: authorUserID,
368-
range: 0..<max(limit, 0),
373+
range: offset..<(offset + max(limit, 0)),
369374
orderBy: orderBy,
370375
descending: descending,
371376
deleteOtherLocalPosts: false,
@@ -415,6 +420,7 @@ extension PostRepository {
415420
type: P.Type,
416421
searchInput: String? = nil,
417422
statuses: [BasePost.Status]?,
423+
tag: String? = nil,
418424
authorUserID: NSNumber?,
419425
range: Range<Int>,
420426
orderBy: PostServiceResultsOrdering = .byDate,
@@ -450,7 +456,8 @@ extension PostRepository {
450456
order: descending ? .descending : .ascending,
451457
orderBy: orderBy,
452458
authorID: authorUserID,
453-
search: searchInput
459+
search: searchInput,
460+
tag: tag
454461
))
455462
let remotePosts = try await remote.getPosts(ofType: postType, options: options)
456463

WordPress/Classes/ViewRelated/Post/Search/PostSearchService.swift

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class PostSearchService {
1717
private let blog: Blog
1818
private let settings: PostListFilterSettings
1919
private let coreDataStack: CoreDataStack
20+
private let repository: PostRepository
2021

2122
private var postIDs: Set<NSManagedObjectID> = []
2223
private var offset = 0
@@ -25,12 +26,13 @@ final class PostSearchService {
2526
init(blog: Blog,
2627
settings: PostListFilterSettings,
2728
criteria: PostSearchCriteria,
28-
coreDataStack: CoreDataStack = ContextManager.shared
29+
coreDataStack: CoreDataStackSwift = ContextManager.shared
2930
) {
3031
self.blog = blog
3132
self.settings = settings
3233
self.criteria = criteria
3334
self.coreDataStack = coreDataStack
35+
self.repository = PostRepository(coreDataStack: coreDataStack)
3436
}
3537

3638
func loadMore() {
@@ -45,26 +47,30 @@ final class PostSearchService {
4547
}
4648

4749
private func _loadMore() {
48-
let options = PostServiceSyncOptions()
49-
options.number = 20
50-
options.offset = NSNumber(value: offset)
51-
options.purgesLocalSync = false
52-
options.search = criteria.searchTerm
53-
options.authorID = criteria.authorID
54-
options.tag = criteria.tag
50+
let postType = settings.postType == .post ? Post.self : Page.self
51+
let blogID = TaggedManagedObjectID(blog)
5552

56-
let postService = PostService(managedObjectContext: coreDataStack.mainContext)
57-
postService.syncPosts(
58-
ofType: settings.postType,
59-
with: options,
60-
for: blog,
61-
success: { [weak self] in
62-
self?.didLoad(with: .success($0 ?? []))
63-
},
64-
failure: { [weak self] in
65-
self?.didLoad(with: .failure($0 ?? URLError(.unknown)))
53+
Task { @MainActor [weak self, offset, criteria, repository, coreDataStack] in
54+
let result: Result<[AbstractPost], Error>
55+
do {
56+
let postIDs: [TaggedManagedObjectID<AbstractPost>] = try await repository.search(
57+
type: postType,
58+
input: criteria.searchTerm,
59+
statuses: [],
60+
tag: criteria.tag,
61+
authorUserID: criteria.authorID,
62+
offset: offset,
63+
limit: 20,
64+
orderBy: .byDate,
65+
descending: true,
66+
in: blogID
67+
)
68+
result = try .success(postIDs.map { try coreDataStack.mainContext.existingObject(with: $0) })
69+
} catch {
70+
result = .failure(error)
6671
}
67-
)
72+
self?.didLoad(with: result)
73+
}
6874
}
6975

7076
private func didLoad(with result: Result<[AbstractPost], Error>) {

WordPress/WordPressTest/PostRepositoryPostsListTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PostRepositoryPostsListTests: CoreDataTestCase {
5454
XCTAssertEqual(total, 15)
5555

5656
// Perform search
57-
let postIDs: [TaggedManagedObjectID<Post>] = try await repository.search(input: "1", statuses: [.publish], limit: 1, orderBy: .byDate, descending: true, in: blogID)
57+
let postIDs: [TaggedManagedObjectID<Post>] = try await repository.search(input: "1", statuses: [.publish], tag: nil, offset: 0, limit: 1, orderBy: .byDate, descending: true, in: blogID)
5858
XCTAssertEqual(postIDs.count, 1)
5959

6060
// There should still be 15 posts after the search: no local posts should be deleted
@@ -82,20 +82,20 @@ extension PostRepositoryPostsListTests {
8282
func testPostsListStubReturnPostsAsRequested() async throws {
8383
try await preparePostsList(type: "post", total: 20)
8484

85-
var result = try await repository.search(type: Post.self, input: nil, statuses: [], limit: 10, orderBy: .byDate, descending: true, in: blogID)
85+
var result = try await repository.search(type: Post.self, input: nil, statuses: [], tag: nil, offset: 0, limit: 10, orderBy: .byDate, descending: true, in: blogID)
8686
XCTAssertEqual(result.count, 10)
8787

88-
result = try await repository.search(type: Post.self, input: nil, statuses: [], limit: 20, orderBy: .byDate, descending: true, in: blogID)
88+
result = try await repository.search(type: Post.self, input: nil, statuses: [], tag: nil, offset: 0, limit: 20, orderBy: .byDate, descending: true, in: blogID)
8989
XCTAssertEqual(result.count, 20)
9090

91-
result = try await repository.search(type: Post.self, input: nil, statuses: [], limit: 30, orderBy: .byDate, descending: true, in: blogID)
91+
result = try await repository.search(type: Post.self, input: nil, statuses: [], tag: nil, offset: 0, limit: 30, orderBy: .byDate, descending: true, in: blogID)
9292
XCTAssertEqual(result.count, 20)
9393
}
9494

9595
func testPostsListStubReturnPostsAtCorrectPosition() async throws {
9696
try await preparePostsList(type: "post", total: 20)
9797

98-
let all = try await repository.search(type: Post.self, input: nil, statuses: [], limit: 30, orderBy: .byDate, descending: true, in: blogID)
98+
let all = try await repository.search(type: Post.self, input: nil, statuses: [], tag: nil, offset: 0, limit: 30, orderBy: .byDate, descending: true, in: blogID)
9999

100100
var result = try await repository.paginate(type: Post.self, statuses: [], offset: 0, number: 5, in: blogID)
101101
XCTAssertEqual(result, Array(all[0..<5]))
@@ -107,19 +107,19 @@ extension PostRepositoryPostsListTests {
107107
func testPostsListStubReturnPostsSearch() async throws {
108108
try await preparePostsList(type: "post", total: 10)
109109

110-
let all = try await repository.search(type: Post.self, input: nil, statuses: [], limit: 30, orderBy: .byDate, descending: true, in: blogID)
110+
let all = try await repository.search(type: Post.self, input: nil, statuses: [], tag: nil, offset: 0, limit: 30, orderBy: .byDate, descending: true, in: blogID)
111111

112-
var result = try await repository.search(type: Post.self, input: "1", statuses: [], limit: 1, orderBy: .byDate, descending: true, in: blogID)
112+
var result = try await repository.search(type: Post.self, input: "1", statuses: [], tag: nil, offset: 0, limit: 1, orderBy: .byDate, descending: true, in: blogID)
113113
XCTAssertEqual(result, [all[0]])
114114

115-
result = try await repository.search(type: Post.self, input: "2", statuses: [], limit: 1, orderBy: .byDate, descending: true, in: blogID)
115+
result = try await repository.search(type: Post.self, input: "2", statuses: [], tag: nil, offset: 0, limit: 1, orderBy: .byDate, descending: true, in: blogID)
116116
XCTAssertEqual(result, [all[1]])
117117
}
118118

119119
func testPostsListStubReturnDefaultNumberOfPosts() async throws {
120120
try await preparePostsList(type: "post", total: 100)
121121

122-
let result = try await repository.search(type: Post.self, input: nil, statuses: [], limit: 0, orderBy: .byDate, descending: true, in: blogID)
122+
let result = try await repository.search(type: Post.self, input: nil, statuses: [], tag: nil, offset: 0, limit: 0, orderBy: .byDate, descending: true, in: blogID)
123123
XCTAssertEqual(result.count, 20)
124124
}
125125

0 commit comments

Comments
 (0)