2026-05-25 12:58:29 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
2026-05-14 15:08:19 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import '../../domain/tab_config.dart';
|
|
|
|
|
|
|
|
|
|
abstract class TabConfigState {}
|
|
|
|
|
|
|
|
|
|
class TabConfigInitial extends TabConfigState {}
|
|
|
|
|
|
|
|
|
|
class TabConfigLoaded extends TabConfigState {
|
|
|
|
|
final TabConfig config;
|
2026-05-25 12:58:29 +08:00
|
|
|
final int selectedIndex;
|
2026-05-14 15:08:19 +08:00
|
|
|
|
2026-05-25 12:58:29 +08:00
|
|
|
TabConfigLoaded(this.config, {this.selectedIndex = 0});
|
2026-05-14 15:08:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TabConfigCubit extends Cubit<TabConfigState> {
|
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
|
static const String _configKey = 'tab_config';
|
|
|
|
|
|
|
|
|
|
TabConfigCubit(this._prefs) : super(TabConfigInitial()) {
|
|
|
|
|
_loadConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _loadConfig() {
|
2026-05-25 12:58:29 +08:00
|
|
|
debugPrint('🔍 [TabConfigCubit] _loadConfig 被调用');
|
|
|
|
|
// 直接使用代码中的默认配置,不持久化
|
|
|
|
|
final defaultConfig = TabConfig.defaultConfig();
|
|
|
|
|
debugPrint('🔍 [TabConfigCubit] 默认配置 Tab 数量: ${defaultConfig.items.length}');
|
|
|
|
|
debugPrint('🔍 [TabConfigCubit] 启用的 Tab: ${defaultConfig.enabledItems.map((e) => e.id).toList()}');
|
|
|
|
|
emit(TabConfigLoaded(defaultConfig));
|
|
|
|
|
debugPrint('✅ [TabConfigCubit] 状态已 emit');
|
2026-05-14 15:08:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _saveConfig(TabConfig config) {
|
|
|
|
|
try {
|
|
|
|
|
final configList = config.items.map((item) => {
|
|
|
|
|
'id': item.id,
|
|
|
|
|
'name': item.name,
|
|
|
|
|
'nameEn': item.nameEn,
|
|
|
|
|
'icon': item.icon,
|
|
|
|
|
'isEnabled': item.isEnabled,
|
|
|
|
|
'order': item.order,
|
|
|
|
|
}).toList();
|
|
|
|
|
_prefs.setString(_configKey, jsonEncode(configList));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print('保存 Tab 配置失败: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void toggleTab(String itemId) {
|
|
|
|
|
if (state is TabConfigLoaded) {
|
|
|
|
|
final currentState = state as TabConfigLoaded;
|
|
|
|
|
final newConfig = currentState.config.toggleItem(itemId);
|
|
|
|
|
|
2026-05-25 12:58:29 +08:00
|
|
|
// 不保存,直接更新状态
|
|
|
|
|
emit(TabConfigLoaded(newConfig, selectedIndex: currentState.selectedIndex));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void selectTab(int index) {
|
|
|
|
|
if (state is TabConfigLoaded) {
|
|
|
|
|
final currentState = state as TabConfigLoaded;
|
|
|
|
|
final enabledTabs = currentState.config.enabledItems;
|
|
|
|
|
|
|
|
|
|
// 确保索引在有效范围内
|
|
|
|
|
if (index >= 0 && index < enabledTabs.length) {
|
|
|
|
|
emit(TabConfigLoaded(currentState.config, selectedIndex: index));
|
2026-05-14 15:08:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 17:18:58 +08:00
|
|
|
void resetToDefault() {
|
|
|
|
|
final defaultConfig = TabConfig.defaultConfig();
|
|
|
|
|
emit(TabConfigLoaded(defaultConfig));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:08:19 +08:00
|
|
|
TabConfig? getConfig() {
|
|
|
|
|
if (state is TabConfigLoaded) {
|
|
|
|
|
return (state as TabConfigLoaded).config;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|