Skip to content

Commit

Permalink
preload metadata using new api
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRe1nk0 committed Dec 6, 2024
1 parent 447b1c3 commit 5e4f0da
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void handleOnBackPressed() {
DialogManager dialogManager = mapActivity.getMyApplication().getDialogManager();
GalleryController controller = (GalleryController) dialogManager.findController(GalleryController.PROCESS_ID);
if (controller == null) {
dialogManager.register(GalleryController.PROCESS_ID, new GalleryController());
dialogManager.register(GalleryController.PROCESS_ID, new GalleryController(app));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.osmand.plus.mapcontextmenu.gallery;

import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -9,21 +11,31 @@
import net.osmand.plus.base.dialog.interfaces.controller.IDialogController;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard;
import net.osmand.plus.mapcontextmenu.gallery.tasks.LoadImagesMetadataTask;
import net.osmand.plus.wikipedia.WikiImageCard;
import net.osmand.util.Algorithms;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GalleryController implements IDialogController {

public static final String PROCESS_ID = "gallery_context_controller";
private final OsmandApplication app;

private ImageCardsHolder currentCardsHolder;
private final List<WeakReference<DownloadMetadataListener>> listeners = new LinkedList<>();
private final Set<WikiImageCard> downloadingMetadata = new HashSet<>();

public GalleryController(@NonNull OsmandApplication app) {
this.app = app;
}

@NonNull
public List<ImageCard> getOnlinePhotoCards() {
Expand All @@ -39,21 +51,37 @@ public ImageCardsHolder getCurrentCardsHolder() {
return currentCardsHolder;
}

public void updateMetadata(@NonNull Map<String, Map<String, String>> metadataMap) {
if (currentCardsHolder != null) {
public void updateMetadata(@Nullable Map<String, Map<String, String>> metadataMap, @NonNull Set<WikiImageCard> cards) {
for (WikiImageCard card : cards) {
downloadingMetadata.remove(card);
}
if (currentCardsHolder != null && metadataMap != null) {
currentCardsHolder.updateWikiMetadata(metadataMap);
notifyMetaDataUpdated();
notifyMetaDataUpdated(metadataMap.keySet());
}
}

public boolean isMetadataDownloading(@NonNull WikiImageCard card){
return downloadingMetadata.contains(card);
}

public void downloadWikiMetaData(@NonNull Set<WikiImageCard> cards) {
if (Algorithms.isEmpty(cards)) {
return;
}
downloadingMetadata.addAll(cards);
LoadImagesMetadataTask task = new LoadImagesMetadataTask(app, cards);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

public void notifyMetaDataUpdated() {
public void notifyMetaDataUpdated(@NonNull Set<String> updatedMediaTagImages) {
Iterator<WeakReference<DownloadMetadataListener>> it = listeners.iterator();
while (it.hasNext()) {
DownloadMetadataListener listener = it.next().get();
if (listener == null) {
it.remove();
} else {
listener.onMetadataUpdated();
listener.onMetadataUpdated(updatedMediaTagImages);
}
}
}
Expand Down Expand Up @@ -120,6 +148,6 @@ public void clearListeners() {
}

public interface DownloadMetadataListener {
void onMetadataUpdated();
void onMetadataUpdated(@NonNull Set<String> updatedMediaTagImages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import net.osmand.util.Algorithms;
import net.osmand.wiki.Metadata;

import org.jetbrains.annotations.NotNull;
import java.util.Set;

public class GalleryDetailsFragment extends BaseOsmAndFragment implements DownloadMetadataListener {

Expand Down Expand Up @@ -185,9 +185,10 @@ private void buildItem(@NonNull ViewGroup container, @NonNull String title, @Non
}

@Override
public void onMetadataUpdated() {
public void onMetadataUpdated(@NonNull Set<String> updatedMediaTagImages) {
ImageCard imageCard = getSelectedCard();
View view = getView();
if (view != null) {
if (view != null && imageCard instanceof WikiImageCard wikiImageCard && updatedMediaTagImages.contains(wikiImageCard.getWikiImage().getWikiMediaTag())) {
updateContent(view);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import net.osmand.wiki.Metadata;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GalleryPhotoPagerFragment extends BaseOsmAndFragment implements DownloadMetadataListener {

Expand Down Expand Up @@ -79,9 +81,9 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
}

@Override
public void onMetadataUpdated() {
public void onMetadataUpdated(@NonNull Set<String> updatedMediaTagImages) {
ImageCard card = getSelectedImageCard();
if (card instanceof WikiImageCard wikiImageCard) {
if (card instanceof WikiImageCard wikiImageCard && updatedMediaTagImages.contains(wikiImageCard.getWikiImage().getWikiMediaTag())) {
Metadata metadata = wikiImageCard.getWikiImage().getMetadata();
setMetaData(metadata.getAuthor(), metadata.getDate(), metadata.getLicense());
}
Expand Down Expand Up @@ -126,7 +128,7 @@ public void onPageSelected(int position) {
boolean shouldPreloadNext = selectedPosition < position;
selectedPosition = position;
preloadThumbNails(shouldPreloadNext);
updateImageDescriptionRow(getSelectedImageCard());
updateImageDescriptionRow(getSelectedImageCard(), shouldPreloadNext);
}

@Override
Expand All @@ -139,7 +141,7 @@ public void onPageScrollStateChanged(int state) {
setupToolbar(view);
setupOnBackPressedCallback();
preloadThumbNails();
updateImageDescriptionRow(getSelectedImageCard());
updateImageDescriptionRow(getSelectedImageCard(), null);
return view;
}

Expand Down Expand Up @@ -197,11 +199,58 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}

private void updateImageDescriptionRow(@NonNull ImageCard imageCard) {
private Set<WikiImageCard> getImagesToDownloadMetadata(@NonNull WikiImageCard wikiImageCard, @Nullable Boolean preloadNext){
Set<WikiImageCard> result = new HashSet<>();
List<ImageCard> imageCards = controller.getOnlinePhotoCards();
if (imageCards.size() <= 1) {
return result;
}

if (shouldDownloadMetadata(wikiImageCard)) {
result.add(wikiImageCard);
}
if (preloadNext == null) {
addImages(imageCards, result, false, 2);
addImages(imageCards, result, true, 2);
} else {
addImages(imageCards, result, preloadNext, 4);
}

return result;
}

private void addImages(@NonNull List<ImageCard> imageList, @NonNull Set<WikiImageCard> result, boolean next, int downloadCount) {
int direction = next ? 1 : -1;
for (int i = 1; i <= downloadCount; i++) {
int currentIndex = selectedPosition + (i * direction);
if (currentIndex >= 0 && currentIndex < imageList.size()) {
ImageCard card = imageList.get(currentIndex);
if (card instanceof WikiImageCard wikiImageCard && shouldDownloadMetadata(wikiImageCard)) {
result.add(wikiImageCard);
}
}
}
}

private boolean shouldDownloadMetadata(@NonNull WikiImageCard wikiImageCard){
Metadata metadata = wikiImageCard.getWikiImage().getMetadata();
String date = metadata.getDate();
String author = metadata.getAuthor();
String license = metadata.getLicense();
return !wikiImageCard.isMetaDataDownloaded() && !controller.isMetadataDownloading(wikiImageCard)
&& (Algorithms.isEmpty(date) || date.equals("Unknown")
|| Algorithms.isEmpty(author) || author.equals("Unknown")
|| Algorithms.isEmpty(license) || license.equals("Unknown"));
}

private void updateImageDescriptionRow(@NonNull ImageCard imageCard, @Nullable Boolean preloadNext) {
if (imageCard instanceof WikiImageCard wikiImageCard) {
dateView.setVisibility(View.VISIBLE);
authorView.setVisibility(View.VISIBLE);
licenseView.setVisibility(View.VISIBLE);
controller.addMetaDataListener(this);
controller.downloadWikiMetaData(getImagesToDownloadMetadata(wikiImageCard, preloadNext));

Metadata metadata = wikiImageCard.getWikiImage().getMetadata();
setMetaData(metadata.getAuthor(), metadata.getDate(), metadata.getLicense());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void updateWikiMetadata(@NonNull Map<String, Map<String, String>> metadat
if (!Algorithms.isEmpty(description) && (Algorithms.isEmpty(metadata.getDescription()) || metadata.getDescription().equals("Unknown"))) {
metadata.setDescription(description);
}
wikiImageCard.setMetaDataDownloaded(true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,6 @@ protected void onPostExecute(ImageCardsHolder holder) {
if (listener != null) {
listener.onFinish(holder);
}
downloadWikiMetaData(holder);
}

public void downloadWikiMetaData(@NonNull ImageCardsHolder holder) {
LoadImagesMetadataTask task = new LoadImagesMetadataTask(app, holder);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

public interface GetImageCardsListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class LoadImagesMetadataTask extends AsyncTask<Void, Void, Map<String, Map<String, String>>> {

Expand All @@ -34,11 +35,11 @@ public class LoadImagesMetadataTask extends AsyncTask<Void, Void, Map<String, Ma
private static final String OSMAND_PARSE_URL = "https://osmand.net/search/parse-images-list-info?";

private final OsmandApplication app;
private final ImageCardsHolder holder;
private final Set<WikiImageCard> cards;
private Map<String, Map<String, String>> resultMap = null;

public LoadImagesMetadataTask(@NonNull OsmandApplication app, @NonNull ImageCardsHolder holder) {
this.holder = holder;
public LoadImagesMetadataTask(@NonNull OsmandApplication app, @NonNull Set<WikiImageCard> cards) {
this.cards = cards;
this.app = app;
}

Expand Down Expand Up @@ -97,14 +98,12 @@ protected Map<String, Map<String, String>> doInBackground(Void... voids) {

private List<WikiImageInfo> getData() {
List<WikiImageInfo> data = new ArrayList<>();
for (ImageCard card : holder.getOrderedCards()) {
if (card instanceof WikiImageCard wikiImageCard) {
String title = wikiImageCard.getWikiImage().getWikiMediaTag();
long pageId = wikiImageCard.getWikiImage().getMediaId();

if (!Algorithms.isEmpty(title) && pageId != -1) {
data.add(new WikiImageInfo(title, pageId, null));
}
for (WikiImageCard card : cards) {
String title = card.getWikiImage().getWikiMediaTag();
long pageId = card.getWikiImage().getMediaId();

if (!Algorithms.isEmpty(title) && pageId != -1) {
data.add(new WikiImageInfo(title, pageId, null));
}
}
return data;
Expand All @@ -113,8 +112,8 @@ private List<WikiImageInfo> getData() {
@Override
protected void onPostExecute(Map<String, Map<String, String>> result) {
GalleryController controller = (GalleryController) app.getDialogManager().findController(GalleryController.PROCESS_ID);
if (controller != null && result != null) {
controller.updateMetadata(result);
if (controller != null) {
controller.updateMetadata(result, cards);
}
}
}

0 comments on commit 5e4f0da

Please sign in to comment.