Skip to content

Commit f63af0e

Browse files
committed
fix: address Copilot review comments for code quality
- Extract magic number 0.01f to SCALING_THRESHOLD constant in SettingFragment - Extract scaling ratios to named constants in FontSizeUtil for better maintainability - Fix potential duplicate scroll listeners by checking if already set - Simplify instanceof check in MainActivity (remove unnecessary null check) - Add field to track scroll listener to prevent duplicates Addresses all code quality feedback from Copilot in PR #131
1 parent 7373ac0 commit f63af0e

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

app/src/main/java/me/ghui/v2er/module/home/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ private void applyFontSizeToNavigationMenu() {
316316

317317
private void applyFontScalingToMenuItems() {
318318
// Apply scaling only once to avoid repeated scaling
319-
ViewGroup menuView = (ViewGroup) mNavigationView.getChildAt(0);
320-
if (menuView != null && menuView instanceof RecyclerView) {
319+
View menuView = mNavigationView.getChildAt(0);
320+
if (menuView instanceof RecyclerView) {
321321
RecyclerView recyclerView = (RecyclerView) menuView;
322322
// Apply to currently visible items
323323
for (int i = 0; i < recyclerView.getChildCount(); i++) {

app/src/main/java/me/ghui/v2er/module/settings/SettingFragment.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
*/
4040

4141
public class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
42+
private static final float SCALING_THRESHOLD = 0.01f;
4243
private ListView mListView;
44+
private android.widget.AbsListView.OnScrollListener mScrollListener;
4345
private Preference cachePref;
4446
private Preference loginPreference;
4547

@@ -116,7 +118,7 @@ private void applyFontScalingToPreferences(ListView listView) {
116118
// Apply font scaling after a short delay to ensure views are created
117119
listView.postDelayed(() -> {
118120
float scalingRatio = FontSizeUtil.getScalingRatio();
119-
if (Math.abs(scalingRatio - 1.0f) < 0.01f) {
121+
if (Math.abs(scalingRatio - 1.0f) < SCALING_THRESHOLD) {
120122
// No scaling needed for default size
121123
return;
122124
}
@@ -127,8 +129,9 @@ private void applyFontScalingToPreferences(ListView listView) {
127129
applyFontScalingToView(child);
128130
}
129131

130-
// Add scroll listener to apply scaling to new items
131-
listView.setOnScrollListener(new android.widget.AbsListView.OnScrollListener() {
132+
// Add scroll listener to apply scaling to new items (only if not already set)
133+
if (mScrollListener == null) {
134+
mScrollListener = new android.widget.AbsListView.OnScrollListener() {
132135
@Override
133136
public void onScrollStateChanged(android.widget.AbsListView view, int scrollState) {
134137
// Not needed
@@ -146,7 +149,9 @@ public void onScroll(android.widget.AbsListView view, int firstVisibleItem,
146149
}
147150
}
148151
}
149-
});
152+
};
153+
listView.setOnScrollListener(mScrollListener);
154+
}
150155
}, 100);
151156
}
152157

app/src/main/java/me/ghui/v2er/util/FontSizeUtil.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import me.ghui.v2er.general.Pref;
66

77
public class FontSizeUtil {
8+
// Scaling ratio constants
9+
private static final float SMALL_SCALE = 0.875f; // 87.5% of default
10+
private static final float MEDIUM_SCALE = 1.0f; // 100% default
11+
private static final float LARGE_SCALE = 1.25f; // 125% of default
12+
private static final float EXTRA_LARGE_SCALE = 1.5f; // 150% of default
813

914
/**
1015
* Common helper method to get font dimension based on size preference
@@ -75,14 +80,14 @@ public static float getScalingRatio() {
7580
String size = Pref.read(R.string.pref_key_fontsize);
7681
switch (size) {
7782
case "小":
78-
return 0.875f; // 87.5% of default
83+
return SMALL_SCALE;
7984
case "大":
80-
return 1.25f; // 125% of default
85+
return LARGE_SCALE;
8186
case "特大":
82-
return 1.5f; // 150% of default
87+
return EXTRA_LARGE_SCALE;
8388
case "中":
8489
default:
85-
return 1.0f; // 100% default
90+
return MEDIUM_SCALE;
8691
}
8792
}
8893

0 commit comments

Comments
 (0)