From aab79a5e3e43698aa1ed850ae9e850a021e3e5b1 Mon Sep 17 00:00:00 2001 From: savsch Date: Tue, 14 Jan 2025 23:05:27 +0530 Subject: [PATCH] Consider depict's P18 when suggesting categories --- .../nrw/commons/category/CategoriesModel.kt | 74 ++++++++++++++----- 1 file changed, 54 insertions(+), 20 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 7e6fee2fc..fd90be95f 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 @@ -127,30 +127,64 @@ class CategoriesModel /** * Fetches details of every category associated with selected depictions, converts them into * CategoryItem and returns them in a list. + * If a selected depiction has no categories, the categories in which its P18 belongs are + * returned in the list. * * @param selectedDepictions selected DepictItems * @return List of CategoryItem associated with selected depictions */ - private fun categoriesFromDepiction(selectedDepictions: List): Observable>? = - 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 categoriesFromDepiction(selectedDepictions: List): Observable>? { + val observables = selectedDepictions.map { depictedItem -> + if (depictedItem.commonsCategories.isEmpty()) { + if (depictedItem.primaryImage == null) { + return@map Observable.just(emptyList()) + } + Observable.just( + depictedItem.primaryImage + ).map { image -> + categoryClient + .getCategoriesOfImage( + image, + SEARCH_CATS_LIMIT, + ).map { + it.map { category -> + CategoryItem( + category.name, + category.description, + category.thumbnail, + category.isSelected, + ) + } + }.blockingGet() + }.flatMapIterable { it }.toList() + .toObservable() + } else { + Observable + .fromIterable( + depictedItem.commonsCategories, + ).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() + } + } + return Observable.concat(observables) + .scan(mutableListOf()) { accumulator, currentList -> + accumulator.apply { addAll(currentList) } + } + } /** * Fetches details of every category by their name, converts them into