mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Convert BookmarkPicturesController to kotlin
This commit is contained in:
parent
a8bf02a019
commit
5d7b7c3be4
2 changed files with 38 additions and 63 deletions
|
|
@ -1,63 +0,0 @@
|
|||
package fr.free.nrw.commons.bookmarks.pictures;
|
||||
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.bookmarks.models.Bookmark;
|
||||
import fr.free.nrw.commons.media.MediaClient;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableSource;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.functions.Function;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class BookmarkPicturesController {
|
||||
|
||||
private final MediaClient mediaClient;
|
||||
private final BookmarkPicturesDao bookmarkDao;
|
||||
|
||||
private List<Bookmark> currentBookmarks;
|
||||
|
||||
@Inject
|
||||
public BookmarkPicturesController(MediaClient mediaClient, BookmarkPicturesDao bookmarkDao) {
|
||||
this.mediaClient = mediaClient;
|
||||
this.bookmarkDao = bookmarkDao;
|
||||
currentBookmarks = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the Media objects from the raw data stored in DB and the API.
|
||||
* @return a list of bookmarked Media object
|
||||
*/
|
||||
Single<List<Media>> loadBookmarkedPictures() {
|
||||
List<Bookmark> bookmarks = bookmarkDao.getAllBookmarks();
|
||||
currentBookmarks = bookmarks;
|
||||
return Observable.fromIterable(bookmarks)
|
||||
.flatMap((Function<Bookmark, ObservableSource<Media>>) this::getMediaFromBookmark)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Observable<Media> getMediaFromBookmark(Bookmark bookmark) {
|
||||
return mediaClient.getMedia(bookmark.getMediaName())
|
||||
.toObservable()
|
||||
.onErrorResumeNext(Observable.empty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the Media objects from the raw data stored in DB and the API.
|
||||
* @return a list of bookmarked Media object
|
||||
*/
|
||||
boolean needRefreshBookmarkedPictures() {
|
||||
List<Bookmark> bookmarks = bookmarkDao.getAllBookmarks();
|
||||
return bookmarks.size() != currentBookmarks.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the requests to the API and the DB
|
||||
*/
|
||||
void stop() {
|
||||
//noop
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package fr.free.nrw.commons.bookmarks.pictures
|
||||
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.bookmarks.models.Bookmark
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class BookmarkPicturesController @Inject constructor(
|
||||
private val mediaClient: MediaClient,
|
||||
private val bookmarkDao: BookmarkPicturesDao
|
||||
) {
|
||||
private var currentBookmarks: List<Bookmark> = listOf()
|
||||
|
||||
/**
|
||||
* Loads the Media objects from the raw data stored in DB and the API.
|
||||
* @return a list of bookmarked Media object
|
||||
*/
|
||||
fun loadBookmarkedPictures(): Single<List<Media>> {
|
||||
val bookmarks = bookmarkDao.getAllBookmarks()
|
||||
currentBookmarks = bookmarks
|
||||
return Observable.fromIterable(bookmarks).flatMap {
|
||||
mediaClient.getMedia(it.mediaName)
|
||||
.toObservable()
|
||||
.onErrorResumeNext(Observable.empty())
|
||||
}.toList()
|
||||
}
|
||||
|
||||
fun needRefreshBookmarkedPictures(): Boolean {
|
||||
val bookmarks = bookmarkDao.getAllBookmarks()
|
||||
return bookmarks.size != currentBookmarks.size
|
||||
}
|
||||
|
||||
fun stop() = Unit
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue