1
1
package fr.free.nrw.commons.category
2
2
3
- import io.reactivex.Observable
3
+ import io.reactivex.Single
4
4
import org.wikipedia.dataclient.mwapi.MwQueryResponse
5
5
import javax.inject.Inject
6
6
import javax.inject.Singleton
7
7
8
8
const val CATEGORY_PREFIX = " Category:"
9
+ const val SUB_CATEGORY_CONTINUATION_PREFIX = " sub_category_"
10
+ const val PARENT_CATEGORY_CONTINUATION_PREFIX = " parent_category_"
11
+
9
12
/* *
10
13
* Category Client to handle custom calls to Commons MediaWiki APIs
11
14
*/
12
15
@Singleton
13
- class CategoryClient @Inject constructor(private val categoryInterface : CategoryInterface ) {
16
+ class CategoryClient @Inject constructor(private val categoryInterface : CategoryInterface ) :
17
+ ContinuationClient <MwQueryResponse , String >() {
18
+
14
19
/* *
15
20
* Searches for categories containing the specified string.
16
21
*
@@ -21,8 +26,8 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category
21
26
*/
22
27
@JvmOverloads
23
28
fun searchCategories (filter : String? , itemLimit : Int , offset : Int = 0):
24
- Observable <List <String >> {
25
- return responseToCategoryName (categoryInterface.searchCategories(filter, itemLimit, offset))
29
+ Single <List <String >> {
30
+ return responseMapper (categoryInterface.searchCategories(filter, itemLimit, offset))
26
31
}
27
32
28
33
/* *
@@ -35,8 +40,8 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category
35
40
*/
36
41
@JvmOverloads
37
42
fun searchCategoriesForPrefix (prefix : String? , itemLimit : Int , offset : Int = 0):
38
- Observable <List <String >> {
39
- return responseToCategoryName (
43
+ Single <List <String >> {
44
+ return responseMapper (
40
45
categoryInterface.searchCategoriesForPrefix(prefix, itemLimit, offset)
41
46
)
42
47
}
@@ -48,8 +53,12 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category
48
53
* @param categoryName Category name as defined on commons
49
54
* @return Observable emitting the categories returned. If our search yielded "Category:Test", "Test" is emitted.
50
55
*/
51
- fun getSubCategoryList (categoryName : String? ): Observable <List <String >> {
52
- return responseToCategoryName(categoryInterface.getSubCategoryList(categoryName))
56
+ fun getSubCategoryList (categoryName : String ): Single <List <String >> {
57
+ return continuationRequest(SUB_CATEGORY_CONTINUATION_PREFIX , categoryName) {
58
+ categoryInterface.getSubCategoryList(
59
+ categoryName, it
60
+ )
61
+ }
53
62
}
54
63
55
64
/* *
@@ -59,19 +68,29 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category
59
68
* @param categoryName Category name as defined on commons
60
69
* @return
61
70
*/
62
- fun getParentCategoryList (categoryName : String? ): Observable <List <String >> {
63
- return responseToCategoryName(categoryInterface.getParentCategoryList(categoryName))
71
+ fun getParentCategoryList (categoryName : String ): Single <List <String >> {
72
+ return continuationRequest(PARENT_CATEGORY_CONTINUATION_PREFIX , categoryName) {
73
+ categoryInterface.getParentCategoryList(categoryName, it)
74
+ }
64
75
}
65
76
66
- /* *
67
- * Internal function to reduce code reuse. Extracts the categories returned from MwQueryResponse.
68
- *
69
- * @param responseObservable The query response observable
70
- * @return Observable emitting the categories returned. If our search yielded "Category:Test", "Test" is emitted.
71
- */
72
- private fun responseToCategoryName (responseObservable : Observable <MwQueryResponse >): Observable <List <String >> {
73
- return responseObservable
74
- .map { it.query()?.pages() ? : emptyList() }
77
+ fun resetSubCategoryContinuation (category : String ) {
78
+ resetContinuation(SUB_CATEGORY_CONTINUATION_PREFIX , category)
79
+ }
80
+
81
+ fun resetParentCategoryContinuation (category : String ) {
82
+ resetContinuation(PARENT_CATEGORY_CONTINUATION_PREFIX , category)
83
+ }
84
+
85
+ override fun responseMapper (
86
+ networkResult : Single <MwQueryResponse >,
87
+ key : String?
88
+ ): Single <List <String >> {
89
+ return networkResult
90
+ .map {
91
+ handleContinuationResponse(it.continuation(), key)
92
+ it.query()?.pages() ? : emptyList()
93
+ }
75
94
.map {
76
95
it.map { page -> page.title().replace(CATEGORY_PREFIX , " " ) }
77
96
}
0 commit comments