Fix review

This commit is contained in:
Dima-1 2021-02-10 18:18:37 +02:00
parent 27ee57339b
commit 8c92c3f87c
6 changed files with 62 additions and 41 deletions

View file

@ -169,6 +169,26 @@ public class TravelArticle {
return new String[]{md5.substring(0, 1), md5.substring(0, 2)};
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TravelArticle that = (TravelArticle) o;
return TravelArticleIdentifier.areLatLonEqual(that.lat, that.lon, lat, lon) &&
Algorithms.objectEquals(file, that.file) &&
Algorithms.stringsEqual(routeId, that.routeId) &&
Algorithms.stringsEqual(routeSource, that.routeSource);
}
@Override
public int hashCode() {
return Algorithms.hash(file, lat, lon, routeId, routeSource);
}
public static class TravelArticleIdentifier implements Parcelable {
@Nullable File file;
double lat;
@ -249,7 +269,7 @@ public class TravelArticle {
return Algorithms.hash(file, lat, lon, routeId, routeSource);
}
private static boolean areLatLonEqual(double lat1, double lon1, double lat2, double lon2) {
public static boolean areLatLonEqual(double lat1, double lon1, double lat2, double lon2) {
boolean latEqual = (Double.isNaN(lat1) && Double.isNaN(lat2)) || Math.abs(lat1 - lat2) < 0.00001;
boolean lonEqual = (Double.isNaN(lon1) && Double.isNaN(lon2)) || Math.abs(lon1 - lon2) < 0.00001;
return latEqual && lonEqual;

View file

@ -174,7 +174,7 @@ public class TravelDbHelper implements TravelHelper {
}
@Override
public void initializeDataToDisplay() {
public void initializeDataToDisplay(boolean resetData) {
localDataHelper.refreshCachedData();
loadPopularArticles();
}
@ -549,13 +549,13 @@ public class TravelDbHelper implements TravelHelper {
@Override
@Nullable
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @Nullable String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle res = null;
SQLiteConnection conn = openConnection();
String routeId = articleId.routeId;
if (conn != null && !Algorithms.isEmpty(routeId)) {
if (conn != null && !Algorithms.isEmpty(routeId) && lang != null) {
SQLiteCursor cursor = conn.rawQuery(ARTICLES_TABLE_SELECT + " WHERE " + ARTICLES_COL_TRIP_ID + " = ? AND "
+ ARTICLES_COL_LANG + " = ?", new String[] { routeId, lang });
+ ARTICLES_COL_LANG + " = ?", new String[]{routeId, lang});
if (cursor != null) {
if (cursor.moveToFirst()) {
res = readArticle(cursor);

View file

@ -25,7 +25,7 @@ public interface TravelHelper {
void initializeDataOnAppStartup();
void initializeDataToDisplay();
void initializeDataToDisplay(boolean resetData);
boolean isAnyTravelBookPresent();
@ -35,14 +35,11 @@ public interface TravelHelper {
@NonNull
List<TravelArticle> getPopularArticles();
@NonNull
List<TravelArticle> loadPopularArticles();
@NonNull
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull TravelArticle article);
@Nullable
TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);
TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @Nullable String lang, boolean readGpx, @Nullable GpxReadCallback callback);
@Nullable
TravelArticle getArticleByTitle(@NonNull String title, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);

View file

@ -80,9 +80,8 @@ public class TravelObfHelper implements TravelHelper {
private final Map<TravelArticleIdentifier, Map<String, TravelArticle>> cachedArticles = new ConcurrentHashMap<>();
private final TravelLocalDataHelper localDataHelper;
private int searchRadius = ARTICLE_SEARCH_RADIUS;
private int count = 0;
private int page = 0;
final List<Pair<File, Amenity>> amenities = new ArrayList<>();
private int foundAmenitiesIndex = 0;
private final List<Pair<File, Amenity>> foundAmenities = new ArrayList<>();
public TravelObfHelper(OsmandApplication app) {
this.app = app;
@ -100,7 +99,13 @@ public class TravelObfHelper implements TravelHelper {
}
@Override
public void initializeDataToDisplay() {
public void initializeDataToDisplay(boolean resetData) {
if (resetData) {
foundAmenities.clear();
foundAmenitiesIndex = 0;
popularArticles.clear();
searchRadius = ARTICLE_SEARCH_RADIUS;
}
localDataHelper.refreshCachedData();
loadPopularArticles();
}
@ -108,19 +113,19 @@ public class TravelObfHelper implements TravelHelper {
@NonNull
public synchronized List<TravelArticle> loadPopularArticles() {
String lang = app.getLanguage();
List<TravelArticle> popularArticles = new ArrayList<>();
if (amenities.size() - count < MAX_POPULAR_ARTICLES_COUNT) {
List<TravelArticle> popularArticles = new ArrayList<>(this.popularArticles);
if (foundAmenities.size() - foundAmenitiesIndex < MAX_POPULAR_ARTICLES_COUNT) {
final LatLon location = app.getMapViewTrackingUtilities().getMapLocation();
for (final BinaryMapIndexReader reader : getReaders()) {
try {
searchAmenity(amenities, location, reader, searchRadius, -1, ROUTE_ARTICLE);
searchAmenity(amenities, location, reader, searchRadius / 5, 15, ROUTE_TRACK);
searchAmenity(foundAmenities, location, reader, searchRadius, -1, ROUTE_ARTICLE);
searchAmenity(foundAmenities, location, reader, searchRadius / 5, 15, ROUTE_TRACK);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
if (amenities.size() > 0) {
Collections.sort(amenities, new Comparator<Pair<File, Amenity>>() {
if (foundAmenities.size() > 0) {
Collections.sort(foundAmenities, new Comparator<Pair<File, Amenity>>() {
@Override
public int compare(Pair article1, Pair article2) {
Amenity amenity1 = (Amenity) article1.second;
@ -136,22 +141,21 @@ public class TravelObfHelper implements TravelHelper {
searchRadius *= 2;
}
int lastIndex;
for (lastIndex = count; lastIndex < amenities.size() - 1; lastIndex++) {
Pair<File, Amenity> amenity = amenities.get(lastIndex);
int pagesCount = popularArticles.size() / MAX_POPULAR_ARTICLES_COUNT;
while (foundAmenitiesIndex < foundAmenities.size() - 1) {
Pair<File, Amenity> amenity = foundAmenities.get(foundAmenitiesIndex);
if (!Algorithms.isEmpty(amenity.second.getName(lang))) {
TravelArticle article = cacheTravelArticles(amenity.first, amenity.second, lang, false, null);
if (article != null) {
if (article != null && !popularArticles.contains(article)) {
popularArticles.add(article);
this.popularArticles.add(article);
if (this.popularArticles.size() >= (page + 1) * MAX_POPULAR_ARTICLES_COUNT) {
page++;
if (popularArticles.size() >= (pagesCount + 1) * MAX_POPULAR_ARTICLES_COUNT) {
break;
}
}
}
foundAmenitiesIndex++;
}
count = ++lastIndex;
this.popularArticles = popularArticles;
return popularArticles;
}
@ -184,10 +188,8 @@ public class TravelObfHelper implements TravelHelper {
}
if (!Algorithms.isEmpty(articles)) {
TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier();
if (!cachedArticles.containsKey(newArticleId)) {
cachedArticles.put(newArticleId, articles);
article = getCachedArticle(newArticleId, lang, readPoints, callback);
}
cachedArticles.put(newArticleId, articles);
article = getCachedArticle(newArticleId, lang, readPoints, callback);
}
return article;
}
@ -628,8 +630,8 @@ public class TravelObfHelper implements TravelHelper {
}
@Override
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang,
boolean readGpx, @Nullable GpxReadCallback callback) {
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @Nullable String lang,
boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle article = getCachedArticle(articleId, lang, readGpx, callback);
if (article == null) {
article = localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang);
@ -641,8 +643,8 @@ public class TravelObfHelper implements TravelHelper {
}
@Nullable
private TravelArticle getCachedArticle(@NonNull TravelArticleIdentifier articleId, @NonNull String lang,
boolean readGpx, @Nullable GpxReadCallback callback) {
private TravelArticle getCachedArticle(@NonNull TravelArticleIdentifier articleId, @Nullable String lang,
boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle article = null;
Map<String, TravelArticle> articles = cachedArticles.get(articleId);
if (articles != null) {

View file

@ -192,7 +192,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
@Override
public void onPrimaryButtonClick() {
if (activity instanceof WikivoyageExploreActivity) {
new LoadWikivoyageData((WikivoyageExploreActivity) activity).execute();
new LoadWikivoyageData((WikivoyageExploreActivity) activity,false).execute();
}
}

View file

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