Skip to content

Commit 24501f2

Browse files
authored
test (#222)
* git-test * 订阅源-本地导入1
1 parent 63847fa commit 24501f2

File tree

2 files changed

+80
-63
lines changed

2 files changed

+80
-63
lines changed

entry/src/main/ets/common/utils/FileHandler.ets

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { BusinessError } from '@ohos.base';
2-
import fs, { Options, WriteOptions } from '@ohos.file.fs';
2+
import fs, { Options } from '@ohos.file.fs';
33
import { common, Context } from '@kit.AbilityKit';
44
import { chaptersItem } from 'ets/componets/dataList/ReaderChaptersItem';
55
import { BookList } from '../../componets/dataList/bookList';
66
import { addShelfBook } from '../../storage/bookListData';
77
import { showMessage } from '../../componets/common/promptShow';
8-
import { fileIo, picker } from '@kit.CoreFileKit';
8+
import { picker } from '@kit.CoreFileKit';
99
import { util } from '@kit.ArkTS';
10+
import { buffer } from '@kit.ArkTS'
1011

1112
let context = getContext(this) as common.UIAbilityContext;
1213
let filePath = context.filesDir;
@@ -32,6 +33,14 @@ export class FileHandler {
3233
});
3334
}
3435

36+
static async ReadJSONFile(srcUrl: string) {
37+
let file = fs.openSync(srcUrl, fs.OpenMode.READ_WRITE);
38+
let arrayBuffer = new ArrayBuffer(40960);
39+
let readLen: number = await fs.read(file.fd, arrayBuffer)
40+
let JSONString = buffer.from(arrayBuffer, 0, readLen).toString();
41+
return JSONString
42+
}
43+
3544
static async readFile(readFileUrl: string) {
3645
let chapterNumber = 0
3746
const chapters: chaptersItem[] = [];
@@ -66,7 +75,7 @@ export class FileHandler {
6675
return chapters
6776
}
6877

69-
static async documentPickerFile(maxSelectNumber:number,fileSuffixFilters:Array<string>) {
78+
static async documentPickerFile(maxSelectNumber: number, fileSuffixFilters: Array<string>) {
7079
// 尝试写一个通用的picker
7180
try {
7281
const documentSelectOptions = new picker.DocumentSelectOptions();

entry/src/main/ets/pages/view/Subscription/SubscriptionIndex.ets

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import { FileHandler } from 'ets/common/utils/FileHandler'
2525
@Component
2626
export default struct SubscriptionIndex {
2727
@State isShowImport: boolean = false
28-
@State isShowNewSources:boolean = false
29-
@Prop @Watch('currentTabIndexChange')currentTabIndex: number
28+
@State isShowNewSources: boolean = false
29+
@Prop @Watch('currentTabIndexChange') currentTabIndex: number
3030
@StorageLink('topRectHeight') topRectHeight: number = 0
3131
@State
3232
moreList: IconTitleVo[] = [
@@ -36,25 +36,22 @@ export default struct SubscriptionIndex {
3636
new IconTitleVo($r('app.media.icon_rule_batch'), '批量管理'),
3737
]
3838
@State RecentlyUsed: rssSourcesHistory[] = []
39-
4039
@State loading: boolean = false
40+
@State subscriptionList: rssSources[] = []
41+
@State rssSourcesList: rssSources[] = []
42+
@State groupList: rssGroupList[] = []
4143
//刷新
42-
refresh = ()=>{
44+
refresh = () => {
4345
this.resetState()
4446
this.getList()
45-
setTimeout(()=>{
46-
rssSourcesHistoryDao.search().then((rssSourcesHistoryList:rssSourcesHistory[])=>{
47+
setTimeout(() => {
48+
rssSourcesHistoryDao.search().then((rssSourcesHistoryList: rssSourcesHistory[]) => {
4749
this.RecentlyUsed = rssSourcesHistoryList
4850
})
49-
},100)
51+
}, 100)
5052
}
5153

52-
@State subscriptionList:rssSources[]=[]
53-
54-
55-
@State rssSourcesList:rssSources[] = []
56-
@State groupList: rssGroupList[] =[]
57-
currentTabIndexChange(){
54+
currentTabIndexChange() {
5855
this.refresh()
5956
}
6057

@@ -96,41 +93,46 @@ export default struct SubscriptionIndex {
9693
this.groupList = Object.values(groupMap)
9794
}
9895

99-
100-
10196
build() {
102-
Column(){
97+
Column() {
10398
//搜索Start
10499
Flex(
105100
{
106-
direction:FlexDirection.Row,
101+
direction: FlexDirection.Row,
107102
alignItems: ItemAlign.Center,
108103
justifyContent: FlexAlign.SpaceBetween
109104
}
110105
) {
111106
Row({
112-
space:CommonConstants.SPACE_20
107+
space: CommonConstants.SPACE_20
113108
}) {
114-
Image($r('app.media.icon_subscription_index')).width(CommonConstants.COMMON_WIDTH_45_36).height(CommonConstants.COMMON_HEIGHT_21_6)
109+
Image($r('app.media.icon_subscription_index'))
110+
.width(CommonConstants.COMMON_WIDTH_45_36)
111+
.height(CommonConstants.COMMON_HEIGHT_21_6)
115112
Search({ placeholder: '搜索订阅', icon: "/common/images/search.svg" })
116113
.layoutWeight(1)
117114
.height(CommonConstants.COMMON_HEIGHT_40)
118115
.backgroundColor(Color.White)
119-
.onClick(()=>{
116+
.onClick(() => {
120117
router.pushUrl({
121118
url: 'pages/view/Subscription/components/SubscriptionSearch'
122119
})
123120
})
124121
.focusOnTouch(false) //不可获取焦点
125122
}
126-
.padding({right: PaddingConstants.PADDING_16})
127-
Image($r('app.media.addSubscription')).width(ImageConstants.IMG_WIDTH_24).height(ImageConstants.IMG_HEIGHT_24).bindMenu(this.MenuBuilder)
123+
.padding({ right: PaddingConstants.PADDING_16 })
124+
125+
Image($r('app.media.addSubscription'))
126+
.width(ImageConstants.IMG_WIDTH_24)
127+
.height(ImageConstants.IMG_HEIGHT_24)
128+
.bindMenu(this.MenuBuilder)
128129
}
129130
.height('8%')
131+
130132
//搜索End
131133
//最近使用Start
132134
RssSourcesHistory({
133-
RecentlyUsed:this.RecentlyUsed
135+
RecentlyUsed: this.RecentlyUsed
134136
})
135137
if (this.loading) {
136138
Column() {
@@ -144,45 +146,45 @@ export default struct SubscriptionIndex {
144146
} else if (!this.groupList.length) {
145147
SubscriptionContent({
146148
subscriptionTitle: '收藏',
147-
showAddDialog:()=>{
149+
showAddDialog: () => {
148150
this.isShowNewSources = true
149151
}
150152
})
151153
} else {
152-
Scroll(){
153-
Column(){
154-
ForEach(this.groupList,(item:rssGroupList)=>{
154+
Scroll() {
155+
Column() {
156+
ForEach(this.groupList, (item: rssGroupList) => {
155157
SubscriptionContent({
156158
subscriptionTitle: item.title,
157159
contentList: item.list,
158-
refresh:this.refresh
160+
refresh: this.refresh
159161
})
160162
})
161163
}
162164
}
163165
.align(Alignment.Top)
164-
.padding({bottom:20})
166+
.padding({ bottom: 20 })
165167
.layoutWeight(1)
166168
.scrollBar(BarState.Off)
167169
.scrollable(ScrollDirection.Vertical)
168170
}
169171

170172
Flex()
171-
.bindSheet($$this.isShowImport,this.dialogImportFunction(),{
173+
.bindSheet($$this.isShowImport, this.dialogImportFunction(), {
172174
detents: [SheetSize.FIT_CONTENT, SheetSize.LARGE, 200],
173175
dragBar: true,
174-
showClose:false,
175-
onDisappear:()=>{
176+
showClose: false,
177+
onDisappear: () => {
176178
this.isShowImport = false
177179
}
178180
})
179181

180182
Flex()
181-
.bindSheet($$this.isShowNewSources,this.dialogNewSourcesFunction(),{
183+
.bindSheet($$this.isShowNewSources, this.dialogNewSourcesFunction(), {
182184
height: 200,
183185
dragBar: true,
184-
showClose:false,
185-
onDisappear:()=>{
186+
showClose: false,
187+
onDisappear: () => {
186188
this.isShowNewSources = false
187189
}
188190
})
@@ -200,24 +202,26 @@ export default struct SubscriptionIndex {
200202
.height('100%')
201203
}
202204

203-
@Builder dialogImportFunction() {
205+
@Builder
206+
dialogImportFunction() {
204207
Flex() {
205208
SubscriptionImport({
206-
subscriptionList:this.subscriptionList
207-
,show: this.isShowImport
208-
}).onDisAppear(()=>{
209+
subscriptionList: this.subscriptionList
210+
, show: this.isShowImport
211+
}).onDisAppear(() => {
209212
this.refresh()
210213
})
211214
}
212215
}
213216

214-
@Builder dialogNewSourcesFunction() {
215-
Column(){
217+
@Builder
218+
dialogNewSourcesFunction() {
219+
Column() {
216220
dialogNewSources({
217-
btnClick:()=>{
221+
btnClick: () => {
218222
this.isShowNewSources = false
219223
}
220-
}).onDisAppear(()=>{
224+
}).onDisAppear(() => {
221225
this.refresh()
222226
})
223227

@@ -245,7 +249,7 @@ export default struct SubscriptionIndex {
245249
this.MenuOnclick(index)
246250
})
247251

248-
if(index !== 5){
252+
if (index !== 5) {
249253
Divider()
250254
}
251255
}
@@ -257,31 +261,27 @@ export default struct SubscriptionIndex {
257261
.width(100)
258262
}
259263

260-
261-
262-
async MenuOnclick(value:number|string){
263-
switch (value){
264+
async MenuOnclick(value: number | string) {
265+
switch (value) {
264266
case 0:
265-
this.pasteAnalyze();
267+
// 粘贴导入
268+
this.pasteAnalyze(0);
266269
break;
267270
case 1:
268-
//todo:本地导入
269-
let result = await FileHandler.documentPickerFile(1, ['.json'])
270-
showMessage('订阅源本地导入成功')
271+
// 本地导入
272+
this.pasteAnalyze(1);
271273
break
272274
case 2:
273-
this.isShowNewSources =true
275+
this.isShowNewSources = true
274276
break;
275277
case 3:
276278
break
277279
case 4:
278-
279280
break
280-
281281
}
282282
}
283283

284-
async pasteAnalyze() {
284+
async pasteAnalyze(value: number) {
285285
try {
286286
const url = getPasteDataSync()
287287
if (!url) {
@@ -294,10 +294,18 @@ export default struct SubscriptionIndex {
294294
// showMessage('复制的链接不正确')
295295
// return
296296
// }
297-
const res: AxiosResponse = await axios
298-
.get(url ?? 'http://yuedu.miaogongzi.net/shuyuan/miaogongziDY.json');
299-
console.log('res', JSON.stringify(res))
300-
this.subscriptionList = res.data;
297+
if (value === 1) {
298+
let FileResult = await FileHandler.documentPickerFile(1, ['.json'])
299+
FileHandler.ReadJSONFile(FileResult[0]).then(result => {
300+
// console.log('result:' + result)
301+
this.subscriptionList = JSON.parse(result) as rssSources[]
302+
})
303+
} else if (value === 0) {
304+
const res: AxiosResponse = await axios
305+
.get(url ?? 'http://yuedu.miaogongzi.net/shuyuan/miaogongziDY.json');
306+
console.log('res', JSON.stringify(res))
307+
this.subscriptionList = res.data;
308+
}
301309
this.isShowImport = true;
302310
} catch (err) {
303311
console.error('错误 ' + err);

0 commit comments

Comments
 (0)