Skip to content

Commit 4b22583

Browse files
authored
#3780 Create media using a combination of Entities & MwQueryResult (#3786)
* #3468 Switch from RvRenderer to AdapterDelegates - replace SearchDepictionsRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace UploadCategoryDepictionsRenderer * #3468 Switch from RvRenderer to AdapterDelegates - update BaseAdapter to be easier to use * #3468 Switch from RvRenderer to AdapterDelegates - replace SearchImagesRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace SearchCategoriesRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace NotificationRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace UploadDepictsRenderer * #3468 Switch from RvRenderer to AdapterDelegates - replace PlaceRenderer * #3756 Convert SearchDepictionsFragment to use Pagination - convert SearchDepictionsFragment * #3756 Convert SearchDepictionsFragment to use Pagination - fix presenter unit tests now that view is not nullable - fix Category prefix imports * #3756 Convert SearchDepictionsFragment to use Pagination - test DataSource related classes * #3756 Convert SearchDepictionsFragment to use Pagination - reset rx scheduler - ignore failing test * #3760 Convert SearchCategoriesFragment to use Pagination - extract functionality of pagination to base classes - add category pagination * #3772 Convert SearchImagesFragment to use Pagination - convert SearchImagesFragment - tidy up showing the empty view - make search fragments show snackbar with appropriate text * #3772 Convert SearchImagesFragment to use Pagination - allow viewpager to load more data * #3760 remove test that got re-added by merge * #3760 remove duplicate dependency * #3772 fix compilation * #3780 Create media using a combination of Entities & MwQueryResult - construct media with an entity - move fields from media down to contribution - move dynamic fields outside of media - remove unused constructors - remove all unnecessary fetching of captions/descriptions - bump database version * #3808 Construct media objects that depict an item id correctly - use generator to construct media for DepictedImages * #3780 Create media using a combination of Entities & MwQueryResult - update wikicode to align with expected behaviour * #3780 Create media using a combination of Entities & MwQueryResult - replace old site of thumbnail title with most relevant caption
1 parent bf4b7e2 commit 4b22583

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+778
-1507
lines changed

app/src/main/java/fr/free/nrw/commons/Media.java

Lines changed: 130 additions & 392 deletions
Large diffs are not rendered by default.
Lines changed: 27 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,51 @@
11
package fr.free.nrw.commons
22

33
import androidx.core.text.HtmlCompat
4-
import fr.free.nrw.commons.depictions.Media.DepictedImagesFragment
5-
import fr.free.nrw.commons.media.Depictions
4+
import fr.free.nrw.commons.depictions.Media.DepictedImagesFragment.PAGE_ID_PREFIX
5+
import fr.free.nrw.commons.media.IdAndCaptions
66
import fr.free.nrw.commons.media.MediaClient
77
import io.reactivex.Single
8-
import io.reactivex.functions.Function5
98
import timber.log.Timber
109
import javax.inject.Inject
1110
import javax.inject.Singleton
1211

