pile_nav_new/lib/pages/task/taskcontroller.dart
2024-11-15 17:42:52 +08:00

136 lines
3.9 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:cpnav/pages/task/model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:path_provider/path_provider.dart';
import 'package:scence_map/scence_map.dart';
import 'task_page.dart';
class TaskController extends GetxController {
final box = GetStorage(); // 实例化 GetStorage
// List<bool> isDialogShown = [false, false, false, false, false].obs;
var currentask = TaskItem(
list: [],
name: " ",
id: 0,
utc: 0,
originLabel: "本地",
workTotal: 0,
centerPileInfo: CenterPileInfo(
x: 0,
y: 0,
// direction: 0,
)).obs;
var updateCount = 0.obs;
var taskList = <TaskItem>[].obs;
@override
void onInit() {
loadTasks();
super.onInit();
TaskItem? sysTask = getTask();
if (sysTask != null) {
currentask.value = sysTask;
}
ever(currentask, (task) {
mapcontroller.pilePoints.value = task.list;
mapcontroller.updateCount++;
plumDataController.centerXY.value =
Offset(task.centerPileInfo.x, task.centerPileInfo.y);
// plumDataController.direction.value=task.centerPileInfo.direction;
plumDataController.angle.value = task.centerPileInfo.angle;
plumDataController.space.value = task.centerPileInfo.space;
plumDataController.pileWidth.value = task.centerPileInfo.pileWidth;
List<double> distances = []; //px
for (var i = 0; i < 4; i++) {
double distance = task.centerPileInfo.distances[i];
// m->px
distances.add(distance / mapcontroller.pixel2MeterRatio);
}
plumDataController.distanceOfCenter.value = distances;
updateCount++;
editTask(task);
loadTasks();
Map data = task.toJson();
setTask(data);
update();
});
TaskItem? loadtask = getTask();
if (loadtask != null) {
currentask.value = loadtask;
}
}
Future<List<File>> _getLocalFiles() async {
final directory = await getApplicationDocumentsDirectory();
final folder = Directory('${directory.path}/tasks');
// 检查文件夹是否存在,不存在则创建
if (!await folder.exists()) {
await folder.create(recursive: true);
}
// 获取所有 JSON 文件
return folder
.listSync()
.whereType<File>()
.where((file) => file.path.endsWith('.json'))
.toList();
}
Future<List<TaskItem>> readAllTaskItems() async {
List<TaskItem> allTasks = [];
List<File> files = await _getLocalFiles();
for (File file in files) {
try {
String contents = await file.readAsString();
allTasks.add(TaskItem.fromJson(jsonDecode(contents)));
} catch (e) {
// 处理读取文件或解析 JSON 的错误
log('Error reading ${file.path}: $e');
}
}
return allTasks;
}
Future<void> loadTasks() async {
List<TaskItem> tasks = await readAllTaskItems();
taskList.value = tasks;
}
Future<void> editTask(TaskItem info) async {
final directory = await getApplicationDocumentsDirectory();
final filePath = '${directory.path}/tasks/${info.utc}.json';
final file = File(filePath);
Map data = info.toJson();
String jsonString = jsonEncode(data);
// 写入 JSON 字符串到文件
await file.writeAsString(jsonString);
}
setTask(Map data) {
box.write('task', data);
}
TaskItem? getTask() {
final p = box.read("task");
if (p != null) {
return TaskItem.fromJson(p);
}
return null;
}
}
// 任务问题
// 2. 二次进入任务页面 可设置的点不重合
// 3. 移动之后 点有问题 (中心点点完之后就跑了)
// 4. 当没点坐标 就点了方向设置就坏了
// 5. 当list 删除部分点之后保存数据有问题(over 直接删了保存到文件中 合适吗?)
// ( )