209 lines
7.1 KiB
TypeScript
209 lines
7.1 KiB
TypeScript
// scripts/ui/ErrorLazyLoadList.ts
|
||
import {
|
||
_decorator,
|
||
Component,
|
||
Prefab,
|
||
ScrollView,
|
||
instantiate,
|
||
UITransform,
|
||
Node,
|
||
Label,
|
||
director
|
||
} from 'cc';
|
||
import { ErrorItemData } from '../stores/ErrorItemData';
|
||
import { ErrorManager } from '../stores/ErrorManager';
|
||
import { ErrorItemButton } from '../ux/ErrorItemButton';
|
||
import { ErrorItemNamedButton } from '../ux/ErrorItemNamedButton';
|
||
import { SensorDescriptionField } from '../stores/SensorDescriptionField';
|
||
import { SensorFieldItem } from './SensorFieldItem';
|
||
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('ErrorLazyLoadList')
|
||
export class ErrorLazyLoadList extends Component {
|
||
public static instance: ErrorLazyLoadList | null = null;
|
||
|
||
@property(Prefab) public errorItemPrefab: Prefab = null!;
|
||
@property(Prefab) public errorItemNamedPrefab: Prefab = null!;
|
||
@property(Prefab) public sensorFieldItemPrefab: Prefab = null!;
|
||
@property(ScrollView) public scrollView: ScrollView = null!;
|
||
@property(Label) public header: Label = null!;
|
||
@property spacing = 10;
|
||
|
||
private _data: ErrorItemData[] = [];
|
||
private _filteredData: ErrorItemData[] | null = null;
|
||
private _mode: 'errors' | 'details' = 'errors';
|
||
private _sensorDescription!: { sensorName: string; fields: SensorDescriptionField[] };
|
||
private _itemHeight = 0;
|
||
private _loadedCount = 0;
|
||
private _content!: Node;
|
||
private _contentUI!: UITransform;
|
||
|
||
public get mode() {
|
||
return this._mode;
|
||
}
|
||
|
||
onLoad() {
|
||
ErrorLazyLoadList.instance = this;
|
||
director.addPersistRootNode(this.node);
|
||
|
||
if (!this.scrollView.content) {
|
||
console.error('ScrollView.content is null!');
|
||
return;
|
||
}
|
||
this._content = this.scrollView.content;
|
||
this._contentUI = this._content.getComponent(UITransform)!;
|
||
|
||
// Вычисляем высоту элемента
|
||
const tmp = instantiate(this.errorItemPrefab);
|
||
const tmpUI = tmp.getComponent(UITransform);
|
||
this._itemHeight = tmpUI ? tmpUI.height : 0;
|
||
tmp.destroy();
|
||
|
||
// Сбрасываем фильтр и заголовок
|
||
this._filteredData = null;
|
||
this._mode = 'errors';
|
||
this.header.string = 'Список всех аварий';
|
||
|
||
this.waitForErrorManager();
|
||
this.scrollView.node.on('scrolling', this.onScrolling, this);
|
||
}
|
||
|
||
onDestroy() {
|
||
// Сбрасываем статический экземпляр, если он указывает на этот компонент
|
||
if (ErrorLazyLoadList.instance === this) {
|
||
ErrorLazyLoadList.instance = null;
|
||
}
|
||
|
||
// Убираем подписку на обновления ошибок
|
||
ErrorManager.instance?.node?.off('errors-updated', this.onDataUpdated, this);
|
||
|
||
// Безопасно отписываемся от скроллинга (если scrollView или его node уже уничтожены, ничего не произойдёт)
|
||
this.scrollView?.node?.off('scrolling', this.onScrolling, this);
|
||
|
||
// Если нужно убрать persistence:
|
||
// director.removePersistRootNode(this.node);
|
||
}
|
||
|
||
|
||
private waitForErrorManager() {
|
||
if (!ErrorManager.instance) {
|
||
this.scheduleOnce(() => this.waitForErrorManager(), 0.1);
|
||
return;
|
||
}
|
||
ErrorManager.instance.node.on('errors-updated', this.onDataUpdated, this);
|
||
this.onDataUpdated();
|
||
}
|
||
|
||
private onDataUpdated() {
|
||
if (this._mode !== 'errors') return;
|
||
|
||
// Собираем все ошибки из API
|
||
const apiErrors = ErrorManager.instance.getErrorList().apiErrors;
|
||
const flat: ErrorItemData[] = [];
|
||
for (const sensorName in apiErrors) {
|
||
for (const err of apiErrors[sensorName]) {
|
||
flat.push({
|
||
sensorName,
|
||
alarmCode: err.alarmCode,
|
||
description: err.description,
|
||
timestamp: err.timestamp,
|
||
});
|
||
}
|
||
}
|
||
this._data = flat;
|
||
|
||
// Полная перерисовка списка
|
||
this._loadedCount = 0;
|
||
this._content.removeAllChildren();
|
||
this.loadNextBatch(20);
|
||
}
|
||
|
||
private onScrolling() {
|
||
const offsetY = this.scrollView.getScrollOffset().y;
|
||
const viewH = this.scrollView.node.getComponent(UITransform)!.height;
|
||
const contentH = this._contentUI.height;
|
||
if (offsetY + viewH + this._itemHeight * 2 >= contentH) {
|
||
this.loadNextBatch(20);
|
||
}
|
||
}
|
||
|
||
private loadNextBatch(batchSize: number) {
|
||
// Определяем источник данных
|
||
const source = this._mode === 'errors'
|
||
? (this._filteredData || this._data)
|
||
: this._sensorDescription.fields;
|
||
|
||
if (this._loadedCount >= source.length) return;
|
||
|
||
const end = Math.min(this._loadedCount + batchSize, source.length);
|
||
for (let i = this._loadedCount; i < end; i++) {
|
||
const itemData = source[i];
|
||
let node: Node;
|
||
|
||
if (this._mode === 'errors') {
|
||
// Ошибки
|
||
const data = itemData as ErrorItemData;
|
||
if (this._filteredData) {
|
||
node = instantiate(this.errorItemNamedPrefab);
|
||
node.getComponent(ErrorItemNamedButton)?.initialize(data);
|
||
} else {
|
||
node = instantiate(this.errorItemPrefab);
|
||
node.getComponent(ErrorItemButton)?.initialize(data);
|
||
}
|
||
} else {
|
||
// Детали
|
||
const field = itemData as SensorDescriptionField;
|
||
node = instantiate(this.sensorFieldItemPrefab);
|
||
node.getComponent(SensorFieldItem)?.setData(field);
|
||
}
|
||
|
||
node.setPosition(
|
||
0,
|
||
-(this._loadedCount * (this._itemHeight + this.spacing) + this._itemHeight / 2),
|
||
0
|
||
);
|
||
this._content.addChild(node);
|
||
this._loadedCount++;
|
||
}
|
||
|
||
this._contentUI.height = this._loadedCount * (this._itemHeight + this.spacing);
|
||
}
|
||
|
||
public filterBySensor(sensorName: string) {
|
||
this._mode = 'errors';
|
||
this._filteredData = this._data.filter(item => item.sensorName === sensorName);
|
||
this._loadedCount = 0;
|
||
this._content.removeAllChildren();
|
||
this.header.string = `Аварии на ${sensorName}`;
|
||
this.loadNextBatch(20);
|
||
}
|
||
|
||
public resetFilter() {
|
||
this._mode = 'errors';
|
||
this._filteredData = null;
|
||
this._loadedCount = 0;
|
||
this._content.removeAllChildren();
|
||
this.header.string = 'Список всех аварий';
|
||
this.loadNextBatch(20);
|
||
}
|
||
|
||
public showSensorDetails(data: { sensorName: string; fields: SensorDescriptionField[] }) {
|
||
this._mode = 'details';
|
||
this._sensorDescription = data;
|
||
this._loadedCount = 0;
|
||
this._content.removeAllChildren();
|
||
this.header.string = `Детали ${data.sensorName}`;
|
||
this.loadNextBatch(20);
|
||
}
|
||
|
||
public showErrors() {
|
||
this._mode = 'errors';
|
||
this._filteredData = null;
|
||
this._loadedCount = 0;
|
||
this._content.removeAllChildren();
|
||
this.header.string = 'Список всех аварий';
|
||
this.loadNextBatch(20);
|
||
}
|
||
}
|
||
|