Small refactoring & grouping

This commit is contained in:
Victor Shcherb 2018-03-29 00:29:53 +02:00
parent 8285fb431f
commit 7019412ae4
6 changed files with 58 additions and 30 deletions

View file

@ -13,16 +13,16 @@ import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
import net.osmand.plus.wikivoyage.data.SearchResult;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
public class WikivoyageArticleDialogFragment extends BaseOsmAndDialogFragment {
public static final String TAG = "WikivoyageArticleDialogFragment";
private SearchResult searchResult;
private WikivoyageSearchResult searchResult;
public void setSearchResult(SearchResult searchResult) {
public void setSearchResult(WikivoyageSearchResult searchResult) {
this.searchResult = searchResult;
}
@ -49,7 +49,7 @@ public class WikivoyageArticleDialogFragment extends BaseOsmAndDialogFragment {
return mainView;
}
public static boolean showInstance(FragmentManager fm, SearchResult searchResult) {
public static boolean showInstance(FragmentManager fm, WikivoyageSearchResult searchResult) {
try {
WikivoyageArticleDialogFragment fragment = new WikivoyageArticleDialogFragment();
fragment.setSearchResult(searchResult);

View file

@ -2,17 +2,19 @@ package net.osmand.plus.wikivoyage.data;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import net.osmand.IndexConstants;
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 gnu.trove.map.hash.TLongObjectHashMap;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class WikivoyageDbHelper {
@ -65,13 +67,13 @@ public class WikivoyageDbHelper {
}
@NonNull
public List<SearchResult> search(String searchQuery) {
List<SearchResult> res = new ArrayList<>();
public Collection<WikivoyageSearchResult> search(String searchQuery) {
List<WikivoyageSearchResult> res = new ArrayList<>();
SQLiteConnection conn = openConnection();
if (conn != null) {
try {
String dbQuery = SEARCH_TABLE_SELECT + " WHERE " + SEARCH_COL_SEARCH_TERM + " LIKE ?";
SQLiteCursor cursor = conn.rawQuery(dbQuery, new String[]{"%" + searchQuery + "%"});
SQLiteCursor cursor = conn.rawQuery(dbQuery, new String[] { searchQuery + "%" });
if (cursor.moveToFirst()) {
do {
res.add(readSearchResult(cursor));
@ -82,7 +84,34 @@ public class WikivoyageDbHelper {
conn.close();
}
}
return res;
return groupSearchResultsByCityId(res);
}
private Collection<WikivoyageSearchResult> groupSearchResultsByCityId(List<WikivoyageSearchResult> res) {
String baseLng = application.getLanguage();
TLongObjectHashMap<WikivoyageSearchResult> wikivoyage = new TLongObjectHashMap<WikivoyageSearchResult>();
for(WikivoyageSearchResult rs: res) {
WikivoyageSearchResult prev = wikivoyage.get(rs.cityId);
if(prev != null) {
int insInd = prev.langs.size();
if(rs.getLang().get(0).equals(baseLng)) {
insInd = 0;
} else if(rs.getLang().get(0).equals("en")) {
if(!prev.getLang().get(0).equals(baseLng)) {
insInd = 0;
} else {
insInd = 1;
}
}
prev.articleTitle.add(insInd, rs.articleTitle.get(0));
prev.langs.add(insInd, rs.langs.get(0));
prev.searchTerm.add(insInd, rs.searchTerm.get(0));
} else {
wikivoyage.put(rs.cityId, rs);
}
}
return wikivoyage.valueCollection();
}
@Nullable
@ -113,8 +142,8 @@ public class WikivoyageDbHelper {
}
@NonNull
private SearchResult readSearchResult(SQLiteCursor cursor) {
SearchResult res = new SearchResult();
private WikivoyageSearchResult readSearchResult(SQLiteCursor cursor) {
WikivoyageSearchResult res = new WikivoyageSearchResult();
res.searchTerm.add(cursor.getString(0));
res.cityId = cursor.getLong(1);

View file

@ -3,7 +3,7 @@ package net.osmand.plus.wikivoyage.data;
import java.util.ArrayList;
import java.util.List;
public class SearchResult {
public class WikivoyageSearchResult {
List<String> searchTerm = new ArrayList<>();
long cityId;

View file

@ -6,16 +6,16 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.data.SearchResult;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecyclerViewAdapter.ViewHolder> {
private List<SearchResult> items = new ArrayList<>();
private List<WikivoyageSearchResult> items = new ArrayList<>();
private View.OnClickListener onItemClickListener;
@ -33,7 +33,7 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecycl
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
SearchResult item = items.get(i);
WikivoyageSearchResult item = items.get(i);
// FIXME
viewHolder.searchTerm.setText(item.getSearchTerm().toString());
viewHolder.cityId.setText(String.valueOf(item.getCityId()));
@ -46,15 +46,15 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecycl
return items.size();
}
public SearchResult getItem(int pos) {
public WikivoyageSearchResult getItem(int pos) {
return items.get(pos);
}
public void setItems(@Nullable List<SearchResult> items) {
public void setItems(@Nullable Collection<WikivoyageSearchResult> items) {
if (items == null) {
this.items.clear();
} else {
this.items = items;
this.items = new ArrayList<>(items);
}
notifyDataSetChanged();
}

View file

@ -15,14 +15,14 @@ import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
import net.osmand.plus.wikivoyage.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.data.SearchResult;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import net.osmand.plus.wikivoyage.search.WikivoyageSearchHelper.SearchListener;
import java.util.Collection;
import java.util.List;
public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment implements SearchListener {
@ -140,7 +140,7 @@ public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment imp
}
@Override
public void onSearchFinished(@Nullable List<SearchResult> results) {
public void onSearchFinished(@Nullable Collection<WikivoyageSearchResult> results) {
adapter.setItems(results);
switchProgressBarVisibility(false);
}

View file

@ -2,12 +2,11 @@ package net.osmand.plus.wikivoyage.search;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.wikivoyage.data.SearchResult;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
@ -21,7 +20,7 @@ public class WikivoyageSearchHelper {
private LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
private ThreadPoolExecutor singleThreadExecutor =
new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, workQueue);
private AsyncTask<Void, Void, List<SearchResult>> currentTask;
private AsyncTask<Void, Void, Collection<WikivoyageSearchResult>> currentTask;
private OsmandApplication application;
private Set<SearchListener> listeners = new HashSet<>();
@ -55,10 +54,10 @@ public class WikivoyageSearchHelper {
void onSearchStarted();
void onSearchFinished(@Nullable List<SearchResult> results);
void onSearchFinished(@Nullable Collection<WikivoyageSearchResult> results);
}
private class SearchAsyncTask extends AsyncTask<Void, Void, List<SearchResult>> {
private class SearchAsyncTask extends AsyncTask<Void, Void, Collection<WikivoyageSearchResult>> {
private String query;
@ -75,7 +74,7 @@ public class WikivoyageSearchHelper {
}
@Override
protected List<SearchResult> doInBackground(Void... voids) {
protected Collection<WikivoyageSearchResult> doInBackground(Void... voids) {
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime <= TIMEOUT_BETWEEN_CHARS) {
if (isCancelled()) {
@ -92,7 +91,7 @@ public class WikivoyageSearchHelper {
}
@Override
protected void onPostExecute(List<SearchResult> results) {
protected void onPostExecute(Collection<WikivoyageSearchResult> results) {
super.onPostExecute(results);
for (SearchListener listener : listeners) {
listener.onSearchFinished(results);