diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 5ac813d95..e1a6b17b7 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -173,13 +173,36 @@ public class DBHelper extends SQLiteOpenHelper return res; } + /** + * Returns a cursor only containing the first loyalty card of the given store. + * + * @param filter + * @return Cursor + */ + public Cursor getOneLoyaltyCardPerStoreCursor(final String filter) + { + String actualFilter = String.format("%%%s%%", filter); + String[] selectionArgs = { actualFilter, actualFilter }; + + SQLiteDatabase db = getReadableDatabase(); + + Cursor res = db.rawQuery("select * from " + LoyaltyCardDbIds.TABLE + + " WHERE " + LoyaltyCardDbIds.STORE + " LIKE ? " + + " OR " + LoyaltyCardDbIds.NOTE + " LIKE ? " + + " GROUP BY " + LoyaltyCardDbIds.STORE + + " HAVING MIN(" + LoyaltyCardDbIds.ID + ")" + + " ORDER BY " + LoyaltyCardDbIds.STORE + " COLLATE NOCASE ASC", selectionArgs, null); + return res; + } + /** * Returns a list of all loyalty cards of the given store. * * @param store * @return List */ - public List getLoyaltyCardsForStore(String store) { + public List getLoyaltyCardsForStore(String store) + { List loyaltyCards = new ArrayList<>(); SQLiteDatabase db = getReadableDatabase(); diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java index 07f04f9f5..81f348b3a 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java @@ -14,11 +14,13 @@ import protect.card_locker.preferences.Settings; class LoyaltyCardCursorAdapter extends CursorAdapter { Settings settings; + DBHelper dbHelper; public LoyaltyCardCursorAdapter(Context context, Cursor cursor) { super(context, cursor, 0); settings = new Settings(context); + dbHelper = new DBHelper(context); } // The newView method is used to inflate a new view and return it, @@ -42,15 +44,25 @@ class LoyaltyCardCursorAdapter extends CursorAdapter // Extract properties from cursor LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + // Get amount of cards for this store + int cardCount = dbHelper.getLoyaltyCardsForStore(loyaltyCard.store).size(); + // Populate fields with extracted properties storeField.setText(loyaltyCard.store); storeField.setTextSize(settings.getCardTitleListFontSize()); - if(loyaltyCard.note.isEmpty() == false) + if(cardCount > 1 || !loyaltyCard.note.isEmpty()) { noteField.setVisibility(View.VISIBLE); - noteField.setText(loyaltyCard.note); + if(cardCount > 1) + { + noteField.setText(context.getResources().getString(R.string.cardBarcodeCount, cardCount)); + } + else + { + noteField.setText(loyaltyCard.note); + } noteField.setTextSize(settings.getCardNoteListFontSize()); } else diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 6488d84ee..891191f9e 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -145,7 +145,7 @@ public class MainActivity extends AppCompatActivity noMatchingCardsText.setVisibility(View.GONE); } - Cursor cardCursor = db.getLoyaltyCardCursor(filterText); + Cursor cardCursor = db.getOneLoyaltyCardPerStoreCursor(filterText); final LoyaltyCardCursorAdapter adapter = new LoyaltyCardCursorAdapter(this, cardCursor); cardList.setAdapter(adapter); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7453d86e9..bb6a51305 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ 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. + %1$d barcodes Store Note