2020-11-07 17:59:37 +02:00
import Logger from '@joplin/lib/Logger' ;
import { Notification } from '@joplin/lib/models/Alarm' ;
2020-10-09 19:35:46 +02:00
2021-05-19 23:26:42 +02:00
const ReactNativeAN = require ( 'joplin-rn-alarm-notification' ) . default ;
2017-11-28 00:50:46 +02:00
2020-10-09 19:35:46 +02:00
export default class AlarmServiceDriver {
2020-11-12 21:13:28 +02:00
private logger_ : Logger ;
2019-01-10 20:49:26 +02:00
2020-11-12 21:13:28 +02:00
constructor ( logger : Logger ) {
2020-10-16 17:26:19 +02:00
this . logger_ = logger ;
2018-12-20 15:52:56 +02:00
}
2020-10-16 17:26:19 +02:00
public hasPersistentNotifications() {
2017-11-28 02:22:38 +02:00
return true ;
}
2020-10-16 17:26:19 +02:00
public notificationIsSet() {
2019-07-29 15:43:53 +02:00
throw new Error ( 'Available only for non-persistent alarms' ) ;
2017-11-28 02:22:38 +02:00
}
2020-11-12 21:13:28 +02:00
public async clearNotification ( id : number ) {
2020-10-16 17:26:19 +02:00
const alarm = await this . alarmByJoplinNotificationId ( id ) ;
if ( ! alarm ) return ;
this . logger_ . info ( 'AlarmServiceDriver: Deleting alarm:' , alarm ) ;
await ReactNativeAN . deleteAlarm ( alarm . id ) ;
}
// Returns -1 if could not be found
2020-11-12 21:13:28 +02:00
private alarmJoplinAlarmId ( alarm : any ) : number {
2021-05-19 23:26:42 +02:00
if ( ! alarm . data || ! alarm . data . joplinNotificationId ) {
return - 1 ;
} else {
return alarm . data . joplinNotificationId ;
}
2017-11-28 00:50:46 +02:00
}
2019-07-29 15:43:53 +02:00
2020-11-12 21:13:28 +02:00
private async alarmByJoplinNotificationId ( joplinNotificationId : number ) {
const alarms : any [ ] = await ReactNativeAN . getScheduledAlarms ( ) ;
2020-10-16 17:26:19 +02:00
for ( const alarm of alarms ) {
const id = this . alarmJoplinAlarmId ( alarm ) ;
if ( id === joplinNotificationId ) return alarm ;
}
this . logger_ . warn ( 'AlarmServiceDriver: Could not find alarm that matches Joplin notification ID. It could be because it has already been triggered:' , joplinNotificationId ) ;
return null ;
}
2020-11-12 21:13:28 +02:00
public async scheduleNotification ( notification : Notification ) {
2020-10-16 17:26:19 +02:00
const alarmNotifData = {
title : notification.title ,
message : notification.body ? notification . body : '-' , // Required
channel : 'net.cozic.joplin.notification' ,
2021-05-19 23:26:42 +02:00
small_icon : 'ic_launcher_foreground' , // Android requires the icon to be transparent
color : 'blue' ,
data : {
2021-11-27 18:22:30 +02:00
joplinNotificationId : notification.id ,
2021-05-19 23:26:42 +02:00
noteId : notification.noteId ,
} ,
2019-01-10 20:49:26 +02:00
} ;
2020-10-16 17:26:19 +02:00
// ReactNativeAN expects a string as a date and it seems this utility
// function converts it to the right format.
const fireDate = ReactNativeAN . parseDate ( notification . date ) ;
const alarm = await ReactNativeAN . scheduleAlarm ( { . . . alarmNotifData , fire_date : fireDate } ) ;
this . logger_ . info ( 'AlarmServiceDriver: Created new alarm:' , alarm ) ;
2017-11-28 00:50:46 +02:00
}
}