From 24501f2064e08464c47dcfb8b8680f39550a88e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=94=E4=B8=89=E7=9A=84=E9=9D=93=E4=BB=94?= <36236732+yindushenwen@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:48:38 +0800 Subject: [PATCH] test (#222) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * git-test * 订阅源-本地导入1 --- .../src/main/ets/common/utils/FileHandler.ets | 15 +- .../view/Subscription/SubscriptionIndex.ets | 128 ++++++++++-------- 2 files changed, 80 insertions(+), 63 deletions(-) diff --git a/entry/src/main/ets/common/utils/FileHandler.ets b/entry/src/main/ets/common/utils/FileHandler.ets index 3c0776ac..39bec1e4 100644 --- a/entry/src/main/ets/common/utils/FileHandler.ets +++ b/entry/src/main/ets/common/utils/FileHandler.ets @@ -1,12 +1,13 @@ import { BusinessError } from '@ohos.base'; -import fs, { Options, WriteOptions } from '@ohos.file.fs'; +import fs, { Options } from '@ohos.file.fs'; import { common, Context } from '@kit.AbilityKit'; import { chaptersItem } from 'ets/componets/dataList/ReaderChaptersItem'; import { BookList } from '../../componets/dataList/bookList'; import { addShelfBook } from '../../storage/bookListData'; import { showMessage } from '../../componets/common/promptShow'; -import { fileIo, picker } from '@kit.CoreFileKit'; +import { picker } from '@kit.CoreFileKit'; import { util } from '@kit.ArkTS'; +import { buffer } from '@kit.ArkTS' let context = getContext(this) as common.UIAbilityContext; let filePath = context.filesDir; @@ -32,6 +33,14 @@ export class FileHandler { }); } + static async ReadJSONFile(srcUrl: string) { + let file = fs.openSync(srcUrl, fs.OpenMode.READ_WRITE); + let arrayBuffer = new ArrayBuffer(40960); + let readLen: number = await fs.read(file.fd, arrayBuffer) + let JSONString = buffer.from(arrayBuffer, 0, readLen).toString(); + return JSONString + } + static async readFile(readFileUrl: string) { let chapterNumber = 0 const chapters: chaptersItem[] = []; @@ -66,7 +75,7 @@ export class FileHandler { return chapters } - static async documentPickerFile(maxSelectNumber:number,fileSuffixFilters:Array) { + static async documentPickerFile(maxSelectNumber: number, fileSuffixFilters: Array) { // 尝试写一个通用的picker try { const documentSelectOptions = new picker.DocumentSelectOptions(); diff --git a/entry/src/main/ets/pages/view/Subscription/SubscriptionIndex.ets b/entry/src/main/ets/pages/view/Subscription/SubscriptionIndex.ets index 06958cb8..39edccb7 100644 --- a/entry/src/main/ets/pages/view/Subscription/SubscriptionIndex.ets +++ b/entry/src/main/ets/pages/view/Subscription/SubscriptionIndex.ets @@ -25,8 +25,8 @@ import { FileHandler } from 'ets/common/utils/FileHandler' @Component export default struct SubscriptionIndex { @State isShowImport: boolean = false - @State isShowNewSources:boolean = false - @Prop @Watch('currentTabIndexChange')currentTabIndex: number + @State isShowNewSources: boolean = false + @Prop @Watch('currentTabIndexChange') currentTabIndex: number @StorageLink('topRectHeight') topRectHeight: number = 0 @State moreList: IconTitleVo[] = [ @@ -36,25 +36,22 @@ export default struct SubscriptionIndex { new IconTitleVo($r('app.media.icon_rule_batch'), '批量管理'), ] @State RecentlyUsed: rssSourcesHistory[] = [] - @State loading: boolean = false + @State subscriptionList: rssSources[] = [] + @State rssSourcesList: rssSources[] = [] + @State groupList: rssGroupList[] = [] //刷新 - refresh = ()=>{ + refresh = () => { this.resetState() this.getList() - setTimeout(()=>{ - rssSourcesHistoryDao.search().then((rssSourcesHistoryList:rssSourcesHistory[])=>{ + setTimeout(() => { + rssSourcesHistoryDao.search().then((rssSourcesHistoryList: rssSourcesHistory[]) => { this.RecentlyUsed = rssSourcesHistoryList }) - },100) + }, 100) } - @State subscriptionList:rssSources[]=[] - - - @State rssSourcesList:rssSources[] = [] - @State groupList: rssGroupList[] =[] - currentTabIndexChange(){ + currentTabIndexChange() { this.refresh() } @@ -96,41 +93,46 @@ export default struct SubscriptionIndex { this.groupList = Object.values(groupMap) } - - build() { - Column(){ + Column() { //搜索Start Flex( { - direction:FlexDirection.Row, + direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween } ) { Row({ - space:CommonConstants.SPACE_20 + space: CommonConstants.SPACE_20 }) { - Image($r('app.media.icon_subscription_index')).width(CommonConstants.COMMON_WIDTH_45_36).height(CommonConstants.COMMON_HEIGHT_21_6) + Image($r('app.media.icon_subscription_index')) + .width(CommonConstants.COMMON_WIDTH_45_36) + .height(CommonConstants.COMMON_HEIGHT_21_6) Search({ placeholder: '搜索订阅', icon: "/common/images/search.svg" }) .layoutWeight(1) .height(CommonConstants.COMMON_HEIGHT_40) .backgroundColor(Color.White) - .onClick(()=>{ + .onClick(() => { router.pushUrl({ url: 'pages/view/Subscription/components/SubscriptionSearch' }) }) .focusOnTouch(false) //不可获取焦点 } - .padding({right: PaddingConstants.PADDING_16}) - Image($r('app.media.addSubscription')).width(ImageConstants.IMG_WIDTH_24).height(ImageConstants.IMG_HEIGHT_24).bindMenu(this.MenuBuilder) + .padding({ right: PaddingConstants.PADDING_16 }) + + Image($r('app.media.addSubscription')) + .width(ImageConstants.IMG_WIDTH_24) + .height(ImageConstants.IMG_HEIGHT_24) + .bindMenu(this.MenuBuilder) } .height('8%') + //搜索End //最近使用Start RssSourcesHistory({ - RecentlyUsed:this.RecentlyUsed + RecentlyUsed: this.RecentlyUsed }) if (this.loading) { Column() { @@ -144,45 +146,45 @@ export default struct SubscriptionIndex { } else if (!this.groupList.length) { SubscriptionContent({ subscriptionTitle: '收藏', - showAddDialog:()=>{ + showAddDialog: () => { this.isShowNewSources = true } }) } else { - Scroll(){ - Column(){ - ForEach(this.groupList,(item:rssGroupList)=>{ + Scroll() { + Column() { + ForEach(this.groupList, (item: rssGroupList) => { SubscriptionContent({ subscriptionTitle: item.title, contentList: item.list, - refresh:this.refresh + refresh: this.refresh }) }) } } .align(Alignment.Top) - .padding({bottom:20}) + .padding({ bottom: 20 }) .layoutWeight(1) .scrollBar(BarState.Off) .scrollable(ScrollDirection.Vertical) } Flex() - .bindSheet($$this.isShowImport,this.dialogImportFunction(),{ + .bindSheet($$this.isShowImport, this.dialogImportFunction(), { detents: [SheetSize.FIT_CONTENT, SheetSize.LARGE, 200], dragBar: true, - showClose:false, - onDisappear:()=>{ + showClose: false, + onDisappear: () => { this.isShowImport = false } }) Flex() - .bindSheet($$this.isShowNewSources,this.dialogNewSourcesFunction(),{ + .bindSheet($$this.isShowNewSources, this.dialogNewSourcesFunction(), { height: 200, dragBar: true, - showClose:false, - onDisappear:()=>{ + showClose: false, + onDisappear: () => { this.isShowNewSources = false } }) @@ -200,24 +202,26 @@ export default struct SubscriptionIndex { .height('100%') } - @Builder dialogImportFunction() { + @Builder + dialogImportFunction() { Flex() { SubscriptionImport({ - subscriptionList:this.subscriptionList - ,show: this.isShowImport - }).onDisAppear(()=>{ + subscriptionList: this.subscriptionList + , show: this.isShowImport + }).onDisAppear(() => { this.refresh() }) } } - @Builder dialogNewSourcesFunction() { - Column(){ + @Builder + dialogNewSourcesFunction() { + Column() { dialogNewSources({ - btnClick:()=>{ + btnClick: () => { this.isShowNewSources = false } - }).onDisAppear(()=>{ + }).onDisAppear(() => { this.refresh() }) @@ -245,7 +249,7 @@ export default struct SubscriptionIndex { this.MenuOnclick(index) }) - if(index !== 5){ + if (index !== 5) { Divider() } } @@ -257,31 +261,27 @@ export default struct SubscriptionIndex { .width(100) } - - - async MenuOnclick(value:number|string){ - switch (value){ + async MenuOnclick(value: number | string) { + switch (value) { case 0: - this.pasteAnalyze(); + // 粘贴导入 + this.pasteAnalyze(0); break; case 1: - //todo:本地导入 - let result = await FileHandler.documentPickerFile(1, ['.json']) - showMessage('订阅源本地导入成功') + // 本地导入 + this.pasteAnalyze(1); break case 2: - this.isShowNewSources =true + this.isShowNewSources = true break; case 3: break case 4: - break - } } - async pasteAnalyze() { + async pasteAnalyze(value: number) { try { const url = getPasteDataSync() if (!url) { @@ -294,10 +294,18 @@ export default struct SubscriptionIndex { // showMessage('复制的链接不正确') // return // } - const res: AxiosResponse = await axios - .get(url ?? 'http://yuedu.miaogongzi.net/shuyuan/miaogongziDY.json'); - console.log('res', JSON.stringify(res)) - this.subscriptionList = res.data; + if (value === 1) { + let FileResult = await FileHandler.documentPickerFile(1, ['.json']) + FileHandler.ReadJSONFile(FileResult[0]).then(result => { + // console.log('result:' + result) + this.subscriptionList = JSON.parse(result) as rssSources[] + }) + } else if (value === 0) { + const res: AxiosResponse = await axios + .get(url ?? 'http://yuedu.miaogongzi.net/shuyuan/miaogongziDY.json'); + console.log('res', JSON.stringify(res)) + this.subscriptionList = res.data; + } this.isShowImport = true; } catch (err) { console.error('错误 ' + err);