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; package net.osmand.plus.wikivoyage.data;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import net.osmand.IndexConstants; 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_TABLE_NAME = "wikivoyage_articles";
private static final String ARTICLES_COL_ID = "article_id"; private static final String ARTICLES_COL_ID = "article_id";
private static final String ARTICLES_COL_TITLE = "title"; 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_IS_PART_OF = "is_part_of";
private static final String ARTICLES_COL_LAT = "lat"; private static final String ARTICLES_COL_LAT = "lat";
private static final String ARTICLES_COL_LON = "lon"; private static final String ARTICLES_COL_LON = "lon";
@ -61,6 +62,7 @@ public class WikivoyageDbHelper {
this.application = application; this.application = application;
} }
@NonNull
public List<SearchResult> search(String searchQuery) { public List<SearchResult> search(String searchQuery) {
List<SearchResult> res = new ArrayList<>(); List<SearchResult> res = new ArrayList<>();
SQLiteConnection conn = openConnection(); SQLiteConnection conn = openConnection();
@ -81,19 +83,61 @@ public class WikivoyageDbHelper {
return res; 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 @Nullable
private SQLiteConnection openConnection() { private SQLiteConnection openConnection() {
String path = getDbFile(application).getAbsolutePath(); String path = getDbFile(application).getAbsolutePath();
return application.getSQLiteAPI().openByAbsolutePath(path, true); return application.getSQLiteAPI().openByAbsolutePath(path, true);
} }
private SearchResult readSearchResult(SQLiteCursor query) { @NonNull
private SearchResult readSearchResult(SQLiteCursor cursor) {
SearchResult res = new SearchResult(); SearchResult res = new SearchResult();
res.searchTerm = query.getString(0); res.searchTerm = cursor.getString(0);
res.cityId = query.getLong(1); res.cityId = cursor.getLong(1);
res.articleTitle = query.getString(2); res.articleTitle = cursor.getString(2);
res.lang = query.getString(3); 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; return res;
} }
@ -102,6 +146,7 @@ public class WikivoyageDbHelper {
return getDbFile(app).exists(); return getDbFile(app).exists();
} }
@NonNull
private static File getDbFile(OsmandApplication app) { private static File getDbFile(OsmandApplication app) {
return app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR + DB_NAME); 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 List<SearchResult> items = new ArrayList<>();
private View.OnClickListener onItemClickListener;
public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()) View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.wikivoyage_search_list_item, viewGroup, false); .inflate(R.layout.wikivoyage_search_list_item, viewGroup, false);
itemView.setOnClickListener(new View.OnClickListener() { itemView.setOnClickListener(onItemClickListener);
@Override
public void onClick(View v) {
}
});
return new ViewHolder(itemView); return new ViewHolder(itemView);
} }
@ -43,6 +44,10 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecycl
return items.size(); return items.size();
} }
public SearchResult getItem(int pos) {
return items.get(pos);
}
public void setItems(List<SearchResult> items) { public void setItems(List<SearchResult> items) {
this.items = items; this.items = items;
notifyDataSetChanged(); notifyDataSetChanged();

View file

@ -17,18 +17,23 @@ import android.widget.TextView;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment; 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 class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment {
public static final String TAG = "WikivoyageSearchDialogFragment"; public static final String TAG = "WikivoyageSearchDialogFragment";
private SearchRecyclerViewAdapter adapter; private WikivoyageDbHelper dbHelper;
private SearchRecyclerViewAdapter adapter;
private EditText searchEt; private EditText searchEt;
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 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); final View mainView = inflater.inflate(R.layout.fragment_wikivoyage_search_dialog, container);
Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
@ -63,15 +68,24 @@ public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment {
}); });
adapter = new SearchRecyclerViewAdapter(); 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.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(adapter); 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; return mainView;
} }
private void runSearch() { private void runSearch() {
adapter.setItems(getMyApplication().getWikivoyageDbHelper().search((searchEt).getText().toString())); adapter.setItems(dbHelper.search((searchEt).getText().toString()));
} }
public static boolean showInstance(FragmentManager fm) { public static boolean showInstance(FragmentManager fm) {