From 07d938701f39a59c597c9d4d9a4078f138c6fba6 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Fri, 22 Nov 2019 17:31:29 +0100 Subject: [PATCH 01/13] Add loyalty card filter --- .../card_locker/CardShortcutConfigure.java | 2 +- .../card_locker/CsvDatabaseExporter.java | 2 +- .../java/protect/card_locker/DBHelper.java | 12 ++++++-- .../protect/card_locker/MainActivity.java | 30 ++++++++++++++++++- app/src/main/res/layout/main_activity.xml | 8 +++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java index 3e2c8f63..8f8117aa 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java @@ -44,7 +44,7 @@ public void onCreate(Bundle bundle) final ListView cardList = findViewById(R.id.list); cardList.setVisibility(View.VISIBLE); - Cursor cardCursor = db.getLoyaltyCardCursor(); + Cursor cardCursor = db.getLoyaltyCardCursor(""); final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor); cardList.setAdapter(adapter); diff --git a/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java b/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java index a5aac91f..fa5226a0 100644 --- a/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java +++ b/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java @@ -27,7 +27,7 @@ public void exportData(DBHelper db, OutputStreamWriter output) throws IOExceptio DBHelper.LoyaltyCardDbIds.HEADER_TEXT_COLOR, DBHelper.LoyaltyCardDbIds.BARCODE_TYPE); - Cursor cursor = db.getLoyaltyCardCursor(); + Cursor cursor = db.getLoyaltyCardCursor(""); while(cursor.moveToNext()) { diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index e096b16e..f3cb5535 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -144,11 +144,17 @@ public boolean deleteLoyaltyCard (final int id) return (rowsDeleted == 1); } - public Cursor getLoyaltyCardCursor() + public Cursor getLoyaltyCardCursor(final String filter) { + String actualFilter = String.format("%%%s%%", filter); + String[] selectionArgs = { actualFilter, actualFilter }; + SQLiteDatabase db = getReadableDatabase(); - Cursor res = db.rawQuery("select * from " + LoyaltyCardDbIds.TABLE + - " ORDER BY " + LoyaltyCardDbIds.STORE + " COLLATE NOCASE ASC", null); + + Cursor res = db.rawQuery("select * from " + LoyaltyCardDbIds.TABLE + + " WHERE " + LoyaltyCardDbIds.STORE + " LIKE ? " + + " OR " + LoyaltyCardDbIds.NOTE + " LIKE ? " + + " ORDER BY " + LoyaltyCardDbIds.STORE + " COLLATE NOCASE ASC", selectionArgs, null); return res; } diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 58d4d6b1..beaeb0d1 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -12,6 +12,8 @@ import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; @@ -20,6 +22,7 @@ import android.view.View; import android.webkit.WebView; import android.widget.AdapterView; +import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -51,6 +54,25 @@ protected void onCreate(Bundle savedInstanceState) startIntro(); prefs.edit().putBoolean("firstrun", false).commit(); } + + final EditText filter = findViewById(R.id.menu_filter); + final TextWatcher FilterWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + updateLoyaltyCardList(); + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + updateLoyaltyCardList(); + } + + @Override + public void afterTextChanged(Editable editable) { + updateLoyaltyCardList(); + } + }; + filter.addTextChangedListener(FilterWatcher); } @Override @@ -63,22 +85,27 @@ protected void onResume() private void updateLoyaltyCardList() { + final EditText filter = findViewById(R.id.menu_filter); final ListView cardList = findViewById(R.id.list); final TextView helpText = findViewById(R.id.helpText); final DBHelper db = new DBHelper(this); if(db.getLoyaltyCardCount() > 0) { + filter.setVisibility(View.VISIBLE); cardList.setVisibility(View.VISIBLE); helpText.setVisibility(View.GONE); } else { + filter.setVisibility(View.GONE); cardList.setVisibility(View.GONE); helpText.setVisibility(View.VISIBLE); } - Cursor cardCursor = db.getLoyaltyCardCursor(); + String filterText = filter.getText().toString(); + + Cursor cardCursor = db.getLoyaltyCardCursor(filterText); final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor); cardList.setAdapter(adapter); @@ -143,6 +170,7 @@ public boolean onContextItemSelected(MenuItem item) public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); + return super.onCreateOptionsMenu(menu); } diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index b4f2b800..f4505d0b 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -20,6 +20,14 @@ android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 831b3274..27df97c6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,6 +4,7 @@ Loyalty Card Keychain Add + Type to search You don\'t have any loyalty cards at the moment. Click the "+" (plus) button up top to get started.\n\nLoyalty Card Locker lets you carry your loyalty cards on your phone, so they are always within reach. Store From 0a8c85d24cb391c2713c9b878a5042fc82be7a12 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Fri, 22 Nov 2019 19:24:03 +0100 Subject: [PATCH 02/13] Cleanup --- .../protect/card_locker/MainActivity.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index beaeb0d1..2f16ad93 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -47,7 +47,9 @@ protected void onCreate(Bundle savedInstanceState) Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - updateLoyaltyCardList(); + final EditText filter = findViewById(R.id.menu_filter); + + updateLoyaltyCardList(filter.getText().toString()); SharedPreferences prefs = getSharedPreferences("protect.card_locker", MODE_PRIVATE); if (prefs.getBoolean("firstrun", true)) { @@ -55,21 +57,16 @@ protected void onCreate(Bundle savedInstanceState) prefs.edit().putBoolean("firstrun", false).commit(); } - final EditText filter = findViewById(R.id.menu_filter); final TextWatcher FilterWatcher = new TextWatcher() { @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - updateLoyaltyCardList(); - } + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - updateLoyaltyCardList(); - } + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { - updateLoyaltyCardList(); + updateLoyaltyCardList(editable.toString()); } }; filter.addTextChangedListener(FilterWatcher); @@ -80,10 +77,12 @@ protected void onResume() { super.onResume(); - updateLoyaltyCardList(); + final EditText filter = findViewById(R.id.menu_filter); + + updateLoyaltyCardList(filter.getText().toString()); } - private void updateLoyaltyCardList() + private void updateLoyaltyCardList(String filterText) { final EditText filter = findViewById(R.id.menu_filter); final ListView cardList = findViewById(R.id.list); @@ -103,8 +102,6 @@ private void updateLoyaltyCardList() helpText.setVisibility(View.VISIBLE); } - String filterText = filter.getText().toString(); - Cursor cardCursor = db.getLoyaltyCardCursor(filterText); final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor); From e048fdff591f745017e3055345a0cb121217182e Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Fri, 22 Nov 2019 20:24:50 +0100 Subject: [PATCH 03/13] Fix tests --- app/src/test/java/protect/card_locker/DatabaseTest.java | 2 +- app/src/test/java/protect/card_locker/ImportExportTest.java | 2 +- .../protect/card_locker/LoyaltyCardCursorAdapterTest.java | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index a19771dd..e78f6958 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -122,7 +122,7 @@ public void giftCardsViaCursor() assertEquals(CARDS_TO_ADD, db.getLoyaltyCardCount()); - Cursor cursor = db.getLoyaltyCardCursor(); + Cursor cursor = db.getLoyaltyCardCursor(""); assertNotNull(cursor); assertEquals(CARDS_TO_ADD, cursor.getCount()); diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index dd182e71..202b74bd 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -83,7 +83,7 @@ private void addLoyaltyCards(int cardsToAdd) */ private void checkLoyaltyCards() { - Cursor cursor = db.getLoyaltyCardCursor(); + Cursor cursor = db.getLoyaltyCardCursor(""); int index = 1; while(cursor.moveToNext()) diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java index 225dfb36..d9ac77ff 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java @@ -86,7 +86,7 @@ public void TestCursorAdapterEmptyNote() db.insertLoyaltyCard("store", "", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); LoyaltyCard card = db.getLoyaltyCard(1); - Cursor cursor = db.getLoyaltyCardCursor(); + Cursor cursor = db.getLoyaltyCardCursor(""); cursor.moveToFirst(); View view = createView(cursor); @@ -100,7 +100,7 @@ public void TestCursorAdapterWithNote() db.insertLoyaltyCard("store", "note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); LoyaltyCard card = db.getLoyaltyCard(1); - Cursor cursor = db.getLoyaltyCardCursor(); + Cursor cursor = db.getLoyaltyCardCursor(""); cursor.moveToFirst(); View view = createView(cursor); @@ -114,7 +114,7 @@ public void TestCursorAdapterFontSizes() db.insertLoyaltyCard("store", "note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); LoyaltyCard card = db.getLoyaltyCard(1); - Cursor cursor = db.getLoyaltyCardCursor(); + Cursor cursor = db.getLoyaltyCardCursor(""); cursor.moveToFirst(); setFontSizes(1, 2); From 21a8dc6867f79b42714dfb0892aa6c768e9df449 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Fri, 22 Nov 2019 20:46:06 +0100 Subject: [PATCH 04/13] Show warning when no matches --- .../java/protect/card_locker/DBHelper.java | 24 +++++++++++++++++++ .../protect/card_locker/MainActivity.java | 10 ++++++++ app/src/main/res/layout/content_main.xml | 8 +++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 43 insertions(+) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index f3cb5535..9b7b42f0 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -175,5 +175,29 @@ public int getLoyaltyCardCount() return numItems; } + + public int getFilteredLoyaltyCardCount(String filter) + { + String actualFilter = String.format("%%%s%%", filter); + String[] selectionArgs = { actualFilter, actualFilter }; + + SQLiteDatabase db = getReadableDatabase(); + Cursor data = db.rawQuery("SELECT Count(*) FROM " + LoyaltyCardDbIds.TABLE + + " WHERE " + LoyaltyCardDbIds.STORE + " LIKE ? " + + " OR " + LoyaltyCardDbIds.NOTE + " LIKE ? " + , selectionArgs, null); + + int numItems = 0; + + if(data.getCount() == 1) + { + data.moveToFirst(); + numItems = data.getInt(0); + } + + data.close(); + + return numItems; + } } diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 2f16ad93..9913370d 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -87,6 +87,7 @@ private void updateLoyaltyCardList(String filterText) final EditText filter = findViewById(R.id.menu_filter); final ListView cardList = findViewById(R.id.list); final TextView helpText = findViewById(R.id.helpText); + final TextView noMatchingCardsText = findViewById(R.id.noMatchingCardsText); final DBHelper db = new DBHelper(this); if(db.getLoyaltyCardCount() > 0) @@ -94,12 +95,21 @@ private void updateLoyaltyCardList(String filterText) filter.setVisibility(View.VISIBLE); cardList.setVisibility(View.VISIBLE); helpText.setVisibility(View.GONE); + if(db.getFilteredLoyaltyCardCount(filterText) > 0) + { + noMatchingCardsText.setVisibility(View.GONE); + } + else + { + noMatchingCardsText.setVisibility(View.VISIBLE); + } } else { filter.setVisibility(View.GONE); cardList.setVisibility(View.GONE); helpText.setVisibility(View.VISIBLE); + noMatchingCardsText.setVisibility(View.GONE); } Cursor cardCursor = db.getLoyaltyCardCursor(filterText); diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 56b5d76d..71eea438 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -17,6 +17,14 @@ android:text="@string/noGiftCards" android:visibility="gone"/> + + Type to search You don\'t have any loyalty cards at the moment. Click the "+" (plus) button up top to get started.\n\nLoyalty Card Locker lets you carry your loyalty cards on your phone, so they are always within reach. + No loyalty cards match the search filter. Please try some different terms. Store Note From 52a09056e802b094cb560775766daaa7dc755f3c Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Fri, 22 Nov 2019 20:54:34 +0100 Subject: [PATCH 05/13] Remove default focus --- app/src/main/res/layout/main_activity.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index f4505d0b..b70251b1 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -6,7 +6,9 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" - tools:context="protect.card_locker.MainActivity"> + tools:context="protect.card_locker.MainActivity" + android:focusable="true" + android:focusableInTouchMode="true"> Date: Fri, 22 Nov 2019 21:03:55 +0100 Subject: [PATCH 06/13] Bump minSdk because of rawQuery usage --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 5ed8332c..5c5b3621 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { defaultConfig { applicationId "protect.card_locker" - minSdkVersion 15 + minSdkVersion 16 targetSdkVersion 27 versionCode 35 versionName "0.25.4" From f0426b98dcf5cfddd9c8cfa6cbfcedab0e82cebb Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sat, 23 Nov 2019 14:29:10 +0100 Subject: [PATCH 07/13] Cleanup --- .../card_locker/CardShortcutConfigure.java | 2 +- .../card_locker/CsvDatabaseExporter.java | 2 +- .../java/protect/card_locker/DBHelper.java | 36 +++++++++++-------- .../protect/card_locker/MainActivity.java | 2 +- .../protect/card_locker/DatabaseTest.java | 2 +- .../protect/card_locker/ImportExportTest.java | 2 +- .../LoyaltyCardCursorAdapterTest.java | 6 ++-- 7 files changed, 29 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java index 8f8117aa..3e2c8f63 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java @@ -44,7 +44,7 @@ public void onCreate(Bundle bundle) final ListView cardList = findViewById(R.id.list); cardList.setVisibility(View.VISIBLE); - Cursor cardCursor = db.getLoyaltyCardCursor(""); + Cursor cardCursor = db.getLoyaltyCardCursor(); final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor); cardList.setAdapter(adapter); diff --git a/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java b/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java index fa5226a0..a5aac91f 100644 --- a/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java +++ b/app/src/main/java/protect/card_locker/CsvDatabaseExporter.java @@ -27,7 +27,7 @@ public void exportData(DBHelper db, OutputStreamWriter output) throws IOExceptio DBHelper.LoyaltyCardDbIds.HEADER_TEXT_COLOR, DBHelper.LoyaltyCardDbIds.BARCODE_TYPE); - Cursor cursor = db.getLoyaltyCardCursor(""); + Cursor cursor = db.getLoyaltyCardCursor(); while(cursor.moveToNext()) { diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 9b7b42f0..6952acca 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -144,6 +144,18 @@ public boolean deleteLoyaltyCard (final int id) return (rowsDeleted == 1); } + public Cursor getLoyaltyCardCursor() + { + // An empty string will match everything + return getLoyaltyCardCursor(""); + } + + /** + * Returns a cursor to all loyalty cards with the filter text in either the store or note. + * + * @param filter + * @return Cursor + */ public Cursor getLoyaltyCardCursor(final String filter) { String actualFilter = String.format("%%%s%%", filter); @@ -160,23 +172,17 @@ public Cursor getLoyaltyCardCursor(final String filter) public int getLoyaltyCardCount() { - SQLiteDatabase db = getReadableDatabase(); - Cursor data = db.rawQuery("SELECT Count(*) FROM " + LoyaltyCardDbIds.TABLE, null); - - int numItems = 0; - - if(data.getCount() == 1) - { - data.moveToFirst(); - numItems = data.getInt(0); - } - - data.close(); - - return numItems; + // An empty string will match everything + return getLoyaltyCardCount(""); } - public int getFilteredLoyaltyCardCount(String filter) + /** + * Returns the amount of loyalty cards with the filter text in either the store or note. + * + * @param filter + * @return Integer + */ + public int getLoyaltyCardCount(String filter) { String actualFilter = String.format("%%%s%%", filter); String[] selectionArgs = { actualFilter, actualFilter }; diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 9913370d..2aba320d 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -95,7 +95,7 @@ private void updateLoyaltyCardList(String filterText) filter.setVisibility(View.VISIBLE); cardList.setVisibility(View.VISIBLE); helpText.setVisibility(View.GONE); - if(db.getFilteredLoyaltyCardCount(filterText) > 0) + if(db.getLoyaltyCardCount(filterText) > 0) { noMatchingCardsText.setVisibility(View.GONE); } diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index e78f6958..a19771dd 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -122,7 +122,7 @@ public void giftCardsViaCursor() assertEquals(CARDS_TO_ADD, db.getLoyaltyCardCount()); - Cursor cursor = db.getLoyaltyCardCursor(""); + Cursor cursor = db.getLoyaltyCardCursor(); assertNotNull(cursor); assertEquals(CARDS_TO_ADD, cursor.getCount()); diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 202b74bd..dd182e71 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -83,7 +83,7 @@ private void addLoyaltyCards(int cardsToAdd) */ private void checkLoyaltyCards() { - Cursor cursor = db.getLoyaltyCardCursor(""); + Cursor cursor = db.getLoyaltyCardCursor(); int index = 1; while(cursor.moveToNext()) diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java index d9ac77ff..225dfb36 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java @@ -86,7 +86,7 @@ public void TestCursorAdapterEmptyNote() db.insertLoyaltyCard("store", "", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); LoyaltyCard card = db.getLoyaltyCard(1); - Cursor cursor = db.getLoyaltyCardCursor(""); + Cursor cursor = db.getLoyaltyCardCursor(); cursor.moveToFirst(); View view = createView(cursor); @@ -100,7 +100,7 @@ public void TestCursorAdapterWithNote() db.insertLoyaltyCard("store", "note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); LoyaltyCard card = db.getLoyaltyCard(1); - Cursor cursor = db.getLoyaltyCardCursor(""); + Cursor cursor = db.getLoyaltyCardCursor(); cursor.moveToFirst(); View view = createView(cursor); @@ -114,7 +114,7 @@ public void TestCursorAdapterFontSizes() db.insertLoyaltyCard("store", "note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); LoyaltyCard card = db.getLoyaltyCard(1); - Cursor cursor = db.getLoyaltyCardCursor(""); + Cursor cursor = db.getLoyaltyCardCursor(); cursor.moveToFirst(); setFontSizes(1, 2); From 30419b58964927c929d5f017e715b3abe319fe8a Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sat, 23 Nov 2019 14:35:03 +0100 Subject: [PATCH 08/13] Explain focusable change --- app/src/main/res/layout/main_activity.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index b70251b1..a9b9c52f 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -9,6 +9,16 @@ tools:context="protect.card_locker.MainActivity" android:focusable="true" android:focusableInTouchMode="true"> + Date: Sat, 23 Nov 2019 15:27:31 +0100 Subject: [PATCH 09/13] Add filtering test --- .../protect/card_locker/MainActivity.java | 3 + .../protect/card_locker/MainActivityTest.java | 114 ++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 2aba320d..5a59eac0 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -93,6 +93,9 @@ private void updateLoyaltyCardList(String filterText) if(db.getLoyaltyCardCount() > 0) { filter.setVisibility(View.VISIBLE); + // We want the cardList to be visible regardless of the filtered match count + // to ensure that the noMatchingCardsText doesn't end up being shown below + // the keyboard cardList.setVisibility(View.VISIBLE); helpText.setVisibility(View.GONE); if(db.getLoyaltyCardCount(filterText) > 0) diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index a7479004..14835528 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -10,6 +10,7 @@ import android.os.Bundle; import android.view.Menu; import android.view.View; +import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; @@ -50,9 +51,15 @@ public void initiallyNoLoyaltyCards() throws Exception Activity activity = Robolectric.setupActivity(MainActivity.class); assertTrue(activity != null); + EditText filter = activity.findViewById(R.id.menu_filter); + assertEquals(View.GONE, filter.getVisibility()); + TextView helpText = activity.findViewById(R.id.helpText); assertEquals(View.VISIBLE, helpText.getVisibility()); + TextView noMatchingCardsText = activity.findViewById(R.id.noMatchingCardsText); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + ListView list = activity.findViewById(R.id.list); assertEquals(View.GONE, list.getVisibility()); } @@ -97,7 +104,9 @@ public void addOneLoyaltyCard() activityController.start(); activityController.resume(); + EditText filter = mainActivity.findViewById(R.id.menu_filter); TextView helpText = mainActivity.findViewById(R.id.helpText); + TextView noMatchingCardsText = mainActivity.findViewById(R.id.noMatchingCardsText); ListView list = mainActivity.findViewById(R.id.list); assertEquals(0, list.getCount()); @@ -105,13 +114,17 @@ public void addOneLoyaltyCard() DBHelper db = new DBHelper(mainActivity); db.insertLoyaltyCard("store", "note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); + assertEquals(View.GONE, filter.getVisibility()); assertEquals(View.VISIBLE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.GONE, list.getVisibility()); activityController.pause(); activityController.resume(); + assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(1, list.getAdapter().getCount()); @@ -119,6 +132,107 @@ public void addOneLoyaltyCard() assertNotNull(cursor); } + @Test + public void testFiltering() + { + ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create(); + + Activity mainActivity = (Activity)activityController.get(); + activityController.start(); + activityController.resume(); + + EditText filter = mainActivity.findViewById(R.id.menu_filter); + TextView helpText = mainActivity.findViewById(R.id.helpText); + TextView noMatchingCardsText = mainActivity.findViewById(R.id.noMatchingCardsText); + ListView list = mainActivity.findViewById(R.id.list); + + DBHelper db = new DBHelper(mainActivity); + db.insertLoyaltyCard("The First Store", "Initial note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); + db.insertLoyaltyCard("The Second Store", "Secondary note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(2, list.getCount()); + + filter.setText("store"); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(2, list.getCount()); + + filter.setText("first"); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(1, list.getCount()); + + filter.setText("initial"); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(1, list.getCount()); + + filter.setText("second"); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(1, list.getCount()); + + filter.setText("company"); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.VISIBLE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(0, list.getCount()); + + filter.setText(""); + + activityController.pause(); + activityController.resume(); + + assertEquals(View.VISIBLE, filter.getVisibility()); + assertEquals(View.GONE, helpText.getVisibility()); + assertEquals(View.GONE, noMatchingCardsText.getVisibility()); + assertEquals(View.VISIBLE, list.getVisibility()); + + assertEquals(2, list.getCount()); + } + @Test public void testFirstRunStartsIntro() { From c936e3c9c0ff8410c37ac81b5e27df239b31167b Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sat, 23 Nov 2019 20:27:37 +0100 Subject: [PATCH 10/13] Switch to SearchView --- .../protect/card_locker/MainActivity.java | 104 +++++++++++++----- .../res/drawable-hdpi/ic_search_white.png | Bin 0 -> 582 bytes .../res/drawable-mdpi/ic_search_white.png | Bin 0 -> 410 bytes .../res/drawable-xhdpi/ic_search_white.png | Bin 0 -> 783 bytes .../res/drawable-xxhdpi/ic_search_white.png | Bin 0 -> 1292 bytes app/src/main/res/layout/main_activity.xml | 22 +--- app/src/main/res/menu/main_menu.xml | 6 + app/src/main/res/values/strings.xml | 2 +- .../protect/card_locker/MainActivityTest.java | 33 ++---- 9 files changed, 97 insertions(+), 70 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_search_white.png create mode 100644 app/src/main/res/drawable-mdpi/ic_search_white.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_search_white.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_search_white.png diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 5a59eac0..a632e9d9 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -1,6 +1,8 @@ package protect.card_locker; +import android.app.SearchManager; import android.content.ClipData; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.ClipboardManager; @@ -11,6 +13,7 @@ import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; @@ -38,6 +41,7 @@ public class MainActivity extends AppCompatActivity { private static final String TAG = "LoyaltyCardLocker"; + private Menu _menu; @Override protected void onCreate(Bundle savedInstanceState) @@ -47,44 +51,66 @@ protected void onCreate(Bundle savedInstanceState) Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - final EditText filter = findViewById(R.id.menu_filter); - - updateLoyaltyCardList(filter.getText().toString()); + updateLoyaltyCardList(""); SharedPreferences prefs = getSharedPreferences("protect.card_locker", MODE_PRIVATE); if (prefs.getBoolean("firstrun", true)) { startIntro(); prefs.edit().putBoolean("firstrun", false).commit(); } + } - final TextWatcher FilterWatcher = new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } + @Override + protected void onResume() { + super.onResume(); - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } + String filter = ""; - @Override - public void afterTextChanged(Editable editable) { - updateLoyaltyCardList(editable.toString()); + if (_menu != null) + { + SearchView searchView = (SearchView) _menu.findItem(R.id.action_search).getActionView(); + + if (!searchView.isIconified()) { + filter = searchView.getQuery().toString(); } - }; - filter.addTextChangedListener(FilterWatcher); + } + + updateLoyaltyCardList(filter); } @Override - protected void onResume() - { - super.onResume(); + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == 1) + { + // 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) + { + MenuItem searchItem = _menu.findItem(R.id.action_search); + searchItem.collapseActionView(); + } + } + } - final EditText filter = findViewById(R.id.menu_filter); + @Override + public void onBackPressed() { + if (_menu == null) + { + super.onBackPressed(); + return; + } - updateLoyaltyCardList(filter.getText().toString()); + SearchView searchView = (SearchView) _menu.findItem(R.id.action_search).getActionView(); + + if (!searchView.isIconified()) { + searchView.setIconified(true); + } else { + super.onBackPressed(); + } } private void updateLoyaltyCardList(String filterText) { - final EditText filter = findViewById(R.id.menu_filter); final ListView cardList = findViewById(R.id.list); final TextView helpText = findViewById(R.id.helpText); final TextView noMatchingCardsText = findViewById(R.id.noMatchingCardsText); @@ -92,7 +118,6 @@ private void updateLoyaltyCardList(String filterText) if(db.getLoyaltyCardCount() > 0) { - filter.setVisibility(View.VISIBLE); // We want the cardList to be visible regardless of the filtered match count // to ensure that the noMatchingCardsText doesn't end up being shown below // the keyboard @@ -109,7 +134,6 @@ private void updateLoyaltyCardList(String filterText) } else { - filter.setVisibility(View.GONE); cardList.setVisibility(View.GONE); helpText.setVisibility(View.VISIBLE); noMatchingCardsText.setVisibility(View.GONE); @@ -138,7 +162,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) ShortcutHelper.updateShortcuts(MainActivity.this, loyaltyCard, i); - startActivity(i); + startActivityForResult(i, 1); } }); } @@ -179,8 +203,36 @@ public boolean onContextItemSelected(MenuItem item) @Override public boolean onCreateOptionsMenu(Menu menu) { + _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; + } + }); + + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + updateLoyaltyCardList(newText); + return true; + } + }); + return super.onCreateOptionsMenu(menu); } @@ -192,21 +244,21 @@ public boolean onOptionsItemSelected(MenuItem item) if (id == R.id.action_add) { Intent i = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); - startActivity(i); + startActivityForResult(i, 1); return true; } if(id == R.id.action_import_export) { Intent i = new Intent(getApplicationContext(), ImportExportActivity.class); - startActivity(i); + startActivityForResult(i, 1); return true; } if(id == R.id.action_settings) { Intent i = new Intent(getApplicationContext(), SettingsActivity.class); - startActivity(i); + startActivityForResult(i, 1); return true; } @@ -314,6 +366,6 @@ public void onClick(DialogInterface dialog, int which) private void startIntro() { Intent intent = new Intent(this, IntroActivity.class); - startActivity(intent); + startActivityForResult(intent, 1); } } \ No newline at end of file diff --git a/app/src/main/res/drawable-hdpi/ic_search_white.png b/app/src/main/res/drawable-hdpi/ic_search_white.png new file mode 100644 index 0000000000000000000000000000000000000000..b74d89872e9916af2dc07855f053534f20f02baa GIT binary patch literal 582 zcmV-M0=fN(P)Q&!DP^^Maprt4ABt_6d(s#16WrQ$nVoY{7 zS=fOOgYJILw>vvK6U#DY%$PCl?0UVH03Cn_SON}!AH)H_^KeY5XwJU@)_?%;dk1XH zip?p4<4=GIz)#>f1)DPkRPu7Z0odGe@)kmIV;Dy>kCop zRh0rJ949^jU4R`k&jvU+md>B3Jg{^XS%#@Uz){CJboU$;sdCL(`FhGTv?W~YxE&NB zphHq6d+ui7b2yo)BU6A!{&1dk#gHS+>B$s8@0B4JEeo0HEoI~6fz}Y993*FM_f^ZI0USxij>mM&N z%J(9>lvoKYC3b{smS-{7c+mL|P8sp4sv@0o%HeI2aKfUKbfV#&t;G-bcWc*UT~Qkpymi1Qxo8h*TWn zMkH|MBQT(adIe)r@TD;;-zunUdp&5AX;|i$z`LuoO`=Z=N>#K@?3|N-1Zl^*t4Xfc z&$ZM#`ofLq*bPI_4s{=trMFvdLf-k1zCVY&6`RstbC;80r5BROwsXIQlTeYWbU0}; zQs>7?O_H8fqM&G$__q>UP`i|bB)`7oU;A1TM;w2QU#Dd9g{$j4O#lD@07*qoM6N<$ Eg5zPi9smFU literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_search_white.png b/app/src/main/res/drawable-xhdpi/ic_search_white.png new file mode 100644 index 0000000000000000000000000000000000000000..a35ff9349e9f6971045a45c40b5ba26f09da8b5a GIT binary patch literal 783 zcmV+q1MvKbP)qQPxIAq#b(;KD@~Vpl>6ZdxC!Lb_29-H0w+ z2)a>Jg4%qi^gl3x5YF72$GM)9-Wm8|Ks___KaV+Q5(H{$YHDg~YHAj&QmOQSDUb(6 za0MQ~3%COpU=J*VQIPsy68{LcILUGooP$Ns*QtrW<=K92GVyw@!CXhJ2n72DT(+5b zJ-@)aPDuV6C|ORv${9%ep8QKh5+u$(SO-~<0Xg0-Ck==|D{I)-WnYR2d>*`s7$*%j zBE~uey}liI(w?Uku}WpI$uUR3?+_8b`y@bK2$JgjbQ|M#flXoTzhK;#I{z%p{In$> z1wn*E9|YJEf?KtGi#!(mOuD|_-U$a@k7ZvIf&c}V0!#@(&Bx^Lz~Jj1bo+Gpm7+>r z8~$bl!N*()P&5RyAEUnk@MQ3rbtS-+A(-`;eO?%R=3NQ!@Y=H>th#Mi0+{aonV5a< z8GQb5C4i{}=3@4F-_5CQEJ4>zvFfGTE82x<`%HXUk0hWb;(lPnV36G*7R|1R* z0&K+Kt6SJ|r!KF;Dem5yZi6G1ePso)*Id5_7lpv6nbBp*$7gVB@bL&fxE7#~Th}HL zHZ6?*URY)B%AX;~xscXXq1C4`&%BiU4H)!AfE4%bB@w8ZG126Kmlc+M_2mB=_+<2@ z1^4a4TG|Kvq#X#U^Eu(PuVpvz(YU4E$TvbyoMe$NQ%AxLuN<-heu5pa;F}1;+_$&= ztq>)>%RlRXx8j=!z0|iCf^CJH>YnGBFMBTn2Rlx!>oyX9#NPFrZrbjhJTAMyWwicnMD#<;}M%x+UNgFi3PtFDoU4JFyJB3KktMNkn`1QkI= zP!UvwmPE)TS%g2JKS+WZL`Y8Z1`(2%e2oa1#3rw%rlzK*rl!`4{R6s{X^PiGL$m+@ N002ovPDHLkV1nZyY^neN literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_search_white.png b/app/src/main/res/drawable-xxhdpi/ic_search_white.png new file mode 100644 index 0000000000000000000000000000000000000000..0f1207e2c8e0eae34bf7e196f695aa1145da4595 GIT binary patch literal 1292 zcmV+n1@roeP)_^si>UbP+$J`cs`8%@j}0n6%>!b8sVV>2EtP+P5cC*4#m{nmo_fY} z`Q~S*iuD(W!^B?1`Hf&3el|p0Bp&AQ`~)#n3O?V9^3RD&P;P~Xbt;Hq;xaC3-1UAD zTV0NJxCd?$x7zS+;&%G%9Xun7y8vfxC1Z_?2AJ`n{8#zBx#EI1jPNM z`^13)$}xa7I5+1mm(mNHOy##1L7rBGo;rCxJCV#oZinM`28t+Cgs~&lE{M30)m-QJ zd;_w*WE{*J>L9n-03D31X4FKg7=reu4$ zCDZwNT-LJ5xMyYR8tDPuLD%a8Ir|xIve`xhj4*OL6+V!&n)p*A>xhj8nAg12f{$cf z(8xMxqXBdg#;}iM9cDyoWwse0rpdST`bgFujRAIvRkj)6NE2VT`AF7gO*k5}E<>ux zJgS;NqmN{5KtU#3o~qt91Dsi8fCm}_sJlR&xS+zx+M+oFc8GW75H6goVT}R4Sl3a< z2t$W_BRwt0kfKyTJ({uiyB#HZ8X5B=A{mOl#1BX$U5k1GA`@`Ir}upI90T{ zZ=(T%=+Dn)fMwGaCtCl#M#imxt-x?ZwwF8eK|w}WmV9Hg0Xi8k7&Q^%pcHKQE460jX;0l8!pzl+_Ll3=*UkP8MV$5;@jF{`x*@;qcVW_|EO zn4GVdeuS|g&agzJz~B4>=8kL+xB^v8^3a@nf~Kip9@npg&rdM>LMH;Q0qyDA9>z+e z`S8IAHP<{$(0p`uz#EmP1COIf%V-^`_LyGi!zC-_VZAu9Dc~}oOs^wKNwkinN0fSC z%mx%wT98ssk0|ZsIOwg;sTah1V7-%r-D)6?>5p~0Baw&aWNMd;gDNtcbaf|$Fq36^ zOf}Ak(577739XpPx-g*l6b{@bxSey23v|{9H3(5tMO40u^C!~L4*Y$baSlznd^mSu zEU1u%IKvIM{o2|b1Gw#upok!Ds~gOTMlXLmXa(m^MTT|jnZtW-yQBXUqUL+M)o=u( zT$s(rZ>fB~E~Sio)uhL;*j;y&A&t?DcK12lkayAcvJV68pmk&w%>aYBlo9dG_Huu4 zdnaTuNEHrmHJ! zj>H`a#2tw{65PfeO~_6#=Z@6>8IwO6ZpIxYoh|NL;Ev8sbbpIG`uEKr7r$@D9f{j9 zXb2VUjEN@9?gWW;1OkCTAP@)y0)apv5C{YU8}SG6lqXK%BOAE@0000 - + tools:context="protect.card_locker.MainActivity"> - - diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml index 5869d946..537c289b 100644 --- a/app/src/main/res/menu/main_menu.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -2,6 +2,12 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="protect.card_locker.MainActivity"> + Loyalty Card Keychain + Search Add - Type to search You don\'t have any loyalty cards at the moment. Click the "+" (plus) button up top to get started.\n\nLoyalty Card Locker lets you carry your loyalty cards on your phone, so they are always within reach. No loyalty cards match the search filter. Please try some different terms. diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index 14835528..6a013a9b 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -8,6 +8,7 @@ import android.database.Cursor; import android.graphics.Color; import android.os.Bundle; +import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.View; import android.widget.EditText; @@ -51,9 +52,6 @@ public void initiallyNoLoyaltyCards() throws Exception Activity activity = Robolectric.setupActivity(MainActivity.class); assertTrue(activity != null); - EditText filter = activity.findViewById(R.id.menu_filter); - assertEquals(View.GONE, filter.getVisibility()); - TextView helpText = activity.findViewById(R.id.helpText); assertEquals(View.VISIBLE, helpText.getVisibility()); @@ -72,9 +70,10 @@ public void onCreateShouldInflateLayout() throws Exception final Menu menu = shadowOf(activity).getOptionsMenu(); assertTrue(menu != null); - // The settings and add button should be present - assertEquals(menu.size(), 5); + // The settings, search and add button should be present + assertEquals(menu.size(), 6); + assertEquals("Search", menu.findItem(R.id.action_search).getTitle().toString()); assertEquals("Add", menu.findItem(R.id.action_add).getTitle().toString()); assertEquals("Import/Export", menu.findItem(R.id.action_import_export).getTitle().toString()); assertEquals("Start Intro", menu.findItem(R.id.action_intro).getTitle().toString()); @@ -104,7 +103,6 @@ public void addOneLoyaltyCard() activityController.start(); activityController.resume(); - EditText filter = mainActivity.findViewById(R.id.menu_filter); TextView helpText = mainActivity.findViewById(R.id.helpText); TextView noMatchingCardsText = mainActivity.findViewById(R.id.noMatchingCardsText); ListView list = mainActivity.findViewById(R.id.list); @@ -114,7 +112,6 @@ public void addOneLoyaltyCard() DBHelper db = new DBHelper(mainActivity); db.insertLoyaltyCard("store", "note", "cardId", BarcodeFormat.UPC_A.toString(), Color.BLACK, Color.WHITE); - assertEquals(View.GONE, filter.getVisibility()); assertEquals(View.VISIBLE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.GONE, list.getVisibility()); @@ -122,7 +119,6 @@ public void addOneLoyaltyCard() activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); @@ -141,7 +137,7 @@ public void testFiltering() activityController.start(); activityController.resume(); - EditText filter = mainActivity.findViewById(R.id.menu_filter); + 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); @@ -153,79 +149,72 @@ public void testFiltering() activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(2, list.getCount()); - filter.setText("store"); + searchView.setQuery("store", false); activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(2, list.getCount()); - filter.setText("first"); + searchView.setQuery("first", false); activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(1, list.getCount()); - filter.setText("initial"); + searchView.setQuery("initial", false); activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(1, list.getCount()); - filter.setText("second"); + searchView.setQuery("second", false); activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(1, list.getCount()); - filter.setText("company"); + searchView.setQuery("company", false); activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.VISIBLE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(0, list.getCount()); - filter.setText(""); + searchView.setQuery("", false); activityController.pause(); activityController.resume(); - assertEquals(View.VISIBLE, filter.getVisibility()); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); From d7556b99519bb89015150d96712657582dbce306 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sun, 24 Nov 2019 19:05:26 +0100 Subject: [PATCH 11/13] Remove unneeded imports --- app/src/main/java/protect/card_locker/MainActivity.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a632e9d9..f5a43310 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -15,8 +15,6 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; -import android.text.Editable; -import android.text.TextWatcher; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; @@ -25,7 +23,6 @@ import android.view.View; import android.webkit.WebView; import android.widget.AdapterView; -import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; From 7a78eadabbd0cce91b7cb3b2c33e5835423abaff Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sun, 24 Nov 2019 20:08:56 +0100 Subject: [PATCH 12/13] Remove unneeded imports --- app/src/test/java/protect/card_locker/MainActivityTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index 6a013a9b..15e014f2 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -11,7 +11,6 @@ import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.View; -import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; From 5a898b4c4c62ba28ad6ca4358662d2698a5f3a73 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Thu, 28 Nov 2019 22:49:06 +0100 Subject: [PATCH 13/13] Address comments --- .../protect/card_locker/MainActivity.java | 81 ++++++++++--------- .../protect/card_locker/MainActivityTest.java | 15 ++-- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index f5a43310..2820fe50 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -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 = ""; @Override protected void onCreate(Bundle savedInstanceState) @@ -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(); @@ -77,13 +78,14 @@ 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(); } } @@ -91,13 +93,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { @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); @@ -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); } }); } @@ -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); } @@ -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; } @@ -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); } } \ No newline at end of file diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index 15e014f2..5523ad61 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -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); @@ -154,7 +153,7 @@ public void testFiltering() assertEquals(2, list.getCount()); - searchView.setQuery("store", false); + mainActivity.filter = "store"; activityController.pause(); activityController.resume(); @@ -165,7 +164,7 @@ public void testFiltering() assertEquals(2, list.getCount()); - searchView.setQuery("first", false); + mainActivity.filter = "first"; activityController.pause(); activityController.resume(); @@ -176,7 +175,7 @@ public void testFiltering() assertEquals(1, list.getCount()); - searchView.setQuery("initial", false); + mainActivity.filter = "initial"; activityController.pause(); activityController.resume(); @@ -187,7 +186,7 @@ public void testFiltering() assertEquals(1, list.getCount()); - searchView.setQuery("second", false); + mainActivity.filter = "second"; activityController.pause(); activityController.resume(); @@ -198,7 +197,7 @@ public void testFiltering() assertEquals(1, list.getCount()); - searchView.setQuery("company", false); + mainActivity.filter = "company"; activityController.pause(); activityController.resume(); @@ -209,7 +208,7 @@ public void testFiltering() assertEquals(0, list.getCount()); - searchView.setQuery("", false); + mainActivity.filter = ""; activityController.pause(); activityController.resume();