调整布局、新增导航栏,调整页面跳转逻辑
This commit is contained in:
@@ -12,7 +12,11 @@ import Utils from "db://assets/Scripts/Main/Common/Utils";
|
||||
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
|
||||
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
|
||||
import { ThemeItem } from "../../uiitems/ThemeItem";
|
||||
import { NavigationManager, PageTransitionType } from "../../manager/NavigationManager";
|
||||
import {
|
||||
NavigationManager,
|
||||
PageTransitionType,
|
||||
PanelType,
|
||||
} from "../../manager/NavigationManager";
|
||||
import { GirlListItem } from "../../uiitems/GirlListItem";
|
||||
import LanguageUtils from "../../../Main/Common/LanguageUtils";
|
||||
import { ViewManager } from "../../../Main/Manager/ViewManager";
|
||||
@@ -29,6 +33,7 @@ import { AccountData } from "../../data/AccountData";
|
||||
import { ShopData } from "../../data/ShopData";
|
||||
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
||||
import GameRootUI from "../../../Main/Common/GameRootUI";
|
||||
import { MainController } from "../../core/MainController";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@@ -38,7 +43,7 @@ export class ThemePanel extends li_BaseView {
|
||||
|
||||
coinNum: Label;
|
||||
uid: Label;
|
||||
itemInst: GirlListItem;
|
||||
itemInst: ThemeItem;
|
||||
content: Node;
|
||||
private vipLeftTime: Label;
|
||||
recId: number;
|
||||
@@ -47,6 +52,14 @@ export class ThemePanel extends li_BaseView {
|
||||
|
||||
cache: ThemeItem[] = [];
|
||||
|
||||
// 子页面管理
|
||||
private currentChildPanel: any = null;
|
||||
|
||||
// 加载状态保护
|
||||
private isLoading: boolean = false;
|
||||
private loadingPromise: Promise<void> | null = null;
|
||||
private abortController: AbortController | null = null;
|
||||
|
||||
onLoadCT() {
|
||||
Utils.parseNode(this.node, this._nodeTab);
|
||||
|
||||
@@ -65,67 +78,206 @@ export class ThemePanel extends li_BaseView {
|
||||
}
|
||||
|
||||
rectNameKey: string;
|
||||
async Show() {
|
||||
this._nodeTab.loadingRec.active = true;
|
||||
//some temp data
|
||||
this.refreshCoinNum();
|
||||
this.refreshVipExpire();
|
||||
const accountData = DataManager.I.getDataById<AccountData>(DataId.Account);
|
||||
this.uid.string = "uid: " + accountData.accId;
|
||||
this.recId = 1;
|
||||
if (this.cache) {
|
||||
for (let i = this.cache.length - 1; i >= 0; i--) {
|
||||
this.cache[i].node.destroy();
|
||||
async Show(forceRefresh: boolean = false) {
|
||||
// 防止重复加载
|
||||
if (this.isLoading) {
|
||||
console.log("[ThemePanel] 已在加载中,等待当前加载完成");
|
||||
if (this.loadingPromise) {
|
||||
await this.loadingPromise;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.isLoading = true;
|
||||
this.loadingPromise = this._showInternal(forceRefresh);
|
||||
|
||||
try {
|
||||
await this.loadingPromise;
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
this.loadingPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async _showInternal(forceRefresh: boolean = false) {
|
||||
// 创建新的AbortController用于取消操作
|
||||
this.abortController = new AbortController();
|
||||
const signal = this.abortController.signal;
|
||||
|
||||
try {
|
||||
this._nodeTab.loadingRec.active = true;
|
||||
//some temp data
|
||||
this.refreshCoinNum();
|
||||
this.refreshVipExpire();
|
||||
const accountData = DataManager.I.getDataById<AccountData>(
|
||||
DataId.Account
|
||||
);
|
||||
this.uid.string = "uid: " + accountData.accId;
|
||||
this.recId = 1;
|
||||
|
||||
// 检查是否已被取消
|
||||
if (signal.aborted) {
|
||||
throw new Error("Operation was aborted");
|
||||
}
|
||||
|
||||
// 清理旧的cache节点(每次都清理,解决内存泄漏问题)
|
||||
this.clearCache();
|
||||
|
||||
// 如果需要强制刷新,先刷新数据
|
||||
if (forceRefresh) {
|
||||
console.log("[ThemePanel] 开始强制刷新数据");
|
||||
await Promise.all([
|
||||
MainController.I.refreshThemeData(),
|
||||
MainController.I.refreshDailyRecommend(),
|
||||
]);
|
||||
console.log("[ThemePanel] 数据刷新完成");
|
||||
|
||||
// 再次检查是否已被取消
|
||||
if (signal.aborted) {
|
||||
throw new Error("Operation was aborted");
|
||||
}
|
||||
}
|
||||
|
||||
// 优先使用MainController中预加载的数据
|
||||
const preloadStatus = MainController.I.getPreloadStatus();
|
||||
console.log("[ThemePanel] 预加载状态:", preloadStatus);
|
||||
|
||||
// 处理主题数据
|
||||
await this.handleThemeData(preloadStatus.themeData);
|
||||
|
||||
// 检查是否已被取消
|
||||
if (signal.aborted) {
|
||||
throw new Error("Operation was aborted");
|
||||
}
|
||||
|
||||
// 处理每日推荐数据
|
||||
await this.handleDailyRecommendData(preloadStatus.dailyRecommend);
|
||||
|
||||
// 检查是否已被取消
|
||||
if (signal.aborted) {
|
||||
throw new Error("Operation was aborted");
|
||||
}
|
||||
|
||||
this._nodeTab.loadingRec.active = false;
|
||||
|
||||
// 处理商品列表数据
|
||||
await this.handleShopListData(preloadStatus.shopList);
|
||||
} catch (error) {
|
||||
console.error("[ThemePanel] Show操作出错:", error);
|
||||
this._nodeTab.loadingRec.active = false;
|
||||
|
||||
// 如果不是取消操作导致的错误,可以考虑显示错误提示
|
||||
if (error.message !== "Operation was aborted") {
|
||||
// 这里可以添加错误提示逻辑
|
||||
console.error("[ThemePanel] 数据加载失败,请稍后重试");
|
||||
}
|
||||
} finally {
|
||||
this.abortController = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理主题数据
|
||||
*/
|
||||
private async handleThemeData(isPreloaded: boolean): Promise<void> {
|
||||
if (isPreloaded) {
|
||||
console.log("[ThemePanel] 使用预加载的主题数据");
|
||||
// 直接使用预加载的数据渲染界面
|
||||
const themeData = DataManager.I.getDataById<ThemeData>(DataId.Theme);
|
||||
if (themeData && themeData.themes) {
|
||||
this.renderThemeData(themeData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 请求主题数据
|
||||
|
||||
// 如果没有预加载数据,则请求数据
|
||||
console.log("[ThemePanel] 请求主题数据");
|
||||
const reqData = {};
|
||||
let res = await ThemeService.I.reqHallTheme(reqData);
|
||||
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
|
||||
// 保存数据
|
||||
const themeData = DataManager.I.getDataById<ThemeData>(DataId.Theme);
|
||||
themeData.themes = res.data;
|
||||
// 根据数据,刷新界面
|
||||
const themeIds = themeData.themeIds;
|
||||
for (let id of themeIds) {
|
||||
let newNode = instantiate(this.itemInst.node);
|
||||
let item = newNode.getComponent(ThemeItem);
|
||||
item.refresh(id, this);
|
||||
newNode.active = true;
|
||||
this.cache.push(item);
|
||||
this.content.addChild(newNode);
|
||||
this.renderThemeData(themeData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染主题数据
|
||||
*/
|
||||
private renderThemeData(themeData: ThemeData): void {
|
||||
const themeIds = themeData.themeIds;
|
||||
for (let id of themeIds) {
|
||||
let newNode = instantiate(this.itemInst.node);
|
||||
let item = newNode.getComponent(ThemeItem);
|
||||
item.refresh(id, this);
|
||||
newNode.active = true;
|
||||
this.cache.push(item);
|
||||
this.content.addChild(newNode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理每日推荐数据
|
||||
*/
|
||||
private async handleDailyRecommendData(isPreloaded: boolean): Promise<void> {
|
||||
if (isPreloaded) {
|
||||
console.log("[ThemePanel] 使用预加载的每日推荐数据");
|
||||
// 直接使用预加载的数据渲染界面
|
||||
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
if (girlData) {
|
||||
this.renderDailyRecommendData(girlData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 请求每日推荐
|
||||
const reqData2 = {};
|
||||
let res2 = await GirlService.I.reqDailyRecommend(reqData2);
|
||||
if (res2 && res2.code === proto.cs.EnmRetCode.SUCCESS) {
|
||||
// 如果没有预加载数据,则请求数据
|
||||
console.log("[ThemePanel] 请求每日推荐数据");
|
||||
const reqData = {};
|
||||
let res = await GirlService.I.reqDailyRecommend(reqData);
|
||||
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
|
||||
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
girlData.setDailyRecommend(res2.data);
|
||||
// 根据数据,刷新界面
|
||||
this.recId = girlData.getRecommendGirlId();
|
||||
this.rectNameKey = girlData.getRecommendGrilName();
|
||||
let avatarPath = girlData.getRecommendGrilAvatar();
|
||||
this.recName.string = LanguageUtils.getText(this.rectNameKey);
|
||||
ResManager.I.changeBundleSpriteFrame(
|
||||
this.recSprite,
|
||||
avatarPath,
|
||||
"Girls",
|
||||
() => {
|
||||
let sizeTran = this.recSprite.node.parent.getComponent(UITransform);
|
||||
Utils.adjustBgPixelRatioToSize(
|
||||
sizeTran.contentSize,
|
||||
this.recSprite.node,
|
||||
1
|
||||
);
|
||||
}
|
||||
);
|
||||
girlData.setDailyRecommend(res.data);
|
||||
this.renderDailyRecommendData(girlData);
|
||||
}
|
||||
this._nodeTab.loadingRec.active = false;
|
||||
}
|
||||
|
||||
// 获取商品列表,提前把数据拿下来
|
||||
this.reqShopList();
|
||||
/**
|
||||
* 渲染每日推荐数据
|
||||
*/
|
||||
private renderDailyRecommendData(girlData: GirlData): void {
|
||||
this.recId = girlData.getRecommendGirlId();
|
||||
this.rectNameKey = girlData.getRecommendGrilName();
|
||||
let avatarPath = girlData.getRecommendGrilAvatar();
|
||||
this.recName.string = LanguageUtils.getText(this.rectNameKey);
|
||||
ResManager.I.changeBundleSpriteFrame(
|
||||
this.recSprite,
|
||||
avatarPath,
|
||||
"Girls",
|
||||
() => {
|
||||
let sizeTran = this.recSprite.node.parent.getComponent(UITransform);
|
||||
Utils.adjustBgPixelRatioToSize(
|
||||
sizeTran.contentSize,
|
||||
this.recSprite.node,
|
||||
1
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理商品列表数据
|
||||
*/
|
||||
private async handleShopListData(isPreloaded: boolean): Promise<void> {
|
||||
if (isPreloaded) {
|
||||
console.log("[ThemePanel] 商品列表数据已预加载");
|
||||
// 数据已经在DataManager中,不需要额外处理
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果没有预加载数据,则请求数据
|
||||
console.log("[ThemePanel] 请求商品列表数据");
|
||||
await this.reqShopList();
|
||||
}
|
||||
|
||||
private registerListener() {
|
||||
@@ -136,15 +288,13 @@ export class ThemePanel extends li_BaseView {
|
||||
|
||||
GButton.BandClick(this._nodeTab.ShopPanelEntry, this.enterShop, this);
|
||||
|
||||
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
|
||||
this.recName.string = LanguageUtils.getText(this.rectNameKey);
|
||||
});
|
||||
Utils.addInnerEL(InnerMsgCode.BalanceChange, this, () => {
|
||||
this.refreshCoinNum();
|
||||
});
|
||||
Utils.addInnerEL(InnerMsgCode.VipExpireChange, this, () => {
|
||||
this.refreshVipExpire();
|
||||
});
|
||||
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, this.onLanguageChange);
|
||||
Utils.addInnerEL(InnerMsgCode.BalanceChange, this, this.onBalanceChange);
|
||||
Utils.addInnerEL(
|
||||
InnerMsgCode.VipExpireChange,
|
||||
this,
|
||||
this.onVipExpireChange
|
||||
);
|
||||
}
|
||||
|
||||
enterShop() {
|
||||
@@ -165,9 +315,11 @@ export class ThemePanel extends li_BaseView {
|
||||
this.recId
|
||||
) == 0;
|
||||
if (isRelease || isFree) {
|
||||
const transitionConfig = NavigationManager.getTransitionPresets().SLIDE_LEFT_TO_RIGHT;
|
||||
NavigationManager.Instance.navigateToGirlDetail(this.recId, transitionConfig, this);
|
||||
GameRootUI.I.hideDefaultView();
|
||||
NavigationManager.Instance.setSelectedGirlId(this.recId);
|
||||
NavigationManager.Instance.setSelectedThemeId(
|
||||
girlData.getGrilCategoryById(this.recId)
|
||||
);
|
||||
NavigationManager.Instance.switchToPanel(PanelType.GIRL_DETAIL);
|
||||
} else {
|
||||
//未解锁
|
||||
ViewManager.I.openBundlesPopupView("GirlListPopupPanel", {
|
||||
@@ -179,7 +331,8 @@ export class ThemePanel extends li_BaseView {
|
||||
}
|
||||
|
||||
openSetting() {
|
||||
ViewManager.I.openBundlesView("SettingPanel");
|
||||
// 通过NavigationManager切换到设置面板,保持动画和状态同步
|
||||
NavigationManager.Instance.switchToPanel(PanelType.SETTING);
|
||||
}
|
||||
|
||||
openMsgBox() {
|
||||
@@ -216,11 +369,107 @@ export class ThemePanel extends li_BaseView {
|
||||
}
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, () => {
|
||||
this.recName.string = LanguageUtils.getText(this.rectNameKey);
|
||||
});
|
||||
Utils.removeInnerEL(InnerMsgCode.BalanceChange, this, () => {});
|
||||
Utils.removeInnerEL(InnerMsgCode.VipExpireChange, this, () => {});
|
||||
/**
|
||||
* 清理缓存节点
|
||||
*/
|
||||
private clearCache(): void {
|
||||
if (this.cache && this.cache.length > 0) {
|
||||
console.log(`[ThemePanel] 清理 ${this.cache.length} 个缓存节点`);
|
||||
for (let i = this.cache.length - 1; i >= 0; i--) {
|
||||
if (this.cache[i] && this.cache[i].node && this.cache[i].node.isValid) {
|
||||
this.cache[i].node.destroy();
|
||||
}
|
||||
}
|
||||
this.cache = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置子页面
|
||||
* @param panel 子页面实例
|
||||
*/
|
||||
public setChildPanel(panel: any): void {
|
||||
console.log("[ThemePanel] Setting child panel:", panel);
|
||||
// 先关闭旧的子页面
|
||||
this.closeChildPanel();
|
||||
this.currentChildPanel = panel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭子页面
|
||||
*/
|
||||
public closeChildPanel(): void {
|
||||
if (this.currentChildPanel) {
|
||||
console.log("[ThemePanel] Closing child panel");
|
||||
if (this.currentChildPanel.onClose) {
|
||||
this.currentChildPanel.onClose();
|
||||
} else if (this.currentChildPanel.destroy) {
|
||||
this.currentChildPanel.destroy();
|
||||
}
|
||||
this.currentChildPanel = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面被隐藏时调用
|
||||
*/
|
||||
public onHide(): void {
|
||||
console.log("[ThemePanel] onHide called");
|
||||
this.closeChildPanel();
|
||||
this.cancelCurrentOperation();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消当前操作
|
||||
*/
|
||||
private cancelCurrentOperation(): void {
|
||||
if (this.abortController) {
|
||||
console.log("[ThemePanel] 取消当前操作");
|
||||
this.abortController.abort();
|
||||
this.abortController = null;
|
||||
}
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
console.log("[ThemePanel] onDestroy called");
|
||||
|
||||
// 取消当前操作
|
||||
this.cancelCurrentOperation();
|
||||
|
||||
// 关闭子页面
|
||||
this.closeChildPanel();
|
||||
|
||||
// 清理缓存
|
||||
this.clearCache();
|
||||
|
||||
// 重置加载状态
|
||||
this.isLoading = false;
|
||||
this.loadingPromise = null;
|
||||
|
||||
// 移除事件监听器(修复回调函数引用问题)
|
||||
Utils.removeInnerEL(
|
||||
InnerMsgCode.LanguageChange,
|
||||
this,
|
||||
this.onLanguageChange
|
||||
);
|
||||
Utils.removeInnerEL(InnerMsgCode.BalanceChange, this, this.onBalanceChange);
|
||||
Utils.removeInnerEL(
|
||||
InnerMsgCode.VipExpireChange,
|
||||
this,
|
||||
this.onVipExpireChange
|
||||
);
|
||||
}
|
||||
|
||||
// 事件回调函数(避免匿名函数导致的内存泄漏)
|
||||
private onLanguageChange = () => {
|
||||
this.recName.string = LanguageUtils.getText(this.rectNameKey);
|
||||
};
|
||||
|
||||
private onBalanceChange = () => {
|
||||
this.refreshCoinNum();
|
||||
};
|
||||
|
||||
private onVipExpireChange = () => {
|
||||
this.refreshVipExpire();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user