[add] HistoryPanel 完成

This commit is contained in:
2022-05-15 14:34:42 +08:00
parent d302a52d8d
commit ded2c7c1ed
5 changed files with 2751 additions and 2482 deletions

View File

@@ -1,21 +1,62 @@
import UIPanel from "../Engine/Component/UIPanel/UIPanel";
import LocalStorageData from "../Engine/Data/LocalStorageData";
import NodePackageManager from "../_BootLoader/Npm/NodePackageManager";
const { ccclass, property } = cc._decorator;
/** HistoryPanel */
@ccclass
export default class HistoryPanel extends UIPanel {
//#region property
@property({ type: cc.Node })
public HistoryView: cc.Node = null;
@property({ type: cc.Prefab })
public PlayerItem: cc.Prefab = null;
@property({ type: cc.Prefab })
public HistoryItem: cc.Prefab = null;
//#endregion
//#region property
private _dateTransform: cc.Node = null;
private _teamTransform: cc.Node = null;
private _isAwake: boolean = false;
private _needRefresh: boolean = true;
//#endregion
//#region get set
public get History(): Map<string, string[]> { return this._m_history; }
private _m_history: Map<string, string[]> = new Map<string, string[]>();
public get History(): Map<string, string[]> { return this._history; }
private _history: Map<string, string[]> = new Map<string, string[]>();
public static get Today(): string { return NodePackageManager.Instance.Dayjs().format("YYYYMMDD"); }
//#endregion
//#region Lifecycle
public Initial(...initData: any[]): void {
super.Initial(...initData);
if (this._history.size === 0) {
this.LoadRecord();
}
// this._dateTransform = this.HistoryView.Find("DateText");
// this._dateTransform.active = false;
// this._teamTransform = this.HistoryView.Find("Players");
// this._teamTransform.active = false;
this._isAwake = true;
}
protected *ImplementReadyShow(...param: any[]): IterableIterator<any> {
cc.log("HistoryPanel ImplementReadyShow");
this.OpenUI();
}
//#endregion
@@ -23,7 +64,111 @@ export default class HistoryPanel extends UIPanel {
//#region Custom
public LoadRecord(): void {
// throw new Error("Method not implemented.");
let self: this = this;
// 格式 json: {"日期":[玩家1,玩家2,...],"日期":[玩家1,玩家2,...]}
let history: Map<string, string[]> = LocalStorageData.Instance.HistoryTeam;
if (history.size === 0) {
return;
}
history.forEach((value: string[], key: string, map: Map<string, string[]>) => {
self._history.set(key, value);
});
}
public RefreshUI(): void {
if (!this._needRefresh) {
return;
}
if (this._isAwake) {
// 先清掉全部
for (let i: number = this.HistoryView.childrenCount - 1; i > 0; i--) {
let child: cc.Node = this.HistoryView.children[i];
if (child.active) {
child.destroy();
}
}
}
let dateList: string[] = [];
this._history.forEach((value: string[], key: string, map: Map<string, string[]>) => {
dateList.push(key);
});
dateList.sort();
// 越前面日期越舊
for (let j: number = dateList.length - 1; j >= 0; j--) {
let date: string = dateList[j];
// 今日不顯示
if (date === HistoryPanel.Today) {
continue;
}
let historyItem: cc.Node = this.HistoryView.ExAddChild(this.HistoryItem);
let playerList: string[] = this._history.get(date);
let dateText: cc.Label = historyItem.getChildByName("DateText").getComponent(cc.Label);
dateText.string = date;
dateText.node.active = true;
// 所有玩家
// let teamList = GameObject.Instantiate(m_TeamTransform.gameObject, this._historyView.transform);
let teamList: cc.Node = historyItem.getChildByName("Players");
teamList.active = true;
// 一個team A player B player
let teamTemplete: cc.Prefab = this.PlayerItem;
for (let i: number = 0; i < playerList.length; i = i + 2) {
let team: cc.Node = teamList.ExAddChild(teamTemplete);
let player1Text: cc.Label = team.Find("Label_1").getComponent(cc.Label);
let player2Text: cc.Label = team.Find("Label_2").getComponent(cc.Label);
team.active = true;
let name: string = playerList[i];
player1Text.string = playerList[i];
player2Text.string = i + 1 < playerList.length ? playerList[i + 1] : "";
}
}
this._needRefresh = false;
}
public OpenUI(): void {
if (this._needRefresh) {
this.RefreshUI();
}
}
public UpdateHistory(memberList): void {
if (!this._history.has(HistoryPanel.Today)) {
this._needRefresh = true;
this._history.set(HistoryPanel.Today, memberList);
} else {
// 不相同 就要重刷UI
this._needRefresh = memberList !== this._history[HistoryPanel.Today];
this._history[HistoryPanel.Today] = memberList;
}
}
// 刪掉最舊的一筆紀錄
public DeleteOldestHistory(): void {
if (this._history.size <= 0) {
return;
}
let dateList: string[] = [];
this._history.forEach((value: string[], key: string, map: Map<string, string[]>) => {
dateList.push(key);
});
dateList.sort();
// 越前面日期越舊
this._history.delete(dateList[0]);
this._needRefresh = true;
this.RefreshUI();
// 要不要及時存擋
LocalStorageData.Instance.HistoryTeam = this._history;
}
//#endregion