125 lines
3.9 KiB
Dart
125 lines
3.9 KiB
Dart
import 'dart:async';
|
||
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;
|
||
|
||
Timer? _autoHideTimer;
|
||
|
||
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;
|
||
|
||
// 如果显示关怀开关,启动3秒自动隐藏定时器
|
||
if (isCareButtonVisible.value) {
|
||
_startAutoHideTimer();
|
||
} else {
|
||
_cancelAutoHideTimer();
|
||
}
|
||
}
|
||
|
||
/// 启动自动隐藏定时器
|
||
void _startAutoHideTimer() {
|
||
_cancelAutoHideTimer();
|
||
_autoHideTimer = Timer(const Duration(seconds: 3), () {
|
||
isCareButtonVisible.value = false;
|
||
});
|
||
}
|
||
|
||
/// 取消自动隐藏定时器
|
||
void _cancelAutoHideTimer() {
|
||
_autoHideTimer?.cancel();
|
||
_autoHideTimer = null;
|
||
}
|
||
|
||
/// 重置自动隐藏定时器(用户交互时调用)
|
||
void resetAutoHideTimer() {
|
||
if (isCareButtonVisible.value) {
|
||
_startAutoHideTimer();
|
||
}
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
_cancelAutoHideTimer();
|
||
super.onClose();
|
||
}
|
||
|
||
Future<void> switchCareNavigation(int index) async {
|
||
_careNavigationIndex.value = index;
|
||
final prefs = await SharedPreferences.getInstance();
|
||
await prefs.setInt(_careNavigationIndexKey, index);
|
||
}
|
||
}
|