Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions app/src/main/java/fr/free/nrw/commons/nearby/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import android.content.res.Resources;
import android.os.Parcel;

import android.util.SparseArray;
import androidx.annotation.DrawableRes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fr.free.nrw.commons.R;

Expand All @@ -29,7 +28,6 @@ public enum Label {
COTTAGE(R.array.cottage_QIDs, R.drawable.round_icon_house),
FARMHOUSE(R.array.farmhouse_QIDs, R.drawable.round_icon_house),
TEMPLE(R.array.temple_QIDs, R.drawable.round_icon_church),

CHURCH(R.array.church_QIDs, R.drawable.round_icon_church),
GAS_STATION(R.array.gas_station_QIDs, R.drawable.round_icon_gas_station),
RAILWAY_STATION(R.array.railway_station_QIDs, R.drawable.round_icon_railway_station),
Expand All @@ -53,9 +51,14 @@ public enum Label {
UNKNOWN(0, R.drawable.round_icon_unknown);

/**
* Lookup map which maps Q-ID -> Label.
* Sorted array of Q-ID integers for binary-search lookup.
*/
private static volatile int[] QIDS = new int[0];

/**
* Labels parallel to {@link #QIDS}: QIDS[i] maps to QID_LABELS[i].
*/
public static final Map<String, Label> TEXT_TO_DESCRIPTION = new HashMap<>();
private static volatile Label[] QID_LABELS = new Label[0];

private final int arrayResId;
@DrawableRes
Expand All @@ -73,21 +76,47 @@ public enum Label {
}

/**
* Loads Q-IDs from Android resources
* Loads Q-IDs from Android resources into sorted primitive arrays.
* This method is idempotent; subsequent calls after the first are no-ops.
*
* @param context any Android context applicationContext - much safer
* @param context any Android context; applicationContext is safest
*/
public static void init(final Context context) {
public static synchronized void init(final Context context) {
if (QIDS.length != 0) {
return;
}
final Resources res = context.getResources();
// load all values in temp map which will be sorted by default
final SparseArray<Label> tempMap = new SparseArray<>();
for (final Label label : values()) {
if (label.arrayResId != 0) {
final int[] resArray = res.getIntArray(label.arrayResId);
for (final int id : resArray) {
final String qid = "Q" + id;
TEXT_TO_DESCRIPTION.put(qid, label);
tempMap.put(id, label);
}
}
}
//separate the ids and labels into arrays for binary search
final int[] ids = new int[tempMap.size()];
final Label[] labels = new Label[tempMap.size()];
for (int i = 0; i < tempMap.size(); i++) {
ids[i] = tempMap.keyAt(i);
labels[i] = tempMap.valueAt(i);
}
tempMap.clear();
QIDS = ids;
QID_LABELS = labels;
}

/**
* Looks up a Label by its numeric Q-ID using binary search.
*
* @param id the integer Q-ID (e.g. 16970)
* @return the matching {@link Label}, or {@link Label#UNKNOWN} if not found
*/
public static Label fromQidInt(final int id) {
final int pos = Arrays.binarySearch(QIDS, id);
return pos >= 0 ? QID_LABELS[pos] : UNKNOWN;
}

/**
Expand Down Expand Up @@ -116,8 +145,10 @@ public static Label fromText(final String text) {
if ("BOOKMARK".equals(text)) {
return BOOKMARKS;
}
final Label label = TEXT_TO_DESCRIPTION.get(text);
return label == null ? UNKNOWN : label;
if (!(text.charAt(0) == 'Q')) {
return UNKNOWN;
}
return fromQidInt(Integer.parseInt(text.substring(1)));
}

public static List<Label> valuesAsList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
if (Label.TEXT_TO_DESCRIPTION.isEmpty())
Label.init(requireContext().applicationContext)
Label.init(requireContext().applicationContext)
loadExploreMapData();

binding = FragmentNearbyParentBinding.inflate(inflater, container, false)
Expand Down
Loading
Loading