Add WikivoyageArticle class; add the ability to receive an article based on the SearchResult

This commit is contained in:
alex 2018-03-27 21:14:21 +03:00
parent 939902053d
commit 9d4ebfadcf
4 changed files with 141 additions and 15 deletions

View file

@ -0,0 +1,62 @@
package net.osmand.plus.wikivoyage.data;
import net.osmand.plus.GPXUtilities.GPXFile;
public class WikivoyageArticle {
String id;
String title;
String content;
String isPartOf;
double lat;
double lon;
String imageTitle;
GPXFile gpxFile;
long cityId;
long originalId;
String lang;
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getIsPartOf() {
return isPartOf;
}
public double getLat() {
return lat;
}
public double getLon() {
return lon;
}
public String getImageTitle() {
return imageTitle;
}
public GPXFile getGpxFile() {
return gpxFile;
}
public long getCityId() {
return cityId;
}
public long getOriginalId() {
return originalId;
}
public String getLang() {
return lang;
}
}

View file

@ -1,5 +1,6 @@
package net.osmand.plus.wikivoyage.data;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import net.osmand.IndexConstants;
@ -18,7 +19,7 @@ public class WikivoyageDbHelper {
private static final String ARTICLES_TABLE_NAME = "wikivoyage_articles";
private static final String ARTICLES_COL_ID = "article_id";
private static final String ARTICLES_COL_TITLE = "title";
private static final String ARTICLES_COL_CONTENT = "content_gzblob";
private static final String ARTICLES_COL_CONTENT = "content_gz";
private static final String ARTICLES_COL_IS_PART_OF = "is_part_of";
private static final String ARTICLES_COL_LAT = "lat";
private static final String ARTICLES_COL_LON = "lon";
@ -61,6 +62,7 @@ public class WikivoyageDbHelper {
this.application = application;
}
@NonNull
public List<SearchResult> search(String searchQuery) {
List<SearchResult> res = new ArrayList<>();
SQLiteConnection conn = openConnection();
@ -81,19 +83,61 @@ public class WikivoyageDbHelper {
return res;
}
@Nullable
public WikivoyageArticle getArticle(SearchResult searchResult) {
WikivoyageArticle res = null;
SQLiteConnection conn = openConnection();
if (conn != null) {
try {
SQLiteCursor cursor = conn.rawQuery(ARTICLES_TABLE_SELECT + " WHERE " +
ARTICLES_COL_CITY_ID + " = ? AND " +
ARTICLES_COL_TITLE + " = ? AND " +
ARTICLES_COL_LANG + " = ?",
new String[]{String.valueOf(searchResult.cityId), searchResult.articleTitle, searchResult.lang});
if (cursor.moveToFirst()) {
res = readArticle(cursor);
}
cursor.close();
} finally {
conn.close();
}
}
return res;
}
@Nullable
private SQLiteConnection openConnection() {
String path = getDbFile(application).getAbsolutePath();
return application.getSQLiteAPI().openByAbsolutePath(path, true);
}
private SearchResult readSearchResult(SQLiteCursor query) {
@NonNull
private SearchResult readSearchResult(SQLiteCursor cursor) {
SearchResult res = new SearchResult();
res.searchTerm = query.getString(0);
res.cityId = query.getLong(1);
res.articleTitle = query.getString(2);
res.lang = query.getString(3);
res.searchTerm = cursor.getString(0);
res.cityId = cursor.getLong(1);
res.articleTitle = cursor.getString(2);
res.lang = cursor.getString(3);
return res;
}
@NonNull
private WikivoyageArticle readArticle(SQLiteCursor cursor) {
WikivoyageArticle res = new WikivoyageArticle();
res.id = cursor.getString(0);
res.title = cursor.getString(1);
byte[] contentBlob = cursor.getBlob(2);
res.isPartOf = cursor.getString(3);
res.lat = cursor.getDouble(4);
res.lon = cursor.getDouble(5);
res.imageTitle = cursor.getString(6);
byte[] gpxFileBlob = cursor.getBlob(7);
res.cityId = cursor.getLong(8);
res.originalId = cursor.getLong(9);
res.lang = cursor.getString(10);
return res;
}
@ -102,6 +146,7 @@ public class WikivoyageDbHelper {
return getDbFile(app).exists();
}
@NonNull
private static File getDbFile(OsmandApplication app) {
return app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR + DB_NAME);
}

View file

@ -16,16 +16,17 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecycl
private List<SearchResult> items = new ArrayList<>();
private View.OnClickListener onItemClickListener;
public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.wikivoyage_search_list_item, viewGroup, false);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
itemView.setOnClickListener(onItemClickListener);
return new ViewHolder(itemView);
}
@ -43,6 +44,10 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecycl
return items.size();
}
public SearchResult getItem(int pos) {
return items.get(pos);
}
public void setItems(List<SearchResult> items) {
this.items = items;
notifyDataSetChanged();

View file

@ -17,18 +17,23 @@ import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
import net.osmand.plus.wikivoyage.data.WikivoyageDbHelper;
public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment {
public static final String TAG = "WikivoyageSearchDialogFragment";
private SearchRecyclerViewAdapter adapter;
private WikivoyageDbHelper dbHelper;
private SearchRecyclerViewAdapter adapter;
private EditText searchEt;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
dbHelper = getMyApplication().getWikivoyageDbHelper();
final View mainView = inflater.inflate(R.layout.fragment_wikivoyage_search_dialog, container);
Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
@ -63,15 +68,24 @@ public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment {
});
adapter = new SearchRecyclerViewAdapter();
RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
final RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(adapter);
adapter.setOnItemClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = rv.getChildAdapterPosition(v);
if (pos != RecyclerView.NO_POSITION) {
WikivoyageArticle article = dbHelper.getArticle(adapter.getItem(pos));
}
}
});
return mainView;
}
private void runSearch() {
adapter.setItems(getMyApplication().getWikivoyageDbHelper().search((searchEt).getText().toString()));
adapter.setItems(dbHelper.search((searchEt).getText().toString()));
}
public static boolean showInstance(FragmentManager fm) {