关怀模式
This commit is contained in:
87
lib/services/get/care_controller.dart
Normal file
87
lib/services/get/care_controller.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class CareController extends GetxController {
|
||||
static const String _careModeKey = 'care_mode_enabled';
|
||||
static const String _userTypeKey = 'care_user_type';
|
||||
static const String _pinyinEnabledKey = 'care_pinyin_enabled';
|
||||
static const String _selectedOptionsKey = 'care_selected_options';
|
||||
static const String _careNavigationIndexKey = 'care_navigation_index';
|
||||
|
||||
final RxBool _isCareModeEnabled = false.obs;
|
||||
final RxString _userType = '儿童'.obs;
|
||||
final RxBool _pinyinEnabled = false.obs;
|
||||
final RxSet<String> _selectedOptions = {'诗词', '出处'}.obs;
|
||||
final RxBool isCareButtonVisible = false.obs;
|
||||
final RxInt _careNavigationIndex = 0.obs;
|
||||
|
||||
bool get isCareModeEnabled => _isCareModeEnabled.value;
|
||||
String get userType => _userType.value;
|
||||
bool get pinyinEnabled => _pinyinEnabled.value;
|
||||
Set<String> get selectedOptions => _selectedOptions.toSet();
|
||||
int get careNavigationIndex => _careNavigationIndex.value;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
_loadCareSettings();
|
||||
}
|
||||
|
||||
Future<void> _loadCareSettings() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_isCareModeEnabled.value = prefs.getBool(_careModeKey) ?? false;
|
||||
_userType.value = prefs.getString(_userTypeKey) ?? '儿童';
|
||||
_pinyinEnabled.value = prefs.getBool(_pinyinEnabledKey) ?? false;
|
||||
_careNavigationIndex.value = prefs.getInt(_careNavigationIndexKey) ?? 0;
|
||||
|
||||
final savedOptions = prefs.getStringList(_selectedOptionsKey);
|
||||
if (savedOptions != null && savedOptions.isNotEmpty) {
|
||||
_selectedOptions.clear();
|
||||
_selectedOptions.addAll(savedOptions);
|
||||
if (!_selectedOptions.contains('诗词')) {
|
||||
_selectedOptions.add('诗词');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> toggleCareMode() async {
|
||||
_isCareModeEnabled.value = !_isCareModeEnabled.value;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_careModeKey, _isCareModeEnabled.value);
|
||||
}
|
||||
|
||||
Future<void> setUserType(String type) async {
|
||||
_userType.value = type;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_userTypeKey, type);
|
||||
}
|
||||
|
||||
Future<void> setPinyinEnabled(bool enabled) async {
|
||||
_pinyinEnabled.value = enabled;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_pinyinEnabledKey, enabled);
|
||||
}
|
||||
|
||||
Future<void> toggleOption(String option) async {
|
||||
if (option == '诗词') return; // 诗词选项不可取消
|
||||
|
||||
if (_selectedOptions.contains(option)) {
|
||||
_selectedOptions.remove(option);
|
||||
} else {
|
||||
_selectedOptions.add(option);
|
||||
}
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(_selectedOptionsKey, _selectedOptions.toList());
|
||||
}
|
||||
|
||||
void toggleCareButtonVisibility() {
|
||||
isCareButtonVisible.value = !isCareButtonVisible.value;
|
||||
}
|
||||
|
||||
Future<void> switchCareNavigation(int index) async {
|
||||
_careNavigationIndex.value = index;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_careNavigationIndexKey, index);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user