diff --git a/app/src/main/java/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDao.java b/app/src/main/java/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDao.java index 05a27a816..08e54a114 100644 --- a/app/src/main/java/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDao.java +++ b/app/src/main/java/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDao.java @@ -5,6 +5,7 @@ import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.RemoteException; +import fr.free.nrw.commons.category.CategoryItem; import fr.free.nrw.commons.upload.structure.depictions.DepictedItem; import java.util.ArrayList; import java.util.Arrays; @@ -141,9 +142,17 @@ public class BookmarkItemsDao { final String instanceListString = cursor.getString(cursor.getColumnIndex(Table.COLUMN_INSTANCE_LIST)); final List instanceList = StringToArray(instanceListString); - final String categoryListString = cursor.getString(cursor - .getColumnIndex(Table.COLUMN_CATEGORIES_LIST)); - final List categoryList = StringToArray(categoryListString); + final String categoryNameListString = cursor.getString(cursor + .getColumnIndex(Table.COLUMN_CATEGORIES_NAME_LIST)); + final List categoryNameList = StringToArray(categoryNameListString); + final String categoryDescriptionListString = cursor.getString(cursor + .getColumnIndex(Table.COLUMN_CATEGORIES_DESCRIPTION_LIST)); + final List categoryDescriptionList = StringToArray(categoryDescriptionListString); + final String categoryThumbnailListString = cursor.getString(cursor + .getColumnIndex(Table.COLUMN_CATEGORIES_THUMBNAIL_LIST)); + final List categoryThumbnailList = StringToArray(categoryThumbnailListString); + final List categoryList = convertToCategoryItems(categoryNameList, + categoryDescriptionList, categoryThumbnailList); final boolean isSelected = Boolean.parseBoolean(cursor.getString(cursor .getColumnIndex(Table.COLUMN_IS_SELECTED))); @@ -160,6 +169,17 @@ public class BookmarkItemsDao { ); } + private List convertToCategoryItems(List categoryNameList, + List categoryDescriptionList, List categoryThumbnailList) { + List categoryItems = new ArrayList<>(); + for(int i=0; i namesOfCommonsCategories = new ArrayList<>(); + for (final CategoryItem category : + depictedItem.getCommonsCategories()) { + namesOfCommonsCategories.add(category.getName()); + } + + final List descriptionsOfCommonsCategories = new ArrayList<>(); + for (final CategoryItem category : + depictedItem.getCommonsCategories()) { + descriptionsOfCommonsCategories.add(category.getDescription()); + } + + final List thumbnailsOfCommonsCategories = new ArrayList<>(); + for (final CategoryItem category : + depictedItem.getCommonsCategories()) { + thumbnailsOfCommonsCategories.add(category.getThumbnail()); + } + final ContentValues cv = new ContentValues(); cv.put(Table.COLUMN_NAME, depictedItem.getName()); cv.put(Table.COLUMN_DESCRIPTION, depictedItem.getDescription()); cv.put(Table.COLUMN_IMAGE, depictedItem.getImageUrl()); cv.put(Table.COLUMN_INSTANCE_LIST, ArrayToString(depictedItem.getInstanceOfs())); - cv.put(Table.COLUMN_CATEGORIES_LIST, ArrayToString(depictedItem.getCommonsCategories())); + cv.put(Table.COLUMN_CATEGORIES_NAME_LIST, ArrayToString(namesOfCommonsCategories)); + cv.put(Table.COLUMN_CATEGORIES_DESCRIPTION_LIST, + ArrayToString(descriptionsOfCommonsCategories)); + cv.put(Table.COLUMN_CATEGORIES_THUMBNAIL_LIST, + ArrayToString(thumbnailsOfCommonsCategories)); cv.put(Table.COLUMN_IS_SELECTED, depictedItem.isSelected()); cv.put(Table.COLUMN_ID, depictedItem.getId()); return cv; @@ -208,7 +251,9 @@ public class BookmarkItemsDao { public static final String COLUMN_DESCRIPTION = "item_description"; public static final String COLUMN_IMAGE = "item_image_url"; public static final String COLUMN_INSTANCE_LIST = "item_instance_of"; - public static final String COLUMN_CATEGORIES_LIST = "item_categories"; + public static final String COLUMN_CATEGORIES_NAME_LIST = "item_name_categories"; + public static final String COLUMN_CATEGORIES_DESCRIPTION_LIST = "item_description_categories"; + public static final String COLUMN_CATEGORIES_THUMBNAIL_LIST = "item_thumbnail_categories"; public static final String COLUMN_IS_SELECTED = "item_is_selected"; public static final String COLUMN_ID = "item_id"; @@ -217,7 +262,9 @@ public class BookmarkItemsDao { COLUMN_DESCRIPTION, COLUMN_IMAGE, COLUMN_INSTANCE_LIST, - COLUMN_CATEGORIES_LIST, + COLUMN_CATEGORIES_NAME_LIST, + COLUMN_CATEGORIES_DESCRIPTION_LIST, + COLUMN_CATEGORIES_THUMBNAIL_LIST, COLUMN_IS_SELECTED, COLUMN_ID }; @@ -228,7 +275,9 @@ public class BookmarkItemsDao { + COLUMN_DESCRIPTION + " STRING," + COLUMN_IMAGE + " STRING," + COLUMN_INSTANCE_LIST + " STRING," - + COLUMN_CATEGORIES_LIST + " STRING," + + COLUMN_CATEGORIES_NAME_LIST + " STRING," + + COLUMN_CATEGORIES_DESCRIPTION_LIST + " STRING," + + COLUMN_CATEGORIES_THUMBNAIL_LIST + " STRING," + COLUMN_IS_SELECTED + " STRING," + COLUMN_ID + " STRING PRIMARY KEY" + ");"; 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 01f74c049..25a4bfde9 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 @@ -56,7 +56,7 @@ class CategoriesModel @Inject constructor( // Newly used category... if (category == null) { - category = Category(null, item.name, Date(), 0) + category = Category(null, item.name, item.description, item.thumbnail, Date(), 0) } category.incTimesUsed() categoryDao.save(category) @@ -74,14 +74,14 @@ class CategoriesModel @Inject constructor( selectedDepictions: List ): Observable> { return suggestionsOrSearch(term, imageTitleList, selectedDepictions) - .map { it.map { CategoryItem(it, false) } } + .map { it.map { CategoryItem(it.name, it.description, it.thumbnail, false) } } } private fun suggestionsOrSearch( term: String, imageTitleList: List, selectedDepictions: List - ): Observable> { + ): Observable> { return if (TextUtils.isEmpty(term)) Observable.combineLatest( categoriesFromDepiction(selectedDepictions), @@ -100,10 +100,10 @@ class CategoriesModel @Inject constructor( Observable.just(selectedDepictions.map { it.commonsCategories }.flatten()) private fun combine( - depictionCategories: List, - locationCategories: List, - titles: List, - recents: List + depictionCategories: List, + locationCategories: List, + titles: List, + recents: List ) = depictionCategories + locationCategories + titles + recents @@ -115,7 +115,7 @@ class CategoriesModel @Inject constructor( private fun titleCategories(titleList: List) = if (titleList.isNotEmpty()) Observable.combineLatest(titleList.map { getTitleCategories(it) }) { searchResults -> - searchResults.map { it as List }.flatten() + searchResults.map { it as List }.flatten() } else Observable.just(emptyList()) @@ -125,7 +125,7 @@ class CategoriesModel @Inject constructor( * @param title * @return */ - private fun getTitleCategories(title: String): Observable> { + private fun getTitleCategories(title: String): Observable> { return categoryClient.searchCategories(title, SEARCH_CATS_LIMIT).toObservable() } diff --git a/app/src/main/java/fr/free/nrw/commons/category/Category.java b/app/src/main/java/fr/free/nrw/commons/category/Category.java index 8ea3c442c..32bba67ba 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/Category.java +++ b/app/src/main/java/fr/free/nrw/commons/category/Category.java @@ -10,15 +10,19 @@ import java.util.Date; public class Category { private Uri contentUri; private String name; + private String description; + private String thumbnail; private Date lastUsed; private int timesUsed; public Category() { } - public Category(Uri contentUri, String name, Date lastUsed, int timesUsed) { + public Category(Uri contentUri, String name, String description, String thumbnail, Date lastUsed, int timesUsed) { this.contentUri = contentUri; this.name = name; + this.description = description; + this.thumbnail = thumbnail; this.lastUsed = lastUsed; this.timesUsed = timesUsed; } @@ -93,4 +97,19 @@ public class Category { this.contentUri = contentUri; } + public String getDescription() { + return description; + } + + public String getThumbnail() { + return thumbnail; + } + + public void setDescription(final String description) { + this.description = description; + } + + public void setThumbnail(final String thumbnail) { + this.thumbnail = thumbnail; + } } 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 bcdb1cdd7..441405fd4 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 @@ -16,7 +16,7 @@ const val CATEGORY_NEEDING_CATEGORIES = "needing categories" */ @Singleton class CategoryClient @Inject constructor(private val categoryInterface: CategoryInterface) : - ContinuationClient() { + ContinuationClient() { /** * Searches for categories containing the specified string. @@ -28,7 +28,7 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category */ @JvmOverloads fun searchCategories(filter: String?, itemLimit: Int, offset: Int = 0): - Single> { + Single> { return responseMapper(categoryInterface.searchCategories(filter, itemLimit, offset)) } @@ -42,7 +42,7 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category */ @JvmOverloads fun searchCategoriesForPrefix(prefix: String?, itemLimit: Int, offset: Int = 0): - Single> { + Single> { return responseMapper( categoryInterface.searchCategoriesForPrefix(prefix, itemLimit, offset) ) @@ -55,7 +55,7 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category * @param categoryName Category name as defined on commons * @return Observable emitting the categories returned. If our search yielded "Category:Test", "Test" is emitted. */ - fun getSubCategoryList(categoryName: String): Single> { + fun getSubCategoryList(categoryName: String): Single> { return continuationRequest(SUB_CATEGORY_CONTINUATION_PREFIX, categoryName) { categoryInterface.getSubCategoryList( categoryName, it @@ -70,7 +70,7 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category * @param categoryName Category name as defined on commons * @return */ - fun getParentCategoryList(categoryName: String): Single> { + fun getParentCategoryList(categoryName: String): Single> { return continuationRequest(PARENT_CATEGORY_CONTINUATION_PREFIX, categoryName) { categoryInterface.getParentCategoryList(categoryName, it) } @@ -87,7 +87,7 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category override fun responseMapper( networkResult: Single, key: String? - ): Single> { + ): Single> { return networkResult .map { handleContinuationResponse(it.continuation(), key) @@ -96,7 +96,10 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category .map { it.filter { page -> page.categoryInfo() == null || !page.categoryInfo().isHidden - }.map { page -> page.title().replace(CATEGORY_PREFIX, "") } + }.map { + CategoryItem(it.title().replace(CATEGORY_PREFIX, ""), + it.description().toString(), it.thumbUrl().toString(), false) + } } } } diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryDao.java b/app/src/main/java/fr/free/nrw/commons/category/CategoryDao.java index f51700313..4369350f4 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryDao.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryDao.java @@ -79,8 +79,8 @@ public class CategoryDao { * @return a list containing recent categories */ @NonNull - List recentCategories(int limit) { - List items = new ArrayList<>(); + List recentCategories(int limit) { + List items = new ArrayList<>(); Cursor cursor = null; ContentProviderClient db = clientProvider.get(); try { @@ -93,7 +93,9 @@ public class CategoryDao { // fixme add a limit on the original query instead of falling out of the loop? while (cursor != null && cursor.moveToNext() && cursor.getPosition() < limit) { - items.add(fromCursor(cursor).getName()); + items.add(new CategoryItem(fromCursor(cursor).getName(), + fromCursor(cursor).getDescription(), fromCursor(cursor).getThumbnail(), + false)); } } catch (RemoteException e) { throw new RuntimeException(e); @@ -112,6 +114,8 @@ public class CategoryDao { return new Category( CategoryContentProvider.uriForId(cursor.getInt(cursor.getColumnIndex(Table.COLUMN_ID))), cursor.getString(cursor.getColumnIndex(Table.COLUMN_NAME)), + cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESCRIPTION)), + cursor.getString(cursor.getColumnIndex(Table.COLUMN_THUMBNAIL)), new Date(cursor.getLong(cursor.getColumnIndex(Table.COLUMN_LAST_USED))), cursor.getInt(cursor.getColumnIndex(Table.COLUMN_TIMES_USED)) ); @@ -120,6 +124,8 @@ public class CategoryDao { private ContentValues toContentValues(Category category) { ContentValues cv = new ContentValues(); cv.put(CategoryDao.Table.COLUMN_NAME, category.getName()); + cv.put(Table.COLUMN_DESCRIPTION, category.getDescription()); + cv.put(Table.COLUMN_THUMBNAIL, category.getThumbnail()); cv.put(CategoryDao.Table.COLUMN_LAST_USED, category.getLastUsed().getTime()); cv.put(CategoryDao.Table.COLUMN_TIMES_USED, category.getTimesUsed()); return cv; @@ -130,6 +136,8 @@ public class CategoryDao { public static final String COLUMN_ID = "_id"; static final String COLUMN_NAME = "name"; + static final String COLUMN_DESCRIPTION = "description"; + static final String COLUMN_THUMBNAIL = "thumbnail"; static final String COLUMN_LAST_USED = "last_used"; static final String COLUMN_TIMES_USED = "times_used"; @@ -137,6 +145,8 @@ public class CategoryDao { public static final String[] ALL_FIELDS = { COLUMN_ID, COLUMN_NAME, + COLUMN_DESCRIPTION, + COLUMN_THUMBNAIL, COLUMN_LAST_USED, COLUMN_TIMES_USED }; @@ -146,6 +156,8 @@ public class CategoryDao { static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME + " STRING," + + COLUMN_DESCRIPTION + " STRING," + + COLUMN_THUMBNAIL + " STRING," + COLUMN_LAST_USED + " INTEGER," + COLUMN_TIMES_USED + " INTEGER" + ");"; diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryEditSearchRecyclerViewAdapter.java b/app/src/main/java/fr/free/nrw/commons/category/CategoryEditSearchRecyclerViewAdapter.java index 78342777d..962e68f20 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryEditSearchRecyclerViewAdapter.java +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryEditSearchRecyclerViewAdapter.java @@ -94,8 +94,14 @@ public class CategoryEditSearchRecyclerViewAdapter @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); - List resultCategories = categoryClient.searchCategories(constraint.toString(), 10).blockingGet(); - results.values = resultCategories; + List resultCategories = categoryClient + .searchCategories(constraint.toString(), 10).blockingGet(); + final List namesOfCommonsCategories = new ArrayList<>(); + for (final CategoryItem category : + resultCategories) { + namesOfCommonsCategories.add(category.getName()); + } + results.values = namesOfCommonsCategories; results.count = resultCategories.size(); return results; } 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 efc77e1df..969de9e55 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 @@ -20,9 +20,11 @@ public interface CategoryInterface { * @return */ @GET("w/api.php?action=query&format=json&formatversion=2" - + "&generator=search&gsrnamespace=14") + + "&generator=search&prop=description|pageimages&piprop=thumbnail&pithumbsize=70" + + "&gsrnamespace=14") Single searchCategories(@Query("gsrsearch") String filter, - @Query("gsrlimit") int itemLimit, @Query("gsroffset") int offset); + @Query("gsrlimit") int itemLimit, + @Query("gsroffset") int offset); /** * Searches for categories starting with the specified prefix. @@ -32,9 +34,11 @@ public interface CategoryInterface { * @return */ @GET("w/api.php?action=query&format=json&formatversion=2" - + "&generator=allcategories&prop=categoryinfo") + + "&generator=allcategories&prop=categoryinfo|description|pageimages&piprop=thumbnail" + + "&pithumbsize=70") Single searchCategoriesForPrefix(@Query("gacprefix") String prefix, - @Query("gaclimit") int itemLimit, @Query("gacoffset") int offset); + @Query("gaclimit") int itemLimit, + @Query("gacoffset") int offset); @GET("w/api.php?action=query&format=json&formatversion=2" + "&generator=categorymembers&gcmtype=subcat" diff --git a/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.kt b/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.kt index e6a95d511..8da0b6b6a 100644 --- a/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.kt +++ b/app/src/main/java/fr/free/nrw/commons/category/CategoryItem.kt @@ -4,7 +4,8 @@ import android.os.Parcelable import kotlinx.android.parcel.Parcelize @Parcelize -data class CategoryItem(val name: String, var isSelected: Boolean) : Parcelable { +data class CategoryItem(val name: String, val description: String, + val thumbnail: String, var isSelected: Boolean) : Parcelable { override fun toString(): String { return "CategoryItem: '$name'" diff --git a/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java b/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java index 5582a8a91..d2835a2ae 100644 --- a/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java +++ b/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java @@ -250,8 +250,10 @@ public class NetworkingModule { @Provides @Singleton - public CategoryInterface provideCategoryInterface(@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) { - return ServiceFactory.get(commonsWikiSite, BuildConfig.COMMONS_URL, CategoryInterface.class); + public CategoryInterface provideCategoryInterface( + @Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) { + return ServiceFactory + .get(commonsWikiSite, BuildConfig.COMMONS_URL, CategoryInterface.class); } @Provides diff --git a/app/src/main/java/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSource.kt b/app/src/main/java/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSource.kt index 24d2356e6..16a7cdb08 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSource.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSource.kt @@ -14,6 +14,6 @@ class PageableParentCategoriesDataSource @Inject constructor( if (startPosition == 0) { categoryClient.resetParentCategoryContinuation(query) } - categoryClient.getParentCategoryList(query).blockingGet() + categoryClient.getParentCategoryList(query).blockingGet().map { it.name } } } diff --git a/app/src/main/java/fr/free/nrw/commons/explore/categories/search/PageableSearchCategoriesDataSource.kt b/app/src/main/java/fr/free/nrw/commons/explore/categories/search/PageableSearchCategoriesDataSource.kt index bab526cbe..775462861 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/categories/search/PageableSearchCategoriesDataSource.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/categories/search/PageableSearchCategoriesDataSource.kt @@ -12,5 +12,6 @@ class PageableSearchCategoriesDataSource @Inject constructor( override val loadFunction = { loadSize: Int, startPosition: Int -> categoryClient.searchCategories(query, loadSize, startPosition).blockingGet() + .map { it.name } } } diff --git a/app/src/main/java/fr/free/nrw/commons/explore/categories/sub/PageableSubCategoriesDataSource.kt b/app/src/main/java/fr/free/nrw/commons/explore/categories/sub/PageableSubCategoriesDataSource.kt index 9e5f44966..4c1df9669 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/categories/sub/PageableSubCategoriesDataSource.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/categories/sub/PageableSubCategoriesDataSource.kt @@ -14,6 +14,6 @@ class PageableSubCategoriesDataSource @Inject constructor( if (startPosition == 0) { categoryClient.resetSubCategoryContinuation(query) } - categoryClient.getSubCategoryList(query).blockingGet() + categoryClient.getSubCategoryList(query).blockingGet().map { it.name } } } diff --git a/app/src/main/java/fr/free/nrw/commons/mwapi/CategoryApi.java b/app/src/main/java/fr/free/nrw/commons/mwapi/CategoryApi.java index 199237951..bf31b8a54 100644 --- a/app/src/main/java/fr/free/nrw/commons/mwapi/CategoryApi.java +++ b/app/src/main/java/fr/free/nrw/commons/mwapi/CategoryApi.java @@ -3,6 +3,7 @@ package fr.free.nrw.commons.mwapi; import static fr.free.nrw.commons.category.CategoryClientKt.CATEGORY_PREFIX; import com.google.gson.Gson; +import fr.free.nrw.commons.category.CategoryItem; import io.reactivex.Single; import java.util.ArrayList; import java.util.Collections; @@ -40,7 +41,7 @@ public class CategoryApi { this.gson = gson; } - public Single> request(String coords) { + public Single> request(String coords) { return Single.fromCallable(() -> { HttpUrl apiUrl = buildUrl(coords); Timber.d("URL: %s", apiUrl.toString()); @@ -53,12 +54,12 @@ public class CategoryApi { } MwQueryResponse apiResponse = gson.fromJson(body.charStream(), MwQueryResponse.class); - Set categories = new LinkedHashSet<>(); + Set categories = new LinkedHashSet<>(); if (apiResponse != null && apiResponse.query() != null && apiResponse.query().pages() != null) { for (MwQueryPage page : apiResponse.query().pages()) { if (page.categories() != null) { for (MwQueryPage.Category category : page.categories()) { - categories.add(category.title().replace(CATEGORY_PREFIX, "")); + categories.add(new CategoryItem(category.title().replace(CATEGORY_PREFIX, ""), "", "", false)); } } } diff --git a/app/src/main/java/fr/free/nrw/commons/upload/GpsCategoryModel.kt b/app/src/main/java/fr/free/nrw/commons/upload/GpsCategoryModel.kt index 088b2f39e..4fab5e6f5 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/GpsCategoryModel.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/GpsCategoryModel.kt @@ -1,18 +1,19 @@ package fr.free.nrw.commons.upload +import fr.free.nrw.commons.category.CategoryItem import io.reactivex.subjects.BehaviorSubject import javax.inject.Inject import javax.inject.Singleton @Singleton class GpsCategoryModel @Inject constructor() { - val categoriesFromLocation = BehaviorSubject.createDefault(emptyList()) + val categoriesFromLocation = BehaviorSubject.createDefault(emptyList()) fun clear() { categoriesFromLocation.onNext(emptyList()) } - fun setCategoriesFromLocation(categoryList: List) { + fun setCategoriesFromLocation(categoryList: List) { categoriesFromLocation.onNext(categoryList) } } diff --git a/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapter.kt b/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapter.kt index 99bd2ff17..e91b74129 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapter.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapter.kt @@ -1,8 +1,10 @@ package fr.free.nrw.commons.upload.categories import fr.free.nrw.commons.category.CategoryItem +import org.jetbrains.annotations.NotNull -class UploadCategoryAdapter(onCategoryClicked: (CategoryItem) -> Unit) : +class UploadCategoryAdapter( + onCategoryClicked: @NotNull() (CategoryItem) -> Unit) : BaseDelegateAdapter( uploadCategoryDelegate(onCategoryClicked), areItemsTheSame = { oldItem, newItem -> oldItem.name == newItem.name }, diff --git a/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapterDelegates.kt b/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapterDelegates.kt index dbe645a75..31863af41 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapterDelegates.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoryAdapterDelegates.kt @@ -1,20 +1,38 @@ package fr.free.nrw.commons.upload.categories +import android.view.View import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding +import fr.free.nrw.commons.R import fr.free.nrw.commons.category.CategoryItem import fr.free.nrw.commons.databinding.LayoutUploadCategoriesItemBinding fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) = - adapterDelegateViewBinding({ layoutInflater, root -> + adapterDelegateViewBinding({ layoutInflater, root -> LayoutUploadCategoriesItemBinding.inflate(layoutInflater, root, false) }) { - binding.root.setOnClickListener { + val onClickListener = { _: View? -> item.isSelected = !item.isSelected binding.uploadCategoryCheckbox.isChecked = item.isSelected onCategoryClicked(item) } + + binding.root.setOnClickListener(onClickListener) + binding.uploadCategoryCheckbox.setOnClickListener(onClickListener) + bind { binding.uploadCategoryCheckbox.isChecked = item.isSelected - binding.uploadCategoryCheckbox.text = item.name + binding.categoryLabel.text = item.name + if(item.thumbnail != "null") { + binding.categoryImage.setImageURI(item.thumbnail) + } else { + binding.categoryImage.setActualImageResource(R.drawable.commons) + } + + if(item.description != "null") { + binding.categoryDescription.text = item.description + } else { + binding.categoryDescription.text = "" + } } } diff --git a/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictedItem.kt b/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictedItem.kt index 018930fc2..c1b4aa079 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictedItem.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictedItem.kt @@ -3,6 +3,7 @@ package fr.free.nrw.commons.upload.structure.depictions import android.os.Parcelable import androidx.room.Entity import androidx.room.PrimaryKey +import fr.free.nrw.commons.category.CategoryItem import fr.free.nrw.commons.nearby.Place import fr.free.nrw.commons.upload.WikidataItem import fr.free.nrw.commons.wikidata.WikidataProperties @@ -28,7 +29,7 @@ data class DepictedItem constructor( val description: String?, val imageUrl: String?, val instanceOfs: List, - val commonsCategories: List, + val commonsCategories: List, var isSelected: Boolean, @PrimaryKey override val id: String ) : WikidataItem, Parcelable { @@ -52,7 +53,8 @@ data class DepictedItem constructor( getImageUrl(it.value, THUMB_IMAGE_SIZE) }, entity[INSTANCE_OF].toIds(), - entity[COMMONS_CATEGORY]?.map { (it.mainSnak.dataValue as DataValue.ValueString).value } + entity[COMMONS_CATEGORY]?.map { CategoryItem((it.mainSnak.dataValue as DataValue.ValueString).value, + "", "", false) } ?: emptyList(), false, entity.id() diff --git a/app/src/main/java/fr/free/nrw/commons/utils/StringSortingUtils.java b/app/src/main/java/fr/free/nrw/commons/utils/StringSortingUtils.java index cbfb93994..314467972 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/StringSortingUtils.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/StringSortingUtils.java @@ -1,5 +1,6 @@ package fr.free.nrw.commons.utils; +import fr.free.nrw.commons.category.CategoryItem; import java.util.Comparator; public class StringSortingUtils { @@ -16,10 +17,10 @@ public class StringSortingUtils { * @param filter String to compare similarity with * @return Comparator with string similarity */ - public static Comparator sortBySimilarity(final String filter) { + public static Comparator sortBySimilarity(final String filter) { return (firstItem, secondItem) -> { - double firstItemSimilarity = calculateSimilarity(firstItem, filter); - double secondItemSimilarity = calculateSimilarity(secondItem, filter); + double firstItemSimilarity = calculateSimilarity(firstItem.getName(), filter); + double secondItemSimilarity = calculateSimilarity(secondItem.getName(), filter); return (int) Math.signum(secondItemSimilarity - firstItemSimilarity); }; } diff --git a/app/src/main/res/layout/layout_upload_categories_item.xml b/app/src/main/res/layout/layout_upload_categories_item.xml index 43927ada8..1c432ca88 100644 --- a/app/src/main/res/layout/layout_upload_categories_item.xml +++ b/app/src/main/res/layout/layout_upload_categories_item.xml @@ -1,9 +1,55 @@ - + + + android:padding="@dimen/tiny_gap" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@+id/category_image" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b56c103e..821fc5ba6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -652,6 +652,8 @@ Upload your first media by tapping on the add button. The shadow of the image view of the location picker Image Location Check whether location is correct + Label + Description Items Custom Selector No Images diff --git a/app/src/test/kotlin/ModelFunctions.kt b/app/src/test/kotlin/ModelFunctions.kt index 6f1f711b8..abd058282 100644 --- a/app/src/test/kotlin/ModelFunctions.kt +++ b/app/src/test/kotlin/ModelFunctions.kt @@ -16,7 +16,7 @@ fun depictedItem( description: String = "desc", imageUrl: String = "", instanceOfs: List = listOf(), - commonsCategories: List = listOf(), + commonsCategories: List = listOf(), isSelected: Boolean = false, id: String = "entityId" ) = DepictedItem( @@ -29,8 +29,9 @@ fun depictedItem( id = id ) -fun categoryItem(name: String = "name", selected: Boolean = false) = - CategoryItem(name, selected) +fun categoryItem(name: String = "name", description: String = "desc", + thumbUrl: String = "thumbUrl", selected: Boolean = false) = + CategoryItem(name, description, thumbUrl, selected) fun media( thumbUrl: String? = "thumbUrl", diff --git a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsControllerTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsControllerTest.kt index 3e1db5a3a..ddafb38f4 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsControllerTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsControllerTest.kt @@ -1,6 +1,7 @@ package fr.free.nrw.commons.bookmarks.items import com.nhaarman.mockitokotlin2.whenever +import fr.free.nrw.commons.category.CategoryItem import fr.free.nrw.commons.upload.structure.depictions.DepictedItem import org.junit.Assert import org.junit.Before @@ -33,7 +34,8 @@ class BookmarkItemsControllerTest { list.add( DepictedItem( "name", "description", "image url", listOf("instance"), - listOf("categories"), true, "id") + listOf(CategoryItem("category name", "category description", + "category thumbnail", false)), true, "id") ) return list } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDaoTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDaoTest.kt index 056722381..73504acdc 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDaoTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDaoTest.kt @@ -10,6 +10,7 @@ import android.os.RemoteException import com.nhaarman.mockitokotlin2.* import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao.Table.* +import fr.free.nrw.commons.category.CategoryItem import fr.free.nrw.commons.upload.structure.depictions.DepictedItem import org.junit.Assert import org.junit.Before @@ -26,7 +27,9 @@ class BookmarkItemsDaoTest { COLUMN_DESCRIPTION, COLUMN_IMAGE, COLUMN_INSTANCE_LIST, - COLUMN_CATEGORIES_LIST, + COLUMN_CATEGORIES_NAME_LIST, + COLUMN_CATEGORIES_DESCRIPTION_LIST, + COLUMN_CATEGORIES_THUMBNAIL_LIST, COLUMN_IS_SELECTED, COLUMN_ID, ) @@ -43,7 +46,10 @@ class BookmarkItemsDaoTest { @Before fun setUp() { exampleItemBookmark = DepictedItem("itemName", "itemDescription", - "itemImageUrl", listOf("instance"), listOf("categories"), false, + "itemImageUrl", listOf("instance"), listOf( + CategoryItem("category name", "category description", + "category thumbnail", false) + ), false, "itemID") testObject = BookmarkItemsDao { client } } @@ -72,7 +78,9 @@ class BookmarkItemsDaoTest { Assert.assertEquals("itemDescription", it.description) Assert.assertEquals("itemImageUrl", it.imageUrl) Assert.assertEquals(listOf("instance"), it.instanceOfs) - Assert.assertEquals(listOf("categories"), it.commonsCategories) + Assert.assertEquals(listOf(CategoryItem("category name", + "category description", + "category thumbnail", false)), it.commonsCategories) Assert.assertEquals(false, it.isSelected) Assert.assertEquals("itemID", it.id) } @@ -131,7 +139,7 @@ class BookmarkItemsDaoTest { Assert.assertTrue(testObject.updateBookmarkItem(exampleItemBookmark)) verify(client).insert(eq(BookmarkItemsContentProvider.BASE_URI), captor.capture()) captor.firstValue.let { cv -> - Assert.assertEquals(7, cv.size()) + Assert.assertEquals(9, cv.size()) Assert.assertEquals( exampleItemBookmark.name, cv.getAsString(COLUMN_NAME) @@ -149,8 +157,16 @@ class BookmarkItemsDaoTest { cv.getAsString(COLUMN_INSTANCE_LIST) ) Assert.assertEquals( - exampleItemBookmark.commonsCategories[0], - cv.getAsString(COLUMN_CATEGORIES_LIST) + exampleItemBookmark.commonsCategories[0].name, + cv.getAsString(COLUMN_CATEGORIES_NAME_LIST) + ) + Assert.assertEquals( + exampleItemBookmark.commonsCategories[0].description, + cv.getAsString(COLUMN_CATEGORIES_DESCRIPTION_LIST) + ) + Assert.assertEquals( + exampleItemBookmark.commonsCategories[0].thumbnail, + cv.getAsString(COLUMN_CATEGORIES_THUMBNAIL_LIST) ) Assert.assertEquals( exampleItemBookmark.isSelected, @@ -263,8 +279,8 @@ class BookmarkItemsDaoTest { for (i in 0 until rowCount) { addRow(listOf("itemName", "itemDescription", - "itemImageUrl", "instance", "categories", false, - "itemID")) + "itemImageUrl", "instance", "category name", "category description", + "category thumbnail", false, "itemID")) } } } \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsFragmentUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsFragmentUnitTest.kt index 9cb8fd5c4..77c5b600b 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsFragmentUnitTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/bookmarks/items/BookmarkItemsFragmentUnitTest.kt @@ -14,6 +14,7 @@ import com.nhaarman.mockitokotlin2.whenever import fr.free.nrw.commons.R import fr.free.nrw.commons.TestAppAdapter import fr.free.nrw.commons.TestCommonsApplication +import fr.free.nrw.commons.category.CategoryItem import fr.free.nrw.commons.profile.ProfileActivity import fr.free.nrw.commons.upload.structure.depictions.DepictedItem import org.junit.Assert @@ -62,7 +63,10 @@ class BookmarkItemsFragmentUnitTest { list.add( DepictedItem( "name", "description", "image url", listOf("instance"), - listOf("categories"), true, "id") + listOf( + CategoryItem("category name", "category description", + "category thumbnail", false) + ), true, "id") ) return list } 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 b87792fba..4f0561b84 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 @@ -34,7 +34,8 @@ class CategoriesModelTest { fun searchAllFoundCaseTest() { val categoriesModel = CategoriesModel(categoryClient, mock(), mock()) - val expectedList = listOf("Test") + val expectedList = listOf(CategoryItem( + "Test", "", "", false)) whenever( categoryClient.searchCategoriesForPrefix( ArgumentMatchers.anyString(), @@ -45,7 +46,8 @@ class CategoriesModelTest { .thenReturn(Single.just(expectedList)) // Checking if both return "Test" - val expectedItems = expectedList.map { CategoryItem(it, false) } + val expectedItems = expectedList.map { CategoryItem( + it.name, it.description, it.thumbnail, false) } var categoryTerm = "Test" categoriesModel.searchAll(categoryTerm, emptyList(), emptyList()) .test() @@ -65,10 +67,12 @@ class CategoriesModelTest { @Test fun `searchAll with empty search terms creates results from gps, title search & recents`() { val gpsCategoryModel: GpsCategoryModel = mock() - val depictedItem = depictedItem(commonsCategories = listOf("depictionCategory")) + val depictedItem = depictedItem(commonsCategories = listOf(CategoryItem( + "depictionCategory", "", "", false))) whenever(gpsCategoryModel.categoriesFromLocation) - .thenReturn(BehaviorSubject.createDefault(listOf("gpsCategory"))) + .thenReturn(BehaviorSubject.createDefault(listOf(CategoryItem( + "gpsCategory", "", "", false)))) whenever( categoryClient.searchCategories( ArgumentMatchers.anyString(), @@ -76,8 +80,10 @@ class CategoriesModelTest { ArgumentMatchers.anyInt() ) ) - .thenReturn(Single.just(listOf("titleSearch"))) - whenever(categoryDao.recentCategories(25)).thenReturn(listOf("recentCategories")) + .thenReturn(Single.just(listOf(CategoryItem( + "titleSearch", "", "", false)))) + whenever(categoryDao.recentCategories(25)).thenReturn(listOf(CategoryItem( + "recentCategories","","", false))) val imageTitleList = listOf("Test") CategoriesModel(categoryClient, categoryDao, gpsCategoryModel) .searchAll("", imageTitleList, listOf(depictedItem)) 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 4bf057d09..07c1f2251 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 @@ -34,10 +34,10 @@ class CategoryClientTest { .thenReturn(Single.just(mockResponse)) categoryClient.searchCategories("tes", 10) .test() - .assertValues(listOf("Test")) + .assertValues(listOf(CategoryItem("Test", "", "", false))) categoryClient.searchCategories("tes", 10, 10) .test() - .assertValues(listOf("Test")) + .assertValues(listOf(CategoryItem("Test", "", "", false))) } @Test @@ -59,10 +59,10 @@ class CategoryClientTest { .thenReturn(Single.just(mockResponse)) categoryClient.searchCategoriesForPrefix("tes", 10) .test() - .assertValues(listOf("Test")) + .assertValues(listOf(CategoryItem("Test", "", "", false))) categoryClient.searchCategoriesForPrefix("tes", 10, 10) .test() - .assertValues(listOf("Test")) + .assertValues(listOf(CategoryItem("Test", "", "", false))) } @Test @@ -84,7 +84,7 @@ class CategoryClientTest { .thenReturn(Single.just(mockResponse)) categoryClient.getParentCategoryList("tes") .test() - .assertValues(listOf("Test")) + .assertValues(listOf(CategoryItem("Test", "", "", false))) } @Test @@ -104,7 +104,7 @@ class CategoryClientTest { .thenReturn(Single.just(mockResponse)) categoryClient.getSubCategoryList("tes") .test() - .assertValues(listOf("Test")) + .assertValues(listOf(CategoryItem("Test", "", "", false))) } @Test diff --git a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryDaoTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryDaoTest.kt index 89e597274..86f2a6ba0 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryDaoTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryDaoTest.kt @@ -24,7 +24,8 @@ import java.util.* @Config(sdk = [21], application = TestCommonsApplication::class) class CategoryDaoTest { - private val columns = arrayOf(COLUMN_ID, COLUMN_NAME, COLUMN_LAST_USED, COLUMN_TIMES_USED) + private val columns = arrayOf(COLUMN_ID, COLUMN_NAME, COLUMN_DESCRIPTION, + COLUMN_THUMBNAIL, COLUMN_LAST_USED, COLUMN_TIMES_USED) private val client: ContentProviderClient = mock() private val database: SQLiteDatabase = mock() private val captor = argumentCaptor() @@ -122,8 +123,10 @@ class CategoryDaoTest { verify(client).update(eq(category.contentUri), captor.capture(), isNull(), isNull()) captor.firstValue.let { cv -> - assertEquals(3, cv.size()) + assertEquals(5, cv.size()) assertEquals(category.name, cv.getAsString(COLUMN_NAME)) + assertEquals(category.description, cv.getAsString(COLUMN_DESCRIPTION)) + assertEquals(category.thumbnail, cv.getAsString(COLUMN_THUMBNAIL)) assertEquals(category.lastUsed.time, cv.getAsLong(COLUMN_LAST_USED)) assertEquals(category.timesUsed, cv.getAsInteger(COLUMN_TIMES_USED)) } @@ -134,14 +137,17 @@ class CategoryDaoTest { fun saveNewCategory() { val contentUri = CategoryContentProvider.uriForId(111) whenever(client.insert(isA(), isA())).thenReturn(contentUri) - val category = Category(null, "showImageWithItem", Date(234L), 1) + val category = Category(null, "showImageWithItem", "description", + "image", Date(234L), 1) testObject.save(category) verify(client).insert(eq(BASE_URI), captor.capture()) captor.firstValue.let { cv -> - assertEquals(3, cv.size()) + assertEquals(5, cv.size()) assertEquals(category.name, cv.getAsString(COLUMN_NAME)) + assertEquals(category.description, cv.getAsString(COLUMN_DESCRIPTION)) + assertEquals(category.thumbnail, cv.getAsString(COLUMN_THUMBNAIL)) assertEquals(category.lastUsed.time, cv.getAsLong(COLUMN_LAST_USED)) assertEquals(category.timesUsed, cv.getAsInteger(COLUMN_TIMES_USED)) assertEquals(contentUri, category.contentUri) @@ -186,6 +192,8 @@ class CategoryDaoTest { assertEquals(uriForId(1), category?.contentUri) assertEquals("showImageWithItem", category?.name) + assertEquals("description", category?.description) + assertEquals("image", category?.thumbnail) assertEquals(123L, category?.lastUsed?.time) assertEquals(2, category?.timesUsed) @@ -241,7 +249,7 @@ class CategoryDaoTest { val result = testObject.recentCategories(10) assertEquals(1, result.size) - assertEquals("showImageWithItem", result[0]) + assertEquals("showImageWithItem", result[0].name) verify(client).query( eq(BASE_URI), @@ -264,7 +272,7 @@ class CategoryDaoTest { private fun createCursor(rowCount: Int) = MatrixCursor(columns, rowCount).apply { for (i in 0 until rowCount) { - addRow(listOf("1", "showImageWithItem", "123", "2")) + addRow(listOf("1", "showImageWithItem", "description", "image", "123", "2")) } } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/upload/CategoriesPresenterTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/upload/CategoriesPresenterTest.kt index c4945e495..e45c52374 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/upload/CategoriesPresenterTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/upload/CategoriesPresenterTest.kt @@ -67,13 +67,15 @@ class CategoriesPresenterTest { ) whenever(repository.containsYear("selected")).thenReturn(false) whenever(repository.containsYear("doesContainYear")).thenReturn(true) - whenever(repository.selectedCategories).thenReturn(listOf(categoryItem("selected", true))) + whenever(repository.selectedCategories).thenReturn(listOf( + categoryItem("selected", "", "",true))) categoriesPresenter.searchForCategories("test") testScheduler.triggerActions() verify(view).showProgress(true) verify(view).showError(null) verify(view).setCategories(null) - verify(view).setCategories(listOf(categoryItem("selected", true))) + verify(view).setCategories(listOf( + categoryItem("selected", "", "", true))) verify(view).showProgress(false) verifyNoMoreInteractions(view) } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/upload/GpsCategoryModelTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/upload/GpsCategoryModelTest.kt index 59b6645bd..5d3988826 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/upload/GpsCategoryModelTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/upload/GpsCategoryModelTest.kt @@ -1,5 +1,6 @@ package fr.free.nrw.commons.upload +import fr.free.nrw.commons.category.CategoryItem import org.junit.Before import org.junit.Test @@ -18,7 +19,8 @@ class GpsCategoryModelTest { @Test fun `setCategoriesFromLocation emits the new value`() { - val expectedList = listOf("category") + val expectedList = listOf( + CategoryItem("category", "", "", false)) gpsCategoryModel.categoriesFromLocation.test() .also { gpsCategoryModel.setCategoriesFromLocation(expectedList) } .assertValues(emptyList(), expectedList) diff --git a/app/src/test/kotlin/fr/free/nrw/commons/upload/structure/depictions/DepictedItemTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/upload/structure/depictions/DepictedItemTest.kt index 43da81ae9..2cf3d046d 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/upload/structure/depictions/DepictedItemTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/upload/structure/depictions/DepictedItemTest.kt @@ -110,7 +110,7 @@ class DepictedItemTest { ) ) ) - ).commonsCategories, + ).commonsCategories.map { it.name }, listOf("1", "2")) } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/utils/StringSortingUtilsTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/utils/StringSortingUtilsTest.kt index a26c0e325..7bdee0c2e 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/utils/StringSortingUtilsTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/utils/StringSortingUtilsTest.kt @@ -1,5 +1,6 @@ package fr.free.nrw.commons.utils +import fr.free.nrw.commons.category.CategoryItem import fr.free.nrw.commons.utils.StringSortingUtils.sortBySimilarity import org.junit.Assert.assertEquals import org.junit.Test @@ -9,8 +10,18 @@ class StringSortingUtilsTest { @Test fun testSortingNumbersBySimilarity() { - val actualList = listOf("1234567", "4567", "12345", "123", "1234") - val expectedList = listOf("1234", "12345", "123", "1234567", "4567") + val actualList = listOf( + CategoryItem("1234567", "", "", false), + CategoryItem("4567", "", "", false), + CategoryItem("12345", "", "", false), + CategoryItem("123", "", "", false), + CategoryItem("1234", "", "", false)) + val expectedList = listOf( + CategoryItem("1234", "", "", false), + CategoryItem("12345", "", "", false), + CategoryItem("123", "", "", false), + CategoryItem("1234567", "", "", false), + CategoryItem("4567", "", "", false)) sort(actualList, sortBySimilarity("1234")) @@ -20,22 +31,22 @@ class StringSortingUtilsTest { @Test fun testSortingTextBySimilarity() { val actualList = listOf( - "The quick brown fox", - "quick brown fox", - "The", - "The quick ", - "The fox", - "brown fox", - "fox" + CategoryItem("The quick brown fox", "", "", false), + CategoryItem("quick brown fox", "", "", false), + CategoryItem("The", "", "", false), + CategoryItem("The quick ", "", "", false), + CategoryItem("The fox", "", "", false), + CategoryItem("brown fox", "", "", false), + CategoryItem("fox", "", "", false) ) val expectedList = listOf( - "The", - "The fox", - "The quick ", - "The quick brown fox", - "quick brown fox", - "brown fox", - "fox" + CategoryItem("The", "", "", false), + CategoryItem("The fox", "", "", false), + CategoryItem("The quick ", "", "", false), + CategoryItem("The quick brown fox", "", "", false), + CategoryItem("quick brown fox", "", "", false), + CategoryItem("brown fox", "", "", false), + CategoryItem("fox", "", "", false) ) sort(actualList, sortBySimilarity("The")) @@ -46,18 +57,18 @@ class StringSortingUtilsTest { @Test fun testSortingSymbolsBySimilarity() { val actualList = listOf( - "$$$$$", - "****", - "**$*", - "*$*$", - ".*$" + CategoryItem("$$$$$", "", "", false), + CategoryItem("****", "", "", false), + CategoryItem("**$*", "", "", false), + CategoryItem("*$*$", "", "", false), + CategoryItem(".*$", "", "", false) ) val expectedList = listOf( - "**$*", - "*$*$", - ".*$", - "****", - "$$$$$" + CategoryItem("**$*", "", "", false), + CategoryItem("*$*$", "", "", false), + CategoryItem(".*$", "", "", false), + CategoryItem("****", "", "", false), + CategoryItem("$$$$$", "", "", false) ) sort(actualList, sortBySimilarity("**$")) @@ -69,25 +80,25 @@ class StringSortingUtilsTest { fun testSortingMixedStringsBySimilarity() { // Sample from Category:2018 Android phones val actualList = listOf( - "ASUS ZenFone 5 (2018)", - "Google Pixel 3", - "HTC U12", - "Huawei P20", - "LG G7 ThinQ", - "Samsung Galaxy A8 (2018)", - "Samsung Galaxy S9", + CategoryItem("ASUS ZenFone 5 (2018)", "", "", false), + CategoryItem("Google Pixel 3", "", "", false), + CategoryItem("HTC U12", "", "", false), + CategoryItem("Huawei P20", "", "", false), + CategoryItem("LG G7 ThinQ", "", "", false), + CategoryItem("Samsung Galaxy A8 (2018)", "", "", false), + CategoryItem("Samsung Galaxy S9", "", "", false), // One with more complicated symbols - "MadeUpPhone 2018.$£#你好" + CategoryItem("MadeUpPhone 2018.$£#你好", "", "", false) ) val expectedList = listOf( - "Samsung Galaxy S9", - "ASUS ZenFone 5 (2018)", - "Samsung Galaxy A8 (2018)", - "Google Pixel 3", - "HTC U12", - "Huawei P20", - "LG G7 ThinQ", - "MadeUpPhone 2018.$£#你好" + CategoryItem("Samsung Galaxy S9", "", "", false), + CategoryItem("ASUS ZenFone 5 (2018)", "", "", false), + CategoryItem("Samsung Galaxy A8 (2018)", "", "", false), + CategoryItem("Google Pixel 3", "", "", false), + CategoryItem("HTC U12", "", "", false), + CategoryItem("Huawei P20", "", "", false), + CategoryItem("LG G7 ThinQ", "", "", false), + CategoryItem("MadeUpPhone 2018.$£#你好", "", "", false) ) sort(actualList, sortBySimilarity("S9")) @@ -98,26 +109,26 @@ class StringSortingUtilsTest { @Test fun testSortingWithEmptyStrings() { val actualList = listOf( - "brown fox", - "", - "quick brown fox", - "the", - "", - "the fox", - "fox", - "", - "" + CategoryItem("brown fox", "", "", false), + CategoryItem("", "", "", false), + CategoryItem("quick brown fox", "", "", false), + CategoryItem("the", "", "", false), + CategoryItem("", "", "", false), + CategoryItem("the fox", "", "", false), + CategoryItem("fox", "", "", false), + CategoryItem("", "", "", false), + CategoryItem("", "", "", false) ) val expectedList = listOf( - "the fox", - "brown fox", - "the", - "fox", - "quick brown fox", - "", - "", - "", - "" + CategoryItem("the fox", "", "", false), + CategoryItem("brown fox", "", "", false), + CategoryItem("the", "", "", false), + CategoryItem("fox", "", "", false), + CategoryItem("quick brown fox", "", "", false), + CategoryItem("", "", "", false), + CategoryItem("", "", "", false), + CategoryItem("", "", "", false), + CategoryItem("", "", "", false) ) sort(actualList, sortBySimilarity("the fox"))