From 15fa93e8d004bd0f55515e6d33c7228b23d63026 Mon Sep 17 00:00:00 2001 From: Ayan Sarkar <71203077+Ayan-10@users.noreply.github.com> Date: Mon, 14 Feb 2022 16:15:59 +0530 Subject: [PATCH] Description and thumbnail is showing for the categories that are associated with Wikidata (#4803) * Depiction.commons categories showing with description and thumbnail * Test added * Test added --- .../nrw/commons/category/CategoriesModel.kt | 24 +++++++++++-- .../nrw/commons/category/CategoryClient.kt | 18 ++++++++++ .../commons/category/CategoryInterface.java | 17 ++++++++++ .../commons/category/CategoriesModelTest.kt | 5 ++- .../commons/category/CategoryClientTest.kt | 34 +++++++++++++++++++ 5 files changed, 95 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoriesModel.kt b/app/src/main/java/fr/free/nrw/commons/category/CategoriesModel.kt index 25a4bfde9..298cfd12e 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoriesModel.kt +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoriesModel.kt @@ -5,6 +5,7 @@ import fr.free.nrw.commons.upload.GpsCategoryModel import fr.free.nrw.commons.upload.structure.depictions.DepictedItem import fr.free.nrw.commons.utils.StringSortingUtils import io.reactivex.Observable +import io.reactivex.Single import io.reactivex.functions.Function4 import timber.log.Timber import java.util.* @@ -96,8 +97,27 @@ class CategoriesModel @Inject constructor( .toObservable() } - private fun categoriesFromDepiction(selectedDepictions: List) = - Observable.just(selectedDepictions.map { it.commonsCategories }.flatten()) + /** + * Fetches details of every category associated with selected depictions, converts them into + * CategoryItem and returns them in a list. + * + * @param selectedDepictions selected DepictItems + * @return List of CategoryItem associated with selected depictions + */ + private fun categoriesFromDepiction(selectedDepictions: List): + Observable>? { + return Observable.fromIterable( + selectedDepictions.map { it.commonsCategories }.flatten()) + .map { categoryItem -> + categoryClient.getCategoriesByName(categoryItem.name, + categoryItem.name, SEARCH_CATS_LIMIT).map { + + CategoryItem(it[0].name, it[0].description, + it[0].thumbnail, it[0].isSelected) + + }.blockingGet() + }.toList().toObservable() + } private fun combine( depictionCategories: List, diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryClient.kt b/app/src/main/java/fr/free/nrw/commons/category/CategoryClient.kt index 441405fd4..34af492c1 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryClient.kt +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryClient.kt @@ -48,6 +48,24 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category ) } + /** + * Fetches categories starting and ending with a specified name. + * + * @param startingCategoryName Name of the category to start + * @param endingCategoryName Name of the category to end + * @param itemLimit How many categories to return + * @param offset offset + * @return MwQueryResponse + */ + @JvmOverloads + fun getCategoriesByName(startingCategoryName: String?, endingCategoryName: String?, + itemLimit: Int, offset: Int = 0): Single> { + return responseMapper( + categoryInterface.getCategoriesByName(startingCategoryName, endingCategoryName, + itemLimit, offset) + ) + } + /** * The method takes categoryName as input and returns a List of Subcategories * It uses the generator query API to get the subcategories in a category, 500 at a time. diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryInterface.java b/app/src/main/java/fr/free/nrw/commons/category/CategoryInterface.java index 969de9e55..72db50e36 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryInterface.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryInterface.java @@ -40,6 +40,23 @@ public interface CategoryInterface { @Query("gaclimit") int itemLimit, @Query("gacoffset") int offset); + /** + * Fetches categories starting and ending with a specified name. + * + * @param startingCategory Name of the category to start + * @param endingCategory Name of the category to end + * @param itemLimit How many categories to return + * @param offset offset + * @return MwQueryResponse + */ + @GET("w/api.php?action=query&format=json&formatversion=2" + + "&generator=allcategories&prop=categoryinfo|description|pageimages&piprop=thumbnail" + + "&pithumbsize=70") + Single getCategoriesByName(@Query("gacfrom") String startingCategory, + @Query("gacto") String endingCategory, + @Query("gaclimit") int itemLimit, + @Query("gacoffset") int offset); + @GET("w/api.php?action=query&format=json&formatversion=2" + "&generator=categorymembers&gcmtype=subcat" + "&prop=info&gcmlimit=50") diff --git a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt index 4f0561b84..b2d40e5be 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt @@ -84,13 +84,16 @@ class CategoriesModelTest { "titleSearch", "", "", false)))) whenever(categoryDao.recentCategories(25)).thenReturn(listOf(CategoryItem( "recentCategories","","", false))) + whenever(categoryClient.getCategoriesByName("depictionCategory", + "depictionCategory", 25)).thenReturn(Single.just(listOf(CategoryItem( + "commonsCategories","","", false)))) val imageTitleList = listOf("Test") CategoriesModel(categoryClient, categoryDao, gpsCategoryModel) .searchAll("", imageTitleList, listOf(depictedItem)) .test() .assertValue( listOf( - categoryItem("depictionCategory"), + categoryItem("commonsCategories"), categoryItem("gpsCategory"), categoryItem("titleSearch"), categoryItem("recentCategories") diff --git a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt index 07c1f2251..9aa09db23 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt @@ -77,6 +77,40 @@ class CategoryClientTest { .test() .assertValues(emptyList()) } + + @Test + fun getCategoriesByNameFound() { + val mockResponse = withMockResponse("Category:Test") + whenever(categoryInterface.getCategoriesByName(anyString(), anyString(), + anyInt(), anyInt())) + .thenReturn(Single.just(mockResponse)) + categoryClient.getCategoriesByName("tes", "tes", 10) + .test() + .assertValues(listOf(CategoryItem("Test", "", + "", false))) + categoryClient.getCategoriesByName("tes" , "tes", + 10, 10) + .test() + .assertValues(listOf(CategoryItem("Test", "", + "", false))) + } + + @Test + fun getCategoriesByNameNull() { + val mockResponse = withNullPages() + whenever(categoryInterface.getCategoriesByName(anyString(), anyString(), + anyInt(), anyInt())) + .thenReturn(Single.just(mockResponse)) + categoryClient.getCategoriesByName("tes", "tes", + 10) + .test() + .assertValues(emptyList()) + categoryClient.getCategoriesByName("tes", "tes", + 10, 10) + .test() + .assertValues(emptyList()) + } + @Test fun getParentCategoryListFound() { val mockResponse = withMockResponse("Category:Test")