Skip to content

Commit 4ab94af

Browse files
committed
feat: add real-time font size update in settings page
- Settings page now updates immediately when font size preference changes - Subscribe to TextSizeChangeEvent using EventBus - Clear all scaling tags before applying new font size - Reset text to original size before reapplying scaling - No need to exit and re-enter settings to see changes Users can now see font size changes instantly in the settings page itself.
1 parent 92a59f9 commit 4ab94af

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
import me.ghui.v2er.util.Utils;
3232
import me.ghui.v2er.util.Voast;
3333
import me.ghui.v2er.widget.dialog.ConfirmDialog;
34+
import org.greenrobot.eventbus.Subscribe;
35+
import org.greenrobot.eventbus.ThreadMode;
3436

3537
/**
3638
* Created by ghui on 10/06/2017.
3739
*/
3840

3941
public class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
42+
private ListView mListView;
4043
private Preference cachePref;
4144
private Preference loginPreference;
4245

@@ -73,6 +76,9 @@ public void onCreate(Bundle savedInstanceState) {
7376
fontItem.setSummary(fontItem.getValue());
7477
fontItem.setOnPreferenceChangeListener((preference, newValue) -> {
7578
fontItem.setSummary(newValue + "");
79+
// Clear all scaling tags before applying new scaling
80+
clearFontScalingTags();
81+
// Post event will trigger onTextSizeChange to reapply scaling
7682
Bus.post(new TextSizeChangeEvent(FontSizeUtil.getContentSize()));
7783
return true;
7884
});
@@ -81,21 +87,28 @@ public void onCreate(Bundle savedInstanceState) {
8187
@Override
8288
public void onStart() {
8389
super.onStart();
90+
Bus.register(this);
91+
}
92+
93+
@Override
94+
public void onStop() {
95+
super.onStop();
96+
Bus.unRegister(this);
8497
}
8598

8699
@Override
87100
public void onActivityCreated(Bundle savedInstanceState) {
88101
super.onActivityCreated(savedInstanceState);
89102
View rootView = getView();
90103
rootView.setBackgroundColor(Theme.getColor(R.attr.page_bg_color, getActivity()));
91-
ListView list = rootView.findViewById(android.R.id.list);
92-
if (list != null) {
93-
list.setDivider(null);
94-
// list.setDivider(getActivity().getDrawable(R.drawable.common_divider));
95-
Utils.setPaddingForNavbar(list);
104+
mListView = rootView.findViewById(android.R.id.list);
105+
if (mListView != null) {
106+
mListView.setDivider(null);
107+
// mListView.setDivider(getActivity().getDrawable(R.drawable.common_divider));
108+
Utils.setPaddingForNavbar(mListView);
96109

97110
// Apply font scaling to preference items
98-
applyFontScalingToPreferences(list);
111+
applyFontScalingToPreferences(mListView);
99112
}
100113
}
101114

@@ -137,6 +150,50 @@ public void onScroll(android.widget.AbsListView view, int firstVisibleItem,
137150
}, 100);
138151
}
139152

153+
@Subscribe(threadMode = ThreadMode.MAIN)
154+
public void onTextSizeChange(TextSizeChangeEvent event) {
155+
// Clear all existing scaling and reapply with new size
156+
if (mListView != null) {
157+
clearFontScalingTags();
158+
applyFontScalingToPreferences(mListView);
159+
}
160+
}
161+
162+
private void clearFontScalingTags() {
163+
if (mListView == null) return;
164+
165+
// Clear tags from all visible items
166+
for (int i = 0; i < mListView.getChildCount(); i++) {
167+
View child = mListView.getChildAt(i);
168+
clearTagsRecursively(child);
169+
}
170+
}
171+
172+
private void clearTagsRecursively(View view) {
173+
if (view == null) return;
174+
175+
// Clear the scaling tags
176+
view.setTag(R.id.font_scaled_tag, null);
177+
view.setTag(R.id.original_text_size_tag, null);
178+
179+
// Reset text size to original if it's a TextView
180+
if (view instanceof TextView) {
181+
TextView textView = (TextView) view;
182+
Object originalSize = textView.getTag(R.id.original_text_size_tag);
183+
if (originalSize instanceof Float) {
184+
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, (Float) originalSize);
185+
}
186+
}
187+
188+
// Recursively clear children
189+
if (view instanceof ViewGroup) {
190+
ViewGroup viewGroup = (ViewGroup) view;
191+
for (int i = 0; i < viewGroup.getChildCount(); i++) {
192+
clearTagsRecursively(viewGroup.getChildAt(i));
193+
}
194+
}
195+
}
196+
140197
private void applyFontScalingToView(View view) {
141198
if (view == null) return;
142199

0 commit comments

Comments
 (0)