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

Adjust width of barcode #1109

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
34 changes: 33 additions & 1 deletion app/src/main/java/protect/card_locker/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Catima.db";
public static final int ORIGINAL_DATABASE_VERSION = 1;
public static final int DATABASE_VERSION = 15;
public static final int DATABASE_VERSION = 16;

public static class LoyaltyCardDbGroups {
public static final String TABLE = "groups";
Expand All @@ -45,6 +45,7 @@ public static class LoyaltyCardDbIds {
public static final String STAR_STATUS = "starstatus";
public static final String LAST_USED = "lastused";
public static final String ZOOM_LEVEL = "zoomlevel";
public static final String ZOOM_WIDTH = "zoomwidth";
public static final String ARCHIVE_STATUS = "archive";
}

Expand Down Expand Up @@ -105,6 +106,7 @@ public void onCreate(SQLiteDatabase db) {
LoyaltyCardDbIds.STAR_STATUS + " INTEGER DEFAULT '0'," +
LoyaltyCardDbIds.LAST_USED + " INTEGER DEFAULT '0', " +
LoyaltyCardDbIds.ZOOM_LEVEL + " INTEGER DEFAULT '100', " +
LoyaltyCardDbIds.ZOOM_WIDTH + " INTEGER DEFAULT '100', " +
LoyaltyCardDbIds.ARCHIVE_STATUS + " INTEGER DEFAULT '0' )");

// create associative table for cards in groups
Expand Down Expand Up @@ -310,10 +312,17 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("ALTER TABLE " + LoyaltyCardDbIds.TABLE
+ " ADD COLUMN " + LoyaltyCardDbIds.ZOOM_LEVEL + " INTEGER DEFAULT '100' ");
}

if (oldVersion < 16 && newVersion >= 16) {
db.execSQL("ALTER TABLE " + LoyaltyCardDbIds.TABLE
+ " ADD COLUMN " + LoyaltyCardDbIds.ZOOM_WIDTH + " INTEGER DEFAULT '100' ");
Comment on lines +317 to +318
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to put the change in a new if statement, right now it will only create ZOOM_WIDTH when upgrading from database version 13 to 14. Users who are already at database version 14 won't be upgraded and the app will crash when trying to access the ZOOM_WIDTH field.

You should also update DATABASE_VERSION at the top of the class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 shouldn't be above 15, please keep consistent ordering.

}

if (oldVersion < 15 && newVersion >= 15) {
db.execSQL("ALTER TABLE " + LoyaltyCardDbIds.TABLE
+ " ADD COLUMN " + LoyaltyCardDbIds.ARCHIVE_STATUS + " INTEGER DEFAULT '0' ");
}

}

private static ContentValues generateFTSContentValues(final int id, final String store, final String note) {
Expand Down Expand Up @@ -494,6 +503,24 @@ public static boolean updateLoyaltyCardZoomLevel(SQLiteDatabase database, int lo
return (rowsUpdated == 1);
}

/**
* Updates the zoom width of a card.
* @param database database where the card is located
* @param loyaltyCardId id of the card
* @param zoomWidth new zoom width of the card
* @return whether exactly 1 row was updated
*/
public static boolean updateLoyaltyCardZoomWidth(SQLiteDatabase database, int loyaltyCardId, int zoomWidth) {
ContentValues contentValues = new ContentValues();
contentValues.put(LoyaltyCardDbIds.ZOOM_WIDTH, zoomWidth);
Log.d("updateLoyaltyCardZWidth", "Card Id = " + loyaltyCardId + " Zoom width= " + zoomWidth);
int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues,
whereAttrs(LoyaltyCardDbIds.ID),
withArgs(loyaltyCardId));
Log.d("updateLoyaltyCardZWidth", "Rows changed = " + rowsUpdated);
return (rowsUpdated == 1);
}

public static boolean updateLoyaltyCardBalance(SQLiteDatabase database, final int id, final BigDecimal newBalance) {
ContentValues contentValues = new ContentValues();
contentValues.put(LoyaltyCardDbIds.BALANCE, newBalance.toString());
Expand Down Expand Up @@ -899,4 +926,9 @@ private static String getDbDirection(LoyaltyCardOrder order, LoyaltyCardOrderDir

return direction == LoyaltyCardOrderDirection.Ascending ? "ASC" : "DESC";
}

public static int getColumnCount(SQLiteDatabase db) {
Cursor cursor = db.rawQuery("SELECT * FROM " + LoyaltyCardDbIds.TABLE + ";", null, null);
return cursor.getColumnCount();
}
Comment on lines +930 to +933
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems completely unused.

}
2 changes: 1 addition & 1 deletion app/src/main/java/protect/card_locker/ImportURIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public LoyaltyCard parse(Uri uri) throws InvalidObjectException {
headerColor = Integer.parseInt(unparsedHeaderColor);
}

