@ -0,0 +1,65 @@
|
||||
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:hmg_patient_app_new/features/active_prescriptions/models/active_prescriptions_response_model.dart';
|
||||
import '../../core/api/api_client.dart';
|
||||
import '../../core/api_consts.dart';
|
||||
import '../../core/common_models/generic_api_model.dart';
|
||||
import '../../core/exceptions/api_failure.dart';
|
||||
import '../../services/logger_service.dart';
|
||||
|
||||
abstract class ActivePrescriptionsRepo {
|
||||
|
||||
Future<Either<Failure, GenericApiModel<List<ActivePrescriptionsResponseModel>>>> getActivePrescriptionsDetails();
|
||||
|
||||
}
|
||||
|
||||
class ActivePrescriptionsRepoImp implements ActivePrescriptionsRepo {
|
||||
final ApiClient apiClient;
|
||||
final LoggerService loggerService;
|
||||
|
||||
ActivePrescriptionsRepoImp({required this.loggerService, required this.apiClient});
|
||||
|
||||
@override
|
||||
|
||||
Future<Either<Failure, GenericApiModel<List<ActivePrescriptionsResponseModel>>>> getActivePrescriptionsDetails() async
|
||||
{
|
||||
try {
|
||||
GenericApiModel<List<ActivePrescriptionsResponseModel>>? apiResponse;
|
||||
Failure? failure;
|
||||
await apiClient.post(
|
||||
ApiConsts.getActivePrescriptionsDetails,
|
||||
body: {},
|
||||
onFailure: (error, statusCode, {messageStatus, failureType}) {
|
||||
failure = failureType;
|
||||
},
|
||||
onSuccess: (response, statusCode, {messageStatus, errorMessage}) {
|
||||
try {
|
||||
var list = response['List_ActiveGetPrescriptionReportByPatientID'];
|
||||
var res = list
|
||||
.map<ActivePrescriptionsResponseModel>(
|
||||
(item) => ActivePrescriptionsResponseModel.fromJson(item))
|
||||
.toList();
|
||||
|
||||
apiResponse = GenericApiModel<List<ActivePrescriptionsResponseModel>>(
|
||||
messageStatus: messageStatus,
|
||||
statusCode: statusCode,
|
||||
errorMessage: null,
|
||||
// data: response,
|
||||
data: res
|
||||
);
|
||||
return apiResponse;
|
||||
} catch (e) {
|
||||
failure = DataParsingFailure(e.toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
if (failure != null) return Left(failure!);
|
||||
if (apiResponse == null) return Left(ServerFailure("Unknown error"));
|
||||
return Right(apiResponse!);
|
||||
} catch (e) {
|
||||
return Left(UnknownFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,101 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hmg_patient_app_new/features/active_prescriptions/models/active_prescriptions_response_model.dart';
|
||||
import 'package:hmg_patient_app_new/features/active_prescriptions/active_prescriptions_repo.dart';
|
||||
import 'package:hmg_patient_app_new/services/error_handler_service.dart';
|
||||
|
||||
class ActivePrescriptionsViewModel extends ChangeNotifier {
|
||||
late ActivePrescriptionsRepo activePrescriptionsRepo;
|
||||
late ErrorHandlerService errorHandlerService;
|
||||
List<ActivePrescriptionsResponseModel> activePrescriptionsDetailsList = [];
|
||||
|
||||
ActivePrescriptionsViewModel({
|
||||
required this.activePrescriptionsRepo,
|
||||
required this.errorHandlerService,
|
||||
});
|
||||
|
||||
Future<void> getActiveMedications({
|
||||
Function(dynamic)? onSuccess,
|
||||
Function(String)? onError,
|
||||
}) async {
|
||||
final result =
|
||||
await activePrescriptionsRepo.getActivePrescriptionsDetails();
|
||||
result.fold(
|
||||
(failure) async =>
|
||||
await errorHandlerService.handleError(failure: failure),
|
||||
(apiResponse) {
|
||||
if (apiResponse.messageStatus == 1) {
|
||||
activePrescriptionsDetailsList = apiResponse.data ?? [];
|
||||
notifyListeners();
|
||||
if (onSuccess != null) onSuccess(apiResponse.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
DateTime parseDate(String? date) {
|
||||
if (date == null) return DateTime.now();
|
||||
final regex = RegExp(r"\/Date\((\d+)([+-]\d+)?\)\/");
|
||||
final match = regex.firstMatch(date);
|
||||
if (match != null) {
|
||||
final millis = int.parse(match.group(1)!);
|
||||
return DateTime.fromMillisecondsSinceEpoch(millis);
|
||||
}
|
||||
return DateTime.tryParse(date) ?? DateTime.now();
|
||||
}
|
||||
|
||||
List<DateTime> generateMedicationDays(ActivePrescriptionsResponseModel med) {
|
||||
final start = parseDate(med.startDate);
|
||||
final duration = med.days ?? 0;
|
||||
if (duration <= 0) return [];
|
||||
final f = (med.frequency ?? "").toLowerCase().trim();
|
||||
int intervalDays = 1;
|
||||
|
||||
if (f.contains("every six hours") ||
|
||||
f.contains("every 6 hours") ||
|
||||
f.contains("every four hours") ||
|
||||
f.contains("every 4 hours") ||
|
||||
f.contains("every eight hours") ||
|
||||
f.contains("every 8 hours") ||
|
||||
f.contains("every 12 hours") ||
|
||||
f.contains("every twelve hours") ||
|
||||
f.contains("every 24 hours") ||
|
||||
f.contains("3 times a day") ||
|
||||
f.contains("once a day")) {
|
||||
intervalDays = 1;
|
||||
}
|
||||
else if (f.contains("once a week")) {
|
||||
intervalDays = 7;
|
||||
}
|
||||
else if (f.contains("every 3 days")) {
|
||||
intervalDays = 3;
|
||||
}
|
||||
else if (f.contains("every other day")) {
|
||||
intervalDays = 2;
|
||||
}
|
||||
|
||||
List<DateTime> result = [];
|
||||
for (int offset = 0; offset < duration; offset += intervalDays) {
|
||||
result.add(start.add(Duration(days: offset)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool sameYMD(DateTime a, DateTime b) =>
|
||||
a.year == b.year && a.month == b.month && a.day == b.day;
|
||||
|
||||
List<ActivePrescriptionsResponseModel> getMedsForSelectedDay(
|
||||
DateTime selectedDate) {
|
||||
final clean = DateTime(selectedDate.year, selectedDate.month, selectedDate.day);
|
||||
|
||||
return activePrescriptionsDetailsList.where((med) {
|
||||
final days = generateMedicationDays(med);
|
||||
return days.any((d) => sameYMD(d, clean));
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,155 @@
|
||||
// import 'package:easy_localization/easy_localization.dart';
|
||||
// import 'package:flutter/material.dart';
|
||||
// import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
|
||||
//
|
||||
// import '../../generated/locale_keys.g.dart';
|
||||
// import '../../theme/colors.dart';
|
||||
//
|
||||
// class ReminderTimerDialog extends StatefulWidget {
|
||||
// final Function()? onSetReminderPress;
|
||||
// final String message;
|
||||
//
|
||||
// const ReminderTimerDialog(this.onSetReminderPress, this.message, {super.key});
|
||||
//
|
||||
//
|
||||
// @override
|
||||
// State<ReminderTimerDialog> createState() => _ReminderTimerDialogState();
|
||||
// }
|
||||
//
|
||||
// class _ReminderTimerDialogState extends State<ReminderTimerDialog> {
|
||||
// final List<String> options = ["Morning", "Afternoon", "Evening", "Midnight"];
|
||||
// final List<String> selectedTimes = ["Morning"]; // Default selection
|
||||
//
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return //
|
||||
// Column(
|
||||
// children: [
|
||||
// Container(
|
||||
// decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24,
|
||||
// hasShadow: true,),
|
||||
// child: Column(
|
||||
// mainAxisSize: MainAxisSize.min,
|
||||
// crossAxisAlignment: CrossAxisAlignment.start,
|
||||
// // Checkboxes list
|
||||
// children: options.map((time) => buildCircleCheckbox(time)).toList(),
|
||||
// ).paddingAll(16),
|
||||
// ),
|
||||
// const SizedBox(height: 25),
|
||||
// // Buttons Row
|
||||
// Row(
|
||||
// children: [
|
||||
// Expanded(
|
||||
// child: ElevatedButton.icon(
|
||||
// onPressed: () => Navigator.pop(context),
|
||||
// icon: const Icon(Icons.close, color: AppColors.errorColor),
|
||||
// label: Text(
|
||||
// LocaleKeys.cancel.tr(),
|
||||
// style: TextStyle(
|
||||
// color: AppColors.errorColor,
|
||||
// fontWeight: FontWeight.w500,
|
||||
// fontSize: 14
|
||||
// ),
|
||||
// ),
|
||||
// style: ElevatedButton.styleFrom(
|
||||
// backgroundColor: AppColors.secondaryLightRedColor,
|
||||
// elevation: 0,
|
||||
// padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
// shape: RoundedRectangleBorder(
|
||||
// borderRadius: BorderRadius.circular(12),
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
// const SizedBox(width: 12),
|
||||
// Expanded(
|
||||
// child: ElevatedButton.icon(
|
||||
// onPressed: () {
|
||||
// Navigator.pop(context, selectedTimes);
|
||||
// },
|
||||
// icon: const Icon(Icons.notifications_rounded),
|
||||
// label: Text(
|
||||
// LocaleKeys.setReminder.tr(),
|
||||
// style: TextStyle(
|
||||
// fontWeight: FontWeight.w500,
|
||||
// fontSize: 14
|
||||
// ),
|
||||
// ),
|
||||
// style: ElevatedButton.styleFrom(
|
||||
// backgroundColor: AppColors.successColor,
|
||||
// foregroundColor: AppColors.whiteColor,
|
||||
// elevation: 0,
|
||||
// padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
// shape: RoundedRectangleBorder(
|
||||
// borderRadius: BorderRadius.circular(12),
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
// ],
|
||||
// ),
|
||||
// const SizedBox(height: 30),
|
||||
// ],
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// Widget buildCircleCheckbox(String label) {
|
||||
// final bool isSelected = selectedTimes.contains(label);
|
||||
// return InkWell(
|
||||
// onTap: () {
|
||||
// setState(() {
|
||||
// if (isSelected) {
|
||||
// selectedTimes.remove(label);
|
||||
// } else {
|
||||
// selectedTimes.add(label);
|
||||
// }
|
||||
// });
|
||||
// },
|
||||
// borderRadius: BorderRadius.circular(25),
|
||||
// child: Padding(
|
||||
// padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
// child: Row(
|
||||
// children: [
|
||||
// // Custom circle checkbox
|
||||
// Container(
|
||||
// width: 15,
|
||||
// height: 15,
|
||||
// decoration: BoxDecoration(
|
||||
// shape: BoxShape.circle,
|
||||
// border: Border.all(
|
||||
// color: isSelected ? AppColors.spacerLineColor: AppColors.spacerLineColor,
|
||||
// width: 1,
|
||||
// ),
|
||||
// color: isSelected ? AppColors.errorColor: AppColors.transparent,
|
||||
// ),
|
||||
// ),
|
||||
// const SizedBox(width: 12),
|
||||
// // Label text
|
||||
// Text(
|
||||
// label,
|
||||
// style: const TextStyle(fontSize: 16, color: Colors.black87),
|
||||
// ),
|
||||
// ],
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
//
|
||||
//
|
||||
// void showCircleCheckboxDialog(BuildContext context) async {
|
||||
// final selected = await showDialog<List<String>>(
|
||||
// context: context,
|
||||
// builder: (context) => const ReminderTimerDialog(),
|
||||
// );
|
||||
//
|
||||
// if (selected != null && selected.isNotEmpty) {
|
||||
// ScaffoldMessenger.of(context).showSnackBar(
|
||||
// SnackBar(content: Text('Reminders set for: ${selected.join(', ')}')),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
Loading…
Reference in New Issue