From b9c2fe7d70b7910959ef37bb1794cbd9b810568a Mon Sep 17 00:00:00 2001 From: Starcom Date: Thu, 2 Jan 2020 02:39:18 +0100 Subject: [PATCH] Added search filter on DownloadMapsView. #118 --- .../activities/DownloadMapActivity.java | 66 ++++++++++--- .../pocketmaps/fragments/MyMapAdapter.java | 69 +++++++++++-- .../app/src/main/res/menu/menu_maps.xml | 97 +++++++++++-------- .../app/src/main/res/values-de/strings.xml | 1 + .../app/src/main/res/values-it/strings.xml | 1 + .../app/src/main/res/values-nl/strings.xml | 1 + .../src/main/res/values-pt-rBR/strings.xml | 1 + .../app/src/main/res/values/strings.xml | 1 + 8 files changed, 173 insertions(+), 64 deletions(-) diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/DownloadMapActivity.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/DownloadMapActivity.java index 1d68070..58c869f 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/DownloadMapActivity.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/DownloadMapActivity.java @@ -13,6 +13,8 @@ import android.os.Bundle; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.SearchView; +import androidx.appcompat.widget.SearchView.OnCloseListener; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -20,6 +22,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.View.OnClickListener; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -55,7 +58,7 @@ * Created by GuoJunjun on July 04, 2015. */ public class DownloadMapActivity extends AppCompatActivity - implements OnClickMapListener { + implements OnClickMapListener, SearchView.OnQueryTextListener{ private MyMapAdapter myDownloadAdapter; private ProgressBar listDownloadPB; @@ -110,9 +113,41 @@ public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_maps, menu); + + MenuItem searchItem = menu.findItem(R.id.menu_search_filter); + SearchView searchView = (SearchView) searchItem.getActionView(); + searchView.setQueryHint(getResources().getString(R.string.search_hint)); + searchView.setOnQueryTextListener(this); + searchView.setOnSearchClickListener(createHideMenuListener(menu)); + searchView.setOnCloseListener(createShowMenuListener(menu)); return true; } + private OnClickListener createHideMenuListener(final Menu menu) + { + return new OnClickListener() + { + @Override + public void onClick(View arg0) + { + menu.setGroupVisible(R.id.menu_map_all_group, false); + } + }; + } + + private OnCloseListener createShowMenuListener(final Menu menu) + { + return new OnCloseListener() + { + @Override + public boolean onClose() + { + menu.setGroupVisible(R.id.menu_map_all_group, true); + return false; + } + }; + } + private boolean isCloudMapsUpdateOld() { long now = System.currentTimeMillis(); @@ -213,7 +248,7 @@ public void logUserThread(String txt) @Override public void updateMapStatus(MyMap map) { - refreshMapEntry(map, DownloadMapActivity.this.myDownloadAdapter); + DownloadMapActivity.this.myDownloadAdapter.refreshMapView(map); } @Override @@ -337,7 +372,7 @@ else if (myMap.getStatus() == MyMap.DlStatus.Complete) return; } tv.setText("downloading..."); - refreshMapEntry(myMap, myDownloadAdapter); + myDownloadAdapter.refreshMapView(myMap); myMap.setStatus(MyMap.DlStatus.Downloading); String vers = "?v=unknown"; DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); @@ -402,18 +437,6 @@ else if (dlStatus == DownloadManager.STATUS_FAILED) return receiver; } - private static void refreshMapEntry(MyMap myMap, MyMapAdapter dlAdapter) - { - int rvIndex = Variable.getVariable().getCloudMaps().indexOf(myMap); - if (rvIndex >= 0) - { - log("Refreshing map-entry at " + rvIndex); - dlAdapter.notifyItemRemoved(rvIndex); - dlAdapter.notifyItemInserted(rvIndex); - } - else { log("No map-entry for refreshing found"); } - } - public static void clearDlFile(MyMap myMap) { log("Clearing dl file for map: " + myMap.getMapName()); @@ -465,5 +488,18 @@ private void logUserThread(final String str) { logUser(str); }}); } + + @Override + public boolean onQueryTextChange(String filterText) + { + myDownloadAdapter.doFilter(filterText); + return true; + } + + @Override + public boolean onQueryTextSubmit(String filterText) + { + return true; + } } \ No newline at end of file diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/MyMapAdapter.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/MyMapAdapter.java index dcc3b48..8e88a41 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/MyMapAdapter.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/MyMapAdapter.java @@ -22,6 +22,7 @@ */ public class MyMapAdapter extends RecyclerView.Adapter { private List myMaps; + private List myMapsFiltered; private OnClickMapListener onClickMapListener; private boolean isDownloadingView; @@ -102,6 +103,7 @@ public void onClick(View v) public MyMapAdapter(List myMaps, OnClickMapListener onClickMapListener, boolean isDownloadingView) { this.myMaps = myMaps; + this.myMapsFiltered = myMaps; this.onClickMapListener = onClickMapListener; this.isDownloadingView = isDownloadingView; } @@ -117,13 +119,27 @@ public MyMapAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { - holder.setItemData(myMaps.get(position)); + holder.setItemData(myMapsFiltered.get(position)); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { - return myMaps.size(); + return myMapsFiltered.size(); + } + + public void refreshMapView(MyMap myMap) + { + int rvIndex = myMapsFiltered.indexOf(myMap); + if (rvIndex >= 0) + { + notifyItemRemoved(rvIndex); + notifyItemInserted(rvIndex); + } + else + { + log("No map-entry for refreshing found, maybe filter active."); + } } /** @@ -131,7 +147,7 @@ public int getItemCount() { * @return MyMap item at the position */ public MyMap getItem(int position) { - return myMaps.get(position); + return myMapsFiltered.get(position); } /** @@ -141,9 +157,17 @@ public MyMap getItem(int position) { */ public MyMap remove(int position) { MyMap mm = null; - if (position >= 0 && position < getItemCount()) { + if (myMaps == myMapsFiltered) + { + if (position >= 0 && position < getItemCount()) + { mm = myMaps.remove(position); notifyItemRemoved(position); + } + } + else + { + log("WARNING: Cannot delete map on filtered mode."); } return mm; } @@ -154,6 +178,7 @@ public MyMap remove(int position) { */ public void clearList() { this.myMaps.clear(); + this.myMapsFiltered.clear(); } /** @@ -163,7 +188,10 @@ public void clearList() { */ public void addAll(List maps) { this.myMaps.addAll(maps); - notifyItemRangeInserted(myMaps.size() - maps.size(), maps.size()); + if (myMaps == myMapsFiltered) + { + notifyItemRangeInserted(myMaps.size() - maps.size(), maps.size()); + } } /** @@ -174,7 +202,10 @@ public void addAll(List maps) { public void insert(MyMap myMap) { if (!getMapNameList().contains(myMap.getMapName())) { myMaps.add(myMap); - notifyItemInserted(getItemCount() - 1); + if (myMaps == myMapsFiltered) + { + notifyItemInserted(getItemCount() - 1); + } } } @@ -194,4 +225,30 @@ static void log(String txt) { Log.i(MyMapAdapter.class.getName(), txt); } + + public void doFilter(String filterText) + { + log("FILTER-START!"); + filterText = filterText.toLowerCase(); + List filteredList = new ArrayList(); + if (filterText.isEmpty()) + { + filteredList = myMaps; + log("FILTER: Empty"); + } + else + { + for (MyMap curMap : myMaps) + { + if (curMap.getCountry().toLowerCase().contains(filterText) || curMap.getContinent().toLowerCase().contains(filterText)) + { + filteredList.add(curMap); + } + } + log("FILTER: " + filteredList.size() + "/" + myMaps.size()); + } + myMapsFiltered = filteredList; + notifyDataSetChanged(); + log("FILTER: Publish: " + myMapsFiltered.size() + "/" + myMaps.size()); + } } diff --git a/PocketMaps/app/src/main/res/menu/menu_maps.xml b/PocketMaps/app/src/main/res/menu/menu_maps.xml index 86e4fbf..18d0f6b 100644 --- a/PocketMaps/app/src/main/res/menu/menu_maps.xml +++ b/PocketMaps/app/src/main/res/menu/menu_maps.xml @@ -3,48 +3,59 @@ xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> - - - - - - - - + android:icon="@drawable/ic_search_white_24dp" + android:title="Filter" + app:showAsAction="always" + app:actionViewClass="androidx.appcompat.widget.SearchView" + tools:ignore="AppCompatResource"/> + + + + + + + + + + + diff --git a/PocketMaps/app/src/main/res/values-de/strings.xml b/PocketMaps/app/src/main/res/values-de/strings.xml index 55becda..cb53c83 100644 --- a/PocketMaps/app/src/main/res/values-de/strings.xml +++ b/PocketMaps/app/src/main/res/values-de/strings.xml @@ -73,6 +73,7 @@ GPS ist deaktiviert GPS Einstellungen Weiche Animation + Texteingabe Bearbeiten Hilfe diff --git a/PocketMaps/app/src/main/res/values-it/strings.xml b/PocketMaps/app/src/main/res/values-it/strings.xml index d18101a..83c801f 100644 --- a/PocketMaps/app/src/main/res/values-it/strings.xml +++ b/PocketMaps/app/src/main/res/values-it/strings.xml @@ -73,6 +73,7 @@ Il GPS non è attivo Impostazioni Levigare il movimento + inserire il testo Modifica Aiuto diff --git a/PocketMaps/app/src/main/res/values-nl/strings.xml b/PocketMaps/app/src/main/res/values-nl/strings.xml index 1897ee8..488de8e 100644 --- a/PocketMaps/app/src/main/res/values-nl/strings.xml +++ b/PocketMaps/app/src/main/res/values-nl/strings.xml @@ -73,6 +73,7 @@ GPS is gedeactiveerd GPS instellingen Beweging gladmaken + Voer tekst in Bewerk Helpen diff --git a/PocketMaps/app/src/main/res/values-pt-rBR/strings.xml b/PocketMaps/app/src/main/res/values-pt-rBR/strings.xml index aa28c33..b3be3d7 100644 --- a/PocketMaps/app/src/main/res/values-pt-rBR/strings.xml +++ b/PocketMaps/app/src/main/res/values-pt-rBR/strings.xml @@ -73,6 +73,7 @@ GPS está desativado GPS configurações Suavizar o movimento + inserir texto Editar Socorro diff --git a/PocketMaps/app/src/main/res/values/strings.xml b/PocketMaps/app/src/main/res/values/strings.xml index 5b9ba19..e102aaf 100644 --- a/PocketMaps/app/src/main/res/values/strings.xml +++ b/PocketMaps/app/src/main/res/values/strings.xml @@ -73,6 +73,7 @@ GPS is turned off GPS settings Smoothen movement + Enter text Edit Help