import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ErrorCode , UserExtraDataName } from '@ulangi/ulangi-common/enums' ; import { ErrorBag } from '@ulangi/ulangi-common/interfaces' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableNotificationStore , ObservableReminderScreen , } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import * as _ from 'lodash' ; import { DialogDelegate } from '../dialog/DialogDelegate' ; import { ReminderDelegate } from './ReminderDelegate' ; @ boundClass export class ReminderScreenDelegate { private eventBus : EventBus ; private notificationStore : ObservableNotificationStore ; private observableScreen : ObservableReminderScreen ; private reminderDelegate : ReminderDelegate ; private dialogDelegate : DialogDelegate ; public constructor ( eventBus , notificationStore , observableScreen , reminderDelegate , dialogDelegate ) { this . eventBus = eventBus ; this . notificationStore = notificationStore ; this . observableScreen = observableScreen ; this . reminderDelegate = reminderDelegate ; this . dialogDelegate = dialogDelegate ; } public toggle ( ) { if ( this . observableScreen . reminderSettings . reminderEnabled === true ) { if ( this . notificationStore . hasPermission === false ) { this . checkAndRequestPermission ( ) ; } else { this . observableScreen . reminderSettings . reminderEnabled = false ; } } else { this . observableScreen . reminderSettings . reminderEnabled = true ; this . checkAndRequestPermission ( ) ; } } public showTimePicker ( ) { this . observableScreen . shouldShowTimePicker = true ; } public handleTimePicked ( date ) { this . observableScreen . reminderSettings . hours = date . getHours ( ) ; this . observableScreen . reminderSettings . minutes = date . getMinutes ( ) ; this . observableScreen . shouldShowTimePicker = false ; } public handleTimeCanceled ( ) { this . observableScreen . shouldShowTimePicker = false ; } public save ( ) { this . eventBus . pubsub ( createAction ( ActionType . USER__EDIT , { user : { extraData : [ { dataName : UserExtraDataName . GLOBAL_REMINDER , dataValue : { reminderEnabled : this . observableScreen . reminderSettings . reminderEnabled , hours : this . observableScreen . reminderSettings . hours , minutes : this . observableScreen . reminderSettings . minutes , } , } , ] , } , } ) , group ( on ( ActionType . USER__EDITING , ( ) : void => { this . dialogDelegate . showSavingDialog ( ) ; } ) , once ( ActionType . USER__EDIT_SUCCEEDED , ( ) : void => { this . dialogDelegate . showSaveSucceededDialog ( ) ; } ) , once ( ActionType . USER__EDIT_FAILED , ( errorBag ) : void => { this . dialogDelegate . showSaveFailedDialog ( errorBag ) ; } ) ) ) ; } private checkAndRequestPermission ( ) { this . reminderDelegate . checkPermission ( { onChecking : ( ) : void => { this . showCheckingPermissionDialog ( ) ; } , onCheckSucceeded : ( hasPermission ) : void => { if ( hasPermission === false ) { this . reminderDelegate . requestPermission ( { onRequesting : _ . noop , onRequestSucceeded : ( ) : void => { this . reminderDelegate . checkPermission ( ) ; this . dialogDelegate . dismiss ( ) ; } , onRequestFailed : this . showRequestPermissionFailedDialog , } ) ; } else { this . dialogDelegate . dismiss ( ) ; } } , onCheckFailed : this . showRequestPermissionFailedDialog , } ) ; } private showCheckingPermissionDialog ( ) { this . dialogDelegate . show ( { message : 'Checking for permission...' , } ) ; } private showRequestPermissionFailedDialog ( errorBag ) { this . dialogDelegate . showFailedDialog ( { errorCode : errorBag . errorCode === ErrorCode . GENERAL__FAILED_TO_GRANT_PERMISSION ? ErrorCode . REMINDER__PERMISSION_REQUIRED : errorBag . errorCode , ... errorBag , } , { title : 'REQUEST PERMISSION FAILED' , } ) ; } }