Skip to content

Commit

Permalink
Merge pull request #2147 from leaumar/feat/1952-columns
Browse files Browse the repository at this point in the history
resolve #1952 custom column count
  • Loading branch information
TheLastProject authored Nov 2, 2024
2 parents 0b7cd32 + d7b8cd7 commit b502f89
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 34 deletions.
27 changes: 16 additions & 11 deletions app/src/main/java/protect/card_locker/CardShortcutConfigure.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
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;

@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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoyaltyCardCursorAdapter.LoyaltyCardListItemViewHolder> {
private int mCurrentSelectedIndex = -1;
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/protect/card_locker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/protect/card_locker/preferences/Settings.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/loyalty_card_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 1 addition & 1 deletion app/src/main/res/values-w600dp/integers.xml
Original file line number Diff line number Diff line change
@@ -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>
34 changes: 34 additions & 0 deletions app/src/main/res/values/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
27 changes: 26 additions & 1 deletion app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -112,4 +137,4 @@

</PreferenceCategory>

</PreferenceScreen>
</PreferenceScreen>

0 comments on commit b502f89

Please sign in to comment.