diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java index 14d8d7e85b..38e0d71d5d 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java @@ -12,15 +12,15 @@ import androidx.core.content.pm.ShortcutInfoCompat; import androidx.core.content.pm.ShortcutManagerCompat; import androidx.recyclerview.widget.GridLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import protect.card_locker.databinding.SimpleToolbarListActivityBinding; +import protect.card_locker.databinding.CardShortcutConfigureActivityBinding; +import protect.card_locker.preferences.Settings; /** * The configuration screen for creating a shortcut. */ public class CardShortcutConfigure extends CatimaAppCompatActivity implements LoyaltyCardCursorAdapter.CardAdapterListener { - private SimpleToolbarListActivityBinding binding; + private CardShortcutConfigureActivityBinding binding; static final String TAG = "Catima"; private SQLiteDatabase mDatabase; private LoyaltyCardCursorAdapter mAdapter; @@ -28,7 +28,7 @@ public class CardShortcutConfigure extends CatimaAppCompatActivity implements Lo @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); - binding = SimpleToolbarListActivityBinding.inflate(getLayoutInflater()); + binding = CardShortcutConfigureActivityBinding.inflate(getLayoutInflater()); mDatabase = new DBHelper(this).getReadableDatabase(); // Set the result to CANCELED. This will cause nothing to happen if the @@ -47,15 +47,20 @@ public void onCreate(Bundle bundle) { finish(); } - final RecyclerView cardList = binding.list; - GridLayoutManager layoutManager = (GridLayoutManager) cardList.getLayoutManager(); - if (layoutManager != null) { - layoutManager.setSpanCount(getResources().getInteger(R.integer.main_view_card_columns)); - } - Cursor cardCursor = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All); mAdapter = new LoyaltyCardCursorAdapter(this, cardCursor, this, null); - cardList.setAdapter(mAdapter); + binding.list.setAdapter(mAdapter); + } + + @Override + protected void onResume() { + super.onResume(); + + var layoutManager = (GridLayoutManager) binding.list.getLayoutManager(); + if (layoutManager != null) { + var settings = new Settings(this); + layoutManager.setSpanCount(settings.getPreferredColumnCount()); + } } private void onClickAction(int position) { diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java index 5ecb89d012..9bd1c8ff18 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import protect.card_locker.databinding.LoyaltyCardLayoutBinding; +import protect.card_locker.preferences.Settings; public class LoyaltyCardCursorAdapter extends BaseCursorAdapter { private int mCurrentSelectedIndex = -1; @@ -123,7 +124,7 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i } inputHolder.mCardIcon.setContentDescription(loyaltyCard.store); - inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText); + inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText, new Settings(mContext).getPreferredColumnCount()); inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition())); @@ -339,10 +340,4 @@ public void toggleCardStateIcon(boolean enableStar, boolean enableArchive, boole } } - - public int dpToPx(int dp, Context mContext) { - Resources r = mContext.getResources(); - int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); - return px; - } } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index ef64ab94c0..71d898d2ec 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -720,7 +720,7 @@ protected void onResume() { binding.fabEdit.setImageDrawable(editButtonIcon); Bitmap icon = Utils.retrieveCardImage(this, loyaltyCard.id, ImageLocationType.icon); - Utils.setIconOrTextWithBackground(this, loyaltyCard, icon, binding.iconImage, binding.iconText); + Utils.setIconOrTextWithBackground(this, loyaltyCard, icon, binding.iconImage, binding.iconText, 1); // If the background is very bright, we should use dark icons backgroundNeedsDarkIcons = Utils.needsDarkForeground(backgroundHeaderColor); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 6b8ff2c795..4a21297a6e 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -27,6 +27,7 @@ import androidx.appcompat.view.ActionMode; import androidx.appcompat.widget.SearchView; import androidx.core.splashscreen.SplashScreen; +import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -43,6 +44,7 @@ import protect.card_locker.databinding.ContentMainBinding; import protect.card_locker.databinding.MainActivityBinding; import protect.card_locker.databinding.SortingOptionBinding; +import protect.card_locker.preferences.Settings; import protect.card_locker.preferences.SettingsActivity; public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCardCursorAdapter.CardAdapterListener { @@ -249,9 +251,6 @@ public void onTabReselected(TabLayout.Tab tab) { mCardList.setAdapter(mAdapter); registerForContextMenu(mCardList); - mGroup = null; - updateLoyaltyCardList(true); - mBarcodeScannerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { // Exit early if the user cancelled the scan (pressed back/home) if (result.getResultCode() != RESULT_OK) { @@ -357,6 +356,12 @@ protected void onResume() { mBarcodeScannerLauncher.launch(intent); }); addButton.bringToFront(); + + var layoutManager = (GridLayoutManager) mCardList.getLayoutManager(); + if (layoutManager != null) { + var settings = new Settings(this); + layoutManager.setSpanCount(settings.getPreferredColumnCount()); + } } private void displayCardSetupOptions(Menu menu, boolean shouldShow) { diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 116f60add3..d44fddb71a 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -934,7 +934,7 @@ public static String linkify(final String input) { * @param textWhenNoImage TextView to write the loyalty card name into if icon is null * @return background colour */ - public static int setIconOrTextWithBackground(Context context, LoyaltyCard loyaltyCard, Bitmap icon, ImageView backgroundOrIcon, TextView textWhenNoImage) { + public static int setIconOrTextWithBackground(Context context, LoyaltyCard loyaltyCard, Bitmap icon, ImageView backgroundOrIcon, TextView textWhenNoImage, int columnCount) { int headerColor = getHeaderColor(context, loyaltyCard); backgroundOrIcon.setImageBitmap(icon); backgroundOrIcon.setBackgroundColor(headerColor); @@ -947,14 +947,16 @@ public static int setIconOrTextWithBackground(Context context, LoyaltyCard loyal // Because we have to write the text before we can actually know the exact laid out size (trying to delay this causes bugs where the autosize fails) we have to take some... weird shortcuts // At this point textWhenNoImage.getWidth() still returns 0, so we cheat by calculating the whole width of the screen and then dividing it by the amount of columns - int textviewWidth = Resources.getSystem().getDisplayMetrics().widthPixels / context.getResources().getInteger(R.integer.main_view_card_columns); + int columnWidth = Resources.getSystem().getDisplayMetrics().widthPixels / columnCount; // Calculate how wide a character is and calculate how many characters fit in a line + // text size is generally based on height, so setting 1:1 as width may be fishy int characterWidth = TextViewCompat.getAutoSizeMinTextSize(textWhenNoImage); - int maxWidthPerLine = textviewWidth - textWhenNoImage.getPaddingStart() - textWhenNoImage.getPaddingEnd(); + int maxWidthPerLine = columnWidth - textWhenNoImage.getPaddingStart() - textWhenNoImage.getPaddingEnd(); - // Set amount of lines based on what could fit at most - int maxLines = ((loyaltyCard.store.length() * characterWidth) / maxWidthPerLine) + 1; + // Set number of lines based on what could fit at most + int fullTextWidth = loyaltyCard.store.length() * characterWidth; + int maxLines = (fullTextWidth / maxWidthPerLine) + 1; textWhenNoImage.setMaxLines(maxLines); // Actually set the text and colour diff --git a/app/src/main/java/protect/card_locker/preferences/Settings.java b/app/src/main/java/protect/card_locker/preferences/Settings.java index 52309886e3..3a00307d90 100644 --- a/app/src/main/java/protect/card_locker/preferences/Settings.java +++ b/app/src/main/java/protect/card_locker/preferences/Settings.java @@ -1,9 +1,13 @@ package protect.card_locker.preferences; +import static android.content.res.Configuration.ORIENTATION_PORTRAIT; + import android.content.Context; import android.content.SharedPreferences; +import android.util.Log; import androidx.annotation.IntegerRes; +import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.PreferenceManager; @@ -14,8 +18,9 @@ import protect.card_locker.Utils; public class Settings { + private static final String TAG = "Catima"; private final Context mContext; - private SharedPreferences mSettings; + private final SharedPreferences mSettings; public Settings(Context context) { mContext = context.getApplicationContext(); @@ -42,10 +47,11 @@ private boolean getBoolean(@StringRes int keyId, boolean defaultValue) { return mSettings.getBoolean(getResString(keyId), defaultValue); } + @Nullable public Locale getLocale() { String value = getString(R.string.settings_key_locale, ""); - if (value.length() == 0) { + if (value.isEmpty()) { return null; } @@ -92,6 +98,21 @@ public String getColor() { return getString(R.string.setting_key_theme_color, mContext.getResources().getString(R.string.settings_key_system_theme)); } + public int getPreferredColumnCount() { + var defaultSymbol = mContext.getResources().getString(R.string.settings_key_automatic_column_count); + var defaultColumnCount = mContext.getResources().getInteger(R.integer.main_view_card_columns); + var orientation = mContext.getResources().getConfiguration().orientation; + var columnCountPrefKey = orientation == ORIENTATION_PORTRAIT ? R.string.setting_key_column_count_portrait : R.string.setting_key_column_count_landscape; + var columnCountSetting = getString(columnCountPrefKey, defaultSymbol); + try { + // the pref may be unset or explicitly set to default + return columnCountSetting.equals(defaultSymbol) ? defaultColumnCount : Integer.parseInt(columnCountSetting); + } catch (NumberFormatException nfe) { + Log.e(TAG, "Failed to parseInt the column count pref", nfe); + return defaultColumnCount; + } + } + public boolean useVolumeKeysForNavigation() { return getBoolean(R.string.settings_key_use_volume_keys_navigation, false); } diff --git a/app/src/main/res/layout/simple_toolbar_list_activity.xml b/app/src/main/res/layout/card_shortcut_configure_activity.xml similarity index 95% rename from app/src/main/res/layout/simple_toolbar_list_activity.xml rename to app/src/main/res/layout/card_shortcut_configure_activity.xml index d8d8eb5413..07f2559b2d 100644 --- a/app/src/main/res/layout/simple_toolbar_list_activity.xml +++ b/app/src/main/res/layout/card_shortcut_configure_activity.xml @@ -27,7 +27,7 @@ diff --git a/app/src/main/res/layout/loyalty_card_layout.xml b/app/src/main/res/layout/loyalty_card_layout.xml index 5daf46c659..d9ad60140a 100644 --- a/app/src/main/res/layout/loyalty_card_layout.xml +++ b/app/src/main/res/layout/loyalty_card_layout.xml @@ -41,9 +41,10 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:textStyle="bold" + app:autoSizeMinTextSize="6sp" app:autoSizeTextType="uniform" android:gravity="center" - android:padding="20dp" /> + android:padding="10dp" /> Gebruik achterzijde van kaart Verwissel kaart met de volume knoppen Gebruik de volume knoppen om te wisselen van getoonde kaart - \ No newline at end of file + diff --git a/app/src/main/res/values-w600dp/integers.xml b/app/src/main/res/values-w600dp/integers.xml index 1993bae6ff..536133da95 100644 --- a/app/src/main/res/values-w600dp/integers.xml +++ b/app/src/main/res/values-w600dp/integers.xml @@ -1,4 +1,4 @@ - 3 + 4 \ No newline at end of file diff --git a/app/src/main/res/values/settings.xml b/app/src/main/res/values/settings.xml index c88566e058..ada5422e6f 100644 --- a/app/src/main/res/values/settings.xml +++ b/app/src/main/res/values/settings.xml @@ -36,6 +36,40 @@ @string/settings_brown_theme + + @string/settings_key_automatic_column_count + 1 + 2 + 3 + 4 + + + + @string/settings_automatic_column_count + @string/settings_column_count_1 + @string/settings_column_count_2 + @string/settings_column_count_3 + @string/settings_column_count_4 + + + + @string/settings_key_automatic_column_count + 3 + 4 + 5 + 6 + 7 + + + + @string/settings_automatic_column_count + @string/settings_column_count_3 + @string/settings_column_count_4 + @string/settings_column_count_5 + @string/settings_column_count_6 + @string/settings_column_count_7 + + @string/settings_key_system_theme @string/settings_key_pink_theme diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 783c027701..0b7b08e16f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -318,6 +318,20 @@ sharedpreference_card_details_show_validity sharedpreference_card_details_show_archived_cards Card view + Cards overview + Columns in portrait mode + Columns in landscape mode + Automatic + default + 1 + 2 + 3 + 4 + 5 + 6 + 7 + pref_column_count_portrait + pref_column_count_landscape General Privacy Display options diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 554592d55b..37035535de 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -46,6 +46,31 @@ app:useSimpleSummaryProvider="true" /> + + + + + + + @@ -112,4 +137,4 @@ - \ No newline at end of file +