import { SpacedRepetitionScheduler , WritingScheduler , } from '@ulangi/ulangi-common/core' ; import { ActivityState , VocabularyStatus } from '@ulangi/ulangi-common/enums' ; import { Vocabulary } from '@ulangi/ulangi-common/interfaces' ; import { ObservableVocabulary , ObservableVocabularyDetailScreen , } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import { config } from '../../constants/config' ; import { DialogDelegate } from '../dialog/dialogDelegate' ; import { SpacedRepetitionSettingsDelegate } from '../spaced-repetition/SpacedRepetitionSettingsDelegate' ; import { VocabularyActionMenuDelegate } from '../vocabulary/VocabularyActionMenuDelegate' ; import { WritingSettingsDelegate } from '../writing/WritingSettingsDelegate' ; import { SpeakDelegate } from './SpeakDelegate' ; @ boundClass export class VocabularyDetailScreenDelegate { private spacedRepetitionScheduler = new SpacedRepetitionScheduler ( ) ; private writingScheduler = new WritingScheduler ( ) ; private observableScreen : ObservableVocabularyDetailScreen ; private vocabularyActionMenuDelegate : VocabularyActionMenuDelegate ; private speakDelegate : SpeakDelegate ; private spacedRepetitionSettingsDelegate : SpacedRepetitionSettingsDelegate ; private writingSettingsDelegate : WritingSettingsDelegate ; private dialogDelegate : DialogDelegate ; public constructor ( observableScreen , vocabularyActionMenuDelegate , speakDelegate , spacedRepetitionSettingsDelegate , writingSettingsDelegate , dialogDelegate ) { this . observableScreen = observableScreen ; this . vocabularyActionMenuDelegate = vocabularyActionMenuDelegate ; this . speakDelegate = speakDelegate ; this . spacedRepetitionSettingsDelegate = spacedRepetitionSettingsDelegate ; this . writingSettingsDelegate = writingSettingsDelegate ; this . dialogDelegate = dialogDelegate ; } public changeStrokeOrderForm ( form ) { this . observableScreen . strokeOrderForm = form ; } public synthesizeAndSpeak ( text , languageCode ) { this . speakDelegate . synthesize ( text , languageCode , { onSynthesizing : ( ) : void => { this . observableScreen . speakState . set ( ActivityState . ACTIVE ) ; } , onSynthesizeSucceeded : ( filePath ) : void => { this . speak ( filePath ) ; } , onSynthesizeFailed : ( errorBag ) : void => { this . observableScreen . speakState . set ( ActivityState . ERROR ) ; this . dialogDelegate . showFailedDialog ( errorBag ) ; } , } ) ; } public showVocabularyActionMenu ( vocabulary ) { this . vocabularyActionMenuDelegate . show ( vocabulary ) ; } public calculateNextSpacedRepetitionReview ( vocabulary ) { const { initialInterval , } = this . spacedRepetitionSettingsDelegate . getCurrentSettings ( ) ; if ( vocabulary . vocabularyStatus !== VocabularyStatus . ACTIVE ) { return 'not active' ; } else { return this . spacedRepetitionScheduler . getReviewTimeFromNow ( initialInterval , vocabulary , config . spacedRepetition . maxLevel ) ; } } public calculateNextWritingReview ( vocabulary ) { const { initialInterval , } = this . writingSettingsDelegate . getCurrentSettings ( ) ; if ( vocabulary . vocabularyStatus !== VocabularyStatus . ACTIVE ) { return 'not active' ; } else { return this . writingScheduler . getReviewTimeFromNow ( initialInterval , vocabulary , config . writing . maxLevel ) ; } } private speak ( filePath ) { this . speakDelegate . speak ( filePath , { onSpeaking : ( ) : void => { this . observableScreen . speakState . set ( ActivityState . ACTIVE ) ; } , onSpeakSucceeded : ( ) : void => { this . observableScreen . speakState . set ( ActivityState . INACTIVE ) ; } , onSpeakFailed : ( ) : void => { this . observableScreen . speakState . set ( ActivityState . INACTIVE ) ; } , } ) ; } }