NewNoticeViewPresenter.ts 11 KB
import { ConfigManager } from "simba-config-manager";
import { SDK } from "simba-sdk";
import { ActicityType, PageIndex } from "../../../../avg/EditorEnums";
import { Presenter } from "../../../../common/classbase/PresenterBase";
import { UIManager } from "../../../../common/gameplay/managers/UIManager";
import { taoBaoShopUrlConfig } from "../../../../config/TaoBaoShopUrlConfig";
import { channel, GameConfig } from "../../../../GameConfig";
import { ActivitiesConfig, ActivitiesItemModelManager } from "../../../model/ActivitiesItemModelManager";
import { GameModelManager } from "../../../model/GameModelManager";
import { HttpRequests } from "../../../network/HttpRequests";
import ActivitiesNoticeItem from "../../ActivitiesNoticeItem";
import { RegPresenter } from "../../PresenterCCViewFactory";
import { NewNoticeViewType, NewNoticeView, NewNoticeViewProps, NoticeType, SourceView } from "../../view/type/notice/NewNoticeView";

@RegPresenter(NewNoticeViewType)
export default class NewNoticeViewPresenter extends Presenter<SourceView, NewNoticeView>{
    static uuid = "NewNoticeViewPresenter"
    private _viewProps: NewNoticeViewProps;
    private _noticeType: NoticeType = NoticeType.SystemNotice;
    private _delayTime: number = 0;
    private _closeTimeTicker: number = -1;
    private _canClose: boolean = false;

    constructor() {
        super();
        this._viewProps = {
            sourceView: SourceView.MessageView,
            onNoticeButtonClickCallback: this.onNoticeButtonClickCallback,
            onCloseButtonClickCalback: this.onCloseButtonClickCalback,
        };
    }

    onOpen(source: SourceView) {
        super.onOpen(source);
        this._viewProps.sourceView = source;
        this.view.setProps(this._viewProps);
        this.onShow();
    }

    onShow() {
        super.onShow();
        this.initView();
    }

    onEnterBackground() {
        super.onEnterBackground();
    }

    onEnterForeground() {
        super.onEnterForeground();
    }

    onClose() {
        super.onClose();
    }

    initView() {
        //重置可关闭状态为false
        this._canClose = false;
        //根据是否是第一次弹起紧急公告判断延迟时间
        //this._delayTime = GameModelManager.getNoticeReaded() ? 0 : 3;
        this._delayTime = 0;
        //重置提示文本状态
        this.view.getCloseTipsLabel().string = "";
        //初始化公告类型
        if (channel == "android") {
            this._noticeType = NoticeType.ActvitiesNotice;
        } else {
            this._noticeType = NoticeType.SystemNotice;
        }
        //根据存档初始化“今日不再提示”的选择框
        let autoShow = GameModelManager.getAutoShowNotice();
        this.view.getTipsToggle().isChecked = !autoShow;
        //初始化公告按钮状态和公告内容
        this.setNoticeState();
        this.setNoticeContent();
        //刷新活动公告按钮的红点状态
        this.refreshActivitiesButtonRedDot();
        //开启关闭倒计时
        this.startTimeTicker();
    }

    /**
     * 设置公告按钮状态和公告内容显隐
     */
    setNoticeState() {
        let nodes = this.view.getNoticeNodes();
        let buttons = this.view.getNoticeButtons();
        switch (this._noticeType) {
            case NoticeType.SystemNotice://系统公告
                nodes[NoticeType.SystemNotice].active = true;
                nodes[NoticeType.ActvitiesNotice].active = false;
                buttons[NoticeType.SystemNotice].node.getChildByName("Background").getComponent(cc.Sprite).spriteFrame = this.view.getButtonSpriteFrames()[1];
                buttons[NoticeType.ActvitiesNotice].node.getChildByName("Background").getComponent(cc.Sprite).spriteFrame = this.view.getButtonSpriteFrames()[2];
                break;
            case NoticeType.ActvitiesNotice://活动公告
                nodes[NoticeType.SystemNotice].active = false;
                nodes[NoticeType.ActvitiesNotice].active = true;
                buttons[NoticeType.SystemNotice].node.getChildByName("Background").getComponent(cc.Sprite).spriteFrame = this.view.getButtonSpriteFrames()[0];
                buttons[NoticeType.ActvitiesNotice].node.getChildByName("Background").getComponent(cc.Sprite).spriteFrame = this.view.getButtonSpriteFrames()[3];
                break;
            default:
                break;
        }
    }

