Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions app/src/main/java/protect/card_locker/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DBHelper extends SQLiteOpenHelper
{
public static final String DATABASE_NAME = "LoyaltyCards.db";
Expand Down Expand Up @@ -170,6 +173,57 @@ public Cursor getLoyaltyCardCursor(final String filter)
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<LoyaltyCard>
*/
public List<LoyaltyCard> getLoyaltyCardsForStore(String store)
{
List<LoyaltyCard> loyaltyCards = new ArrayList<>();

SQLiteDatabase db = getReadableDatabase();
String[] selectionArgs = { store };

Cursor res = db.rawQuery("select * from " + LoyaltyCardDbIds.TABLE +
" WHERE " + LoyaltyCardDbIds.STORE + " IS ? " +
" ORDER BY " + LoyaltyCardDbIds.STORE + " COLLATE NOCASE ASC", selectionArgs, null);

try
{
while (res.moveToNext()) {
loyaltyCards.add(LoyaltyCard.toLoyaltyCard(res));
}
} finally {
res.close();
}

return loyaltyCards;
}

public int getLoyaltyCardCount()
{
// An empty string will match everything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
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,
Expand All @@ -42,15 +44,25 @@ public void bindView(View view, Context context, Cursor cursor)
// 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.cardCount, cardCount));
}
else
{
noteField.setText(loyaltyCard.note);
}
noteField.setTextSize(settings.getCardNoteListFontSize());
}
else
Expand Down
28 changes: 20 additions & 8 deletions app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -39,6 +41,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity

protected static final int SELECT_BARCODE_REQUEST = 1;

FloatingActionButton fabSave;
EditText storeFieldEdit;
EditText noteFieldEdit;
ImageView headingColorSample;
Expand All @@ -58,6 +61,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity

int loyaltyCardId;
boolean updateLoyaltyCard;
String loyaltyCardStorePrefill = "";
Uri importLoyaltyCardUri = null;
Integer headingColorValue = null;
Integer headingStoreTextColorValue = null;
Expand All @@ -70,6 +74,7 @@ private void extractIntentFields(Intent intent)
final Bundle b = intent.getExtras();
loyaltyCardId = b != null ? b.getInt("id") : 0;
updateLoyaltyCard = b != null && b.getBoolean("update", false);
loyaltyCardStorePrefill = b != null ? b.getString("store", "") : "";
importLoyaltyCardUri = intent.getData();

Log.d(TAG, "View activity: id=" + loyaltyCardId
Expand All @@ -95,6 +100,7 @@ protected void onCreate(Bundle savedInstanceState)
db = new DBHelper(this);
importUriHelper = new ImportURIHelper(this);

fabSave = findViewById(R.id.fabSave);
storeFieldEdit = findViewById(R.id.storeNameEdit);
noteFieldEdit = findViewById(R.id.noteEdit);
headingColorSample = findViewById(R.id.headingColorSample);
Expand Down Expand Up @@ -135,6 +141,14 @@ public void onResume()

Log.i(TAG, "To view card: " + loyaltyCardId);

fabSave.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
doSave();
}
});

if(updateLoyaltyCard)
{
final LoyaltyCard loyaltyCard = db.getLoyaltyCard(loyaltyCardId);
Expand Down Expand Up @@ -210,6 +224,12 @@ else if(importLoyaltyCardUri != null)
setTitle(R.string.addCardTitle);
}

// Set prefill values if nothing is set
if(storeFieldEdit.getText().length() == 0 && !loyaltyCardStorePrefill.isEmpty())
{
storeFieldEdit.setText(loyaltyCardStorePrefill);
}

if(headingColorValue == null)
{
// Select a random color to start out with.
Expand Down Expand Up @@ -413,10 +433,6 @@ public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.card_update_menu, menu);
}
else
{
getMenuInflater().inflate(R.menu.card_add_menu, menu);
}

return super.onCreateOptionsMenu(menu);
}
Expand Down Expand Up @@ -464,10 +480,6 @@ public void onClick(DialogInterface dialog, int which)
dialog.show();

return true;

case R.id.action_save:
doSave();
return true;
}

return super.onOptionsItemSelected(item);
Expand Down
Loading