import { ScreenName } from '@ulangi/ulangi-common/enums' ; import { ErrorBag } from '@ulangi/ulangi-common/interfaces' ; import { ObservableAtomScreen } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import { observable } from 'mobx' ; import { RemoteLogger } from '../../RemoteLogger' ; import { DialogDelegate } from '../dialog/DialogDelegate' ; import { NavigatorDelegate } from '../navigator/NavigatorDelegate' ; import { FetchVocabularyDelegate } from './FetchVocabularyDelegate' ; import { PrepareFetchVocabularyDelegate } from './PrepareFetchVocabularyDelegate' ; @ boundClass export class AtomScreenDelegate { private observableScreen : ObservableAtomScreen ; private prepareFetchVocabularyDelegate : PrepareFetchVocabularyDelegate ; private fetchVocabularyDelegate : FetchVocabularyDelegate ; private dialogDelegate : DialogDelegate ; private navigatorDelegate : NavigatorDelegate ; public constructor ( observableScreen , prepareFetchVocabularyDelegate , fetchVocabularyDelegate , dialogDelegate , navigatorDelegate ) { this . observableScreen = observableScreen ; this . prepareFetchVocabularyDelegate = prepareFetchVocabularyDelegate ; this . fetchVocabularyDelegate = fetchVocabularyDelegate ; this . dialogDelegate = dialogDelegate ; this . navigatorDelegate = navigatorDelegate ; } public startGame ( ) { RemoteLogger . logEvent ( 'start_atom' ) ; this . prepareFetchVocabularyDelegate . prepareFetch ( { onPreparing : this . showPreparingDialog , onPrepareSucceeded : ( ) : void => { this . fetchVocabularyDelegate . fetch ( { onFetching : this . showPreparingDialog , onFetchSucceeded : ( vocabularyList , noMore ) : void => { this . dialogDelegate . dismiss ( ) ; this . navigatorDelegate . push ( ScreenName . ATOM_PLAY_SCREEN , { firstVocabularyBatch : vocabularyList , noMoreVocabulary : noMore , startGame : this . startGame , } ) ; } , onFetchFailed : ( errorBag ) : void => { this . fetchVocabularyDelegate . clearFetch ( ) ; this . showFailedToStartDialog ( errorBag ) ; } , } ) ; } , onPrepareFailed : ( errorBag ) : void => { this . fetchVocabularyDelegate . clearFetch ( ) ; this . showFailedToStartDialog ( errorBag ) ; } , } ) ; } public showPreparingDialog ( ) { this . dialogDelegate . show ( { message : 'Preparing. Please wait...' , } ) ; } public showFailedToStartDialog ( errorBag ) { this . dialogDelegate . showFailedDialog ( errorBag , { title : 'FAILED TO START' , } ) ; } public goToTutorial ( ) { this . navigatorDelegate . push ( ScreenName . ATOM_TUTORIAL_SCREEN , { } ) ; } public back ( ) { this . navigatorDelegate . dismissScreen ( ) ; } public selectCategory ( ) { this . navigatorDelegate . showModal ( ScreenName . CATEGORY_SELECTOR_SCREEN , { initialCategoryName : undefined , onSelect : ( categoryName ) : void => { this . observableScreen . selectedCategoryNames = observable . array ( [ categoryName , ] ) ; } , } ) ; } }