@ -1,12 +1,18 @@
import ' dart:async ' ;
import ' dart:collection ' ;
import ' dart:convert ' ;
import ' dart:io ' ;
import ' dart:ui ' ;
import ' package:device_calendar/device_calendar.dart ' ;
import ' package:flutter/widgets.dart ' ;
import ' package:hmg_patient_app_new/core/dependencies.dart ' ;
import ' package:hmg_patient_app_new/core/utils/date_util.dart ' ;
import ' package:hmg_patient_app_new/extensions/string_extensions.dart ' ;
import ' package:hmg_patient_app_new/presentation/prescriptions/prescription_reminder_view.dart ' ;
import ' package:hmg_patient_app_new/services/dialog_service.dart ' ;
import ' package:hmg_patient_app_new/services/permission_service.dart ' ;
import ' package:hmg_patient_app_new/widgets/common_bottom_sheet.dart ' ;
import ' package:jiffy/jiffy.dart ' ;
import ' package:manage_calendar_events/manage_calendar_events.dart ' as ios ;
import ' package:permission_handler/permission_handler.dart ' ;
@ -49,94 +55,6 @@ class CalendarUtils {
/ / return _completer ! . future ;
/ / }
Future < Map < Permission , PermissionStatus > > requestPermissions ( ) async {
var permissionResults = [ Permission . calendarFullAccess ] . request ( ) ;
return permissionResults ;
}
/ / showReminderDialog ( BuildContext context , DateTime dateTime , String doctorName , String eventId , String appoDateFormatted , String appoTimeFormatted ,
/ / { required Function ( ) onSuccess , String ? title , String ? description , Function ( int ) ? onMultiDateSuccess , bool isMultiAllowed = false } ) async {
/ / if ( Platform . isAndroid ) {
/ / if ( await PermissionService . isCalendarPermissionEnabled ( ) ) {
/ / _showReminderDialog ( context , dateTime , doctorName , eventId , appoDateFormatted , appoTimeFormatted ,
/ / onSuccess: onSuccess , title: title , description: description , onMultiDateSuccess: onMultiDateSuccess , isMultiAllowed: isMultiAllowed ) ;
/ / } else {
/ / / / Utils . showPermissionConsentDialog ( context , TranslationBase . of ( context ) . calendarPermission , ( ) async {
/ / / / if ( await Permission . calendarFullAccess . request ( ) . isGranted ) {
/ / / / _showReminderDialog ( context , dateTime , doctorName , eventId , appoDateFormatted , appoTimeFormatted ,
/ / / / onSuccess: onSuccess , title: title , description: description , onMultiDateSuccess: onMultiDateSuccess , isMultiAllowed: isMultiAllowed ) ;
/ / / / }
/ / / / } ) ;
/ / }
/ / } else {
/ / if ( await Permission . calendarWriteOnly . request ( ) . isGranted ) {
/ / if ( await Permission . calendarFullAccess . request ( ) . isGranted ) {
/ / _showReminderDialog ( context , dateTime , doctorName , eventId , appoDateFormatted , appoTimeFormatted ,
/ / onSuccess: onSuccess , title: title , description: description , onMultiDateSuccess: onMultiDateSuccess , isMultiAllowed: isMultiAllowed ) ;
/ / }
/ / }
/ / }
/ / }
/ / Future < void > _showReminderDialog ( BuildContext providedContext , DateTime dateTime , String doctorName , String eventId , String appoDateFormatted , String appoTimeFormatted ,
/ / { required Function onSuccess , String ? title , String ? description , Function ( int ) ? onMultiDateSuccess , bool ? isMultiAllowed } ) async {
/ / return showDialog < void > (
/ / context: providedContext ,
/ / barrierDismissible: true , / / user must tap button !
/ / builder: ( BuildContext context ) {
/ / return Dialog (
/ / shape: RoundedRectangleBorder ( borderRadius: BorderRadius . circular ( 0.0 ) ) ,
/ / insetPadding: EdgeInsets . all ( 21 ) ,
/ / child: ReminderDialog (
/ / onClick: ( int i ) async {
/ / String text = " " ;
/ / if ( i = = 0 ) {
/ / / / Before 30 mints
/ / dateTime = Jiffy . parseFromDateTime ( dateTime ) . subtract ( minutes: 30 ) . dateTime ;
/ / text = " 30 minutes " ;
/ / / / dateTime . add ( new Duration ( minutes: - 30 ) ) ;
/ / } else if ( i = = 1 ) {
/ / / / Before 1 hour
/ / / / dateTime . add ( new Duration ( minutes: - 60 ) ) ;
/ / dateTime = Jiffy . parseFromDateTime ( dateTime ) . subtract ( hours: 1 ) . dateTime ;
/ / text = " 1 hours " ;
/ / } else if ( i = = 2 ) {
/ / / / Before 1 hour and 30 mints
/ / / / dateTime . add ( new Duration ( minutes: - 90 ) ) ;
/ / dateTime = Jiffy . parseFromDateTime ( dateTime ) . subtract ( hours: 1 , minutes: 30 ) . dateTime ;
/ / text = " 1 hours 30 minutes " ;
/ / } else if ( i = = 3 ) {
/ / / / Before 2 hours
/ / / / dateTime . add ( new Duration ( minutes: - 120 ) ) ;
/ / dateTime = Jiffy . parseFromDateTime ( dateTime ) . subtract ( hours: 2 ) . dateTime ;
/ / text = " 2 hours " ;
/ / }
/ / if ( ! isMultiAllowed ! ) {
/ / if ( onMultiDateSuccess = = null ) {
/ / CalendarUtils calendarUtils = await CalendarUtils . getInstance ( ) ;
/ / await calendarUtils . createOrUpdateEvent (
/ / title: title ? ? " TranslationBase.of(providedContext).reminderTitle " . needTranslation + " " + doctorName ,
/ / description: description ? ? " At " + appoDateFormatted + " " + appoTimeFormatted ,
/ / scheduleDateTime: dateTime ,
/ / eventId: eventId , location: ' ' ) ;
/ / onSuccess ( ) ;
/ / }
/ / } else {
/ / onMultiDateSuccess ! ( i ) ;
/ / }
/ / / / await _analytics . logEvent (
/ / / / name: name . trim ( ) . toLowerCase ( ) ,
/ / / / parameters: safeParameters ,
/ / / / ) ;
/ / / / todo @ sikander discuss analytics for reminder
/ / / / locator < GAnalytics > ( ) . appointment . appointment_reminder_time ( reminde_before: text ) ;
/ / } ,
/ / ) ,
/ / ) ;
/ / } ,
/ / ) ;
/ / }
static Future < CalendarUtils > getInstance ( ) async {
tzl . initializeTimeZones ( ) ;
if ( _completer ! = null ) {
@ -265,3 +183,134 @@ class CalendarUtils {
return await deviceCalendarPlugin . createCalendar ( calendarName , calendarColor: calendarColor , localAccountName: localAccountName ) ;
}
}
Future < Map < Permission , PermissionStatus > > requestPermissions ( ) async {
var permissionResults = [ Permission . calendarFullAccess ] . request ( ) ;
return permissionResults ;
}
showReminderBottomSheet ( BuildContext context , DateTime dateTime , String doctorName , String eventId , String appoDateFormatted , String appoTimeFormatted ,
{ required Function ( ) onSuccess , String ? title , String ? description , Function ( int ) ? onMultiDateSuccess , bool isMultiAllowed = false } ) async {
if ( Platform . isAndroid ) {
if ( await PermissionService . isCalendarPermissionEnabled ( ) ) {
_showReminderBottomSheet ( context , dateTime , doctorName , eventId , appoDateFormatted , appoTimeFormatted ,
onSuccess: onSuccess , title: title , description: description , onMultiDateSuccess: onMultiDateSuccess , isMultiAllowed: isMultiAllowed ) ;
} else {
/ / Utils . showPermissionConsentDialog ( context , TranslationBase . of ( context ) . calendarPermission , ( ) async {
/ / if ( await Permission . calendarFullAccess . request ( ) . isGranted ) {
/ / _showReminderDialog ( context , dateTime , doctorName , eventId , appoDateFormatted , appoTimeFormatted ,
/ / onSuccess: onSuccess , title: title , description: description , onMultiDateSuccess: onMultiDateSuccess , isMultiAllowed: isMultiAllowed ) ;
/ / }
/ / } ) ;
}
} else {
if ( await Permission . calendarWriteOnly . request ( ) . isGranted ) {
if ( await Permission . calendarFullAccess . request ( ) . isGranted ) {
_showReminderBottomSheet ( context , dateTime , doctorName , eventId , appoDateFormatted , appoTimeFormatted ,
onSuccess: onSuccess , title: title , description: description , onMultiDateSuccess: onMultiDateSuccess , isMultiAllowed: isMultiAllowed ) ;
}
}
}
}
Future < void > _showReminderBottomSheet ( BuildContext providedContext , DateTime dateTime , String doctorName , String eventId , String appoDateFormatted , String appoTimeFormatted ,
{ required Function onSuccess , String ? title , String ? description , Function ( int ) ? onMultiDateSuccess , bool ? isMultiAllowed } ) async {
showCommonBottomSheetWithoutHeight ( providedContext , title: " Set the timer of reminder " . needTranslation , child: PrescriptionReminderView (
setReminder: ( int value ) async {
if ( ! isMultiAllowed ! ) {
if ( onMultiDateSuccess = = null ) {
CalendarUtils calendarUtils = await CalendarUtils . getInstance ( ) ;
await calendarUtils . createOrUpdateEvent (
title: title ? ? " You have appointment with Dr. " . needTranslation + doctorName ,
description: description ? ? " At " + appoDateFormatted + " " + appoTimeFormatted ,
scheduleDateTime: dateTime ,
eventId: eventId ,
location: ' ' ) ;
onSuccess ( ) ;
}
} else {
onMultiDateSuccess ! ( value ) ;
}
} ,
) , callBackFunc: ( ) { } , isFullScreen: false ) ;
}
setCalender ( BuildContext context ,
{ required String eventId , required int selectedMinutes , int ? frequencyNumber , required int days , required String orderDate , required String itemDescriptionN , required String route } ) async {
DateTime actualDate = DateTime ( DateTime . now ( ) . year , DateTime . now ( ) . month , DateTime . now ( ) . day , 8 , 0 ) ;
frequencyNumber ? ? = 2 ; / / Some time frequency number is null so by default will be 2
int remainingDays = days - ( Jiffy . parseFromDateTime ( DateTime . now ( ) ) . diff ( Jiffy . parseFromDateTime ( DateUtil . convertStringToDate ( orderDate ) ) , unit: Unit . day ) as int ) ;
if ( remainingDays . isNegative ) {
getIt . get < DialogService > ( ) . showErrorBottomSheet ( message: " Prescription date has been already passed you can not add a reminder for this prescription. " ) ;
return ;
}
CalendarUtils calendarUtils = await CalendarUtils . getInstance ( ) ;
try {
for ( int i = 0 ; i < remainingDays ; i + + ) {
/ / event for number of days .
for ( int j = 0 ; j < frequencyNumber ; j + + ) {
/ / event for number of times per day .
if ( j ! = 0 ) {
actualDate . add ( new Duration ( hours: 8 ) ) ; / / 8 hours addition for daily dose .
}
/ / Time subtraction from actual reminder time . like before 30 , or 1 hour .
actualDate = Jiffy . parseFromDateTime ( actualDate ) . subtract ( minutes: selectedMinutes ) . dateTime ;
calendarUtils . createOrUpdateEvent (
title: " $ itemDescriptionN } Prescription Reminder " ,
description: " $ itemDescriptionN $ frequencyNumber $ route " ,
scheduleDateTime: actualDate ,
eventId: eventId + ( i . toString ( ) + j . toString ( ) ) ,
location: ' ' , / / event id with varitions
) ;
actualDate = DateTime ( actualDate . year , actualDate . month , actualDate . day , 8 , 0 ) ;
}
actualDate = Jiffy . parseFromDateTime ( actualDate ) . add ( days: 1 ) . dateTime ;
}
} catch ( ex ) {
getIt . get < DialogService > ( ) . showErrorBottomSheet ( message: " catch: $ ex " ) ;
}
}
Future < void > checkAndRemove ( hasReminder , { bool delete = false , String itemDescriptionN = " " } ) async {
final ios . CalendarPlugin _myPlugin = ios . CalendarPlugin ( ) ;
CalendarUtils calendarUtils = await CalendarUtils . getInstance ( ) ;
DateTime startEventsDate = Jiffy . parseFromDateTime ( DateTime . now ( ) ) . subtract ( days: 30 ) . dateTime ;
DateTime endEventsDate = Jiffy . parseFromDateTime ( DateTime . now ( ) ) . add ( days: 120 ) . dateTime ;
RetrieveEventsParams params = RetrieveEventsParams ( startDate: startEventsDate , endDate: endEventsDate ) ;
if ( calendarUtils . calendars ! = null ) {
if ( Platform . isAndroid ) {
await processEvents ( calendarUtils . calendars , calendarUtils , params , delete , itemDescriptionN , hasReminder ) ;
} else {
List < ios . Calendar > ? iosCalendars = await _myPlugin . getCalendars ( ) ;
if ( iosCalendars ! = null ) {
await processEvents ( iosCalendars . map ( ( cal ) = > Calendar ( id: cal . id , name: cal . name , accountName: cal . accountName ) ) . toList ( ) , calendarUtils , params , delete , itemDescriptionN , hasReminder ) ;
}
}
}
}
Future < void > processEvents ( List < Calendar > calendars , calendarUtils , params , delete , String itemDescriptionN , hasReminder ) async {
for ( var calendar in calendars ) {
Result < UnmodifiableListView < Event > > events = await calendarUtils . retrieveEvents ( calendar . id ! , params ) ;
for ( var event in events . data ! ) {
if ( event . title ! . contains ( itemDescriptionN ) ) {
if ( delete ) {
await calendarUtils . deleteEvent ( calendar , event ) ;
/ / AppToast . showSuccessToast ( message: TranslationBase . of ( context ) . reminderCancelSuccess ) ;
hasReminder = false ;
} else {
hasReminder = false ;
/ / setState ( ( ) {
/ / hasReminder = true ;
/ / } ) ;
}
}
}
}
}