return new LoyaltyCard(-1, store, note, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, 0, Utils.getUnixTime(), 100,0);
return new LoyaltyCard(-1, store, note, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, 0, Utils.getUnixTime(), 100, 100, 0);
} catch (NullPointerException | NumberFormatException | UnsupportedEncodingException ex) {
throw new InvalidObjectException("Not a valid import URI");
}
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/protect/card_locker/LoyaltyCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public class LoyaltyCard implements Parcelable {
public final int archiveStatus;
public final long lastUsed;
public int zoomLevel;
public int zoomWidth;

// Another constructor, with zoomWidth as an extra parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment makes no sense, this is the only constructor.

public LoyaltyCard(final int id, final String store, final String note, final Date expiry,
final BigDecimal balance, final Currency balanceType, final String cardId,
@Nullable final String barcodeId, @Nullable final CatimaBarcode barcodeType,
@Nullable final Integer headerColor, final int starStatus,
final long lastUsed, final int zoomLevel, final int archiveStatus) {
final long lastUsed, final int zoomLevel, final int zoomWidth, final int archiveStatus) {
this.id = id;
this.store = store;
this.note = note;
Expand All @@ -51,6 +53,7 @@ public LoyaltyCard(final int id, final String store, final String note, final Da
this.starStatus = starStatus;
this.lastUsed = lastUsed;
this.zoomLevel = zoomLevel;
this.zoomWidth = zoomWidth;
this.archiveStatus = archiveStatus;
}

Expand All @@ -71,6 +74,7 @@ protected LoyaltyCard(Parcel in) {
starStatus = in.readInt();
lastUsed = in.readLong();
zoomLevel = in.readInt();
zoomWidth = in.readInt();
archiveStatus = in.readInt();
}

Expand All @@ -89,6 +93,7 @@ public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(starStatus);
parcel.writeLong(lastUsed);
parcel.writeInt(zoomLevel);
parcel.writeInt(zoomWidth);
parcel.writeInt(archiveStatus);
}

Expand All @@ -103,6 +108,7 @@ public static LoyaltyCard toLoyaltyCard(Cursor cursor) {
int starred = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STAR_STATUS));
long lastUsed = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.LAST_USED));
int zoomLevel = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ZOOM_LEVEL));
int zoomWidth = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ZOOM_WIDTH));
int archived = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ARCHIVE_STATUS));

int barcodeTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_TYPE);
Expand Down Expand Up @@ -130,7 +136,7 @@ public static LoyaltyCard toLoyaltyCard(Cursor cursor) {
headerColor = cursor.getInt(headerColorColumn);
}

return new LoyaltyCard(id, store, note, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, starred, lastUsed, zoomLevel,archived);
return new LoyaltyCard(id, store, note, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, starred, lastUsed, zoomLevel, zoomWidth, archived);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ private static LoyaltyCard updateTempState(LoyaltyCard loyaltyCard, LoyaltyCardF
(int) (fieldName == LoyaltyCardField.starStatus ? value : loyaltyCard.starStatus),
0, // Unimportant, always set to null in doSave so the DB updates it to the current timestamp
100, // Unimportant, not updated in doSave, defaults to 100 for new cards
100,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have the same note as above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments were actually from someone else (and they don't seem to be commenting out code, but rather just a reminder of what those values are for?), but I can remove them if you think they no longer need to be there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line that lacks the comment needs a comment so it's clear what the last 100 value is for.

(int) (fieldName == LoyaltyCardField.archiveStatus ? value : loyaltyCard.archiveStatus)
);
}
Expand Down Expand Up @@ -779,7 +780,7 @@ public void onResume() {
}
} else {
// New card, use default values
tempLoyaltyCard = new LoyaltyCard(-1, "", "", null, new BigDecimal("0"), null, "", null, null, null, 0, Utils.getUnixTime(), 100,0);
tempLoyaltyCard = new LoyaltyCard(-1, "", "", null, new BigDecimal("0"), null, "", null, null, null, 0, Utils.getUnixTime(), 100, 100, 0);

}
}
Expand Down
126 changes: 87 additions & 39 deletions app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.ColorStateList;
Expand All @@ -15,7 +14,6 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
Expand Down Expand Up @@ -58,7 +56,6 @@
import androidx.core.graphics.BlendModeCompat;
import androidx.core.graphics.ColorUtils;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.core.widget.TextViewCompat;

