MainViewPresenter.ts 4.21 KB
import { GameRecord } from "../../../avg/game-data/GameRecord";
import { ReadonlyPlots, SpecialPlotId } from "../../../avg/model/PlotModel";
import { PlotManager } from "../../../avg/PlotManager";
import { createPresenter, Presenter } from "../../../common/classbase/PresenterBase";
import { View } from "../../../common/classbase/ViewBase";
import { GameTextData } from "../../../common/gameplay/gamedata/GameTextData";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { PlotSceneType } from "../../Enums";
import { GameModelManager } from "../../model/GameModelManager";
import { RegPresenter } from "../PresenterCCViewFactory";
import { showToBeContinue } from "../view/impl/AlertToBeContinueImpl";
import { MainView, MainViewProps, MainViewType } from "../view/type/MainView";
import DatingEventViewPresenter from "./DatingEventViewPresenter";
import MessageViewPresenter from "./MessageViewPresenter";

const enum Tabs {
    messageTab = 0,
    datingEventTab = 1,
}

const TabPresenterFactory: ({ new(): Presenter } & { uuid: string })[] = [MessageViewPresenter, DatingEventViewPresenter];

@RegPresenter(MainViewType)
export default class MainViewPresenter extends Presenter<undefined, MainView> {
    static uuid = "MainViewPresenter";
    private _subPresenters: Presenter<any, View>[] = [];
    private _currentTab = 0;
    private _viewProps: MainViewProps;
    private _loadingTab = false;

    constructor() {
        super();
        this._viewProps = {
            selectedTab: 0,
            messageRedDot: false,
            datingEventRedDot: false,
            onTabClick: this.onTabClick
        };

    }

    checkRedDot = (plots: ReadonlyPlots) => {
        if (plots[0].plotSceneType === PlotSceneType.Message || plots[0].plotSceneType === PlotSceneType.PhoneCall) {
            this._viewProps.messageRedDot = true;
        } else {
            this._viewProps.messageRedDot = false;
        }
        this._viewProps.datingEventRedDot = false;
        for (let plot of plots) {
            if (plot.plotSceneType === PlotSceneType.DatingEvent) {
                this._viewProps.datingEventRedDot = true;
            }
        }
        this.view.setProps(this._viewProps);
    }

    async start() { 
        // 开始游戏
        this._subPresenters[0] = await createPresenter(TabPresenterFactory[0]);
        this._subPresenters[0].open(this._view.getTabContentContainer(), undefined);

        this.checkRedDot(PlotManager.getCurrentPlots());
        PlotManager.PlotStartEvent.on(this.checkRedDot);


        if (PlotManager.getCurrentPlots()[0].id === SpecialPlotId.ToBeContinued) {
            showToBeContinue();
        }
    }

    onClose() {
        super.onClose();
        UIManager.popToPresenter(this);
    }

    onOpen() {
        super.onOpen(undefined);
    }

    onEnterBackground() {
        super.onEnterBackground();
        this._subPresenters[this._currentTab].onEnterBackground();
    }

    onEnterForeground() {
        super.onEnterForeground();
        this._subPresenters[this._currentTab].onEnterForeground();
        if (PlotManager.getCurrentPlots()[0].id === SpecialPlotId.ToBeContinued) {
            showToBeContinue();
        }
    }

    onTabClick = (index: number) => {
        if (!GameModelManager.checkFuncUnloced(TabPresenterFactory[index].uuid)) {
            UIManager.showToast(GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_MAIN_FUNCTION_NOT_OPEN));
            return;
        }
        if (this._loadingTab) return;
        if (this._currentTab !== index) {
            this._subPresenters[this._currentTab].view.hide();
            this._currentTab = index;
            this._viewProps.selectedTab = index;

            this._view.setProps(this._viewProps);
            if (!this._subPresenters[this._currentTab]) {
                this._loadingTab = true;
                createPresenter(TabPresenterFactory[this._currentTab]).then((presenter) => {
                    this._loadingTab = false;
                    this._subPresenters[this._currentTab] = presenter;
                    presenter.open(this.view.getTabContentContainer(), undefined);
                });
            } else {
                this._subPresenters[this._currentTab].view.show();
            }
        }
    }
}