页面UI替换1远程遥控和各项弹窗和作业主页面和子页面+路径规划的主页面重构等
This commit is contained in:
84
lib/features/v2/device_list/data/voice_preset_store.dart
Normal file
84
lib/features/v2/device_list/data/voice_preset_store.dart
Normal file
@@ -0,0 +1,84 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// 一条预设语音:名称 + 播报文字
|
||||
class VoicePreset {
|
||||
final String id;
|
||||
final String name;
|
||||
final String text;
|
||||
|
||||
const VoicePreset({required this.id, required this.name, required this.text});
|
||||
|
||||
VoicePreset copyWith({String? name, String? text}) {
|
||||
return VoicePreset(
|
||||
id: id,
|
||||
name: name ?? this.name,
|
||||
text: text ?? this.text,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {'id': id, 'name': name, 'text': text};
|
||||
|
||||
static VoicePreset? fromJson(Object? raw) {
|
||||
if (raw is! Map) return null;
|
||||
final id = raw['id']?.toString() ?? '';
|
||||
if (id.isEmpty) return null;
|
||||
return VoicePreset(
|
||||
id: id,
|
||||
name: raw['name']?.toString() ?? '',
|
||||
text: raw['text']?.toString() ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 预设语音本地存储。
|
||||
///
|
||||
/// 后端目前没有语音合成 / 语音包接口(设备详情页也只标注「语音合成需设备支持」),
|
||||
/// 所以预设语音先按设备名存在本地 prefs:改机型/换设备互不影响,
|
||||
/// 后续接口就绪时只需把 [loadPresets] / [savePresets] 换成远端读写,页面不用动。
|
||||
class VoicePresetStore {
|
||||
/// 每条语音按设备名分 key,避免多台机器人共用同一份预设
|
||||
static String _keyOf(String deviceName) => 'voice_presets_$deviceName';
|
||||
|
||||
/// 出厂默认三条,与设计稿一致;首次进入时落盘,之后以本地为准
|
||||
static const List<VoicePreset> defaults = [
|
||||
VoicePreset(
|
||||
id: 'default_safety',
|
||||
name: '安全警示',
|
||||
text: '请注意,机器人正在作业,请保持安全距离。',
|
||||
),
|
||||
VoicePreset(id: 'default_start', name: '启动提醒', text: '设备即将启动,请远离作业区域。'),
|
||||
VoicePreset(id: 'default_polite', name: '礼貌提示', text: '请注意避让,感谢配合。'),
|
||||
];
|
||||
|
||||
static Future<List<VoicePreset>> loadPresets(String deviceName) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_keyOf(deviceName));
|
||||
if (raw == null || raw.isEmpty) {
|
||||
await savePresets(deviceName, defaults);
|
||||
return List<VoicePreset>.of(defaults);
|
||||
}
|
||||
try {
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! List) return List<VoicePreset>.of(defaults);
|
||||
final list = decoded
|
||||
.map(VoicePreset.fromJson)
|
||||
.whereType<VoicePreset>()
|
||||
.toList();
|
||||
return list.isEmpty ? List<VoicePreset>.of(defaults) : list;
|
||||
} catch (_) {
|
||||
// 本地数据损坏时不阻塞页面,回退默认值
|
||||
return List<VoicePreset>.of(defaults);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> savePresets(
|
||||
String deviceName,
|
||||
List<VoicePreset> presets,
|
||||
) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = jsonEncode(presets.map((e) => e.toJson()).toList());
|
||||
await prefs.setString(_keyOf(deviceName), raw);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user