Skip to content

Commit

Permalink
Prepare for a "autocomplete including translations" setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertware committed Sep 24, 2022
1 parent e817862 commit 5cce062
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ private static class LoadAutoCompleteTask extends AsyncTask<TransportStopsDataSo
@Override
protected String[] doInBackground(TransportStopsDataSource... provider) {
Thread.currentThread().setName("LoadAutoCompleteTask");
return provider[0].getStoplocationsNames(provider[0].getStoplocationsOrderedBySize());
// TODO: "make use of translations" a setting
return provider[0].getStoplocationsNames(provider[0].getStoplocationsOrderedBySize(), false);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package be.hyperrail.opentransportdata.common.contracts;

import android.location.Location;

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

Expand All @@ -28,11 +29,13 @@ public interface TransportStopsDataSource {
/**
* Get all station names, localized.
*
* @param stopLocations The list of stations for which a name should be retrieved.
* @param stopLocations The list of stations for which a name should be retrieved.
* @param includeTranslations Whether translations should be included in the name list.
* If this is set to false, only localized names are used.
* @return An array of localized station names.
*/
@NonNull
String[] getStoplocationsNames(@NonNull StopLocation[] stopLocations);
String[] getStoplocationsNames(@NonNull StopLocation[] stopLocations, boolean includeTranslations);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import be.hyperrail.opentransportdata.common.contracts.TransportStopsDataSource;
import be.hyperrail.opentransportdata.common.exceptions.StopLocationNotResolvedException;
Expand Down Expand Up @@ -167,17 +169,20 @@ public void preloadDatabase() {
*/
@NonNull
@Override
public String[] getStoplocationsNames(@NonNull StopLocation[] stopLocations) {
public String[] getStoplocationsNames(@NonNull StopLocation[] stopLocations, boolean includeTranslations) {
if (stopLocations.length == 0) {
log.warning("Tried to load station names on empty station list!");
return new String[0];
}

String[] results = new String[stopLocations.length];
for (int i = 0; i < stopLocations.length; i++) {
results[i] = stopLocations[i].getLocalizedName();
Set<String> results = new HashSet<>();
for (StopLocation stopLocation : stopLocations) {
results.add(stopLocation.getName());
if (includeTranslations) {
results.addAll(stopLocation.getTranslations().values());
}
}
return results;
return results.toArray(new String[0]);
}

@Nullable
Expand Down

0 comments on commit 5cce062

Please sign in to comment.