APP_NEW/lib/Provider/Diagnosis/diagnosis_provider.dart
2025-03-11 21:17:14 +08:00

142 lines
3.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:lamiter/Class/API/api.dart';
import 'package:lamiter/Class/Diagnosis/diagnosis.dart';
import 'package:lamiter/Class/Form/constitution_form.dart';
import 'package:lamiter/Class/Form/health_index_form.dart';
import 'package:lamiter/Class/Form/physical_index_form.dart';
import 'package:lamiter/Class/Form/posture_issue_form.dart';
import 'package:lamiter/Class/Form/sleep_well_index_form.dart';
import 'package:lamiter/Class/Form/stress_index_form.dart';
import 'package:lamiter/Class/Form/urban_disease_form.dart';
import 'package:lamiter/Class/Form/zong_fu_index_form.dart';
import 'package:lamiter/Class/User/client.dart';
import 'package:lamiter/Class/User/manager.dart';
class DiagnosisProvider extends ChangeNotifier {
late Diagnosis? diagnosis;
late bool submitting;
late HealthIndexForm? healthIndexForm;
late PhysicalIndexForm? physicalIndexForm;
late UrbanDiseaseForm? urbanDiseaseForm;
late StressIndexForm? stressIndexForm;
late SleepWellIndexForm? sleepWellIndexForm;
late ConstitutionForm? constitutionForm;
late ZongFuIndexForm? zongFuIndexForm;
late PostureIssueForm? postureIssueForm;
DiagnosisProvider() {
init();
}
void init() {
diagnosis = null;
submitting = false;
healthIndexForm = null;
physicalIndexForm = null;
urbanDiseaseForm = null;
stressIndexForm = null;
sleepWellIndexForm = null;
constitutionForm = null;
zongFuIndexForm = null;
postureIssueForm = null;
notifyListeners();
}
void start(Manager manager, Client? client) async {
init();
diagnosis = Diagnosis(
managerId: manager.id!,
managerName: manager.name,
clientId: client?.id,
);
notifyListeners();
}
void update(DiagnosisType type, dynamic form, dynamic result) {
_updateForm(type, form);
_updateResult(type, result);
}
void _updateForm(DiagnosisType type, dynamic form) {
switch (type) {
case DiagnosisType.healthIndex:
try {
form as HealthIndexForm?;
healthIndexForm = form;
} catch (e) {}
break;
case DiagnosisType.physicalIndex:
try {
form as PhysicalIndexForm?;
physicalIndexForm = form;
} catch (e) {}
break;
case DiagnosisType.urbanDisease:
try {
form as UrbanDiseaseForm?;
urbanDiseaseForm = form;
} catch (e) {}
break;
case DiagnosisType.stressIndex:
try {
form as StressIndexForm?;
stressIndexForm = form;
} catch (e) {}
break;
case DiagnosisType.sleepWellIndex:
try {
form as SleepWellIndexForm?;
sleepWellIndexForm = form;
} catch (e) {}
break;
case DiagnosisType.constitution:
try {
form as ConstitutionForm?;
constitutionForm = form;
} catch (e) {}
break;
case DiagnosisType.zongFuIndex:
try {
form as ZongFuIndexForm?;
zongFuIndexForm = form;
} catch (e) {}
break;
case DiagnosisType.postureIssue:
try {
form as PostureIssueForm?;
postureIssueForm = form;
} catch (e) {}
break;
}
notifyListeners();
}
void _updateResult(DiagnosisType type, dynamic result) {
if (diagnosis == null) return;
diagnosis!.update(type, result);
notifyListeners();
}
bool isReadyToSubmit() {
if (diagnosis == null) return false;
return diagnosis!.isReadyToSubmit();
}
void updateSubmitting(bool b) {
submitting = b;
notifyListeners();
}
Future<Map<String, dynamic>> submit() async {
updateSubmitting(true);
if (diagnosis?.clientId != null) {
diagnosis?.endTime = DateTime.now();
final res = await API().create_diagnosis(diagnosis!);
return res;
}
updateSubmitting(false);
return {"success": null};
}
}