Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loyalty card filter #320

Merged
merged 13 commits into from
Nov 29, 2019
81 changes: 43 additions & 38 deletions app/src/main/java/protect/card_locker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
public class MainActivity extends AppCompatActivity
{
private static final String TAG = "LoyaltyCardLocker";
private Menu _menu;
private static final int MAIN_REQUEST_CODE = 1;

private Menu menu;
protected String filter = "";
brarcher marked this conversation as resolved.
Show resolved Hide resolved

@Override
protected void onCreate(Bundle savedInstanceState)
Expand All @@ -61,11 +64,9 @@ protected void onCreate(Bundle savedInstanceState)
protected void onResume() {
super.onResume();

String filter = "";

if (_menu != null)
if (menu != null)
{
SearchView searchView = (SearchView) _menu.findItem(R.id.action_search).getActionView();
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

if (!searchView.isIconified()) {
filter = searchView.getQuery().toString();
Expand All @@ -77,27 +78,28 @@ protected void onResume() {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1)
if (requestCode == MAIN_REQUEST_CODE)
{
// We're coming back from another view so clear the search
// We only do this now to prevent a flash of all entries right after picking one
if (_menu != null)
filter = "";
if (menu != null)
{
MenuItem searchItem = _menu.findItem(R.id.action_search);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.collapseActionView();
}
}
}

@Override
public void onBackPressed() {
if (_menu == null)
if (menu == null)
{
super.onBackPressed();
return;
}

SearchView searchView = (SearchView) _menu.findItem(R.id.action_search).getActionView();
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

if (!searchView.isIconified()) {
searchView.setIconified(true);
Expand Down Expand Up @@ -159,7 +161,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)

ShortcutHelper.updateShortcuts(MainActivity.this, loyaltyCard, i);

startActivityForResult(i, 1);
startActivityForResult(i, MAIN_REQUEST_CODE);
}
});
}
Expand Down Expand Up @@ -200,35 +202,38 @@ public boolean onContextItemSelected(MenuItem item)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
_menu = menu;
this.menu = menu;

getMenuInflater().inflate(R.menu.main_menu, menu);

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
invalidateOptionsMenu();
return false;
}
});
if (searchManager != null) {
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
invalidateOptionsMenu();
return false;
}
});

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
updateLoyaltyCardList(newText);
return true;
}
});
@Override
public boolean onQueryTextChange(String newText) {
filter = newText;
updateLoyaltyCardList(newText);
return true;
}
});
}

return super.onCreateOptionsMenu(menu);
}
Expand All @@ -241,21 +246,21 @@ public boolean onOptionsItemSelected(MenuItem item)
if (id == R.id.action_add)
{
Intent i = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class);
startActivityForResult(i, 1);
startActivityForResult(i, MAIN_REQUEST_CODE);
return true;
}

if(id == R.id.action_import_export)
{
Intent i = new Intent(getApplicationContext(), ImportExportActivity.class);
startActivityForResult(i, 1);
startActivityForResult(i, MAIN_REQUEST_CODE);
return true;
}

if(id == R.id.action_settings)
{
Intent i = new Intent(getApplicationContext(), SettingsActivity.class);
startActivityForResult(i, 1);
startActivityForResult(i, MAIN_REQUEST_CODE);
return true;
}

Expand Down Expand Up @@ -363,6 +368,6 @@ public void onClick(DialogInterface dialog, int which)
private void startIntro()
{
Intent intent = new Intent(this, IntroActivity.class);
startActivityForResult(intent, 1);
startActivityForResult(intent, MAIN_REQUEST_CODE);
}
}
15 changes: 7 additions & 8 deletions app/src/test/java/protect/card_locker/MainActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ public void testFiltering()
{
ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create();

Activity mainActivity = (Activity)activityController.get();
MainActivity mainActivity = (MainActivity)activityController.get();
activityController.start();
activityController.resume();

SearchView searchView = mainActivity.findViewById(R.id.action_search);
TextView helpText = mainActivity.findViewById(R.id.helpText);
TextView noMatchingCardsText = mainActivity.findViewById(R.id.noMatchingCardsText);
ListView list = mainActivity.findViewById(R.id.list);
Expand All @@ -154,7 +153,7 @@ public void testFiltering()

assertEquals(2, list.getCount());

searchView.setQuery("store", false);
mainActivity.filter = "store";

activityController.pause();
activityController.resume();
Expand All @@ -165,7 +164,7 @@ public void testFiltering()

assertEquals(2, list.getCount());

searchView.setQuery("first", false);
mainActivity.filter = "first";

activityController.pause();
activityController.resume();
Expand All @@ -176,7 +175,7 @@ public void testFiltering()

assertEquals(1, list.getCount());

searchView.setQuery("initial", false);
mainActivity.filter = "initial";

activityController.pause();
activityController.resume();
Expand All @@ -187,7 +186,7 @@ public void testFiltering()

assertEquals(1, list.getCount());

searchView.setQuery("second", false);
mainActivity.filter = "second";

activityController.pause();
activityController.resume();
Expand All @@ -198,7 +197,7 @@ public void testFiltering()

assertEquals(1, list.getCount());

searchView.setQuery("company", false);
mainActivity.filter = "company";

activityController.pause();
activityController.resume();
Expand All @@ -209,7 +208,7 @@ public void testFiltering()

assertEquals(0, list.getCount());

searchView.setQuery("", false);
mainActivity.filter = "";

activityController.pause();
activityController.resume();
Expand Down