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
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package protect.card_locker;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.GrantPermissionRule;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.Rule;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.typeText;


import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.endsWith;

@RunWith(AndroidJUnit4.class)
public class LoyaltyCardEditActivityDefaultCurrencyTest {

@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.CAMERA);

@Test
public void defaultCurrencyPreselected() {
Context context = ApplicationProvider.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = context.getString(R.string.settings_key_default_currency);
prefs.edit().putString(key, "USD").commit();

try (ActivityScenario<LoyaltyCardEditActivity> scenario = ActivityScenario.launch(LoyaltyCardEditActivity.class)) {
onView(withText(R.string.options)).perform(click());
onView(withId(R.id.balanceCurrencyField))
.check(matches(withText("$")));
}
}

@Test
public void manualCurrencyPersistsAfterSave() {
Context context = ApplicationProvider.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = context.getString(R.string.settings_key_default_currency);
prefs.edit().putString(key, "USD").commit();

try (ActivityScenario<MainActivity> scenario = ActivityScenario.launch(MainActivity.class)) {

onView(withId(R.id.fabAdd)).perform(click());

// handle if camera option shows up
try {
onView(withText("Got It")).perform(click());
} catch (Exception e) { /* already clicked or never showed */ }

onView(withText("More options")).perform(click());
onView(withText("Add a card with no barcode")).perform(click());
onView(withClassName(endsWith("EditText"))).perform(typeText("123"), closeSoftKeyboard());
onView(withText("OK")).perform(click());
onView(withId(R.id.storeNameEdit)).perform(typeText("card name 123"), closeSoftKeyboard());
onView(withText(R.string.options)).perform(click());
onView(withId(R.id.balanceCurrencyField))
.check(matches(withText("$")));
onView(withId(R.id.balanceCurrencyField)).perform(replaceText("£"), closeSoftKeyboard());
onView(withId(R.id.fabSave)).perform(click());
onView(withText("mr no hands")).perform(click());
onView(withId(R.id.fabEdit)).perform(click());
onView(withText(R.string.options)).perform(click());
onView(withId(R.id.balanceCurrencyField))
.check(matches(withText("£")));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import protect.card_locker.databinding.LayoutChipChoiceBinding;
import protect.card_locker.databinding.LoyaltyCardEditActivityBinding;
import protect.card_locker.viewmodels.LoyaltyCardEditActivityViewModel;
import protect.card_locker.preferences.Settings;

public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements BarcodeImageWriterResultCallback, ColorPickerDialogListener {
private static final String TAG = "Catima";
Expand Down Expand Up @@ -144,6 +145,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
Toolbar toolbar;

SQLiteDatabase mDatabase;
Settings settings;

String tempStoredOldBarcodeValue = null;
boolean initDone = false;
Expand Down Expand Up @@ -320,8 +322,12 @@ protected void onCreate(Bundle savedInstanceState) {
enableToolbarBackButton();

mDatabase = new DBHelper(this).getWritableDatabase();

if (settings == null) { settings = new Settings(this); }
if (!viewModel.getInitialized()) {
var prefCurrency = settings.getPreferredCurrency();
if(prefCurrency != null) {
setLoyaltyCardBalanceType(prefCurrency);
}
if (!extractIntentFields(getIntent())) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/protect/card_locker/preferences/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import androidx.preference.PreferenceManager;

import java.util.Locale;
import java.util.Currency;

import protect.card_locker.R;
import protect.card_locker.Utils;
Expand Down Expand Up @@ -105,6 +106,23 @@ public int getPreferredColumnCount() {
}
}

@Nullable
public Currency getPreferredCurrency() {
String points = getResString(R.string.points);
String stored = getString(R.string.settings_key_default_currency, points);

if (points.equals(stored)) {
return null;
}

// Guard against invalid or legacy stored currencies
try {
return Currency.getInstance(stored);
} catch (IllegalArgumentException ignored) {
return null;
}
}

public boolean useVolumeKeysForNavigation() {
return getBoolean(R.string.settings_key_use_volume_keys_navigation, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import protect.card_locker.MainActivity
import protect.card_locker.R
import protect.card_locker.Utils
import protect.card_locker.databinding.SettingsActivityBinding
import java.util.Currency

class SettingsActivity : CatimaAppCompatActivity() {

Expand Down Expand Up @@ -160,6 +161,38 @@ class SettingsActivity : CatimaAppCompatActivity() {
// Hide crash reporter settings on builds it's not enabled on
val crashReporterPreference = findPreference<Preference>("acra.enable")
crashReporterPreference!!.isVisible = BuildConfig.useAcraCrashReporter

// Set entries for preferred currency
val currencyPreference = findPreference<ListPreference>(getString(R.string.settings_key_default_currency))!!
currencyPreference.let { pref ->
val currencies = Currency.getAvailableCurrencies()
.associateBy { it.symbol } // Deduplicates currencies by symbol to match behaviour in LoyaltyCardEditActivity
.values
.sortedWith { c1, c2 ->
val s1 = c1.symbol
val s2 = c2.symbol

val s1Ascii = s1.matches("^[^a-zA-Z]*$".toRegex())
val s2Ascii = s2.matches("^[^a-zA-Z]*$".toRegex())

when {
!s1Ascii && s2Ascii -> 1
s1Ascii && !s2Ascii -> -1
else -> s1.compareTo(s2)
}
}

val symbols = currencies.map { it.symbol }.toMutableList()
val codes = currencies.map { it.currencyCode }.toMutableList()

// Add points as an option
val points = getString(R.string.points)
symbols.add(0, points)
codes.add(0, points)

pref.entries = symbols.toTypedArray()
pref.entryValues = codes.toTypedArray()
}
}

private fun refreshActivity(reloadMain: Boolean) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@
<string name="sharedpreference_card_details_show_archived_cards" translatable="false">sharedpreference_card_details_show_archived_cards</string>
<string name="settings_category_title_cards">Card view</string>
<string name="settings_category_title_cards_overview">Cards overview</string>
<string name="settings_category_title_card_creation">Card creation</string>
<string name="settings_column_count_portrait">Columns in portrait mode</string>
<string name="settings_column_count_landscape">Columns in landscape mode</string>
<string name="settings_default_currency">Default currency type</string>
<string name="settings_key_default_currency">Preferred currency</string>
<string name="settings_automatic_column_count">Automatic</string>
<string name="settings_key_automatic_column_count" translatable="false">default</string>
<string name="settings_column_count_1">1</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@
app:singleLineTitle="false" />
</PreferenceCategory>

<PreferenceCategory
android:title="@string/settings_category_title_card_creation"
app:iconSpaceReserved="false">

<ListPreference
android:key="@string/settings_key_default_currency"
android:title="@string/settings_default_currency"
android:defaultValue="@string/points"
app:iconSpaceReserved="false"
app:singleLineTitle="false"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

<PreferenceCategory
android:title="@string/settings_category_title_privacy"
app:iconSpaceReserved="false">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package protect.card_locker;

import android.widget.TextView;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;

import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.RobolectricTestRunner;

import java.util.Currency;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import protect.card_locker.preferences.Settings;

@RunWith(RobolectricTestRunner.class)
public class LoyaltyCardEditActivityTest {

private ActivityController<LoyaltyCardEditActivity> controller;
private LoyaltyCardEditActivity activity;

@Before
public void setUp() {
controller = Robolectric.buildActivity(LoyaltyCardEditActivity.class);
activity = controller.get();
}

@After
public void tearDown() {
if (activity != null && activity.mDatabase != null && activity.mDatabase.isOpen()) {
activity.mDatabase.close();
}
}

@Test
public void onCreate_setsBalanceTypeFromSettings_SEK() {
Currency currency = Currency.getInstance("SEK");

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
prefs.edit()
.putString(
activity.getString(R.string.settings_key_default_currency),
currency.getSymbol()
)
.commit();

controller.create();

assertEquals(currency, activity.viewModel.getLoyaltyCard().balanceType);

controller.resume();

TextView field = activity.findViewById(R.id.balanceCurrencyField);
assertNotNull(field);
assertEquals(
currency.getSymbol(),
field.getText().toString()
);
}

@Test
public void onCreate_setsBalanceTypeWhenSettingsNull_points() {
controller.create();

assertEquals(
null,
activity.viewModel.getLoyaltyCard().balanceType
);

controller.resume();

TextView field = activity.findViewById(R.id.balanceCurrencyField);
assertNotNull(field);
assertEquals(
activity.getString(R.string.points),
field.getText().toString()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand All @@ -41,6 +42,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.preference.PreferenceManager;
import androidx.test.core.app.ApplicationProvider;

import com.google.android.material.bottomappbar.BottomAppBar;
Expand Down Expand Up @@ -1400,4 +1402,26 @@ public void importCardOldFormat() {
checkAllFields(activity, ViewMode.ADD_CARD, "Example Store", "", context.getString(R.string.anyDate), context.getString(R.string.never), "0", context.getString(R.string.points), "123456", context.getString(R.string.sameAsCardId), "Aztec", "ISO-8859-1", null, null);
assertEquals(-416706, ((ColorDrawable) activity.findViewById(R.id.thumbnail).getBackground()).getColor());
}


@Test
public void verifyDefaultCurrencyPreferenceInitiallySetToPoints(){
final Context context = ApplicationProvider.getApplicationContext();

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().clear().commit();

ActivityController activityController = Robolectric.buildActivity(LoyaltyCardEditActivity.class).create();

activityController.start();
activityController.visible();
activityController.resume();

shadowOf(getMainLooper()).idle();

Activity activity = (Activity) activityController.get();

checkAllFields(activity, ViewMode.ADD_CARD, "", "", context.getString(R.string.anyDate), context.getString(R.string.never), "0", context.getString(R.string.points), "", context.getString(R.string.sameAsCardId), context.getString(R.string.noBarcode), "ISO-8859-1", null, null);
}

}