mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	Fix fetching of bookmarks (#2905)
* Fix fetching of bookmarks * With more robust tests
This commit is contained in:
		
							parent
							
								
									a003e9706f
								
							
						
					
					
						commit
						af9d991a15
					
				
					 7 changed files with 143 additions and 28 deletions
				
			
		|  | @ -9,8 +9,8 @@ public class Bookmark { | |||
|     private String mediaName; | ||||
|     private String mediaCreator; | ||||
| 
 | ||||
|     public Bookmark(String mediaName, String mediaCreator) { | ||||
|         this.contentUri = BookmarkPicturesContentProvider.uriForName(mediaName); | ||||
|     public Bookmark(String mediaName, String mediaCreator, Uri contentUri) { | ||||
|         this.contentUri = contentUri; | ||||
|         this.mediaName = mediaName == null ? "" : mediaName; | ||||
|         this.mediaCreator = mediaCreator == null ? "" : mediaCreator; | ||||
|     } | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| package fr.free.nrw.commons.bookmarks.pictures; | ||||
| 
 | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
|  | @ -9,6 +11,10 @@ import javax.inject.Singleton; | |||
| import fr.free.nrw.commons.Media; | ||||
| import fr.free.nrw.commons.bookmarks.Bookmark; | ||||
| import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient; | ||||
| import io.reactivex.Observable; | ||||
| import io.reactivex.ObservableSource; | ||||
| import io.reactivex.Single; | ||||
| import io.reactivex.functions.Function; | ||||
| 
 | ||||
| @Singleton | ||||
| public class BookmarkPicturesController { | ||||
|  | @ -30,22 +36,21 @@ public class BookmarkPicturesController { | |||
|      * Loads the Media objects from the raw data stored in DB and the API. | ||||
|      * @return a list of bookmarked Media object | ||||
|      */ | ||||
|     List<Media> loadBookmarkedPictures() { | ||||
|     Single<List<Media>> loadBookmarkedPictures() { | ||||
|         List<Bookmark> bookmarks = bookmarkDao.getAllBookmarks(); | ||||
|         currentBookmarks = bookmarks; | ||||
|         ArrayList<Media> medias = new ArrayList<>(); | ||||
|         for (Bookmark bookmark : bookmarks) { | ||||
|             List<Media> tmpMedias = okHttpJsonApiClient | ||||
|                     .getMediaList("search", bookmark.getMediaName()) | ||||
|                     .blockingGet(); | ||||
|             for (Media m : tmpMedias) { | ||||
|                 if (m.getCreator().trim().equals(bookmark.getMediaCreator().trim())) { | ||||
|                     medias.add(m); | ||||
|                     break; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         return medias; | ||||
|         return Observable.fromIterable(bookmarks) | ||||
|                 .flatMap((Function<Bookmark, ObservableSource<Media>>) this::getMediaFromBookmark) | ||||
|                 .filter(media -> media != null && !StringUtils.isBlank(media.getFilename())) | ||||
|                 .toList(); | ||||
|     } | ||||
| 
 | ||||
|     private Observable<Media> getMediaFromBookmark(Bookmark bookmark) { | ||||
|         Media dummyMedia = new Media(""); | ||||
|         return okHttpJsonApiClient.getMedia(bookmark.getMediaName(), false) | ||||
|                 .map(media -> media == null ? dummyMedia : media) | ||||
|                 .onErrorReturn(throwable -> dummyMedia) | ||||
|                 .toObservable(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | @ -54,10 +59,7 @@ public class BookmarkPicturesController { | |||
|      */ | ||||
|     boolean needRefreshBookmarkedPictures() { | ||||
|         List<Bookmark> bookmarks = bookmarkDao.getAllBookmarks(); | ||||
|         if (bookmarks.size() == currentBookmarks.size()) { | ||||
|             return false; | ||||
|         } | ||||
|         return true; | ||||
|         return bookmarks.size() != currentBookmarks.size(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  |  | |||
|  | @ -149,9 +149,11 @@ public class BookmarkPicturesDao { | |||
| 
 | ||||
|     @NonNull | ||||
|     Bookmark fromCursor(Cursor cursor) { | ||||
|         String fileName = cursor.getString(cursor.getColumnIndex(Table.COLUMN_MEDIA_NAME)); | ||||
|         return new Bookmark( | ||||
|                 cursor.getString(cursor.getColumnIndex(Table.COLUMN_MEDIA_NAME)), | ||||
|                 cursor.getString(cursor.getColumnIndex(Table.COLUMN_CREATOR)) | ||||
|                 fileName, | ||||
|                 cursor.getString(cursor.getColumnIndex(Table.COLUMN_CREATOR)), | ||||
|                 BookmarkPicturesContentProvider.uriForName(fileName) | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -2,8 +2,6 @@ package fr.free.nrw.commons.bookmarks.pictures; | |||
| 
 | ||||
| import android.annotation.SuppressLint; | ||||
| import android.os.Bundle; | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
|  | @ -19,6 +17,8 @@ import java.util.concurrent.TimeUnit; | |||
| 
 | ||||
| import javax.inject.Inject; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import butterknife.BindView; | ||||
| import butterknife.ButterKnife; | ||||
| import dagger.android.support.DaggerFragment; | ||||
|  | @ -28,7 +28,6 @@ import fr.free.nrw.commons.bookmarks.BookmarksActivity; | |||
| import fr.free.nrw.commons.category.GridViewAdapter; | ||||
| import fr.free.nrw.commons.utils.NetworkUtils; | ||||
| import fr.free.nrw.commons.utils.ViewUtil; | ||||
| import io.reactivex.Observable; | ||||
| import io.reactivex.android.schedulers.AndroidSchedulers; | ||||
| import io.reactivex.disposables.CompositeDisposable; | ||||
| import io.reactivex.schedulers.Schedulers; | ||||
|  | @ -121,7 +120,7 @@ public class BookmarkPicturesFragment extends DaggerFragment { | |||
|         progressBar.setVisibility(VISIBLE); | ||||
|         statusTextView.setVisibility(GONE); | ||||
| 
 | ||||
|         compositeDisposable.add(Observable.fromCallable(() -> controller.loadBookmarkedPictures()) | ||||
|         compositeDisposable.add(controller.loadBookmarkedPictures() | ||||
|                 .subscribeOn(Schedulers.io()) | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|                 .timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS) | ||||
|  |  | |||
|  | @ -30,6 +30,7 @@ import fr.free.nrw.commons.Media; | |||
| import fr.free.nrw.commons.R; | ||||
| import fr.free.nrw.commons.auth.SessionManager; | ||||
| import fr.free.nrw.commons.bookmarks.Bookmark; | ||||
| import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider; | ||||
| import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao; | ||||
| import fr.free.nrw.commons.category.CategoryDetailsActivity; | ||||
| import fr.free.nrw.commons.category.CategoryImagesActivity; | ||||
|  | @ -262,7 +263,8 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple | |||
|                     // Initialize bookmark object | ||||
|                     bookmark = new Bookmark( | ||||
|                             m.getFilename(), | ||||
|                             m.getCreator() | ||||
|                             m.getCreator(), | ||||
|                             BookmarkPicturesContentProvider.uriForName(m.getFilename()) | ||||
|                     ); | ||||
|                     updateBookmarkState(menu.findItem(R.id.menu_bookmark_current_image)); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vivek Maskara
						Vivek Maskara