1312
/**
1413
* Fetch additional media data from the network that we don't store locally.
1514
*
16-
* This includes things like category lists and multilingual descriptions,
17-
* which are not intrinsic to the media and may change due to editing.
15+
*
16+
* This includes things like category lists and multilingual descriptions, which are not intrinsic
17+
* to the media and may change due to editing.
1818
*/
1919
@Singleton
2020
class MediaDataExtractor @Inject constructor(private val mediaClient: MediaClient) {
2121

22-
/**
23-
* Simplified method to extract all details required to show media details.
24-
* It fetches media object, deletion status, talk page and captions for the filename
25-
* @param filename for which the details are to be fetched
26-
* @return full Media object with all details including deletion status and talk page
27-
*/
28-
fun fetchMediaDetails(filename: String, pageId: String?): Single<Media> {
29-
return Single.zip(
30-
getMediaFromFileName(filename),
31-
mediaClient.checkPageExistsUsingTitle("Commons:Deletion_requests/$filename"),
32-
getDiscussion(filename),
33-
if (pageId != null)
34-
getCaption(DepictedImagesFragment.PAGE_ID_PREFIX + pageId)
35-
else Single.just(MediaClient.NO_CAPTION),
36-
getDepictions(filename),
37-
Function5 { media: Media, deletionStatus: Boolean, discussion: String, caption: String, depictions: Depictions ->
38-
combineToMedia(
39-
media,
40-
deletionStatus,
41-
discussion,
42-
caption,
43-
depictions
44-
)
22+
fun fetchDepictionIdsAndLabels(media: Media) =
23+
mediaClient.getEntities(media.depictionIds)
24+
.map {
25+
it.entities()
26+
.mapValues { entry -> entry.value.labels().mapValues { it.value.value() } }
4527
}
46-
)
47-
}
28+
.map { it.map { (key, value) -> IdAndCaptions(key, value) } }
29+
.onErrorReturn { emptyList() }
4830

49-
private fun combineToMedia(
50-
media: Media,
51-
deletionStatus: Boolean,
52-
discussion: String,
53-
caption: String,
54-
depictions: Depictions
55-
): Media {
56-
media.discussion = discussion
57-
media.caption = caption
58-
media.depictions = depictions
59-
if (deletionStatus) {
60-
media.isRequestedDeletion = true
61-
}
62-
return media
63-
}
31+
fun checkDeletionRequestExists(media: Media) =
32+
mediaClient.checkPageExistsUsingTitle("Commons:Deletion_requests/" + media.filename)
6433

65-
/**
66-
* Obtains captions using filename
67-
* @param wikibaseIdentifier
68-
*
69-
* @return caption for the image in user's locale
70-
* Ex: "a nice painting" (english locale) and "No Caption" in case the caption is not available for the image
71-
*/
72-
private fun getCaption(wikibaseIdentifier: String): Single<String> {
73-
return mediaClient.getCaptionByWikibaseIdentifier(wikibaseIdentifier)
74-
}
75-
76-
/**
77-
* Fetch depictions from the MediaWiki API
78-
* @param filename the filename we will return the caption for
79-
* @return Depictions
80-
*/
81-
private fun getDepictions(filename: String): Single<Depictions> {
82-
return mediaClient.getDepictions(filename)
83-
.doOnError { throwable: Throwable? ->
84-
Timber.e(
85-
throwable,
86-
"error while fetching depictions"
87-
)
34+
fun fetchDiscussion(media: Media) =
35+
mediaClient.getPageHtml(media.filename.replace("File", "File talk"))
36+
.map { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY).toString() }
37+
.onErrorReturn {
38+
Timber.d("Error occurred while fetching discussion")
39+
""
8840
}
89-
}
9041

91-
/**
92-
* Method can be used to fetch media for a given filename
93-
* @param filename Eg. File:Test.jpg
94-
* @return return data rich Media object
95-
*/
96-
fun getMediaFromFileName(filename: String?): Single<Media> {
97-
return mediaClient.getMedia(filename)
98-
}
42+
fun refresh(media: Media): Single<Media> {
43+
return Single.ambArray(
44+
mediaClient.getMediaById(PAGE_ID_PREFIX + media.pageId)
45+
.onErrorResumeNext { Single.never() },
46+
mediaClient.getMedia(media.filename)
47+
.onErrorResumeNext { Single.never() }
48+
)
9949

100-
/**
101-
* Fetch talk page from the MediaWiki API
102-
* @param filename
103-
* @return
104-
*/
105-
private fun getDiscussion(filename: String): Single<String> {
106-
return mediaClient.getPageHtml(filename.replace("File", "File talk"))
107-
.map { discussion: String? ->
108-
HtmlCompat.fromHtml(
109-
discussion!!,
110-
HtmlCompat.FROM_HTML_MODE_LEGACY
111-
).toString()
112-
}
113-
.onErrorReturn { throwable: Throwable? ->
114-
Timber.e(throwable, "Error occurred while fetching discussion")
115-
""
116-
}
11750
}
118-
11951
}

app/src/main/java/fr/free/nrw/commons/bookmarks/pictures/BookmarkPicturesController.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package fr.free.nrw.commons.bookmarks.pictures;
22

3-
import org.apache.commons.lang3.StringUtils;
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
8-
import javax.inject.Inject;
9-
import javax.inject.Singleton;
10-
113
import fr.free.nrw.commons.Media;
124
import fr.free.nrw.commons.bookmarks.Bookmark;
135
import fr.free.nrw.commons.media.MediaClient;
146
import io.reactivex.Observable;
157
import io.reactivex.ObservableSource;
168
import io.reactivex.Single;
179
import io.reactivex.functions.Function;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import javax.inject.Inject;
13+
import javax.inject.Singleton;
1814

1915
@Singleton
2016
public class BookmarkPicturesController {
@@ -40,16 +36,13 @@ Single<List<Media>> loadBookmarkedPictures() {
4036
currentBookmarks = bookmarks;
4137
return Observable.fromIterable(bookmarks)
4238
.flatMap((Function<Bookmark, ObservableSource<Media>>) this::getMediaFromBookmark)
43-
.filter(media -> media != null && !StringUtils.isBlank(media.getFilename()))
4439
.toList();
4540
}
4641

4742
private Observable<Media> getMediaFromBookmark(Bookmark bookmark) {
48-
Media dummyMedia = new Media("");
4943
return mediaClient.getMedia(bookmark.getMediaName())
50-
.map(media -> media == null ? dummyMedia : media)
51-
.onErrorReturn(throwable -> dummyMedia)
52-
.toObservable();
44+
.toObservable()
45+
.onErrorResumeNext(Observable.empty());
5346
}
5447

5548
/**

app/src/main/java/fr/free/nrw/commons/category/CategoryImagesListFragment.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static android.view.View.GONE;
44
import static android.view.View.VISIBLE;
5-
import static fr.free.nrw.commons.depictions.Media.DepictedImagesFragment.PAGE_ID_PREFIX;
65

76
import android.annotation.SuppressLint;
87
import android.os.Bundle;
@@ -253,37 +252,6 @@ private void handleSuccess(List<Media> collection) {
253252
progressBar.setVisibility(GONE);
254253
isLoading = false;
255254
statusTextView.setVisibility(GONE);
256-
for (Media m : collection) {
257-
final String pageId = m.getPageId();
258-
if (pageId != null) {
259-
replaceTitlesWithCaptions(PAGE_ID_PREFIX + pageId, mediaSize++);
260-
}
261-
}
262-
}
263-
264-
/**
265-
* fetch captions for the image using filename and replace title of on the image thumbnail(if captions are available)
266-
* else show filename
267-
*/
268-
public void replaceTitlesWithCaptions(String wikibaseIdentifier, int i) {
269-
compositeDisposable.add(mediaClient.getCaptionByWikibaseIdentifier(wikibaseIdentifier)
270-
.subscribeOn(Schedulers.io())
271-
.observeOn(AndroidSchedulers.mainThread())
272-
.subscribe(subscriber -> {
273-
handleLabelforImage(subscriber, i);
274-
}));
275-
276-
}
277-
278-
/**
279-
* If caption is available for the image, then modify grid adapter
280-
* to show captions
281-
*/
282-
private void handleLabelforImage(String s, int position) {
283-
if (!s.trim().equals(getString(R.string.detail_caption_empty))) {
284-
gridAdapter.getItem(position).setThumbnailTitle(s);
285-
gridAdapter.notifyDataSetChanged();
286-
}
287255
}
288256

289257
/**

app/src/main/java/fr/free/nrw/commons/category/GridViewAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
8888
SimpleDraweeView imageView = convertView.findViewById(R.id.categoryImageView);
8989
TextView fileName = convertView.findViewById(R.id.categoryImageTitle);
9090
TextView author = convertView.findViewById(R.id.categoryImageAuthor);
91-
fileName.setText(item.getThumbnailTitle());
91+
fileName.setText(item.getMostRelevantCaption());
9292
setAuthorView(item, author);
9393
imageView.setImageURI(item.getThumbUrl());
9494
return convertView;

0 commit comments

Comments
 (0)