本质上,点赞
、关注
、收藏
这三连
的逻辑都是一样的,所以就只以点赞
为例来说明。
点赞数据源
在src/main/ets/datasource/
目录中创建LikeDataSource.ets
文件,内容如下。
import { VideoInfo } from '../model/VideoInfo';
import { BaseDataSource } from './BaseDataSource';
/**
* 点赞信息数据源
*
*/
export class LikeDataSource extends BaseDataSource<VideoInfo> {
constructor(videoArray: Array<VideoInfo>) {
super(videoArray)
}
/**
* 是否已经点赞过
*/
existLike(id: number): boolean {
let dataSource: Array<VideoInfo> = this.getDataSource();
// 遍历数组
for (let i: number = 0; i < dataSource.length; i++) {
if (dataSource[i].videoId === id) {
return true;
}
}
return false;
}
/**
* 根据ID删除数据
*/
removeById(id: number): void {
let dataSource: Array<VideoInfo> = this.getDataSource();
// 遍历数据并删除
for (let i: number = 0; i < dataSource.length; i++) {
if (dataSource[i].videoId === id) {
this.remove(i);
break;
}
}
}
}
原创大约 4 分钟