Expand Down Expand Up @@ -123,7 +120,12 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
FloatingActionButton editButton;

Guideline centerGuideline;
SeekBar barcodeScaler;
SeekBar barcodeHeightScaler;
SeekBar barcodeWidthScaler;
TextView zoomHeightText;
TextView zoomWidthText;
LinearLayout widthScalerLayout;
LinearLayout heightScalerLayout;
Comment on lines +123 to +128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be consistent in order.

Now it is:
Height
Width
Height
Width
Width
Height


Bitmap frontImageBitmap;
Bitmap backImageBitmap;
Expand Down Expand Up @@ -369,6 +371,8 @@ protected void onCreate(Bundle savedInstanceState) {
iconImage = binding.iconImage;
portraitToolbar = binding.toolbar;
landscapeToolbar = binding.toolbarLandscape;
zoomHeightText = binding.zoomHeightText;
zoomWidthText = binding.zoomWidthText;

bottomAppBarInfoButton = binding.buttonShowInfo;
bottomAppBarPreviousButton = binding.buttonPrevious;
Expand All @@ -388,38 +392,19 @@ protected void onCreate(Bundle savedInstanceState) {
}
};

widthScalerLayout = binding.widthScalerLayout;
heightScalerLayout = binding.heightScalerLayout;
centerGuideline = binding.centerGuideline;
barcodeScaler = binding.barcodeScaler;
barcodeScaler.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser) {
Log.d(TAG, "non user triggered onProgressChanged, ignoring, progress is " + progress);
return;
}
Log.d(TAG, "Progress is " + progress);
Log.d(TAG, "Max is " + barcodeScaler.getMax());
float scale = (float) progress / (float) barcodeScaler.getMax();
Log.d(TAG, "Scaling to " + scale);
barcodeHeightScaler = binding.barcodeHeightScaler;

loyaltyCard.zoomLevel = progress;
DBHelper.updateLoyaltyCardZoomLevel(database, loyaltyCardId, loyaltyCard.zoomLevel);
SeekBarListener heightScalerListener = new SeekBarListener(barcodeHeightScaler);
barcodeHeightScaler.setOnSeekBarChangeListener(heightScalerListener);

setCenterGuideline(loyaltyCard.zoomLevel);

drawMainImage(mainImageIndex, true, isFullscreen);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
// set zoom width of barcode
barcodeWidthScaler = binding.barcodeWidthScaler;
zoomWidthText = binding.zoomWidthText;
SeekBarListener widthScalerListener = new SeekBarListener(barcodeWidthScaler);
Comment on lines +395 to +406
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again inconsistent ordering:

Width
Height
Height
Width

And only the width has a comment and the height doesn't

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the extra barcodeXScaler = binding.barcodeXScaler seems unnecessary and zoomWidthText seems completely unused.

barcodeWidthScaler.setOnSeekBarChangeListener(widthScalerListener);

rotationEnabled = true;

