Files
18xchat/assets/Scripts/chat18x/ui/panels/GirlListPanel.ts
T

161 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-08-11 16:08:59 +08:00
import { _decorator, Component, Node, instantiate } from "cc";
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import { GirlListItem } from "../../uiitems/GirlListItem";
import { ConfigManager } from "../../manager/ConfigManager";
2025-09-02 16:38:01 +08:00
import { GirlService } from "db://assets/Scripts/chat18x/network/services/GirlService";
import { DataManager, DataId } from "../../data/DataManager";
import { GirlData } from "../../data/GirlData";
2025-09-09 14:31:47 +08:00
import proto from "db://assets/Scripts/proto/proto.pb.js";
import GameRootUI from "../../../Main/Common/GameRootUI";
2025-09-11 17:07:29 +08:00
import { UITransitionHelper } from "../../utils/UITransitionHelper";
import { NavigationManager, PanelType } from "../../manager/NavigationManager";
2025-09-02 16:38:01 +08:00
2025-08-11 16:08:59 +08:00
const { ccclass, property } = _decorator;
@ccclass("GirlListPanel")
export class GirlListPanel extends li_BaseView {
private _nodeTab: any = {};
itemInst: GirlListItem;
content: Node;
cache: GirlListItem[] = [];
2025-09-15 11:46:55 +08:00
// 缓存池相关属性
private itemPool: GirlListItem[] = [];
private maxPoolSize: number = 20;
2025-08-12 16:58:02 +08:00
category: number;
private parentPanelName: string = null;
2025-09-11 17:07:29 +08:00
2025-08-11 16:08:59 +08:00
onLoadCT() {
Utils.parseNode(this.node, this._nodeTab);
this.registerListener();
this.itemInst = this._nodeTab.BubbleItem.getComponent(GirlListItem);
this.itemInst.node.active = false;
this.content = this._nodeTab.content;
2025-09-15 16:11:15 +08:00
this.refresh();
2025-08-11 16:08:59 +08:00
}
2025-09-15 11:29:23 +08:00
cacheCategory: number = -1;
2025-09-15 11:46:55 +08:00
// 从缓存池获取节点
private getItemFromPool(): GirlListItem {
if (this.itemPool.length > 0) {
return this.itemPool.pop();
} else {
// 缓存池为空,检查是否有隐藏的节点可以复用
for (let i = 0; i < this.content.children.length; i++) {
let childNode = this.content.children[i];
if (!childNode.active && childNode !== this.itemInst.node) {
let item = childNode.getComponent(GirlListItem);
if (item) {
return item;
}
}
}
2025-09-15 16:11:15 +08:00
2025-09-15 11:46:55 +08:00
// 没有可复用的节点,创建新节点
let newNode = instantiate(this.itemInst.node);
let item = newNode.getComponent(GirlListItem);
this.content.addChild(newNode);
return item;
}
}
// 将节点回收到缓存池
private returnItemToPool(item: GirlListItem) {
if (this.itemPool.length < this.maxPoolSize) {
item.reset();
this.itemPool.push(item);
} else {
// 缓存池已满,隐藏节点但不销毁
item.reset();
}
}
2025-09-15 16:11:15 +08:00
async refresh() {
const category = NavigationManager.Instance.getSelectedCategoryId();
2025-09-15 11:29:23 +08:00
if (category == this.cacheCategory) return;
this.cacheCategory = category;
2025-09-15 11:46:55 +08:00
// 回收当前显示的节点到缓存池
2025-08-11 16:08:59 +08:00
if (this.cache) {
for (let i = this.cache.length - 1; i >= 0; i--) {
2025-09-15 11:46:55 +08:00
let item = this.cache[i];
this.returnItemToPool(item);
2025-08-11 16:08:59 +08:00
}
2025-09-15 11:46:55 +08:00
this.cache = [];
2025-08-11 16:08:59 +08:00
}
2025-09-02 16:38:01 +08:00
// 请求列表
const reqData = {
category,
page: 1,
limit: 10,
};
let res = await GirlService.I.reqGetGirlList(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
// 保存数据
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
2025-09-08 15:10:51 +08:00
girlData.setGirlList(category.toString(), res.data);
2025-09-02 16:38:01 +08:00
// 根据数据,刷新界面
const allId = girlData.getGirlIds(category.toString());
for (let id of allId) {
2025-09-15 11:46:55 +08:00
let item = this.getItemFromPool();
2025-09-02 16:38:01 +08:00
item.refreshData(category, id, this.node);
2025-09-15 11:46:55 +08:00
item.node.active = true;
2025-09-02 16:38:01 +08:00
this.cache.push(item);
2025-09-15 16:11:15 +08:00
2025-09-15 11:46:55 +08:00
// 确保节点在 content 中(可能已经在了)
if (item.node.parent !== this.content) {
this.content.addChild(item.node);
}
2025-09-02 16:38:01 +08:00
}
2025-08-11 16:08:59 +08:00
}
}
2025-09-15 11:29:23 +08:00
hide() {
this.node.active = false;
}
2025-08-11 16:08:59 +08:00
private registerListener() {
GButton.BandClick(this._nodeTab.ReturnBtn, this.Return, this);
}
Return() {
// 直接关闭,不需要动画
this.node.active = false;
2025-08-11 16:08:59 +08:00
}
2025-09-15 11:46:55 +08:00
onDestroy() {
// 清理缓存池,销毁所有缓存的节点
for (let item of this.itemPool) {
if (item && item.node) {
item.node.destroy();
}
}
this.itemPool = [];
// 清理当前显示的节点
for (let item of this.cache) {
if (item && item.node) {
item.node.destroy();
}
}
this.cache = [];
2025-09-15 16:11:15 +08:00
2025-09-15 11:46:55 +08:00
// 清理 content 中所有的 GirlListItem 节点
for (let i = this.content.children.length - 1; i >= 0; i--) {
let child = this.content.children[i];
if (child !== this.itemInst.node) {
let item = child.getComponent(GirlListItem);
if (item) {
child.destroy();
}
}
}
}
2025-08-11 16:08:59 +08:00
}