import { ActionType } from '@ulangi/ulangi-action' ; import { ButtonSize , DiscoverListType , ErrorCode , ScreenName , } from '@ulangi/ulangi-common/enums' ; import { ButtonStyles , ErrorBag , PublicVocabulary , TranslationWithLanguages , } from '@ulangi/ulangi-common/interfaces' ; import { EventBus , on } from '@ulangi/ulangi-event' ; import { ObservableDiscoverScreen , ObservablePublicSet , ObservableSetStore , } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import { runInAction } from 'mobx' ; import { RemoteLogger } from '../../RemoteLogger' ; import { LightBoxDialogIds } from '../../constants/ids/LightBoxDialogIds' ; import { fullRoundedButtonStyles } from '../../styles/FullRoundedButtonStyles' ; import { DialogDelegate } from '../dialog/DialogDelegate' ; import { NavigatorDelegate } from '../navigator/NavigatorDelegate' ; import { TranslationListDelegate } from '../translation/TranslationListDelegate' ; import { AddVocabularyDelegate } from './AddVocabularyDelegate' ; import { PublicSetListDelegate } from './PublicSetListDelegate' ; import { PublicVocabularyActionMenuDelegate } from './PublicVocabularyActionMenuDelegate' ; import { PublicVocabularyListDelegate } from './PublicVocabularyListDelegate' ; import { TranslationActionMenuDelegate } from './TranslationActionMenuDelegate' ; @ boundClass export class DiscoverScreenDelegate { private eventBus : EventBus ; private setStore : ObservableSetStore ; private observableScreen : ObservableDiscoverScreen ; private addVocabularyDelegate : AddVocabularyDelegate ; private publicSetListDelegate : PublicSetListDelegate ; private publicVocabularyListDelegate : PublicVocabularyListDelegate ; private publicVocabularyActionMenuDelegate : PublicVocabularyActionMenuDelegate ; private translationListDelegate : TranslationListDelegate ; private translationActionMenuDelegate : TranslationActionMenuDelegate ; private dialogDelegate : DialogDelegate ; private navigatorDelegate : NavigatorDelegate ; public constructor ( eventBus , setStore , observableScreen , addVocabularyDelegate , publicSetListDelegate , publicVocabularyListDelegate , publicVocabularyActionMenuDelegate , translationListDelegate , translationActionMenuDelegate , dialogDelegate , navigatorDelegate ) { this . eventBus = eventBus ; this . setStore = setStore ; this . observableScreen = observableScreen ; this . addVocabularyDelegate = addVocabularyDelegate ; this . publicSetListDelegate = publicSetListDelegate ; this . publicVocabularyListDelegate = publicVocabularyListDelegate ; this . publicVocabularyActionMenuDelegate = publicVocabularyActionMenuDelegate ; this . translationListDelegate = translationListDelegate ; this . translationActionMenuDelegate = translationActionMenuDelegate ; this . dialogDelegate = dialogDelegate ; this . navigatorDelegate = navigatorDelegate ; } public handleInputEnded ( ) { this . clearAllList ( ) ; this . setListTypeByCurrentInput ( ) ; this . refreshCurrentList ( ) ; } public setInputAndRefresh ( term ) { this . observableScreen . searchInput . set ( term ) ; this . setListTypeByCurrentInput ( ) ; this . refreshCurrentList ( ) ; } public prepareAndSearch ( ) { if ( this . observableScreen . listType . get ( ) === DiscoverListType . TRANSLATION_AND_PUBLIC_VOCABULARY_LIST ) { this . translationListDelegate . translateBidrection ( this . observableScreen . searchInput . get ( ) ) ; this . publicVocabularyListDelegate . prepareAndSearch ( this . observableScreen . searchInput . get ( ) ) ; } else if ( this . observableScreen . listType . get ( ) === DiscoverListType . PREMADE_SET_LIST || this . observableScreen . listType . get ( ) === DiscoverListType . PUBLIC_SET_LIST ) { this . publicSetListDelegate . prepareAndSearch ( this . observableScreen . searchInput . get ( ) ) ; } } public setListTypeByCurrentInput ( ) { let listType ; if ( this . observableScreen . searchInput . get ( ) === '' ) { if ( this . setStore . existingCurrentSet . shouldShowPremadeFlashcards === true ) { listType = DiscoverListType . PREMADE_SET_LIST ; } else { listType = null ; } } else { listType = DiscoverListType . TRANSLATION_AND_PUBLIC_VOCABULARY_LIST ; } this . observableScreen . listType . set ( listType ) ; } public setListTypeAndRefresh ( listType ) { this . observableScreen . listType . set ( listType ) ; this . refreshCurrentList ( ) ; } public focusSearchInput ( ) { this . observableScreen . shouldFocusSearchInput . set ( true ) ; } public getPublicSetCount ( ) { this . publicSetListDelegate . getPublicSetCount ( ) ; } public searchPublicSets ( ) { this . publicSetListDelegate . search ( ) ; } public searchPublicVocabulary ( ) { this . publicVocabularyListDelegate . search ( ) ; } public clearSearchInput ( ) { this . clearAllList ( ) ; runInAction ( ( ) : void => { this . observableScreen . searchInput . set ( '' ) ; this . setListTypeByCurrentInput ( ) ; this . refreshCurrentList ( ) ; this . observableScreen . searchInputAutoFocus . set ( true ) ; } ) ; } public clearAllList ( ) { this . publicSetListDelegate . clearSearch ( ) ; this . publicVocabularyListDelegate . clearSearch ( ) ; this . translationListDelegate . clearBidirectionalTranslations ( ) ; } public refreshCurrentList ( ) { if ( this . observableScreen . listType . get ( ) === DiscoverListType . TRANSLATION_AND_PUBLIC_VOCABULARY_LIST ) { this . publicVocabularyListDelegate . refresh ( this . observableScreen . searchInput . get ( ) ) ; if ( this . translationListDelegate . canTranslate ( ) === false ) { this . translationListDelegate . clearTranslations ( ) ; } else { this . translationListDelegate . refreshBidirectionalTranslations ( this . observableScreen . searchInput . get ( ) ) ; } } else if ( this . observableScreen . listType . get ( ) === DiscoverListType . PREMADE_SET_LIST || this . observableScreen . listType . get ( ) === DiscoverListType . PUBLIC_SET_LIST ) { this . publicSetListDelegate . refresh ( this . observableScreen . searchInput . get ( ) ) ; } } public addVocabularyFromPublicVocabulary ( publicVocabulary , checkDuplicate = true ) { this . addVocabularyDelegate . addVocabularyFromPublicVocabulary ( publicVocabulary , undefined , checkDuplicate , { onAdding : this . showAddingDialog , onAddSucceeded : this . showAddSucceededDialog , onAddFailed : ( errorBag ) : void => { this . showAddFailedDialogWithRetry ( errorBag , ( shouldCheckDuplicate ) : void => { this . addVocabularyFromPublicVocabulary ( publicVocabulary , shouldCheckDuplicate ) ; } ) ; } , } ) ; } public addVocabularyFromTranslation ( translation , checkDuplicate = true ) { this . addVocabularyDelegate . addVocabularyFromTranslation ( translation , checkDuplicate , { onAdding : this . showAddingDialog , onAddSucceeded : this . showAddSucceededDialog , onAddFailed : ( errorBag ) : void => { this . showAddFailedDialogWithRetry ( errorBag , ( shouldCheckDuplicate ) : void => { this . addVocabularyFromTranslation ( translation , shouldCheckDuplicate ) ; } ) ; } , } ) ; } public showSetDetailModal ( publicSet ) { this . navigatorDelegate . push ( ScreenName . PUBLIC_SET_DETAIL_SCREEN , { publicSet : { ... publicSet , vocabularyList : publicSet . vocabularyList . map ( ( vocabulary ) : PublicVocabulary => { return { ... vocabulary , definitions : vocabulary . definitions . slice ( ) , } ; } ) , } , } ) ; } public showPublicVocabularyDetail ( vocabulary ) { this . navigatorDelegate . showModal ( ScreenName . PUBLIC_VOCABULARY_DETAIL_SCREEN , { vocabulary , } ) ; } public autoRefreshOnSetChange ( ) { this . eventBus . subscribe ( on ( ActionType . SET__SELECT , ( ) : void => { this . clearAllList ( ) ; this . setListTypeByCurrentInput ( ) ; this . refreshCurrentList ( ) ; } ) ) ; } public showLink ( link , screenTitle ) { this . navigatorDelegate . showModal ( ScreenName . BROWSER_SCREEN , { link , screenTitle , } ) ; } public showTipScreen ( ) { RemoteLogger . logEvent ( 'show_discover_tip' ) ; this . navigatorDelegate . push ( ScreenName . DISCOVER_FAQ_SCREEN , { } ) ; } public showPublicVocabularyActionMenu ( vocabulary ) { this . publicVocabularyActionMenuDelegate . show ( vocabulary ) ; } public showTranslationActionMenu ( translation ) { this . translationActionMenuDelegate . show ( translation ) ; } private showAddingDialog ( ) { this . dialogDelegate . show ( { message : 'Adding. Please wait...' , } ) ; } private showAddSucceededDialog ( ) { this . dialogDelegate . showSuccessDialog ( { message : 'Added successfully.' , } ) ; } private showAddFailedDialogWithRetry ( errorBag , retry ) { if ( errorBag . errorCode === ErrorCode . VOCABULARY__DUPLICATE_TERM ) { this . dialogDelegate . show ( { testID : LightBoxDialogIds . SUCCESS_DIALOG , message : 'You have added this term before. Do you want to add it again?' , onBackgroundPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; } , buttonList : [ { testID : LightBoxDialogIds . CANCEL_BTN , text : 'NO' , onPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidPrimaryBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , { testID : LightBoxDialogIds . OKAY_BTN , text : 'YES' , onPress : ( ) : void => { retry ( false ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidGreyBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , ] , } ) ; } else { this . dialogDelegate . showFailedDialog ( errorBag , { title : 'ADD FAILED' , } ) ; } } }