mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-03-31 05:41:51 -04:00
Merge pull request #2147 from leaumar/feat/1952-columns
resolve #1952 custom column count
This commit is contained in:
@@ -12,15 +12,15 @@ import androidx.appcompat.widget.Toolbar;
|
||||
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 class CardShortcutConfigure extends CatimaAppCompatActivity implements Lo
|
||||
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) {
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.text.DateFormat;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import protect.card_locker.databinding.LoyaltyCardLayoutBinding;
|
||||
import protect.card_locker.preferences.Settings;
|
||||
|
||||
public class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCursorAdapter.LoyaltyCardListItemViewHolder> {
|
||||
private int mCurrentSelectedIndex = -1;
|
||||
@@ -123,7 +124,7 @@ public class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCurso
|
||||
}
|
||||
|
||||
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 class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCurso
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -720,7 +720,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
|
||||
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);
|
||||
|
||||
@@ -27,6 +27,7 @@ import androidx.appcompat.app.AlertDialog;
|
||||
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 java.util.concurrent.atomic.AtomicInteger;
|
||||
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 class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
|
||||
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 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
|
||||
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) {
|
||||
|
||||
@@ -934,7 +934,7 @@ public class Utils {
|
||||
* @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 class Utils {
|
||||
// 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
|
||||
|
||||
@@ -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.R;
|
||||
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 @@ public class Settings {
|
||||
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 class Settings {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/list"
|
||||
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
||||
app:spanCount="1"
|
||||
app:spanCount="@integer/main_view_card_columns"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="match_parent" />
|
||||
</RelativeLayout>
|
||||
@@ -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" />
|
||||
|
||||
<ImageView
|
||||
android:importantForAccessibility="no"
|
||||
|
||||
@@ -294,4 +294,4 @@
|
||||
<string name="useBackImage">Gebruik achterzijde van kaart</string>
|
||||
<string name="settings_use_volume_keys_navigation">Verwissel kaart met de volume knoppen</string>
|
||||
<string name="settings_use_volume_keys_navigation_summary">Gebruik de volume knoppen om te wisselen van getoonde kaart</string>
|
||||
</resources>
|
||||
</resources>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<integer name="main_view_card_columns">3</integer>
|
||||
<integer name="main_view_card_columns">4</integer>
|
||||
</resources>
|
||||
@@ -36,6 +36,40 @@
|
||||
<item>@string/settings_brown_theme</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="column_count_portrait_values">
|
||||
<item>@string/settings_key_automatic_column_count</item>
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="column_count_portrait_value_strings">
|
||||
<item>@string/settings_automatic_column_count</item>
|
||||
<item>@string/settings_column_count_1</item>
|
||||
<item>@string/settings_column_count_2</item>
|
||||
<item>@string/settings_column_count_3</item>
|
||||
<item>@string/settings_column_count_4</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="column_count_landscape_values">
|
||||
<item>@string/settings_key_automatic_column_count</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
<item>5</item>
|
||||
<item>6</item>
|
||||
<item>7</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="column_count_landscape_value_strings">
|
||||
<item>@string/settings_automatic_column_count</item>
|
||||
<item>@string/settings_column_count_3</item>
|
||||
<item>@string/settings_column_count_4</item>
|
||||
<item>@string/settings_column_count_5</item>
|
||||
<item>@string/settings_column_count_6</item>
|
||||
<item>@string/settings_column_count_7</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="color_values_no_dynamic">
|
||||
<item>@string/settings_key_system_theme</item>
|
||||
<item>@string/settings_key_pink_theme</item>
|
||||
|
||||
@@ -318,6 +318,20 @@
|
||||
<string name="sharedpreference_card_details_show_validity" translatable="false">sharedpreference_card_details_show_validity</string>
|
||||
<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_column_count_portrait">Columns in portrait mode</string>
|
||||
<string name="settings_column_count_landscape">Columns in landscape mode</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>
|
||||
<string name="settings_column_count_2">2</string>
|
||||
<string name="settings_column_count_3">3</string>
|
||||
<string name="settings_column_count_4">4</string>
|
||||
<string name="settings_column_count_5">5</string>
|
||||
<string name="settings_column_count_6">6</string>
|
||||
<string name="settings_column_count_7">7</string>
|
||||
<string name="setting_key_column_count_portrait" translatable="false">pref_column_count_portrait</string>
|
||||
<string name="setting_key_column_count_landscape" translatable="false">pref_column_count_landscape</string>
|
||||
<string name="settings_category_title_general">General</string>
|
||||
<string name="settings_category_title_privacy">Privacy</string>
|
||||
<string name="action_display_options">Display options</string>
|
||||
|
||||
@@ -46,6 +46,31 @@
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/settings_category_title_cards_overview"
|
||||
app:iconSpaceReserved="false">
|
||||
|
||||
<ListPreference
|
||||
android:key="@string/setting_key_column_count_portrait"
|
||||
android:title="@string/settings_column_count_portrait"
|
||||
android:defaultValue="@string/settings_key_automatic_column_count"
|
||||
android:entries="@array/column_count_portrait_value_strings"
|
||||
android:entryValues="@array/column_count_portrait_values"
|
||||
app:iconSpaceReserved="false"
|
||||
app:singleLineTitle="false"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
|
||||
<ListPreference
|
||||
android:key="@string/setting_key_column_count_landscape"
|
||||
android:title="@string/settings_column_count_landscape"
|
||||
android:defaultValue="@string/settings_key_automatic_column_count"
|
||||
android:entries="@array/column_count_landscape_value_strings"
|
||||
android:entryValues="@array/column_count_landscape_values"
|
||||
app:iconSpaceReserved="false"
|
||||
app:singleLineTitle="false"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/settings_category_title_cards"
|
||||
app:iconSpaceReserved="false">
|
||||
@@ -112,4 +137,4 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
</PreferenceScreen>
|
||||
|
||||
Reference in New Issue
Block a user