Added shared preferences check in the prescription reminders

pull/293/head
haroon amjad 12 hours ago
parent 91f38203f2
commit 16f3298f37

@ -247,6 +247,7 @@ class PrescriptionsRepoImp implements PrescriptionsRepo {
"AppointmentNo": appointmentNo, "AppointmentNo": appointmentNo,
"DischargeID": dischargeID, "DischargeID": dischargeID,
"ProjectID": projectID, "ProjectID": projectID,
"Channel": 3,
}; };
try { try {

@ -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/loader/bottomsheet_loader.dart';
import 'package:hmg_patient_app_new/widgets/map/map_utility_screen.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/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:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -78,9 +79,22 @@ class PrescriptionsViewModel extends ChangeNotifier {
} }
Future<bool> checkIfReminderExistForPrescription(int index) async { Future<bool> checkIfReminderExistForPrescription(int index) async {
prescriptionDetailsList[index].hasReminder = await CalenderUtilsNew.instance.checkIfEventExist(prescriptionDetailsList[index].itemID?.toString() ?? ""); final item = prescriptionDetailsList[index];
// notifyListeners(); // Check device calendar
return prescriptionDetailsList[index].hasReminder ?? false; 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() { setPrescriptionsDetailsLoading() {
@ -94,6 +108,18 @@ class PrescriptionsViewModel extends ChangeNotifier {
if (index != -1) { if (index != -1) {
prescriptionDetailsList[index].hasReminder = value; prescriptionDetailsList[index].hasReminder = value;
notifyListeners(); 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: () {}); // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
} else if (apiResponse.messageStatus == 1) { } else if (apiResponse.messageStatus == 1) {
prescriptionDetailsList = apiResponse.data!; 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) { if (await Permission.calendarFullAccess.isGranted && await Permission.calendarWriteOnly.isGranted) {
prescriptionDetailsList.forEach((element) async { prescriptionDetailsList.forEach((element) async {
await checkIfReminderExistForPrescription(prescriptionDetailsList.indexOf(element)); await checkIfReminderExistForPrescription(prescriptionDetailsList.indexOf(element));

@ -113,7 +113,6 @@ class PrescriptionItemView extends StatelessWidget {
// value: prescriptionVM.prescriptionDetailsList[index].hasReminder ?? false, // value: prescriptionVM.prescriptionDetailsList[index].hasReminder ?? false,
onChanged: (newValue) async { onChanged: (newValue) async {
CalenderUtilsNew calender = CalenderUtilsNew.instance; CalenderUtilsNew calender = CalenderUtilsNew.instance;
if (await prescriptionVM.checkIfReminderExistForPrescription(index)) { if (await prescriptionVM.checkIfReminderExistForPrescription(index)) {
prescriptionVM.prescriptionDetailsList[index].hasReminder = true; prescriptionVM.prescriptionDetailsList[index].hasReminder = true;

@ -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<String, dynamic> json) {
return PrescriptionReminderEntry(
appointmentNo: json['appointmentNo'] ?? 0,
itemDescription: json['itemDescription'] ?? '',
itemID: json['itemID'] ?? 0,
projectID: json['projectID'] ?? 0,
hasReminder: json['hasReminder'] ?? false,
);
}
Map<String, dynamic> 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<List<PrescriptionReminderEntry>> getAllReminders() async {
final prefs = await SharedPreferences.getInstance();
final jsonString = prefs.getString(prescriptionRemindersKey);
if (jsonString == null || jsonString.isEmpty) return [];
try {
final List<dynamic> jsonList = json.decode(jsonString);
return jsonList
.map((e) => PrescriptionReminderEntry.fromJson(e as Map<String, dynamic>))
.toList();
} catch (_) {
return [];
}
}
/// Saves the full list of reminder entries to SharedPreferences.
Future<void> _saveAllReminders(List<PrescriptionReminderEntry> 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<bool?> 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<void> 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<void> 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<void> clearAllReminders() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(prescriptionRemindersKey);
}
}
Loading…
Cancel
Save