This commit is contained in:
ytc1012
2025-11-26 16:32:47 +08:00
parent 96658339e1
commit 0195cdf54b
18 changed files with 1052 additions and 240 deletions

View File

@@ -11,20 +11,34 @@ class SettingsScreen extends StatefulWidget {
/// Get the saved default duration (for use in other screens)
static Future<int> getDefaultDuration() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt(_durationKey) ?? 25;
try {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt(_durationKey) ?? 25;
} catch (e) {
// Return default duration if loading fails
return 25;
}
}
/// Get the saved locale
static Future<String?> getSavedLocale() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_localeKey);
try {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_localeKey);
} catch (e) {
// Return null if loading fails
return null;
}
}
/// Save the locale
static Future<void> saveLocale(String localeCode) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_localeKey, localeCode);
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_localeKey, localeCode);
} catch (e) {
// Ignore save errors
}
}
static const String _durationKey = 'default_duration';
@@ -48,36 +62,58 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
Future<void> _loadSavedDuration() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_selectedDuration = prefs.getInt(SettingsScreen._durationKey) ?? 25;
});
try {
final prefs = await SharedPreferences.getInstance();
setState(() {
_selectedDuration = prefs.getInt(SettingsScreen._durationKey) ?? 25;
});
} catch (e) {
// Use default duration if loading fails
setState(() {
_selectedDuration = 25;
});
}
}
Future<void> _loadSavedLocale() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_selectedLocale = prefs.getString(SettingsScreen._localeKey) ?? 'en';
});
try {
final prefs = await SharedPreferences.getInstance();
setState(() {
_selectedLocale = prefs.getString(SettingsScreen._localeKey) ?? 'en';
});
} catch (e) {
// Use default locale if loading fails
setState(() {
_selectedLocale = 'en';
});
}
}
Future<void> _saveDuration(int duration) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(SettingsScreen._durationKey, duration);
setState(() {
_selectedDuration = duration;
});
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(SettingsScreen._durationKey, duration);
setState(() {
_selectedDuration = duration;
});
} catch (e) {
// Ignore save errors, state will be reset on next load
}
}
Future<void> _saveLocale(String localeCode) async {
await SettingsScreen.saveLocale(localeCode);
setState(() {
_selectedLocale = localeCode;
});
try {
await SettingsScreen.saveLocale(localeCode);
setState(() {
_selectedLocale = localeCode;
});
// Update locale immediately without restart
if (!mounted) return;
MyApp.updateLocale(context, localeCode);
// Update locale immediately without restart
if (!mounted) return;
MyApp.updateLocale(context, localeCode);
} catch (e) {
// Ignore save errors
}
}
@override