Fix travel bugs show 30 articles, gpx bookmark
This commit is contained in:
vshcherb 2021-02-15 16:00:36 +01:00 committed by GitHub
commit ec493acbea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 157 additions and 102 deletions

View file

@ -3,13 +3,19 @@ package net.osmand.plus.wikivoyage.data;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -23,6 +29,8 @@ import java.util.Set;
public class TravelLocalDataHelper {
private static final Log LOG = PlatformUtil.getLog(TravelLocalDataHelper.class);
private static final int HISTORY_ITEMS_LIMIT = 300;
private final WikivoyageLocalDataDbHelper dbHelper;
@ -185,7 +193,7 @@ public class TravelLocalDataHelper {
private static class WikivoyageLocalDataDbHelper {
private static final int DB_VERSION = 7;
private static final int DB_VERSION = 8;
private static final String DB_NAME = "wikivoyage_local_data";
private static final String HISTORY_TABLE_NAME = "wikivoyage_search_history";
@ -223,6 +231,7 @@ public class TravelLocalDataHelper {
private static final String BOOKMARKS_COL_CONTENT_JSON = "content_json";
private static final String BOOKMARKS_COL_CONTENT = "content";
private static final String BOOKMARKS_COL_LAST_MODIFIED = "last_modified";
private static final String BOOKMARKS_COL_GPX_GZ = "gpx_gz";
private static final String BOOKMARKS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
BOOKMARKS_TABLE_NAME + " (" +
@ -236,7 +245,8 @@ public class TravelLocalDataHelper {
BOOKMARKS_COL_ROUTE_ID + " TEXT, " +
BOOKMARKS_COL_CONTENT_JSON + " TEXT, " +
BOOKMARKS_COL_CONTENT + " TEXT, " +
BOOKMARKS_COL_LAST_MODIFIED + " long" + ");";
BOOKMARKS_COL_LAST_MODIFIED + " long, " +
BOOKMARKS_COL_GPX_GZ + " blob" + ");";
private static final String BOOKMARKS_TABLE_SELECT = "SELECT " +
BOOKMARKS_COL_ARTICLE_TITLE + ", " +
@ -249,7 +259,8 @@ public class TravelLocalDataHelper {
BOOKMARKS_COL_ROUTE_ID + ", " +
BOOKMARKS_COL_CONTENT_JSON + ", " +
BOOKMARKS_COL_CONTENT + ", " +
BOOKMARKS_COL_LAST_MODIFIED +
BOOKMARKS_COL_LAST_MODIFIED + ", " +
BOOKMARKS_COL_GPX_GZ +
" FROM " + BOOKMARKS_TABLE_NAME;
private final OsmandApplication context;
@ -318,6 +329,9 @@ public class TravelLocalDataHelper {
conn.execSQL("UPDATE " + BOOKMARKS_TABLE_NAME +
" SET " + BOOKMARKS_COL_PARTIAL_CONTENT + " = null");
}
if (oldVersion < 8) {
conn.execSQL("ALTER TABLE " + BOOKMARKS_TABLE_NAME + " ADD " + BOOKMARKS_COL_GPX_GZ + " blob");
}
}
@NonNull
@ -461,11 +475,20 @@ public class TravelLocalDataHelper {
return count > 0;
}
void addSavedArticle(@NonNull TravelArticle article) {
String travelBook = article.getTravelBook(context);
void addSavedArticle(@NonNull final TravelArticle article) {
final String travelBook = article.getTravelBook(context);
if (travelBook == null) {
return;
}
context.getTravelHelper().getArticleById(article.generateIdentifier(), article.lang, true,
new TravelHelper.GpxReadCallback() {
@Override
public void onGpxFileReading() {
}
@Override
public void onGpxFileRead(@Nullable GPXUtilities.GPXFile gpxFile) {
SQLiteConnection conn = openConnection(false);
if (conn != null) {
try {
@ -480,17 +503,21 @@ public class TravelLocalDataHelper {
BOOKMARKS_COL_ROUTE_ID + ", " +
BOOKMARKS_COL_CONTENT_JSON + ", " +
BOOKMARKS_COL_CONTENT + ", " +
BOOKMARKS_COL_LAST_MODIFIED +
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
BOOKMARKS_COL_LAST_MODIFIED + ", " +
BOOKMARKS_COL_GPX_GZ +
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
conn.execSQL(query, new Object[]{article.title, article.lang,
article.aggregatedPartOf, article.imageTitle,
travelBook, article.lat, article.lon, article.routeId, article.contentsJson,
article.content, article.getFile().lastModified()});
article.content, article.getFile().lastModified(),
Algorithms.stringToGzip(GPXUtilities.asString(article.gpxFile))});
} finally {
conn.close();
}
}
}
});
}
void removeSavedArticle(@NonNull TravelArticle article) {
String travelBook = article.getTravelBook(context);
@ -580,6 +607,15 @@ public class TravelLocalDataHelper {
res.file = context.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR + travelBook);
res.lastModified = cursor.getLong(cursor.getColumnIndex(BOOKMARKS_COL_LAST_MODIFIED));
}
try {
byte[] blob = cursor.getBlob(cursor.getColumnIndex(BOOKMARKS_COL_GPX_GZ));
if (blob != null) {
String gpxContent = Algorithms.gzipToString(blob);
res.gpxFile = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxContent.getBytes("UTF-8")));
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return res;
}
}

View file

@ -116,6 +116,9 @@ public class TravelObfHelper implements TravelHelper {
public synchronized List<TravelArticle> loadPopularArticles() {
String lang = app.getLanguage();
List<TravelArticle> popularArticles = new ArrayList<>(this.popularArticles);
int pagesCount;
if (isAnyTravelBookPresent()) {
do {
if (foundAmenities.size() - foundAmenitiesIndex < MAX_POPULAR_ARTICLES_COUNT) {
final LatLon location = app.getMapViewTrackingUtilities().getMapLocation();
for (final BinaryMapIndexReader reader : getReaders()) {
@ -142,8 +145,7 @@ public class TravelObfHelper implements TravelHelper {
}
searchRadius *= 2;
}
int pagesCount = popularArticles.size() / MAX_POPULAR_ARTICLES_COUNT;
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))) {
@ -157,6 +159,8 @@ public class TravelObfHelper implements TravelHelper {
}
foundAmenitiesIndex++;
}
} while (popularArticles.size() < (pagesCount + 1) * MAX_POPULAR_ARTICLES_COUNT);
}
this.popularArticles = popularArticles;
return popularArticles;
}
@ -669,7 +673,7 @@ public class TravelObfHelper implements TravelHelper {
if (article == null && articles == null) {
article = findArticleById(articleId, lang, readGpx, callback);
}
if (article != null && readGpx && !Algorithms.isEmpty(lang)) {
if (article != null && readGpx && (!Algorithms.isEmpty(lang) || article instanceof TravelGpx)) {
readGpxFile(article, callback);
}
return article;
@ -1003,11 +1007,7 @@ public class TravelObfHelper implements TravelHelper {
@Override
public File createGpxFile(@NonNull TravelArticle article) {
final GPXFile gpx;
if (article instanceof TravelGpx) {
gpx = buildTravelGpxFile((TravelGpx) article);
} else {
gpx = article.getGpxFile();
}
File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
writeGpxFile(file, gpx);
return file;
@ -1043,6 +1043,9 @@ public class TravelObfHelper implements TravelHelper {
@Override
protected GPXFile doInBackground(Void... voids) {
GPXFile gpxFile = null;
if (article instanceof TravelGpx) {
gpxFile = buildTravelGpxFile((TravelGpx) article);
} else {
List<Amenity> pointList = getPointList(article);
if (!Algorithms.isEmpty(pointList)) {
gpxFile = new GPXFile(article.getTitle(), article.getLang(), article.getContent());
@ -1052,6 +1055,7 @@ public class TravelObfHelper implements TravelHelper {
gpxFile.addPoint(wptPt);
}
}
}
return gpxFile;
}

View file

@ -186,13 +186,13 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
}
}
}
if (app.getTravelHelper().isAnyTravelBookPresent()) {
TravelButtonCard travelButtonCard = new TravelButtonCard(app, nightMode);
travelButtonCard.setListener(new TravelNeededMapsCard.CardListener() {
@Override
public void onPrimaryButtonClick() {
if (activity instanceof WikivoyageExploreActivity) {
new LoadWikivoyageData((WikivoyageExploreActivity) activity,false).execute();
new LoadWikivoyageData((WikivoyageExploreActivity) activity, false).execute();
}
}
@ -207,7 +207,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
}
});
items.add(travelButtonCard);
}
items.add(new StartEditingTravelCard(activity, nightMode));
adapter.setItems(items);
final DownloadIndexesThread downloadThread = app.getDownloadThread();
@ -248,11 +248,14 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
}
private void addIndexItemCards(List<IndexItem> mainIndexItem, List<IndexItem> neededIndexItems) {
final OsmandApplication app = getMyApplication();
if (app != null && !app.getTravelHelper().isAnyTravelBookPresent()) {
this.mainIndexItems.clear();
this.mainIndexItems.addAll(mainIndexItem);
addDownloadUpdateCard();
}
this.neededIndexItems.clear();
this.neededIndexItems.addAll(neededIndexItems);
addDownloadUpdateCard();
addNeededMapsCard();
}

View file

@ -7,16 +7,19 @@ import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.osm.RouteActivityType;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.track.TrackMenuFragment;
import net.osmand.plus.wikivoyage.data.TravelGpx;
import net.osmand.plus.wikivoyage.data.TravelHelper;
import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper;
import net.osmand.util.Algorithms;
@ -64,9 +67,19 @@ public class TravelGpxCard extends BaseTravelCard {
@Override
public void onClick(View v) {
if (activity != null) {
app.getTravelHelper().getArticleById(article.generateIdentifier(), null, true,
new TravelHelper.GpxReadCallback() {
@Override
public void onGpxFileReading() {
}
@Override
public void onGpxFileRead(@Nullable GPXUtilities.GPXFile gpxFile) {
File file = app.getTravelHelper().createGpxFile(article);
TrackMenuFragment.openTrack(activity, file, null);
}
});
}
}
};
holder.leftButton.setOnClickListener(readClickListener);
@ -102,7 +115,6 @@ public class TravelGpxCard extends BaseTravelCard {
if (saved) {
helper.removeArticleFromSaved(article);
} else {
app.getTravelHelper().createGpxFile(article);
helper.addArticleToSaved(article);
}
updateSaveButton(holder);