Fix compare by distance

This commit is contained in:
Dima-1 2021-02-08 21:57:12 +02:00
parent 08b5e9fdf9
commit 27ee57339b
5 changed files with 23 additions and 22 deletions

View file

@ -174,9 +174,9 @@ public class TravelDbHelper implements TravelHelper {
} }
@Override @Override
public void initializeDataToDisplay(boolean showMore) { public void initializeDataToDisplay() {
localDataHelper.refreshCachedData(); localDataHelper.refreshCachedData();
loadPopularArticles(showMore); loadPopularArticles();
} }
@ -283,7 +283,7 @@ public class TravelDbHelper implements TravelHelper {
} }
@NonNull @NonNull
public List<TravelArticle> loadPopularArticles(boolean showMore) { public List<TravelArticle> loadPopularArticles() {
String language = application.getLanguage(); String language = application.getLanguage();
SQLiteConnection conn = openConnection(); SQLiteConnection conn = openConnection();
if (conn == null) { if (conn == null) {

View file

@ -17,6 +17,7 @@ public interface TravelHelper {
interface GpxReadCallback { interface GpxReadCallback {
void onGpxFileReading(); void onGpxFileReading();
void onGpxFileRead(@Nullable GPXFile gpxFile); void onGpxFileRead(@Nullable GPXFile gpxFile);
} }
@ -24,7 +25,7 @@ public interface TravelHelper {
void initializeDataOnAppStartup(); void initializeDataOnAppStartup();
void initializeDataToDisplay(boolean showMore); void initializeDataToDisplay();
boolean isAnyTravelBookPresent(); boolean isAnyTravelBookPresent();
@ -35,7 +36,7 @@ public interface TravelHelper {
List<TravelArticle> getPopularArticles(); List<TravelArticle> getPopularArticles();
@NonNull @NonNull
List<TravelArticle> loadPopularArticles(boolean showMore); List<TravelArticle> loadPopularArticles();
@NonNull @NonNull
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull TravelArticle article); Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull TravelArticle article);

View file

@ -100,13 +100,13 @@ public class TravelObfHelper implements TravelHelper {
} }
@Override @Override
public void initializeDataToDisplay(boolean showMore) { public void initializeDataToDisplay() {
localDataHelper.refreshCachedData(); localDataHelper.refreshCachedData();
loadPopularArticles(showMore); loadPopularArticles();
} }
@NonNull @NonNull
public synchronized List<TravelArticle> loadPopularArticles(boolean showMore) { public synchronized List<TravelArticle> loadPopularArticles() {
String lang = app.getLanguage(); String lang = app.getLanguage();
List<TravelArticle> popularArticles = new ArrayList<>(); List<TravelArticle> popularArticles = new ArrayList<>();
if (amenities.size() - count < MAX_POPULAR_ARTICLES_COUNT) { if (amenities.size() - count < MAX_POPULAR_ARTICLES_COUNT) {
@ -123,9 +123,13 @@ public class TravelObfHelper implements TravelHelper {
Collections.sort(amenities, new Comparator<Pair<File, Amenity>>() { Collections.sort(amenities, new Comparator<Pair<File, Amenity>>() {
@Override @Override
public int compare(Pair article1, Pair article2) { public int compare(Pair article1, Pair article2) {
int d1 = (int) (MapUtils.getDistance(((Amenity) article1.second).getLocation(), location)); Amenity amenity1 = (Amenity) article1.second;
int d2 = (int) (MapUtils.getDistance(((Amenity) article2.second).getLocation(), location)); double d1 = MapUtils.getDistance(amenity1.getLocation(), location)
return d1 < d2 ? -1 : (d1 == d2 ? 0 : 1); / (ROUTE_ARTICLE.equals(amenity1.getSubType()) ? 5 : 1);
Amenity amenity2 = (Amenity) article2.second;
double d2 = MapUtils.getDistance(amenity2.getLocation(), location)
/ (ROUTE_ARTICLE.equals(amenity2.getSubType()) ? 5 : 1);
return Double.compare(d1, d2);
} }
}); });
} }

View file

@ -191,7 +191,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
travelButtonCard.setListener(new TravelNeededMapsCard.CardListener() { travelButtonCard.setListener(new TravelNeededMapsCard.CardListener() {
@Override @Override
public void onPrimaryButtonClick() { public void onPrimaryButtonClick() {
new LoadWikivoyageData(ExploreTabFragment.this.getExploreActivity(), true).execute(); if (activity instanceof WikivoyageExploreActivity) {
new LoadWikivoyageData((WikivoyageExploreActivity) activity).execute();
}
} }
@Override @Override

View file

@ -314,10 +314,6 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
} }
public void populateData() { public void populateData() {
populateData(false);
}
public void populateData(final boolean showMore) {
switchProgressBarVisibility(true); switchProgressBarVisibility(true);
if (app.isApplicationInitializing()) { if (app.isApplicationInitializing()) {
final WeakReference<WikivoyageExploreActivity> activityRef = new WeakReference<>(this); final WeakReference<WikivoyageExploreActivity> activityRef = new WeakReference<>(this);
@ -330,12 +326,12 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
public void onFinish(AppInitializer init) { public void onFinish(AppInitializer init) {
WikivoyageExploreActivity activity = activityRef.get(); WikivoyageExploreActivity activity = activityRef.get();
if (AndroidUtils.isActivityNotDestroyed(activity)) { if (AndroidUtils.isActivityNotDestroyed(activity)) {
new LoadWikivoyageData(activity, showMore).execute(); new LoadWikivoyageData(activity).execute();
} }
} }
}); });
} else { } else {
new LoadWikivoyageData(this, showMore).execute(); new LoadWikivoyageData(this).execute();
} }
} }
@ -388,17 +384,15 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
private final WeakReference<WikivoyageExploreActivity> activityRef; private final WeakReference<WikivoyageExploreActivity> activityRef;
private final TravelHelper travelHelper; private final TravelHelper travelHelper;
private final boolean showMore;
LoadWikivoyageData(WikivoyageExploreActivity activity, boolean showMore) { LoadWikivoyageData(WikivoyageExploreActivity activity) {
travelHelper = activity.getMyApplication().getTravelHelper(); travelHelper = activity.getMyApplication().getTravelHelper();
activityRef = new WeakReference<>(activity); activityRef = new WeakReference<>(activity);
this.showMore = showMore;
} }
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
travelHelper.initializeDataToDisplay(showMore); travelHelper.initializeDataToDisplay();
return null; return null;
} }