切换设备到时候 清除路径规划缓存

This commit is contained in:
mmc
2026-03-11 16:11:51 +08:00
parent ddb6441413
commit bb3972dbca
3 changed files with 70 additions and 12 deletions

View File

@@ -4,6 +4,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/commonFn.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../../core/app/app_user_cubit.dart';
import '../../../../core/router/route_paths.dart';
@@ -310,6 +312,8 @@ class ImmersionHeader extends StatelessWidget {
: () async {
// 💡 执行切换逻辑
await context.read<DevicesCubit>().switchDevice(device);
await LocationUtils.clearAllPlotCache();
// 切换成功后,UI 会自动更新(因为 BlocBuilder 在监听),我们可以关闭弹窗
if (context.mounted) {
Navigator.pop(context);

View File

@@ -1,6 +1,17 @@
// common/location_utils.dart
/// 定位质量解析工具类
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
/// 定位质量解析 + 缓存管理 工具类
class LocationUtils {
// ==================== 1. 缓存键常量(全局复用) ====================
static const String kSavedPlotData = 'saved_plot_data';
static const String kSavedGcjPathPoints = 'saved_gcj_path_points';
static const String kSavedGcjOuterPoints = 'saved_gcj_outer_points';
static const String kSavedMarkedPoints = 'saved_marked_points';
static const String kSavedObstacleHoles = 'saved_obstacle_holes';
static const String kSavedIsWorkAreaCompleted = 'saved_is_work_area_completed';
// ==================== 2. 定位质量解析相关方法 ====================
/// 解析定位质量值为可读字符串
/// [locationValue] 定位质量编码(0/1/2/4/5)
/// 返回:对应的中文描述
@@ -25,17 +36,60 @@ class LocationUtils {
switch (value) {
case 3:
return '远程控制';
default:
return '本地控制';
}
}
// 【可选扩展】可添加更多定位相关工具方法
// 示例:校验定位质量是否有效
/// 校验定位质量是否有效
static bool isLocationValid(int locationValue) {
return locationValue != 0;
}
// ==================== 3. 缓存操作相关方法(可选扩展,按需添加) ====================
/// 保存坐标点数组到 SharedPreferences(自动序列化)
static Future<void> saveLatLngList(String key, List<dynamic> points) async {
final prefs = await SharedPreferences.getInstance();
// 将数组序列化为 JSON 字符串存储
await prefs.setString(key, json.encode(points));
}
/// 读取坐标点数组(自动反序列化)
static Future<List<dynamic>> getLatLngList(String key) async {
final prefs = await SharedPreferences.getInstance();
final jsonStr = prefs.getString(key);
if (jsonStr == null || jsonStr.isEmpty) return [];
try {
return json.decode(jsonStr) as List<dynamic>;
} catch (e) {
print('读取缓存数组失败: $e, key=$key');
return [];
}
}
/// 清除指定缓存键
static Future<void> clearCache(String key) async {
final prefs = await SharedPreferences.getInstance();
if (prefs.containsKey(key)) {
await prefs.remove(key);
}
}
/// 清除所有定位/地块相关缓存(仅清除本类定义的键)
static Future<void> clearAllPlotCache() async {
final prefs = await SharedPreferences.getInstance();
// 批量清除本类定义的缓存键
final cacheKeys = [kSavedPlotData, kSavedGcjPathPoints, kSavedGcjOuterPoints, kSavedMarkedPoints, kSavedObstacleHoles, kSavedIsWorkAreaCompleted];
for (final key in cacheKeys) {
if (prefs.containsKey(key)) {
await prefs.remove(key);
}
}
}
// 在 LocationUtils 类中添加
static Future<bool> getBool(String key, {bool defaultValue = false}) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(key) ?? defaultValue;
}
}

View File

@@ -31,6 +31,7 @@ import 'package:maibu_satabot_v2/features/devices/presentation/bloc/device_statu
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_state.dart';
import 'package:maibu_satabot_v2/features/home/presentation/widgets/BottomDirectionLine.dart';
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/commonFn.dart';
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/enum.dart';
import 'package:maibu_satabot_v2/components/input_confirm_dialog.dart';
import 'package:maibu_satabot_v2/features/home/presentation/widgets/common/tracepoint.dart';
@@ -205,12 +206,11 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
// 新增:手动清除本地存储数据
Future<void> _clearLocalData() async {
try {
final prefs = await SharedPreferences.getInstance();
prefs.remove(kSavedGcjPathPoints);
prefs.remove(kSavedGcjOuterPoints);
prefs.remove(kSavedMarkedPoints);
prefs.remove(kSavedObstacleHoles);
prefs.remove(kSavedIsWorkAreaCompleted);
await LocationUtils.clearCache(LocationUtils.kSavedGcjPathPoints);
await LocationUtils.clearCache(LocationUtils.kSavedGcjOuterPoints);
await LocationUtils.clearCache(LocationUtils.kSavedMarkedPoints);
await LocationUtils.clearCache(LocationUtils.kSavedObstacleHoles);
await LocationUtils.clearCache(LocationUtils.kSavedIsWorkAreaCompleted);
debugPrint('本地存储的绘制数据已清空');
} catch (e) {
debugPrint('清空本地数据失败:$e');