Expand Down Expand Up @@ -758,8 +743,10 @@ public void onResume() {

// Also apply colours to UI elements
int darkenedColor = ColorUtils.blendARGB(backgroundHeaderColor, Color.BLACK, 0.1f);
barcodeScaler.setProgressTintList(ColorStateList.valueOf(darkenedColor));
barcodeScaler.setThumbTintList(ColorStateList.valueOf(darkenedColor));
barcodeHeightScaler.setProgressTintList(ColorStateList.valueOf(darkenedColor));
barcodeHeightScaler.setThumbTintList(ColorStateList.valueOf(darkenedColor));
barcodeWidthScaler.setProgressTintList(ColorStateList.valueOf(darkenedColor));
barcodeWidthScaler.setThumbTintList(ColorStateList.valueOf(darkenedColor));
Comment on lines +746 to +749
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now it's suddenly

Height
Height
Width
Width

These inconsistencies make things really hard to maintain and prone to future bugs.

maximizeButton.setBackgroundColor(darkenedColor);
minimizeButton.setBackgroundColor(darkenedColor);
bottomAppBar.setBackgroundColor(darkenedColor);
Expand Down Expand Up @@ -1124,13 +1111,21 @@ private void setFullscreen(boolean enabled) {

drawMainImage(mainImageIndex, true, isFullscreen);

barcodeScaler.setProgress(loyaltyCard.zoomLevel);
barcodeHeightScaler.setProgress(loyaltyCard.zoomLevel);
barcodeWidthScaler.setProgress(loyaltyCard.zoomWidth);
setCenterGuideline(loyaltyCard.zoomLevel);

// Hide maximize and show minimize button and scaler
widthScalerLayout.setVisibility(View.VISIBLE);
heightScalerLayout.setVisibility(View.VISIBLE);
maximizeButton.setVisibility(View.GONE);
minimizeButton.setVisibility(View.VISIBLE);
barcodeScaler.setVisibility(View.VISIBLE);
barcodeHeightScaler.setVisibility(View.VISIBLE);
barcodeWidthScaler.setVisibility(View.VISIBLE);
zoomWidthText.setText("Width");
zoomHeightText.setText("Height");
zoomWidthText.setVisibility(View.VISIBLE);
zoomHeightText.setVisibility(View.VISIBLE);
Comment on lines +1114 to +1128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again inconsistent ordering:

Height
Width
Width
Height
Height
Width
Width
Height
Width
Height


// Hide actionbar
if (actionBar != null) {
Expand Down Expand Up @@ -1170,7 +1165,16 @@ private void setFullscreen(boolean enabled) {
maximizeButton.setVisibility(imageTypes.isEmpty() ? View.GONE : View.VISIBLE);

minimizeButton.setVisibility(View.GONE);
barcodeScaler.setVisibility(View.GONE);
widthScalerLayout.setVisibility(View.GONE);
heightScalerLayout.setVisibility(View.GONE);
barcodeHeightScaler.setVisibility(View.GONE);
barcodeWidthScaler.setVisibility(View.GONE);
zoomWidthText.setVisibility(View.GONE);
zoomHeightText.setVisibility(View.GONE);
Comment on lines +1168 to +1173
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again inconsistent ordering


// reset displaying width after exiting maximize mode
mainImage.getLayoutParams().width = mainLayout.getWidth();
mainImage.requestLayout();

// Show actionbar
if (actionBar != null) {
Expand Down Expand Up @@ -1198,7 +1202,8 @@ private void setFullscreen(boolean enabled) {
}
}

Log.d("setFullScreen", "Is full screen enabled? " + enabled + " Zoom Level = " + barcodeScaler.getProgress());
Log.d("setFullScreen", "Is full screen enabled? " + enabled + " Zoom Level = " + barcodeHeightScaler.getProgress() + ", Width level = " + barcodeWidthScaler.getProgress());

}

@SuppressWarnings("deprecation")
Expand All @@ -1218,4 +1223,47 @@ private void setFullscreenModeSdkLessThan30() {
| View.SYSTEM_UI_FLAG_FULLSCREEN
);
}

/**
* Helper class for the barcode zoom scalers.
*/
public class SeekBarListener implements SeekBar.OnSeekBarChangeListener {
private SeekBar seekBar;

public SeekBarListener(SeekBar sb) {
seekBar = sb;
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser) {
Log.d(TAG, "non user triggered onProgressChanged, ignoring, progress is " + progress);
return;
}
Log.d(TAG, "Progress is " + progress);
Log.d(TAG, "Max is " + seekBar.getMax());
float scale = (float) progress / (float) seekBar.getMax();
Log.d(TAG, "Scaling to " + scale);

if (seekBar == barcodeHeightScaler) {
loyaltyCard.zoomLevel = progress;
DBHelper.updateLoyaltyCardZoomLevel(database, loyaltyCardId, loyaltyCard.zoomLevel);
setCenterGuideline(loyaltyCard.zoomLevel);
} else if (seekBar == barcodeWidthScaler) {
loyaltyCard.zoomWidth = progress;
DBHelper.updateLoyaltyCardZoomWidth(database, loyaltyCardId, loyaltyCard.zoomWidth);
mainImage.getLayoutParams().width = mainLayout.getWidth() * loyaltyCard.zoomWidth / 100;
mainImage.requestLayout();
}
Comment on lines +1247 to +1256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems better to me if this class gets told with a separate parameter if it's working on height and width so it doesn't break in unexpected ways if and variable name changes and so it doesn't depend on variable names outside of itself.


drawMainImage(mainImageIndex, true, isFullscreen);
}

public void onStartTrackingTouch(SeekBar seekBar) {

}

public void onStopTrackingTouch(SeekBar seekBar) {

}
}
}
Loading