mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-02-02 19:22:26 -05:00
Merge branch 'master' of github.com:TheLastProject/loyalty-card-locker into feature/import-local-image
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package protect.card_locker;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.DatePickerDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
@@ -8,6 +9,7 @@ import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.google.android.material.chip.Chip;
|
||||
@@ -20,6 +22,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
import android.os.LocaleList;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
@@ -31,26 +34,29 @@ import android.view.ViewTreeObserver;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.AutoCompleteTextView;
|
||||
import android.widget.Button;
|
||||
import android.widget.CalendarView;
|
||||
import android.widget.DatePicker;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.jaredrummler.android.colorpicker.ColorPickerDialog;
|
||||
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener;
|
||||
|
||||
import java.io.InvalidObjectException;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Currency;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
{
|
||||
@@ -63,7 +69,8 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
EditText noteFieldEdit;
|
||||
ChipGroup groupsChips;
|
||||
AutoCompleteTextView expiryField;
|
||||
View cardAndBarcodeLayout;
|
||||
EditText balanceField;
|
||||
AutoCompleteTextView balanceCurrencyField;
|
||||
TextView cardIdFieldView;
|
||||
AutoCompleteTextView barcodeTypeField;
|
||||
ImageView barcodeImage;
|
||||
@@ -89,6 +96,10 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
boolean initDone = false;
|
||||
AlertDialog confirmExitDialog = null;
|
||||
|
||||
boolean validBalance = true;
|
||||
|
||||
HashMap<String, Currency> currencies = new HashMap<>();
|
||||
|
||||
private void extractIntentFields(Intent intent)
|
||||
{
|
||||
final Bundle b = intent.getExtras();
|
||||
@@ -123,13 +134,18 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
db = new DBHelper(this);
|
||||
importUriHelper = new ImportURIHelper(this);
|
||||
|
||||
for (Currency currency : Currency.getAvailableCurrencies()) {
|
||||
currencies.put(currency.getSymbol(), currency);
|
||||
}
|
||||
|
||||
tabs = findViewById(R.id.tabs);
|
||||
thumbnail = findViewById(R.id.thumbnail);
|
||||
storeFieldEdit = findViewById(R.id.storeNameEdit);
|
||||
noteFieldEdit = findViewById(R.id.noteEdit);
|
||||
groupsChips = findViewById(R.id.groupChips);
|
||||
expiryField = findViewById(R.id.expiryField);
|
||||
cardAndBarcodeLayout = findViewById(R.id.cardAndBarcodeLayout);
|
||||
balanceField = findViewById(R.id.balanceField);
|
||||
balanceCurrencyField = findViewById(R.id.balanceCurrencyField);
|
||||
cardIdFieldView = findViewById(R.id.cardIdView);
|
||||
barcodeTypeField = findViewById(R.id.barcodeTypeField);
|
||||
barcodeImage = findViewById(R.id.barcode);
|
||||
@@ -186,6 +202,98 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
}
|
||||
});
|
||||
|
||||
balanceField.setOnFocusChangeListener((v, hasFocus) -> {
|
||||
if (!hasFocus) {
|
||||
balanceField.setText(Utils.formatBalanceWithoutCurrencySymbol((BigDecimal) balanceField.getTag(), (Currency) balanceCurrencyField.getTag()));
|
||||
}
|
||||
});
|
||||
|
||||
balanceField.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
hasChanged = true;
|
||||
|
||||
try {
|
||||
BigDecimal balance = Utils.parseCurrency(s.toString(), Utils.currencyHasDecimals((Currency) balanceCurrencyField.getTag()));
|
||||
validBalance = true;
|
||||
|
||||
balanceField.setTag(balance);
|
||||
} catch (NumberFormatException e) {
|
||||
validBalance = false;
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) { }
|
||||
});
|
||||
|
||||
balanceCurrencyField.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
hasChanged = true;
|
||||
|
||||
Currency currency;
|
||||
|
||||
if (s.toString().equals(getString(R.string.points))) {
|
||||
currency = null;
|
||||
} else {
|
||||
currency = currencies.get(s.toString());
|
||||
}
|
||||
|
||||
balanceCurrencyField.setTag(currency);
|
||||
|
||||
BigDecimal balance = (BigDecimal) balanceField.getTag();
|
||||
|
||||
if (balance != null) {
|
||||
balanceField.setText(Utils.formatBalanceWithoutCurrencySymbol(balance, currency));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
ArrayList<String> currencyList = new ArrayList<>(currencies.keySet());
|
||||
Collections.sort(currencyList, (o1, o2) -> {
|
||||
boolean o1ascii = o1.matches("^[^a-zA-Z]*$");
|
||||
boolean o2ascii = o2.matches("^[^a-zA-Z]*$");
|
||||
|
||||
if (!o1ascii && o2ascii) {
|
||||
return 1;
|
||||
} else if (o1ascii && !o2ascii) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return o1.compareTo(o2);
|
||||
});
|
||||
|
||||
// Sort locale currencies on top
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
LocaleList locales = getApplicationContext().getResources().getConfiguration().getLocales();
|
||||
|
||||
for (int i = locales.size() - 1; i > 0; i--) {
|
||||
Locale locale = locales.get(i);
|
||||
String currencySymbol = Currency.getInstance(locale).getSymbol();
|
||||
currencyList.remove(currencySymbol);
|
||||
currencyList.add(0, currencySymbol);
|
||||
}
|
||||
} else {
|
||||
String currencySymbol = Currency.getInstance(getApplicationContext().getResources().getConfiguration().locale).getSymbol();
|
||||
currencyList.remove(currencySymbol);
|
||||
currencyList.add(0, currencySymbol);
|
||||
}
|
||||
|
||||
currencyList.add(0, getString(R.string.points));
|
||||
ArrayAdapter<String> currencyAdapter = new ArrayAdapter<>(LoyaltyCardEditActivity.this, android.R.layout.select_dialog_item, currencyList);
|
||||
balanceCurrencyField.setAdapter(currencyAdapter);
|
||||
}
|
||||
});
|
||||
|
||||
cardIdFieldView.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
||||
@@ -265,10 +373,15 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
noteFieldEdit.setText("");
|
||||
expiryField.setTag(null);
|
||||
expiryField.setText("");
|
||||
balanceCurrencyField.setTag(null);
|
||||
balanceCurrencyField.setText("");
|
||||
balanceField.setTag(null);
|
||||
balanceField.setText("");
|
||||
cardIdFieldView.setText("");
|
||||
barcodeTypeField.setText("");
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
@@ -300,11 +413,19 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
if(expiryField.getText().length() == 0)
|
||||
{
|
||||
expiryField.setTag(loyaltyCard.expiry);
|
||||
if (loyaltyCard.expiry == null) {
|
||||
expiryField.setText(getString(R.string.never));
|
||||
} else {
|
||||
expiryField.setText(DateFormat.getDateInstance(DateFormat.LONG).format(loyaltyCard.expiry));
|
||||
}
|
||||
formatExpiryField(loyaltyCard.expiry);
|
||||
}
|
||||
|
||||
if(balanceCurrencyField.getText().length() == 0)
|
||||
{
|
||||
balanceCurrencyField.setTag(loyaltyCard.balanceType);
|
||||
formatBalanceCurrencyField(loyaltyCard.balanceType);
|
||||
}
|
||||
|
||||
if(balanceField.getText().length() == 0)
|
||||
{
|
||||
balanceField.setTag(loyaltyCard.balance);
|
||||
balanceField.setText(Utils.formatBalanceWithoutCurrencySymbol(loyaltyCard.balance, loyaltyCard.balanceType));
|
||||
}
|
||||
|
||||
if(cardIdFieldView.getText().length() == 0)
|
||||
@@ -343,11 +464,10 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
storeFieldEdit.setText(importCard.store);
|
||||
noteFieldEdit.setText(importCard.note);
|
||||
expiryField.setTag(importCard.expiry);
|
||||
if (importCard.expiry != null) {
|
||||
expiryField.setText(DateFormat.getDateInstance(DateFormat.LONG).format(importCard.expiry));
|
||||
} else {
|
||||
expiryField.setText(R.string.never);
|
||||
}
|
||||
formatExpiryField(importCard.expiry);
|
||||
balanceField.setTag(importCard.balance);
|
||||
balanceCurrencyField.setTag(importCard.balanceType);
|
||||
formatBalanceCurrencyField(importCard.balanceType);
|
||||
cardIdFieldView.setText(importCard.cardId);
|
||||
barcodeTypeField.setText(importCard.barcodeType);
|
||||
headingColorValue = importCard.headerColor;
|
||||
@@ -357,6 +477,9 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
setTitle(R.string.addCardTitle);
|
||||
expiryField.setTag(null);
|
||||
expiryField.setText(getString(R.string.never));
|
||||
balanceField.setTag(new BigDecimal("0"));
|
||||
balanceCurrencyField.setTag(null);
|
||||
formatBalanceCurrencyField(null);
|
||||
hideBarcode();
|
||||
}
|
||||
|
||||
@@ -384,13 +507,10 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
break;
|
||||
}
|
||||
}
|
||||
chip.setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
hasChanged = true;
|
||||
chip.setOnTouchListener((v, event) -> {
|
||||
hasChanged = true;
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
groupsChips.addView(chip);
|
||||
@@ -449,16 +569,27 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
barcodeTypeField.setAdapter(barcodeAdapter);
|
||||
|
||||
FloatingActionButton saveButton = findViewById(R.id.fabSave);
|
||||
saveButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doSave();
|
||||
}
|
||||
});
|
||||
saveButton.setOnClickListener(v -> doSave());
|
||||
|
||||
generateIcon(storeFieldEdit.getText().toString());
|
||||
}
|
||||
|
||||
private void formatExpiryField(Date expiry) {
|
||||
if (expiry == null) {
|
||||
expiryField.setText(getString(R.string.never));
|
||||
} else {
|
||||
expiryField.setText(DateFormat.getDateInstance(DateFormat.LONG).format(expiry));
|
||||
}
|
||||
}
|
||||
|
||||
private void formatBalanceCurrencyField(Currency balanceType) {
|
||||
if (balanceType == null) {
|
||||
balanceCurrencyField.setText(getString(R.string.points));
|
||||
} else {
|
||||
balanceCurrencyField.setText(balanceType.getSymbol());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
askBeforeQuitIfChanged();
|
||||
@@ -588,12 +719,13 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void doSave()
|
||||
{
|
||||
String store = storeFieldEdit.getText().toString();
|
||||
String note = noteFieldEdit.getText().toString();
|
||||
Date expiry = (Date) expiryField.getTag();
|
||||
BigDecimal balance = (BigDecimal) balanceField.getTag();
|
||||
Currency balanceType = balanceCurrencyField.getTag() != null ? ((Currency) balanceCurrencyField.getTag()) : null;
|
||||
String cardId = cardIdFieldView.getText().toString();
|
||||
String barcodeType = barcodeTypeField.getText().toString();
|
||||
|
||||
@@ -616,6 +748,12 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
return;
|
||||
}
|
||||
|
||||
if(!validBalance)
|
||||
{
|
||||
Snackbar.make(balanceField, getString(R.string.parsingBalanceFailed, balanceField.getText().toString()), Snackbar.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
List<Group> selectedGroups = new ArrayList<>();
|
||||
|
||||
for (Integer chipId : groupsChips.getCheckedChipIds()) {
|
||||
@@ -625,12 +763,12 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
|
||||
if(updateLoyaltyCard)
|
||||
{ //update of "starStatus" not necessary, since it cannot be changed in this activity (only in ViewActivity)
|
||||
db.updateLoyaltyCard(loyaltyCardId, store, note, expiry, cardId, barcodeType, headingColorValue);
|
||||
db.updateLoyaltyCard(loyaltyCardId, store, note, expiry, balance, balanceType, cardId, barcodeType, headingColorValue);
|
||||
Log.i(TAG, "Updated " + loyaltyCardId + " to " + cardId);
|
||||
}
|
||||
else
|
||||
{
|
||||
loyaltyCardId = (int)db.insertLoyaltyCard(store, note, expiry, cardId, barcodeType, headingColorValue, 0);
|
||||
loyaltyCardId = (int)db.insertLoyaltyCard(store, note, expiry, balance, balanceType, cardId, barcodeType, headingColorValue, 0);
|
||||
}
|
||||
|
||||
db.setLoyaltyCardGroups(loyaltyCardId, selectedGroups);
|
||||
|
||||
Reference in New Issue
Block a user