PhoneCallViewPresenter.ts 3.62 KB
import { Presenter, createPresenter } from "../../../common/classbase/PresenterBase";
import { PhoneCallView, PhoneCallViewType, PhoneCallItemProps, PhoneCallViewProps } from "../view/type/PhoneCallView";
import { RegPresenter } from "../PresenterCCViewFactory";
import { GameModelManager } from "../../model/GameModelManager";
import { PlotSceneType } from "../../Enums";
import { ConfigManager } from "../../../common/gameplay/managers/ConfigManager";
import { role } from "../../../config/Role";
import { PlotManager } from "../../../avg/PlotManager";
import SentenceSelectorViewPresenter from "./SentenceSelectorViewPresenter";
import { SentenceContent, SentenceSelectContent } from "../../../avg/model/PlotModel";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { SentenceSelectorViewProps } from "../view/type/SentenceSelectorView";
import { DeepReadonly } from "simba-utils";
import { runAction } from "../../../../../build-templates/wechatgame/launcher/launcher-scene";

@RegPresenter(PhoneCallViewType)
export class PhoneCallViewPresenter extends Presenter<undefined, PhoneCallView> {
    static uuid = "PhoneCallViewPresenter";
    private _items: PhoneCallItemProps[] = [];
    private _viewProps: PhoneCallViewProps;
    onSetContent = (content: DeepReadonly<SentenceSelectContent> | undefined) => {
        let props: SentenceSelectorViewProps =
        {
            sentence: content,
            backgroundpath: "",
            clickcausehide: false,
            onSelectIndexCallback: this.onSelectIndexCallback

        };
        UIManager.pushPresenter(SentenceSelectorViewPresenter, props);
    }


    onSelectIndexCallback = (index: number) => {
        this.view.select(index);
    }

    isSentenceSelectVisible = (): boolean => {
        let preset = UIManager.getTopPresenter();
        if (preset instanceof SentenceSelectorViewPresenter) {
            let selectorPresenter = preset as SentenceSelectorViewPresenter;
            return !selectorPresenter.view.isHidden;
        }
        return false;
    }


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

        this._items = [];
        GameModelManager.rollbackPhoneCallIfNeeded().then((plots) => {
            let plot = plots[0];
            if (plot.plotSceneType !== PlotSceneType.PhoneCall) {
                console.error("Phone call plot data error.");
            } else {
                let roleData = ConfigManager.getConfig(role, plot.plotSceneTypeId);
                this._items = [{ key: plot.id + "", isSelf: plot.sentences[0].roleId === 2, sentence: plot.sentences[0] }];
                this._viewProps =
                {
                    name: roleData.name, // TODO i18n
                    icon: GameModelManager.getRoleData(plot.plotSceneTypeId)!.getHeadIcon(),
                    items: this._items,
                    onCompletePlot: (index?: number) => PlotManager.completePlot(plot, index),
                    onSetContent: this.onSetContent,
                    isSentenceSelectVisible: this.isSentenceSelectVisible
                };
                this._view.setProps(this._viewProps);

                this._disposable.add(PlotManager.PlotStartEvent.on((plots) => {
                    plot = plots[0];
                    if (plot.plotSceneType !== PlotSceneType.PhoneCall) {
                        this._view.close();
                    } else {
                        this._items = [...this._items, { key: plot.id + "", isSelf: plot.sentences[0].roleId === 2, sentence: plot.sentences[0] }];
                        this._view.updateProps({ items: this._items });
                    }
                }));
            }
        });

    }
}