diff --git a/lib/features/prescriptions/prescriptions_repo.dart b/lib/features/prescriptions/prescriptions_repo.dart index d3f329a4..2681b2da 100644 --- a/lib/features/prescriptions/prescriptions_repo.dart +++ b/lib/features/prescriptions/prescriptions_repo.dart @@ -247,6 +247,7 @@ class PrescriptionsRepoImp implements PrescriptionsRepo { "AppointmentNo": appointmentNo, "DischargeID": dischargeID, "ProjectID": projectID, + "Channel": 3, }; try { diff --git a/lib/features/prescriptions/prescriptions_view_model.dart b/lib/features/prescriptions/prescriptions_view_model.dart index 9c0285eb..ad44d33f 100644 --- a/lib/features/prescriptions/prescriptions_view_model.dart +++ b/lib/features/prescriptions/prescriptions_view_model.dart @@ -21,6 +21,7 @@ import 'package:hmg_patient_app_new/services/navigation_service.dart'; import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart'; import 'package:hmg_patient_app_new/widgets/map/map_utility_screen.dart'; import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart'; +import 'package:hmg_patient_app_new/services/prescription_reminder_prefs_service.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; @@ -78,9 +79,22 @@ class PrescriptionsViewModel extends ChangeNotifier { } Future checkIfReminderExistForPrescription(int index) async { - prescriptionDetailsList[index].hasReminder = await CalenderUtilsNew.instance.checkIfEventExist(prescriptionDetailsList[index].itemID?.toString() ?? ""); - // notifyListeners(); - return prescriptionDetailsList[index].hasReminder ?? false; + final item = prescriptionDetailsList[index]; + // Check device calendar + final calendarHasEvent = await CalenderUtilsNew.instance.checkIfEventExist(item.itemID?.toString() ?? ""); + prescriptionDetailsList[index].hasReminder = calendarHasEvent; + + // Sync the SharedPreferences status to match the calendar reality + if (item.appointmentNo != null && item.itemID != null && item.projectID != null) { + await PrescriptionReminderPrefsService.instance.setReminderStatus( + appointmentNo: item.appointmentNo!, + itemDescription: item.itemDescription ?? '', + itemID: item.itemID!, + projectID: item.projectID!, + hasReminder: calendarHasEvent, + ); + } + return calendarHasEvent; } setPrescriptionsDetailsLoading() { @@ -94,6 +108,18 @@ class PrescriptionsViewModel extends ChangeNotifier { if (index != -1) { prescriptionDetailsList[index].hasReminder = value; notifyListeners(); + + // Persist to SharedPreferences + final target = prescriptionDetailsList[index]; + if (target.appointmentNo != null && target.itemID != null && target.projectID != null) { + PrescriptionReminderPrefsService.instance.setReminderStatus( + appointmentNo: target.appointmentNo!, + itemDescription: target.itemDescription ?? '', + itemID: target.itemID!, + projectID: target.projectID!, + hasReminder: value, + ); + } } } @@ -172,6 +198,23 @@ class PrescriptionsViewModel extends ChangeNotifier { // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {}); } else if (apiResponse.messageStatus == 1) { prescriptionDetailsList = apiResponse.data!; + + // Always load reminder status from SharedPreferences first + final savedReminders = await PrescriptionReminderPrefsService.instance.getAllReminders(); + for (int i = 0; i < prescriptionDetailsList.length; i++) { + final item = prescriptionDetailsList[i]; + if (item.appointmentNo != null && item.itemID != null && item.projectID != null) { + final key = '${item.appointmentNo}_${item.itemID}_${item.projectID}'; + try { + final saved = savedReminders.firstWhere((e) => e.uniqueKey == key); + prescriptionDetailsList[i].hasReminder = saved.hasReminder; + } catch (_) { + // No entry found — keep default false + } + } + } + + // If calendar permissions are granted, verify against actual calendar events if (await Permission.calendarFullAccess.isGranted && await Permission.calendarWriteOnly.isGranted) { prescriptionDetailsList.forEach((element) async { await checkIfReminderExistForPrescription(prescriptionDetailsList.indexOf(element)); diff --git a/lib/presentation/prescriptions/prescription_item_view.dart b/lib/presentation/prescriptions/prescription_item_view.dart index 5e675ff1..aaac5b3c 100644 --- a/lib/presentation/prescriptions/prescription_item_view.dart +++ b/lib/presentation/prescriptions/prescription_item_view.dart @@ -113,7 +113,6 @@ class PrescriptionItemView extends StatelessWidget { // value: prescriptionVM.prescriptionDetailsList[index].hasReminder ?? false, onChanged: (newValue) async { CalenderUtilsNew calender = CalenderUtilsNew.instance; - if (await prescriptionVM.checkIfReminderExistForPrescription(index)) { prescriptionVM.prescriptionDetailsList[index].hasReminder = true; diff --git a/lib/services/prescription_reminder_prefs_service.dart b/lib/services/prescription_reminder_prefs_service.dart new file mode 100644 index 00000000..6c732e60 --- /dev/null +++ b/lib/services/prescription_reminder_prefs_service.dart @@ -0,0 +1,142 @@ +import 'dart:convert'; +import 'package:shared_preferences/shared_preferences.dart'; + +/// Represents the reminder status for a single prescription item, +/// identified by AppointmentNo, ItemDescription, ItemID & ProjectID. +class PrescriptionReminderEntry { + final num appointmentNo; + final String itemDescription; + final num itemID; + final num projectID; + bool hasReminder; + + PrescriptionReminderEntry({ + required this.appointmentNo, + required this.itemDescription, + required this.itemID, + required this.projectID, + required this.hasReminder, + }); + + factory PrescriptionReminderEntry.fromJson(Map json) { + return PrescriptionReminderEntry( + appointmentNo: json['appointmentNo'] ?? 0, + itemDescription: json['itemDescription'] ?? '', + itemID: json['itemID'] ?? 0, + projectID: json['projectID'] ?? 0, + hasReminder: json['hasReminder'] ?? false, + ); + } + + Map toJson() => { + 'appointmentNo': appointmentNo, + 'itemDescription': itemDescription, + 'itemID': itemID, + 'projectID': projectID, + 'hasReminder': hasReminder, + }; + + /// Unique key to identify this prescription item. + String get uniqueKey => '${appointmentNo}_${itemID}_${projectID}'; +} + +/// Service to manage prescription reminder statuses in SharedPreferences. +/// All entries are stored under the single key [prescriptionRemindersKey]. +class PrescriptionReminderPrefsService { + static const String prescriptionRemindersKey = 'PrescriptionReminders'; + + static PrescriptionReminderPrefsService? _instance; + static PrescriptionReminderPrefsService get instance { + _instance ??= PrescriptionReminderPrefsService._(); + return _instance!; + } + + PrescriptionReminderPrefsService._(); + + /// Reads all reminder entries from SharedPreferences. + Future> getAllReminders() async { + final prefs = await SharedPreferences.getInstance(); + final jsonString = prefs.getString(prescriptionRemindersKey); + if (jsonString == null || jsonString.isEmpty) return []; + try { + final List jsonList = json.decode(jsonString); + return jsonList + .map((e) => PrescriptionReminderEntry.fromJson(e as Map)) + .toList(); + } catch (_) { + return []; + } + } + + /// Saves the full list of reminder entries to SharedPreferences. + Future _saveAllReminders(List entries) async { + final prefs = await SharedPreferences.getInstance(); + final jsonString = json.encode(entries.map((e) => e.toJson()).toList()); + await prefs.setString(prescriptionRemindersKey, jsonString); + } + + /// Returns the reminder status for a specific item. + /// Returns null if no entry exists for this item. + Future getReminderStatus({ + required num appointmentNo, + required num itemID, + required num projectID, + }) async { + final entries = await getAllReminders(); + final key = '${appointmentNo}_${itemID}_${projectID}'; + try { + final entry = entries.firstWhere((e) => e.uniqueKey == key); + return entry.hasReminder; + } catch (_) { + return null; + } + } + + /// Saves or updates the reminder status for a specific prescription item. + Future setReminderStatus({ + required num appointmentNo, + required String itemDescription, + required num itemID, + required num projectID, + required bool hasReminder, + }) async { + final entries = await getAllReminders(); + final key = '${appointmentNo}_${itemID}_${projectID}'; + final existingIndex = entries.indexWhere((e) => e.uniqueKey == key); + + if (existingIndex != -1) { + // Update existing entry + entries[existingIndex].hasReminder = hasReminder; + } else { + // Add new entry + entries.add(PrescriptionReminderEntry( + appointmentNo: appointmentNo, + itemDescription: itemDescription, + itemID: itemID, + projectID: projectID, + hasReminder: hasReminder, + )); + } + + await _saveAllReminders(entries); + } + + /// Removes a specific reminder entry from SharedPreferences. + Future removeReminderEntry({ + required num appointmentNo, + required num itemID, + required num projectID, + }) async { + final entries = await getAllReminders(); + final key = '${appointmentNo}_${itemID}_${projectID}'; + entries.removeWhere((e) => e.uniqueKey == key); + await _saveAllReminders(entries); + } + + /// Clears all prescription reminder entries from SharedPreferences. + Future clearAllReminders() async { + final prefs = await SharedPreferences.getInstance(); + await prefs.remove(prescriptionRemindersKey); + } +} +