Skip to content

Commit

Permalink
Added search filter on DownloadMapsView. #118
Browse files Browse the repository at this point in the history
  • Loading branch information
Starcommander committed Jan 2, 2020
1 parent 4c3e652 commit b9c2fe7
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
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;
import android.util.Log;
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;
Expand Down Expand Up @@ -55,7 +58,7 @@
* Created by GuoJunjun <junjunguo.com> on July 04, 2015.
*/
public class DownloadMapActivity extends AppCompatActivity
implements OnClickMapListener {
implements OnClickMapListener, SearchView.OnQueryTextListener{
private MyMapAdapter myDownloadAdapter;

private ProgressBar listDownloadPB;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public class MyMapAdapter extends RecyclerView.Adapter<MyMapAdapter.ViewHolder> {
private List<MyMap> myMaps;
private List<MyMap> myMapsFiltered;
private OnClickMapListener onClickMapListener;
private boolean isDownloadingView;

Expand Down Expand Up @@ -102,6 +103,7 @@ public void onClick(View v)
public MyMapAdapter(List<MyMap> myMaps, OnClickMapListener onClickMapListener, boolean isDownloadingView)
{
this.myMaps = myMaps;
this.myMapsFiltered = myMaps;
this.onClickMapListener = onClickMapListener;
this.isDownloadingView = isDownloadingView;
}
Expand All @@ -117,21 +119,35 @@ 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.");
}
}

/**
* @param position
* @return MyMap item at the position
*/
public MyMap getItem(int position) {
return myMaps.get(position);
return myMapsFiltered.get(position);
}

/**
Expand All @@ -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;
}
Expand All @@ -154,6 +178,7 @@ public MyMap remove(int position) {
*/
public void clearList() {
this.myMaps.clear();
this.myMapsFiltered.clear();
}

/**
Expand All @@ -163,7 +188,10 @@ public void clearList() {
*/
public void addAll(List<MyMap> maps) {
this.myMaps.addAll(maps);
notifyItemRangeInserted(myMaps.size() - maps.size(), maps.size());
if (myMaps == myMapsFiltered)
{
notifyItemRangeInserted(myMaps.size() - maps.size(), maps.size());
}
}

/**
Expand All @@ -174,7 +202,10 @@ public void addAll(List<MyMap> maps) {
public void insert(MyMap myMap) {
if (!getMapNameList().contains(myMap.getMapName())) {
myMaps.add(myMap);
notifyItemInserted(getItemCount() - 1);
if (myMaps == myMapsFiltered)
{
notifyItemInserted(getItemCount() - 1);
}
}
}

Expand All @@ -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<MyMap> filteredList = new ArrayList<MyMap>();
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());
}
}
97 changes: 54 additions & 43 deletions PocketMaps/app/src/main/res/menu/menu_maps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,59 @@
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/menu_map_downloaded"
android:id="@+id/menu_search_filter"
android:orderInCategory="100"
android:title="Downloaded"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_africa"
android:orderInCategory="100"
android:title="Africa"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_asia"
android:orderInCategory="100"
android:title="Asia"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_australia_oceania"
android:orderInCategory="100"
android:title="Australia-oceania"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_central_america"
android:orderInCategory="100"
android:title="Central-america"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_europe"
android:orderInCategory="100"
android:title="Europe"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_north_america"
android:orderInCategory="100"
android:title="North-america"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_russia"
android:orderInCategory="100"
android:title="Russia"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_south_america"
android:orderInCategory="100"
android:title="South-america"
android:showAsAction="never"/>
android:icon="@drawable/ic_search_white_24dp"
android:title="Filter"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
tools:ignore="AppCompatResource"/>
<group
android:id="@+id/menu_map_all_group">
<item
android:id="@+id/menu_map_downloaded"
android:orderInCategory="100"
android:title="Downloaded"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_africa"
android:orderInCategory="100"
android:title="Africa"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_asia"
android:orderInCategory="100"
android:title="Asia"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_australia_oceania"
android:orderInCategory="100"
android:title="Australia-oceania"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_central_america"
android:orderInCategory="100"
android:title="Central-america"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_europe"
android:orderInCategory="100"
android:title="Europe"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_north_america"
android:orderInCategory="100"
android:title="North-america"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_russia"
android:orderInCategory="100"
android:title="Russia"
android:showAsAction="never"/>
<item
android:id="@+id/menu_map_south_america"
android:orderInCategory="100"
android:title="South-america"
android:showAsAction="never"/>
</group>
</menu>
1 change: 1 addition & 0 deletions PocketMaps/app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<string name="gps_is_off">GPS ist deaktiviert</string>
<string name="gps_settings">GPS Einstellungen</string>
<string name="gps_smooth">Weiche Animation</string>
<string name="search_hint">Texteingabe</string>
<string name="edit">Bearbeiten</string>
<string name="help">Hilfe</string>
</resources>
1 change: 1 addition & 0 deletions PocketMaps/app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<string name="gps_is_off">Il GPS non è attivo</string>
<string name="gps_settings">Impostazioni</string>
<string name="gps_smooth">Levigare il movimento</string>
<string name="search_hint">inserire il testo</string>
<string name="edit">Modifica</string>
<string name="help">Aiuto</string>
</resources>
1 change: 1 addition & 0 deletions PocketMaps/app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<string name="gps_is_off">GPS is gedeactiveerd</string>
<string name="gps_settings">GPS instellingen</string>
<string name="gps_smooth">Beweging gladmaken</string>
<string name="search_hint">Voer tekst in</string>
<string name="edit">Bewerk</string>
<string name="help">Helpen</string>
</resources>
1 change: 1 addition & 0 deletions PocketMaps/app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<string name="gps_is_off">GPS está desativado</string>
<string name="gps_settings">GPS configurações</string>
<string name="gps_smooth">Suavizar o movimento</string>
<string name="search_hint">inserir texto</string>
<string name="edit">Editar</string>
<string name="help">Socorro</string>
</resources>
Loading

0 comments on commit b9c2fe7

Please sign in to comment.