From 7019412ae409995e5628d1d38a3dd0ba3413b6c3 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 29 Mar 2018 00:29:53 +0200 Subject: [PATCH] Small refactoring & grouping --- .../WikivoyageArticleDialogFragment.java | 8 ++-- .../wikivoyage/data/WikivoyageDbHelper.java | 43 ++++++++++++++++--- ...esult.java => WikivoyageSearchResult.java} | 2 +- .../search/SearchRecyclerViewAdapter.java | 14 +++--- .../WikivoyageSearchDialogFragment.java | 6 +-- .../search/WikivoyageSearchHelper.java | 15 +++---- 6 files changed, 58 insertions(+), 30 deletions(-) rename OsmAnd/src/net/osmand/plus/wikivoyage/data/{SearchResult.java => WikivoyageSearchResult.java} (92%) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java index 06c55fd97d..0b0ee97c12 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageArticleDialogFragment.java @@ -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); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java index 5df6e9d8bc..810191ebf5 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java @@ -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 search(String searchQuery) { - List res = new ArrayList<>(); + public Collection search(String searchQuery) { + List 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 groupSearchResultsByCityId(List res) { + String baseLng = application.getLanguage(); + TLongObjectHashMap wikivoyage = new TLongObjectHashMap(); + 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); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/SearchResult.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java similarity index 92% rename from OsmAnd/src/net/osmand/plus/wikivoyage/data/SearchResult.java rename to OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java index d5a34d7f87..557daedf0b 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/SearchResult.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java @@ -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 searchTerm = new ArrayList<>(); long cityId; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/SearchRecyclerViewAdapter.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/SearchRecyclerViewAdapter.java index eaeac3f693..d048f705a4 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/SearchRecyclerViewAdapter.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/SearchRecyclerViewAdapter.java @@ -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 { - private List items = new ArrayList<>(); + private List items = new ArrayList<>(); private View.OnClickListener onItemClickListener; @@ -33,7 +33,7 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter items) { + public void setItems(@Nullable Collection items) { if (items == null) { this.items.clear(); } else { - this.items = items; + this.items = new ArrayList<>(items); } notifyDataSetChanged(); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java index 1f93fde51a..3ebdc87a1c 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java @@ -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 results) { + public void onSearchFinished(@Nullable Collection results) { adapter.setItems(results); switchProgressBarVisibility(false); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java index 7b61659ff7..f0f528e767 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java @@ -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 workQueue = new LinkedBlockingQueue<>(); private ThreadPoolExecutor singleThreadExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, workQueue); - private AsyncTask> currentTask; + private AsyncTask> currentTask; private OsmandApplication application; private Set listeners = new HashSet<>(); @@ -55,10 +54,10 @@ public class WikivoyageSearchHelper { void onSearchStarted(); - void onSearchFinished(@Nullable List results); + void onSearchFinished(@Nullable Collection results); } - private class SearchAsyncTask extends AsyncTask> { + private class SearchAsyncTask extends AsyncTask> { private String query; @@ -75,7 +74,7 @@ public class WikivoyageSearchHelper { } @Override - protected List doInBackground(Void... voids) { + protected Collection 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 results) { + protected void onPostExecute(Collection results) { super.onPostExecute(results); for (SearchListener listener : listeners) { listener.onSearchFinished(results);