MessageViewPresenter.ts 5.05 KB
import { Presenter } from "../../../common/classbase/PresenterBase";
import { MessageView, MessageEntryItemProps, MessageViewType } from "../view/type/MessageView";
import { GameModelManager } from "../../model/GameModelManager";
import { ReadonlyPlot, SentenceType, SentenceTextContent, SentenceMediaContent } from "../../../avg/model/PlotModel";
import { DeepReadonly, shallowEqual } from "simba-utils";
import { richNodesToCocosString } from "../../../avg/utils/RichTextUtils";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { ChatListViewPresenter } from "./ChatListViewPresenter";
import { RegPresenter } from "../PresenterCCViewFactory";
import { MessageSceneModel } from "../../model/MessageSceneModel";
import { PlotManager } from "../../../avg/PlotManager";
import { PlotSceneType } from "../../Enums";
import { PhoneCallViewPresenter } from "./PhoneCallViewPresenter";

function getSentenceText(content: DeepReadonly<SentenceTextContent> | DeepReadonly<SentenceMediaContent>) {
    let text = ""
    switch (content.type) {
        case SentenceType.TEXT:
            text = richNodesToCocosString(content.value);
            break;
        case SentenceType.AUDIO:
            text = "[Voice]";
            break;
        case SentenceType.IMAGE:
            text = "[Image]";
            break;
        case SentenceType.VIDEO:
            text = "[Video]";
            break;
    }
    return text;
}

function getPlotString(plot?: ReadonlyPlot) {
    if (plot) {
        let text = "";
        let sentence:any = plot.sentences[0];
        if (sentence.content) {
            if (sentence.content.type === SentenceType.SELECT) {
                let select = PlotManager.getPlotSelection(plot.id);
                if (select !== undefined) {
                    let option = sentence.content.value[select];
                    if (option.content) {
                        text = getSentenceText(option.content);
                    } else {
                        text = option.summary;
                    }
                }
            } else {
                text = getSentenceText(sentence.content);
            }
        }
        return text;
    }
    return "";
}

@RegPresenter(MessageViewType)
export default class MessageViewPresenter extends Presenter<undefined, MessageView> {
    static uuid = "MessageViewPresenter";
    private _scenesActive: { [key: number]: boolean } = {};
    private _currPlotHasMsg = false;

    onOpen() {
        super.onOpen(undefined);
        this.checkScenesActive();
        this.setViewProps();
        this._disposable.add(GameModelManager.MessageSceneChanged.on(() => {
            if (!this._currPlotHasMsg && !this._view.isHidden && !this._isInBackground) {
                this.checkScenesActive();
                this.setViewProps();
            }
        }));
        this._disposable.add(PlotManager.PlotStartEvent.on(() => {
            if (this._currPlotHasMsg && !this._view.isHidden && !this._isInBackground) {
                this.checkScenesActive();
                this.setViewProps();
            }
        }));
        this.checkPhoneCall();
    }

    onShow() {
        super.onShow();
        this.checkScenesActive();
        this.setViewProps();
        this.checkPhoneCall();
    }

    checkPhoneCall() {
        if (PlotManager.getCurrentPlots()[0].plotSceneType === PlotSceneType.PhoneCall) {
            UIManager.pushPresenter(PhoneCallViewPresenter, undefined);
        }
    }

    onEnterForeground() {
        super.onEnterForeground();
        this.checkScenesActive();
        this.setViewProps();
        this.checkPhoneCall();
    }

    private checkScenesActive = () => {
        this._currPlotHasMsg = false;
        this._scenesActive = GameModelManager.getMessageScenesModel().reduce((pv, v) => (pv[v.id] = this.checkSceneActive(v), pv), {});
    }

    private checkSceneActive = (model: MessageSceneModel) => {
        let ret = PlotManager.getCurrentPlots().findIndex(v => v.plotSceneType === PlotSceneType.Message && v.plotSceneTypeId === model.id) >= 0;
        if (ret) this._currPlotHasMsg = true;
        return ret;
    }

    private setViewProps() {
        this._view.setProps({ // 暂每条只有一个头像,群聊显示单独头像,以后可能群聊显示多人头像
            items: GameModelManager.getMessageScenesModel().map(
                v => {
                    return {
                        key: v.id + "", icons: [GameModelManager.getRoleData(v.config.roles[0])!.getHeadIcon(true)],
                        title: GameModelManager.getConfigLanguageTxt(v.config.title), lastMsg: getPlotString(v.lastPlot),
                        redDot: this._scenesActive[v.id]
                    }
                }),
            onItemClick: this.onItemClick
        });
    }

    private onItemClick = (key: string) => {
        console.log("item click", key);
        let sceneModel = GameModelManager.getMessageSceneModel(parseInt(key));
        if (!sceneModel) { console.error("message scene model not found ", key); return; }
        UIManager.pushPresenter(ChatListViewPresenter, sceneModel);
    }
}