import ApprovalData from "../../models/approval/ApprovalData"; import {…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import ApprovalData from "../../models/approval/ApprovalData"; import { findMyApprovalOptions } from "../../api/Approval"; import QueryDTO from "../../models/QueryDTO"; import { ResultBody, STORAGE_AUTH_USER } from "../../utils/HttpHelper"; import Loadable from "../../utils/LoadingHelper"; import { BasicDataSource } from "@xmky_ohos/ui"; import { Type } from "@kit.ArkUI"; import util from "@ohos.util"; import { DateUtil } from "@xmky_ohos/util"; import { AuthUserInfo } from "../../common/CommonUrl";
@ObservedV2 export class ApprovalViewModel { @Trace id?: string; @Trace employeeNumber?: string; @Trace employeeId?: string; @Trace employeeName?: string; @Trace departmentName?: string; @Trace theme?: string; @Trace applicationTime?: Date; /**
updateDetail(detail: ApprovalData) { this.id = detail.id; this.employeeNumber = detail.employeeNumber; this.employeeId = detail.employeeId; this.employeeName = detail.employeeName; this.departmentName = detail.departmentName; this.theme = detail.theme; this.applicationTime = !detail.applicationTime || new util.types().isDate(detail.applicationTime) ? detail.applicationTime : DateUtil.parse(detail.applicationTime + ''); this.currentStatus = detail.currentStatus; this.remark = detail.remark; this.createTime = !detail.createTime || new util.types().isDate(detail.createTime) ? detail.createTime : DateUtil.parse(detail.createTime + ''); this.billId = detail.billId; this.billType = detail.billType; this.level = detail.level; this.delete = detail.delete; this.type = detail.type; this.startTime = !detail.startTime || new util.types().isDate(detail.startTime) ? detail.startTime : DateUtil.parse(detail.startTime + ''); this.endTime = !detail.endTime || new util.types().isDate(detail.endTime) ? detail.endTime : DateUtil.parse(detail.endTime + ''); this.processInstanceId = detail.processInstanceId; this.currentAssignee = detail.currentAssignee; this.currentAssigneeNumber = detail.currentAssigneeNumber; this.currentAssigneeName = detail.currentAssigneeName; this.myResult = detail.myResult; this.myReason = detail.myReason; this.billSealType = detail.billSealType; }
/**
@ObservedV2 export class ApprovalListViewModel extends BasicDataSource { @Type(ApprovalViewModel) @Trace approvals: ApprovalViewModel[] = [];
totalCount(): number { return this.approvals.length; }
getData(index: number): ApprovalViewModel { return this.approvals[index]; }
push(approval: ApprovalViewModel) { this.approvals.push(approval); this.notifyDataAdd(this.approvals.length - 1); } }
@ObservedV2 export default class ListViewModel extends Loadable { @Trace loading: number = 0; @Trace error?: string; @Type(ApprovalListViewModel) @Trace approvalListViewModel: ApprovalListViewModel = new ApprovalListViewModel(); @Trace scroller: Scroller; @Trace showSideBar: boolean = false; @Trace pageNum: number = 1; @Trace approvalOption?: number | null; context?: UIContext; pageInfos?: NavPathStack;
@Monitor('pageNum', 'approvalOption') async onQueryParamsChange(_monitor: IMonitor) { await this.queryApprovalList(); }
constructor(approvalOption: number | null = null, scroller: Scroller = new Scroller()) { super(); this.scroller = scroller; this.approvalOption = approvalOption; }
async queryApprovalList() { const res: ResultBody = await findMyApprovalOptions({ pageNum: this.pageNum, pageSize: 10, // stringEndTime: searchData.stringEndTime ? moment(searchData.stringEndTime).add(1, 'days').format("YYYY-MM-DD") : null, fromApp: true, myResult: this.approvalOption, } as QueryDTO); if (!res.success) { return; } const approvals = (res.data as ResultBody).data as ApprovalData[]; const oldCount = this.approvalListViewModel.approvals.length; if (1 >= this.pageNum) { this.approvalListViewModel.approvals = []; } this.approvalListViewModel.approvals.concat(approvals.map(approval => { const approvalViewModel = new ApprovalViewModel(); approvalViewModel.updateDetail(approval); return approvalViewModel; })); if (1 >= this.pageNum) { this.approvalListViewModel.notifyDataReload(); } else { for (let i = oldCount; i < this.approvalListViewModel.approvals.length; i++) { this.approvalListViewModel.notifyDataAdd(i); } } }
secondaryContentFontSizeVp(resource: Resource) { return this.context!.px2vp(this.context!.fp2px(getContext().resourceManager.getNumber(resource))) }
secondaryContentWidthVp(resource: Resource, length: number) { return this.secondaryContentFontSizeVp(resource) * length; } } 在鸿蒙中我既想支持刷新又想支持加载下一页,数据都是通过调用queryApprovalList更新,怎么设计才是优雅合理的
为了在鸿蒙中实现既支持下拉刷新又支持上拉加载更多的功能,我们可以按照以下步骤进行设计:
ListViewModel中添加状态变量,如isLoading和hasMore,用于管理加载状态和是否还有更多数据。pageNum和pageSize管理分页数据,确保每次加载正确的数据量。refresh():重置页码并重新加载数据,用于下拉刷新。loadMore():增加页码并加载下一页数据,用于上拉加载更多。queryApprovalList方法:根据当前页码决定是刷新还是加载更多,并更新数据和UI。@ObservedV2
export default class ListViewModel extends Loadable {
@Trace loading: number = 0;
@Trace error?: string;
@Type(ApprovalListViewModel)
@Trace approvalListViewModel: ApprovalListViewModel = new ApprovalListViewModel();
@Trace scroller: Scroller;
@Trace showSideBar: boolean = false;
@Trace pageNum: number = 1;
@Trace approvalOption?: number | null;
@Trace isLoading: boolean = false;
@Trace hasMore: boolean = true;
context?: UIContext;
pageInfos?: NavPathStack;
constructor(approvalOption: number | null = null, scroller: Scroller = new Scroller()) {
super();
this.scroller = scroller;
this.approvalOption = approvalOption;
}
async refresh() {
this.pageNum = 1;
this.hasMore = true;
await this.queryApprovalList();
}
async loadMore() {
if (this.isLoading || !this.hasMore) {
return;
}
this.pageNum++;
await this.queryApprovalList();
}
async queryApprovalList() {
if (this.isLoading) {
return;
}
this.isLoading = true;
this.loading = 1;
try {
const res: ResultBody = await findMyApprovalOptions({
pageNum: this.pageNum,
pageSize: 10,
fromApp: true,
myResult: this.approvalOption,
} as QueryDTO);
if (!res.success) {
this.error = res.message || '加载失败';
return;
}
const approvals = (res.data as ResultBody).data as ApprovalData[];
const oldCount = this.approvalListViewModel.approvals.length;
if (this.pageNum === 1) {
this.approvalListViewModel.approvals = [];
}
const newApprovals = approvals.map(approval => {
const approvalViewModel = new ApprovalViewModel();
approvalViewModel.updateDetail(approval);
return approvalViewModel;
});
this.approvalListViewModel.approvals = this.approvalListViewModel.approvals.concat(newApprovals);
if (this.pageNum === 1) {
this.approvalListViewModel.notifyDataReload();
} else {
for (let i = oldCount; i < this.approvalListViewModel.approvals.length; i++) {
this.approvalListViewModel.notifyDataAdd(i);
}
}
this.hasMore = approvals.length >= 10;
} catch (error) {
this.error = '加载失败';
} finally {
this.isLoading = false;
this.loading = 0;
}
}
// 其他方法保持不变...
}
在UI组件中,监听下拉和上拉事件,并调用对应的方法:
List({ space: 10 }) {
ForEach(this.listViewModel.approvalListViewModel.approvals, (item: ApprovalViewModel) => {
ListItem() {
// 渲染列表项
}
}, (item: ApprovalViewModel) => item.id)
// 下拉刷新
.onPullDown(() => {
this.listViewModel.refresh();
})
// 上拉加载更多
.onReachBottom(() => {
this.listViewModel.loadMore();
})
}
通过上述设计,我们实现了一个支持下拉刷新和上拉加载更多的数据列表。关键点包括:
这样设计既保持了代码的简洁性,又确保了功能的完整性和用户体验的流畅性。