implement #1952: custom column count

This commit is contained in:
Marnes
2024-10-29 21:50:52 +01:00
parent 1b0fd94a8c
commit 6f456437f7
7 changed files with 112 additions and 12 deletions

View File

@@ -12,9 +12,9 @@ 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.preferences.Settings;
/**
* The configuration screen for creating a shortcut.
@@ -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) {

View File

@@ -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 {
@@ -357,6 +359,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) {
@@ -369,6 +377,7 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
mLoyaltyCardCount = DBHelper.getLoyaltyCardCount(mDatabase);
}
// TODO isn't this called too often, in onResume AND onCreate?
private void updateLoyaltyCardList(boolean updateCount) {
Group group = null;
if (mGroup != null) {

View File

@@ -946,16 +946,22 @@ public class Utils {
// This is necessary because Android's auto sizing will split over lines way before reaching the minimum font size and store names split over multiple lines are harder to scan with a quick glance so we should try to prevent it
// 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
var settings = new Settings(context);
// 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 / settings.getPreferredColumnCount();
// 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;
// default of 20dp crushes the text
var relativePadding = (int) (columnWidth * 0.1);
textWhenNoImage.setMaxLines(maxLines);
textWhenNoImage.setPadding(relativePadding, relativePadding, relativePadding, relativePadding);
// Actually set the text and colour
textWhenNoImage.setVisibility(View.VISIBLE);

View File

@@ -1,7 +1,10 @@
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;
@@ -15,6 +18,7 @@ import protect.card_locker.R;
import protect.card_locker.Utils;
public class Settings {
private static final String TAG = "Catima";
private final Context mContext;
private final SharedPreferences mSettings;
@@ -94,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);
}

View File

@@ -36,6 +36,28 @@
<item>@string/settings_brown_theme</item>
</string-array>
<string-array name="column_count_values">
<item>@string/settings_key_automatic_column_count</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
<string-array name="column_count_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>
<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>

View File

@@ -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>

View File

@@ -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_value_strings"
android:entryValues="@array/column_count_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_value_strings"
android:entryValues="@array/column_count_values"
app:iconSpaceReserved="false"
app:singleLineTitle="false"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/settings_category_title_cards"
app:iconSpaceReserved="false">