UrgentNoticeViewPresenter.ts 2.8 KB
import { Presenter } from "../../../../common/classbase/PresenterBase";
import { GameModelManager } from "../../../model/GameModelManager";
import { RegPresenter } from "../../PresenterCCViewFactory";
import { UrgentNoticeViewType, UrgentNoticeView, UrgentNoticeViewProps } from "../../view/type/notice/UrgentNoticeView";

@RegPresenter(UrgentNoticeViewType)
export default class UrgentNoticeViewPresenter extends Presenter<undefined, UrgentNoticeView>{
    static uuid = "UrgentNoticeViewPresenter"
    private _viewProps: UrgentNoticeViewProps;
    private _delayTime: number = 0;
    private _closeTimeTicker: number = -1;
    private _canClose: boolean = false;

    constructor() {
        super();
        this._viewProps = {
            onCloseButtonClickCalback: this.onCloseButtonClickCalback,
        };
    }

    onOpen() {
        super.onOpen(undefined);
        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.getUrgentNoticeInfo().readed ? 0 : 3;
        //重置提示文本状态
        this.view.getCloseTipsLabel().string = "";
        //根据存档初始化“今日不再提示”的选择框
        let autoShow = GameModelManager.getUrgentNoticeInfo().autoShow;
        this.view.getTipsToggle().isChecked = !autoShow;
        this.startTimeTicker();
    }

    /**
     * 开启可关闭定时器
     */
    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);
        }
    }

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

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