feat: 重构局域网数据同步与物理药瓶生命周期(Bottle Entity),实现开新瓶、软删除与多端精确余量核算
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import '../models/dose_record.dart';
|
||||
import '../services/storage_service.dart';
|
||||
import '../services/sync_service.dart';
|
||||
|
||||
class HistoryScreen extends StatefulWidget {
|
||||
final StorageService storageService;
|
||||
@@ -47,6 +48,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
if (confirmed == true) {
|
||||
await widget.storageService.deleteRecord(record.id);
|
||||
_loadRecords();
|
||||
SyncService(widget.storageService).syncSilently();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -136,7 +136,6 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
);
|
||||
|
||||
if (confirmed == true && _activeCanisters.isNotEmpty) {
|
||||
final now = DateTime.now();
|
||||
final Map<String, int> deductions = {};
|
||||
int flixotideDoses = 0;
|
||||
int ventolinDoses = 0;
|
||||
@@ -150,12 +149,19 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
final nowUtc = DateTime.now().toUtc();
|
||||
final activeFlix = widget.storageService.getActiveFlixotideBottle();
|
||||
final activeVent = widget.storageService.getActiveVentolinBottle();
|
||||
|
||||
final record = DoseRecord(
|
||||
id: const Uuid().v4(),
|
||||
startTime: now,
|
||||
endTime: now,
|
||||
startTime: nowUtc,
|
||||
endTime: nowUtc,
|
||||
flixotidePuffs: flixotideDoses,
|
||||
ventolinPuffs: ventolinDoses,
|
||||
flixotideBottleId: flixotideDoses > 0 ? activeFlix?.id : null,
|
||||
ventolinBottleId: ventolinDoses > 0 ? activeVent?.id : null,
|
||||
updatedAt: nowUtc,
|
||||
);
|
||||
|
||||
await widget.storageService.deductSessionDoses(deductions);
|
||||
@@ -185,15 +191,20 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
);
|
||||
|
||||
if (confirmed == true) {
|
||||
final now = DateTime.now();
|
||||
final nowUtc = DateTime.now().toUtc();
|
||||
final activeBottle = widget.storageService.getActiveBottle(canister.id);
|
||||
|
||||
final record = DoseRecord(
|
||||
id: const Uuid().v4(),
|
||||
startTime: now,
|
||||
endTime: now,
|
||||
startTime: nowUtc,
|
||||
endTime: nowUtc,
|
||||
flixotidePuffs: canister.isSteroid ? 1 : 0,
|
||||
ventolinPuffs: canister.isSteroid ? 0 : 1,
|
||||
flixotideBottleId: canister.isSteroid ? activeBottle?.id : null,
|
||||
ventolinBottleId: !canister.isSteroid ? activeBottle?.id : null,
|
||||
isPrime: true,
|
||||
note: '${canister.name.split(" ")[0]}试喷',
|
||||
updatedAt: nowUtc,
|
||||
);
|
||||
await widget.storageService.deductSessionDoses({canister.id: 1});
|
||||
await widget.storageService.addRecord(record);
|
||||
@@ -203,28 +214,36 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
Future<void> _resetCanister(Canister canister) async {
|
||||
final activeBottle = widget.storageService.getActiveBottle(canister.id);
|
||||
final currentSeq = activeBottle?.sequenceNumber ?? 1;
|
||||
final nextSeq = currentSeq + 1;
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: Text('换新药瓶 - ${canister.name.split(" ")[0]}'),
|
||||
content: Text('确认启用新的一瓶【${canister.name}】,将其剩余次数重置为 ${canister.totalDoses} 喷吗?'),
|
||||
content: Text('当前正在使用第 $currentSeq 瓶。\n确认拆封启用【第 $nextSeq 瓶】,并将剩余次数重置为满瓶 (${canister.totalDoses} 喷) 吗?'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('取消')),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(ctx, true),
|
||||
child: Text('确认重置为 ${canister.totalDoses} 喷'),
|
||||
child: Text('确认启用第 $nextSeq 瓶'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true) {
|
||||
await widget.storageService.resetCanisterDoses(canister.id, canister.totalDoses);
|
||||
final deviceName = await SyncService.getDeviceName();
|
||||
final newBottle = await widget.storageService.openNewBottle(canister.id, deviceName: deviceName);
|
||||
_loadData();
|
||||
SyncService(widget.storageService).syncSilently();
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('已成功将 ${canister.name.split(" ")[0]} 重置为满瓶!')),
|
||||
SnackBar(
|
||||
content: Text('已成功启用第 ${newBottle.sequenceNumber} 瓶【${canister.name.split(" ")[0]}】,剩余次数已更新为满瓶!'),
|
||||
backgroundColor: const Color(0xFF10B981),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user