mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-05-10 17:13:02 -04:00
feat: split complex loyaltyCardViewActivity (#3072)
This commit is contained in:
@@ -57,7 +57,7 @@ public class BarcodeImageWriterTask implements CompatCallable<Bitmap> {
|
||||
private final boolean showFallback;
|
||||
private final BarcodeImageWriterResultCallback callback;
|
||||
|
||||
BarcodeImageWriterTask(
|
||||
public BarcodeImageWriterTask(
|
||||
Context context, ImageView imageView, String cardIdString,
|
||||
CatimaBarcode barcodeFormat, @NonNull Charset barcodeEncoding, TextView textView,
|
||||
boolean showFallback, BarcodeImageWriterResultCallback callback, boolean roundCornerPadding, boolean isFullscreen
|
||||
|
||||
@@ -22,6 +22,8 @@ import java.util.List;
|
||||
import java.util.concurrent.Flow;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import protect.card_locker.cardview.LoyaltyCardViewActivity;
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.R)
|
||||
public class CardsOnPowerScreenService extends ControlsProviderService {
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.widget.RemoteViewsCompat
|
||||
import protect.card_locker.DBHelper.LoyaltyCardArchiveFilter
|
||||
import protect.card_locker.cardview.LoyaltyCardViewActivity
|
||||
|
||||
class ListWidget : AppWidgetProvider() {
|
||||
fun updateAll(context: Context) {
|
||||
@@ -129,4 +130,4 @@ class ListWidget : AppWidgetProvider() {
|
||||
|
||||
return remoteViews
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -719,6 +719,14 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (mDatabase != null && mDatabase.isOpen()) {
|
||||
mDatabase.close();
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private void selectTab(int index) {
|
||||
binding.tabs.selectTab(binding.tabs.getTabAt(index));
|
||||
viewModel.setTabIndex(index);
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
|
||||
import protect.card_locker.DBHelper.LoyaltyCardOrder
|
||||
import protect.card_locker.DBHelper.LoyaltyCardOrderDirection
|
||||
import protect.card_locker.LoyaltyCardCursorAdapter.CardAdapterListener
|
||||
import protect.card_locker.cardview.LoyaltyCardViewActivity
|
||||
import protect.card_locker.databinding.ContentMainBinding
|
||||
import protect.card_locker.databinding.MainActivityBinding
|
||||
import protect.card_locker.databinding.SortingOptionBinding
|
||||
|
||||
@@ -15,11 +15,11 @@ import androidx.core.content.pm.ShortcutManagerCompat;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
|
||||
class ShortcutHelper {
|
||||
import protect.card_locker.cardview.LoyaltyCardViewActivity;
|
||||
|
||||
public class ShortcutHelper {
|
||||
/**
|
||||
* This variable controls the maximum number of shortcuts available.
|
||||
* It is made public only to make testing easier and should not be
|
||||
@@ -43,33 +43,36 @@ class ShortcutHelper {
|
||||
* based on the lastUsed field. Archived cards are excluded from the shortcuts
|
||||
* list. The list keeps at most maxShortcuts number of elements.
|
||||
*/
|
||||
static void updateShortcuts(Context context) {
|
||||
public static void updateShortcuts(Context context) {
|
||||
if (maxShortcuts == -1) {
|
||||
maxShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context);
|
||||
}
|
||||
LinkedList<ShortcutInfoCompat> finalList = new LinkedList<>();
|
||||
SQLiteDatabase database = new DBHelper(context).getReadableDatabase();
|
||||
Cursor loyaltyCardCursor = DBHelper.getLoyaltyCardCursor(
|
||||
DBHelper dbHelper = new DBHelper(context);
|
||||
SQLiteDatabase database = dbHelper.getReadableDatabase();
|
||||
try (Cursor loyaltyCardCursor = DBHelper.getLoyaltyCardCursor(
|
||||
database,
|
||||
"",
|
||||
null,
|
||||
DBHelper.LoyaltyCardOrder.LastUsed,
|
||||
DBHelper.LoyaltyCardOrderDirection.Ascending,
|
||||
DBHelper.LoyaltyCardArchiveFilter.Unarchived
|
||||
);
|
||||
)) {
|
||||
int rank = 0;
|
||||
|
||||
int rank = 0;
|
||||
while (rank < maxShortcuts && loyaltyCardCursor.moveToNext()) {
|
||||
int id = loyaltyCardCursor.getInt(loyaltyCardCursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID));
|
||||
LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(context, database, id);
|
||||
|
||||
while (rank < maxShortcuts && loyaltyCardCursor.moveToNext()) {
|
||||
int id = loyaltyCardCursor.getInt(loyaltyCardCursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID));
|
||||
LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(context, database, id);
|
||||
ShortcutInfoCompat updatedShortcut = createShortcutBuilder(context, loyaltyCard)
|
||||
.setRank(rank)
|
||||
.build();
|
||||
|
||||
ShortcutInfoCompat updatedShortcut = createShortcutBuilder(context, loyaltyCard)
|
||||
.setRank(rank)
|
||||
.build();
|
||||
|
||||
finalList.addLast(updatedShortcut);
|
||||
rank++;
|
||||
finalList.addLast(updatedShortcut);
|
||||
rank++;
|
||||
}
|
||||
} finally {
|
||||
dbHelper.close();
|
||||
}
|
||||
|
||||
ShortcutManagerCompat.setDynamicShortcuts(context, finalList);
|
||||
@@ -110,4 +113,4 @@ class ShortcutHelper {
|
||||
.setIntent(intent)
|
||||
.setIcon(icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package protect.card_locker.cardview;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
final class LoyaltyCardImageNavigator {
|
||||
private final List<LoyaltyCardImageType> imageTypes;
|
||||
private int currentIndex;
|
||||
|
||||
LoyaltyCardImageNavigator(List<LoyaltyCardImageType> imageTypes, int currentIndex) {
|
||||
this.imageTypes = new ArrayList<>(imageTypes);
|
||||
this.currentIndex = clampIndex(currentIndex);
|
||||
}
|
||||
|
||||
LoyaltyCardImageType getCurrent() {
|
||||
if (isEmpty()) {
|
||||
return LoyaltyCardImageType.NONE;
|
||||
}
|
||||
return imageTypes.get(currentIndex);
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
return imageTypes.isEmpty();
|
||||
}
|
||||
|
||||
int size() {
|
||||
return imageTypes.size();
|
||||
}
|
||||
|
||||
boolean remove(LoyaltyCardImageType type) {
|
||||
int removedIndex = imageTypes.indexOf(type);
|
||||
if (removedIndex == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
imageTypes.remove(removedIndex);
|
||||
currentIndex = clampIndex(currentIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean canGoPrevious() {
|
||||
return currentIndex > 0;
|
||||
}
|
||||
|
||||
boolean canGoNext() {
|
||||
return currentIndex < imageTypes.size() - 1;
|
||||
}
|
||||
|
||||
boolean movePrevious() {
|
||||
if (!canGoPrevious()) {
|
||||
return false;
|
||||
}
|
||||
currentIndex--;
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean moveNext(boolean overflow) {
|
||||
if (isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (canGoNext()) {
|
||||
currentIndex++;
|
||||
return true;
|
||||
}
|
||||
if (overflow) {
|
||||
currentIndex = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int getCurrentIndex() {
|
||||
return currentIndex;
|
||||
}
|
||||
|
||||
LoyaltyCardImageType peekNext(boolean overflow) {
|
||||
if (isEmpty()) {
|
||||
return LoyaltyCardImageType.NONE;
|
||||
}
|
||||
|
||||
if (canGoNext()) {
|
||||
return imageTypes.get(currentIndex + 1);
|
||||
}
|
||||
|
||||
return overflow ? imageTypes.get(0) : getCurrent();
|
||||
}
|
||||
|
||||
private int clampIndex(int index) {
|
||||
if (imageTypes.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Math.max(0, Math.min(index, imageTypes.size() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
enum LoyaltyCardImageType {
|
||||
NONE,
|
||||
ICON,
|
||||
BARCODE,
|
||||
IMAGE_FRONT,
|
||||
IMAGE_BACK
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package protect.card_locker.cardview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.view.View;
|
||||
import android.view.ViewTreeObserver;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.android.material.card.MaterialCardView;
|
||||
import com.google.android.material.color.MaterialColors;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import protect.card_locker.*;
|
||||
import protect.card_locker.async.TaskHandler;
|
||||
|
||||
final class LoyaltyCardMainImageRenderer {
|
||||
private final Context context;
|
||||
private final TaskHandler tasks = new TaskHandler();
|
||||
private final BarcodeImageWriterResultCallback barcodeCallback;
|
||||
|
||||
LoyaltyCardMainImageRenderer(
|
||||
Context context,
|
||||
BarcodeImageWriterResultCallback barcodeCallback
|
||||
) {
|
||||
this.context = context;
|
||||
this.barcodeCallback = barcodeCallback;
|
||||
}
|
||||
|
||||
void renderCurrent(
|
||||
LoyaltyCardImageType imageType,
|
||||
Bitmap frontImageBitmap,
|
||||
Bitmap backImageBitmap,
|
||||
CatimaBarcode format,
|
||||
Charset barcodeEncoding,
|
||||
String cardIdString,
|
||||
String barcodeIdString,
|
||||
ImageView barcodeRenderTarget,
|
||||
TextView mainImageDescription,
|
||||
MaterialCardView mainCardView,
|
||||
boolean isFullscreen,
|
||||
boolean waitForResize
|
||||
) {
|
||||
if (imageType == LoyaltyCardImageType.NONE) {
|
||||
// With no renderable media left, show the raw card ID instead of an empty card area.
|
||||
barcodeRenderTarget.setVisibility(View.GONE);
|
||||
mainCardView.setCardBackgroundColor(Color.TRANSPARENT);
|
||||
mainImageDescription.setTextColor(
|
||||
MaterialColors.getColor(
|
||||
mainImageDescription,
|
||||
com.google.android.material.R.attr.colorOnSurfaceVariant
|
||||
)
|
||||
);
|
||||
mainImageDescription.setText(cardIdString);
|
||||
return;
|
||||
}
|
||||
|
||||
if (imageType == LoyaltyCardImageType.BARCODE) {
|
||||
barcodeRenderTarget.setBackgroundColor(Color.WHITE);
|
||||
mainCardView.setCardBackgroundColor(Color.WHITE);
|
||||
mainImageDescription.setTextColor(context.getResources().getColor(R.color.md_theme_light_onSurfaceVariant));
|
||||
|
||||
if (waitForResize) {
|
||||
redrawBarcodeAfterResize(
|
||||
barcodeRenderTarget,
|
||||
barcodeIdString,
|
||||
cardIdString,
|
||||
format,
|
||||
barcodeEncoding,
|
||||
!isFullscreen,
|
||||
isFullscreen
|
||||
);
|
||||
} else {
|
||||
drawBarcode(
|
||||
barcodeRenderTarget,
|
||||
barcodeIdString,
|
||||
cardIdString,
|
||||
format,
|
||||
barcodeEncoding,
|
||||
!isFullscreen,
|
||||
isFullscreen
|
||||
);
|
||||
}
|
||||
|
||||
mainImageDescription.setText(cardIdString);
|
||||
barcodeRenderTarget.setContentDescription(
|
||||
context.getString(R.string.barcodeImageDescriptionWithType, format.prettyName())
|
||||
);
|
||||
} else if (imageType == LoyaltyCardImageType.IMAGE_FRONT) {
|
||||
barcodeRenderTarget.setImageBitmap(frontImageBitmap);
|
||||
barcodeRenderTarget.setBackgroundColor(Color.TRANSPARENT);
|
||||
mainCardView.setCardBackgroundColor(Color.TRANSPARENT);
|
||||
mainImageDescription.setTextColor(
|
||||
MaterialColors.getColor(
|
||||
mainImageDescription,
|
||||
com.google.android.material.R.attr.colorOnSurfaceVariant
|
||||
)
|
||||
);
|
||||
mainImageDescription.setText(context.getString(R.string.frontImageDescription));
|
||||
barcodeRenderTarget.setContentDescription(context.getString(R.string.frontImageDescription));
|
||||
} else if (imageType == LoyaltyCardImageType.IMAGE_BACK) {
|
||||
barcodeRenderTarget.setImageBitmap(backImageBitmap);
|
||||
barcodeRenderTarget.setBackgroundColor(Color.TRANSPARENT);
|
||||
mainCardView.setCardBackgroundColor(Color.TRANSPARENT);
|
||||
mainImageDescription.setTextColor(
|
||||
MaterialColors.getColor(
|
||||
mainImageDescription,
|
||||
com.google.android.material.R.attr.colorOnSurfaceVariant
|
||||
)
|
||||
);
|
||||
mainImageDescription.setText(context.getString(R.string.backImageDescription));
|
||||
barcodeRenderTarget.setContentDescription(context.getString(R.string.backImageDescription));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown image type: " + imageType);
|
||||
}
|
||||
|
||||
barcodeRenderTarget.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private void redrawBarcodeAfterResize(
|
||||
ImageView barcodeRenderTarget,
|
||||
String barcodeIdString,
|
||||
String cardIdString,
|
||||
CatimaBarcode format,
|
||||
Charset barcodeEncoding,
|
||||
boolean addPadding,
|
||||
boolean isFullscreen
|
||||
) {
|
||||
if (format == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Barcode dimensions depend on the final ImageView size, so wait for layout before rendering.
|
||||
barcodeRenderTarget.getViewTreeObserver().addOnGlobalLayoutListener(
|
||||
new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
barcodeRenderTarget.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
||||
drawBarcode(
|
||||
barcodeRenderTarget,
|
||||
barcodeIdString,
|
||||
cardIdString,
|
||||
format,
|
||||
barcodeEncoding,
|
||||
addPadding,
|
||||
isFullscreen
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void drawBarcode(
|
||||
ImageView barcodeRenderTarget,
|
||||
String barcodeIdString,
|
||||
String cardIdString,
|
||||
CatimaBarcode format,
|
||||
Charset barcodeEncoding,
|
||||
boolean addPadding,
|
||||
boolean isFullscreen
|
||||
) {
|
||||
// Barcodes are regenerated eagerly because zoom/fullscreen changes affect the output bitmap.
|
||||
tasks.flushTaskList(TaskHandler.TYPE.BARCODE, true, false, false);
|
||||
if (format == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
BarcodeImageWriterTask barcodeWriter = new BarcodeImageWriterTask(
|
||||
context.getApplicationContext(),
|
||||
barcodeRenderTarget,
|
||||
barcodeIdString != null ? barcodeIdString : cardIdString,
|
||||
format,
|
||||
barcodeEncoding,
|
||||
null,
|
||||
false,
|
||||
barcodeCallback,
|
||||
addPadding,
|
||||
isFullscreen
|
||||
);
|
||||
tasks.executeTask(TaskHandler.TYPE.BARCODE, barcodeWriter);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,246 @@
|
||||
package protect.card_locker.cardview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.text.InputType;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.method.DigitsKeyListener;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
import android.text.util.Linkify;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import protect.card_locker.*;
|
||||
|
||||
final class LoyaltyCardViewDialogs {
|
||||
interface BalanceUpdateListener {
|
||||
void onBalanceUpdated(BigDecimal newBalance);
|
||||
}
|
||||
|
||||
void showInfoDialog(Context context, LoyaltyCard loyaltyCard, List<Group> loyaltyCardGroups) {
|
||||
AlertDialog.Builder infoDialog = new MaterialAlertDialogBuilder(context);
|
||||
|
||||
int dialogContentPadding = context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_content_padding);
|
||||
infoDialog.setTitle(loyaltyCard.store);
|
||||
|
||||
TextView infoTextview = new TextView(context);
|
||||
infoTextview.setPadding(
|
||||
dialogContentPadding,
|
||||
dialogContentPadding / 2,
|
||||
dialogContentPadding,
|
||||
0
|
||||
);
|
||||
infoTextview.setAutoLinkMask(Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);
|
||||
infoTextview.setTextIsSelectable(true);
|
||||
|
||||
SpannableStringBuilder infoText = new SpannableStringBuilder();
|
||||
if (!loyaltyCard.note.isEmpty()) {
|
||||
infoText.append(loyaltyCard.note);
|
||||
}
|
||||
|
||||
if (!loyaltyCardGroups.isEmpty()) {
|
||||
List<String> groupNames = new ArrayList<>();
|
||||
for (Group group : loyaltyCardGroups) {
|
||||
groupNames.add(group._id);
|
||||
}
|
||||
|
||||
padSpannableString(infoText);
|
||||
infoText.append(context.getString(R.string.groupsList, TextUtils.join(", ", groupNames)));
|
||||
}
|
||||
|
||||
if (hasBalance(loyaltyCard)) {
|
||||
padSpannableString(infoText);
|
||||
infoText.append(context.getString(
|
||||
R.string.balanceSentence,
|
||||
Utils.formatBalance(context, loyaltyCard.balance, loyaltyCard.balanceType)
|
||||
));
|
||||
}
|
||||
|
||||
appendDateInfo(
|
||||
context,
|
||||
infoText,
|
||||
loyaltyCard.validFrom,
|
||||
Utils::isNotYetValid,
|
||||
R.string.validFromSentence,
|
||||
R.string.validFromSentence
|
||||
);
|
||||
appendDateInfo(
|
||||
context,
|
||||
infoText,
|
||||
loyaltyCard.expiry,
|
||||
Utils::hasExpired,
|
||||
R.string.expiryStateSentenceExpired,
|
||||
R.string.expiryStateSentence
|
||||
);
|
||||
|
||||
infoTextview.setText(infoText);
|
||||
infoDialog.setView(infoTextview);
|
||||
infoDialog.setPositiveButton(R.string.ok, (dialogInterface, i) -> dialogInterface.dismiss());
|
||||
infoDialog.create().show();
|
||||
}
|
||||
|
||||
void showBalanceUpdateDialog(
|
||||
Context context,
|
||||
LoyaltyCard loyaltyCard,
|
||||
BalanceUpdateListener balanceUpdateListener
|
||||
) {
|
||||
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(context);
|
||||
builder.setTitle(R.string.updateBalanceTitle);
|
||||
|
||||
FrameLayout container = new FrameLayout(context);
|
||||
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
int contentPadding = context.getResources().getDimensionPixelSize(R.dimen.alert_dialog_content_padding);
|
||||
params.leftMargin = contentPadding;
|
||||
params.topMargin = contentPadding / 2;
|
||||
params.rightMargin = contentPadding;
|
||||
|
||||
LinearLayout layout = new LinearLayout(context);
|
||||
layout.setOrientation(LinearLayout.VERTICAL);
|
||||
|
||||
TextView currentTextview = new TextView(context);
|
||||
currentTextview.setText(context.getString(
|
||||
R.string.currentBalanceSentence,
|
||||
Utils.formatBalance(context, loyaltyCard.balance, loyaltyCard.balanceType)
|
||||
));
|
||||
layout.addView(currentTextview);
|
||||
|
||||
final TextInputEditText input = new TextInputEditText(context);
|
||||
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
|
||||
input.setKeyListener(DigitsKeyListener.getInstance("0123456789,."));
|
||||
input.setHint(R.string.updateBalanceHint);
|
||||
|
||||
layout.addView(input);
|
||||
layout.setLayoutParams(params);
|
||||
container.addView(layout);
|
||||
builder.setView(container);
|
||||
|
||||
builder.setPositiveButton(R.string.spend, (dialogInterface, i) -> {
|
||||
try {
|
||||
BigDecimal balanceChange = Utils.parseBalance(input.getText().toString(), loyaltyCard.balanceType);
|
||||
BigDecimal newBalance = loyaltyCard.balance.subtract(balanceChange).max(new BigDecimal(0));
|
||||
balanceUpdateListener.onBalanceUpdated(newBalance);
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.getString(
|
||||
R.string.newBalanceSentence,
|
||||
Utils.formatBalance(context, newBalance, loyaltyCard.balanceType)
|
||||
),
|
||||
Toast.LENGTH_LONG
|
||||
).show();
|
||||
} catch (ParseException e) {
|
||||
Toast.makeText(context, R.string.amountParsingFailed, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(R.string.receive, (dialogInterface, i) -> {
|
||||
try {
|
||||
BigDecimal balanceChange = Utils.parseBalance(input.getText().toString(), loyaltyCard.balanceType);
|
||||
BigDecimal newBalance = loyaltyCard.balance.add(balanceChange);
|
||||
balanceUpdateListener.onBalanceUpdated(newBalance);
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.getString(
|
||||
R.string.newBalanceSentence,
|
||||
Utils.formatBalance(context, newBalance, loyaltyCard.balanceType)
|
||||
),
|
||||
Toast.LENGTH_LONG
|
||||
).show();
|
||||
} catch (ParseException e) {
|
||||
Toast.makeText(context, R.string.amountParsingFailed, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
builder.setNeutralButton(context.getString(R.string.cancel), (dialog, which) -> dialog.cancel());
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
// Button state depends on the parsed input, so listeners must be bound after the dialog exists.
|
||||
input.addTextChangedListener(new SimpleTextWatcher() {
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
BigDecimal balanceChange;
|
||||
|
||||
try {
|
||||
balanceChange = Utils.parseBalance(s.toString(), loyaltyCard.balanceType);
|
||||
} catch (ParseException e) {
|
||||
input.setError(context.getString(R.string.amountParsingFailed));
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
|
||||
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
|
||||
return;
|
||||
}
|
||||
|
||||
input.setError(null);
|
||||
boolean hasNonZeroValue = !balanceChange.equals(new BigDecimal(0));
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(hasNonZeroValue);
|
||||
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(hasNonZeroValue);
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
// Touching dialog buttons before show() can crash because they are not created yet.
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
|
||||
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
|
||||
|
||||
if (dialog.getWindow() != null) {
|
||||
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
||||
}
|
||||
// Focus immediately so the keyboard opens on the amount field.
|
||||
input.requestFocus();
|
||||
}
|
||||
|
||||
private boolean hasBalance(LoyaltyCard loyaltyCard) {
|
||||
return !loyaltyCard.balance.equals(new BigDecimal(0));
|
||||
}
|
||||
|
||||
private SpannableStringBuilder padSpannableString(SpannableStringBuilder spannableStringBuilder) {
|
||||
if (spannableStringBuilder.length() > 0) {
|
||||
spannableStringBuilder.append("\n\n");
|
||||
}
|
||||
|
||||
return spannableStringBuilder;
|
||||
}
|
||||
|
||||
private void appendDateInfo(
|
||||
Context context,
|
||||
SpannableStringBuilder infoText,
|
||||
Date date,
|
||||
Predicate<Date> dateCheck,
|
||||
@StringRes int dateCheckTrueString,
|
||||
@StringRes int dateCheckFalseString
|
||||
) {
|
||||
if (date == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String formattedDate = DateFormat.getDateInstance(DateFormat.LONG).format(date);
|
||||
|
||||
padSpannableString(infoText);
|
||||
if (dateCheck.test(date)) {
|
||||
int start = infoText.length();
|
||||
infoText.append(context.getString(dateCheckTrueString, formattedDate));
|
||||
infoText.setSpan(new ForegroundColorSpan(Color.RED), start, infoText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||
} else {
|
||||
infoText.append(context.getString(dateCheckFalseString, formattedDate));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user