    /**
     * 设置公告内容
     */
    async setNoticeContent() {
        switch (this._noticeType) {
            case NoticeType.SystemNotice://系统公告
                HttpRequests.getNewNotice().then((res) => {
                    if (res != null && res.length > 0) {
                        this.view.getContentRichText().string = res[0].msg;
                    } else {
                        this.view.getContentRichText().string = "<color=#FF0000>公告获取失败,请检查网络或稍后重试<color/>";
                    }
                });
                break;
            case NoticeType.ActvitiesNotice://活动公告
                this.view.getActivitiesContentNode().removeAllChildren();
                let configs = ActivitiesItemModelManager.getValidActivitiesConfigs();
                for (let i = 0; i < configs.length; i++) {
                    if (!configs[i].canClick) {
                        ActivitiesItemModelManager.setActivitiesReaded(configs[i].activityId);
                        this.refreshActivitiesButtonRedDot();
                    }
                    let item = cc.instantiate(this.view.getActivitiesItemPrefab());
                    item.getComponent(ActivitiesNoticeItem).setData(configs[i].activityId, configs[i].canClick, configs[i].describe, () => {
                        //刷新活动列表和“活动公告”按钮的红点
                        ActivitiesItemModelManager.setActivitiesReaded(configs[i].activityId);
                        item.getComponent(ActivitiesNoticeItem).refreshRedDot();
                        this.refreshActivitiesButtonRedDot();
                        //执行活动逻辑
                        this.executeActivitiesEvent(configs[i], this._viewProps.sourceView);
                    });
                    item.parent = this.view.getActivitiesContentNode();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 检查“今日不再提示”单选框的选择状态
     */
    checkTipsToggleState() {
        let isChecked = this.view.getTipsToggle().isChecked;
        let autoShow = GameModelManager.getAutoShowNotice();
        if (autoShow !== !isChecked) {
            GameModelManager.setAutoShowNotice(!isChecked);
        }
    }

    /**
     * 刷新“活动公告”按钮的红点状态
     */
    refreshActivitiesButtonRedDot() {
        let buttons = this.view.getNoticeButtons();
        let configs = ActivitiesItemModelManager.getValidActivitiesConfigs();
        for (let i = 0; i < configs.length; i++) {
            let isReaded = ActivitiesItemModelManager.getActivitiesReaded(configs[i].activityId);
            if (!isReaded) {
                buttons[NoticeType.ActvitiesNotice].node.getChildByName("RedDot").active = true;
                return;
            } else {
                buttons[NoticeType.ActvitiesNotice].node.getChildByName("RedDot").active = false;
            }
        }
    }

    /**
     * 根据活动配置处理活动事件
     * @param config 活动配置
     */
    executeActivitiesEvent(config: ActivitiesConfig, source: SourceView) {
        switch (config.activityTypeConfig.activityType) {
            case ActicityType.JumpToTaoBao:
                if (channel == "wechat") {
                    // GameCenter.getInstance().openSubProgram(GameConfig.youZanShopAppId);
                    if (config.activityTypeConfig.taobaoTempId) {
                        let taobaoConfig = ConfigManager.getConfig(taoBaoShopUrlConfig, config.activityTypeConfig.taobaoTempId);
                        SDK.openProgramV2(GameConfig.youZanShopAppId, taobaoConfig.youZanUrl);
                    } else {
                        console.error("The taobaoTempId is undefined.");
                    }
                } else if (channel == "android") {
                    if (config.activityTypeConfig.taobaoTempId) {
                        let taobaoConfig = ConfigManager.getConfig(taoBaoShopUrlConfig, config.activityTypeConfig.taobaoTempId);
                        GameModelManager.jumpToTaobaoShop(taobaoConfig.websiteUrl);
                    } else {
                        console.error("The taobaoTempId is undefined.");
                    }
                } else {
                    UIManager.showToast("该功能在当前平台不可使用");
                }
                break;
            case ActicityType.PageInGame:
                let pageIndex: PageIndex = config.activityTypeConfig.pageIndex;
                if (pageIndex) {
                    this.executePageInGameEvent(pageIndex, source);
                } else {
                    console.error("The pageIndex is undefined.");
                }
                break;
            default:
                break;
        }
    }

    /**
     * 处理游戏内的活动事件
     * @param activityId 活动ID
     */
    executePageInGameEvent(pageIndex: PageIndex, source: SourceView) {
        switch (source) {
            case SourceView.MessageView:
                this.onCloseButtonClickCalback();
                GameModelManager.JumpToViewInGame.emit(pageIndex);
                break;
            case SourceView.SettingView:
                this.onCloseButtonClickCalback();
                GameModelManager.CloseSettingView.emit(pageIndex);
            default:
                break;
        }
    }

    /**
     * 开启可关闭定时器
     */
    startTimeTicker() {
        if (this._delayTime == 0) {
            this._canClose = true;
            this.view.getCloseTipsLabel().string = "点击空白区域可关闭";
        } else {
            this.view.getCloseTipsLabel().string = this._delayTime + "秒后可关闭";
            this._closeTimeTicker = window.setTimeout(() => {
                this._delayTime -= 1;
                this.startTimeTicker();
            }, 1000);
        }
    }

    /**
     * 公告按钮的点击事件
     * @param index 系统公告和活动公告的Index
     */
    onNoticeButtonClickCallback = (index: number) => {
        if (this._noticeType !== index) {
            this._noticeType = index;
            this.setNoticeState();
            this.setNoticeContent();
        }
    }

    /**
     * 关闭按钮的点击事件
     */
    onCloseButtonClickCalback = () => {
        if (this._canClose) {
            this.checkTipsToggleState();
            GameModelManager.setNoticeReaded(true);
            clearTimeout(this._closeTimeTicker);
            this.view.close();
        }
    }
}