From d292ec2ff92e27abcd94ed01adafbfa7ba6fdbe0 Mon Sep 17 00:00:00 2001 From: Andy <454846659@qq.com> Date: Thu, 11 Jul 2024 11:28:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B9=A6=E6=BA=90=E7=AE=A1=E7=90=86,?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=8C=E6=9B=B4=E6=96=B0=EF=BC=8C=E5=B7=B2?= =?UTF-8?q?=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/utils/utils.ets | 6 +++ .../ets/componets/import/ImportCommon.ets | 44 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/entry/src/main/ets/common/utils/utils.ets b/entry/src/main/ets/common/utils/utils.ets index 3b24a588..528b706f 100644 --- a/entry/src/main/ets/common/utils/utils.ets +++ b/entry/src/main/ets/common/utils/utils.ets @@ -28,3 +28,9 @@ export const sleep = (duration: number = 1000): Promise => { }, duration) }) } + +// 去除对象数组中重复的内容 +export const deduplicateObjects = (list: T[]): T[] => { + const uniqueSet = new Set(list.map(item => JSON.stringify(item))); + return Array.from(uniqueSet).map(strItem => JSON.parse(strItem) as T); +}; \ No newline at end of file diff --git a/entry/src/main/ets/componets/import/ImportCommon.ets b/entry/src/main/ets/componets/import/ImportCommon.ets index 4bf1d646..a679f648 100644 --- a/entry/src/main/ets/componets/import/ImportCommon.ets +++ b/entry/src/main/ets/componets/import/ImportCommon.ets @@ -10,6 +10,9 @@ import { showMessage } from '../common/promptShow'; import Score from '../common/Score'; import Tag from '../common/Tag'; import { BookSource, GroupList } from '../dataList/BookSource'; +import { JSON } from '@kit.ArkTS'; +import json from '@ohos.util.json'; +import { deduplicateObjects } from '../../common/utils/utils'; @Component export default struct ImportCommon { @@ -20,6 +23,8 @@ export default struct ImportCommon { @State groupList: GroupList[] = [] @State currentIndex: number = 0 @State activeNameMap: Record = {} + @State alreadyNameMap: Record = {} + @State updateNameMap: Record = {} @State fullActive: boolean = false @State waringActive: boolean = false private scroll: Scroller = new Scroller() @@ -29,20 +34,34 @@ export default struct ImportCommon { FileHandler.readJsonFile(this.pathBookSource).then(data => { const bookSource = data ?? [] this.localSourceList = bookSource + this.getGroupList() + }).catch(() => { + this.getGroupList() }) - this.getGroupList() } getGroupList() { const groupMap: Record = {}; this.sourceList.forEach((item: BookSource) => { const title = item.bookSourceGroup; + const bookSourceName = item.bookSourceName; // 如果groupMap中还没有这个group,则创建一个新的GroupList对象 if (!groupMap[title]) { groupMap[title] = { title: title, list: [] }; } // 向当前group的list中添加BookSource对象 groupMap[title].list.push(item); + for (let i = 0; i < this.localSourceList.length; i++) { + const localItem = this.localSourceList[i]; + if (localItem.bookSourceGroup + localItem.bookSourceName === title + bookSourceName) { + this.alreadyNameMap[title + bookSourceName] = true + if (JSON.stringify(localItem) !== JSON.stringify(item)) { + this.activeNameMap[title + bookSourceName] = true + this.updateNameMap[title + bookSourceName] = true + } + break; + } + } }) this.groupList = Object.values(groupMap); } @@ -105,7 +124,7 @@ export default struct ImportCommon { .onChange((val: boolean) => { this.fullActive = val this.sourceList.forEach((item: BookSource) => { - this.activeNameMap[item.bookSourceName] = val + this.activeNameMap[item.bookSourceGroup + item.bookSourceName] = val }) }) @@ -162,8 +181,18 @@ export default struct ImportCommon { .backgroundColor(0xff6600) .layoutWeight(1) .onClick(() => { - const dataList = this.sourceList.filter(item => this.activeNameMap[item.bookSourceName] ?? false) - FileHandler.writeJsonFile(this.pathBookSource, JSON.stringify(dataList)) + const newDataList = [...this.localSourceList]; + const activeDataList = + this.sourceList.filter(item => this.activeNameMap[item.bookSourceGroup + item.bookSourceName] ?? false) + activeDataList.forEach(item => { + const localIndex = newDataList.findIndex(localItem => localItem.bookSourceGrade + localItem.bookSourceName === item.bookSourceGrade + item.bookSourceName) + if (localIndex != -1) { + newDataList[localIndex] = item + } else { + newDataList.push(item) + } + }) + FileHandler.writeJsonFile(this.pathBookSource, JSON.stringify(deduplicateObjects(newDataList))) this.show = false; this.resetState() showMessage('添加成功') @@ -217,10 +246,10 @@ export default struct ImportCommon { Blank() Checkbox({ name: 'checkbox' + j, group: 'checkboxGroup' + i }) - .select(this.activeNameMap[item.bookSourceName] ?? false) + .select(this.activeNameMap[item.bookSourceGroup + item.bookSourceName] ?? false) .selectedColor(0xff6600) .onChange((val: boolean) => { - this.activeNameMap[item.bookSourceName] = val + this.activeNameMap[item.bookSourceGroup + item.bookSourceName] = val }) } .width('100%') @@ -231,7 +260,8 @@ export default struct ImportCommon { Row() { Score({ score: j > 5 ? 5 : j, isBoutique: j >= 6 }) - Text('更新') + Text(this.updateNameMap[item.bookSourceGroup + item.bookSourceName] ?? false ? + '更新' : (this.alreadyNameMap[item.bookSourceGroup + item.bookSourceName] ?? false ? '已有' : '新增')) .fontSize(12) .fontColor('rgba(0, 0, 0, 0.45)') }