• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { nameList, phoneList } from '../application/ItemListData';
17import { common, autoFillManager } from '@kit.AbilityKit';
18import { hilog } from '@kit.PerformanceAnalysisKit';
19
20let storage = LocalStorage.getShared();
21let fillCallback = storage.get<autoFillManager.FillRequestCallback>('fillCallback');
22let viewData: autoFillManager.ViewData | undefined = storage.get<autoFillManager.ViewData>('viewData');
23let type: number | undefined = storage.get<number>('autoFillType');
24let context = AppStorage.get('contextEvent') as common.UIAbilityContext;
25let itemList: Array<string> | undefined;
26let typeToDataMap: Map<number, Array<string>> = new Map();
27const TAG: string = 'autoFill';
28const DOMAIN_NUMBER: number = 0xFF00;
29const FULL_PHONE_NUMBER: number = 15;
30const PERSON_FULL_NAME: number = 10;
31
32typeToDataMap.set(PERSON_FULL_NAME, nameList);
33typeToDataMap.set(FULL_PHONE_NUMBER, phoneList);
34
35function successFunc(index: number, item: string) {
36  if (viewData && index >= 0 && type != undefined) {
37    itemList = typeToDataMap.get(type);
38    if (itemList) {
39      for (let i = 0; i < viewData.pageNodeInfos.length; i++) {
40        if (viewData.pageNodeInfos[i].isFocus === true) {
41          viewData.pageNodeInfos[i].value = item;
42          break;
43        }
44      }
45    }
46    if (fillCallback) {
47      let response: autoFillManager.FillResponse = { viewData: viewData };
48      fillCallback.onSuccess(response);
49    }
50  }
51}
52
53@Entry
54@Component
55struct SelectorList {
56  @State itemListData: Array<string> = [];
57
58  aboutToAppear(): void {
59    if (type != undefined) {
60      let maybeUndefined: Array<string> | undefined = typeToDataMap.get(type);
61      if (maybeUndefined != undefined) {
62        this.itemListData = maybeUndefined;
63      }
64    }
65    context.eventHub.on('updateRequestData', (eventData: Array<string>) => {
66      hilog.info(DOMAIN_NUMBER, TAG, 'eventHub called' + eventData);
67      this.itemListData = eventData;
68    });
69  }
70
71  build() {
72    Row() {
73      Column() {
74        List({ space: 10, initialIndex: 0 }) {
75          ForEach(this.itemListData, (item: string, index: number) => {
76            ListItem() {
77              Text(item ? item : '')
78                .width('100%')
79                .height(40)
80                .fontSize($r('sys.float.ohos_id_text_size_body1'))
81                .fontColor($r('sys.color.ohos_id_color_text_primary'))
82                .textAlign(TextAlign.Start)
83                .margin({ left: '4.8%' })
84                .borderRadius($r('sys.float.ohos_id_corner_radius_card'))
85            }
86            .onClick(() => {
87              hilog.info(DOMAIN_NUMBER, TAG, 'click item: ', item);
88              successFunc(index, item);
89            })
90          })
91        }
92        .listDirection(Axis.Vertical)
93        .scrollBar(BarState.Off)
94        .friction(0.6)
95        .divider({ strokeWidth: 1, color: $r('app.color.placeholderColor'), startMargin: 20, endMargin: 20 })
96        .edgeEffect(EdgeEffect.Spring)
97        .onScrollIndex((firstIndex: number, lastIndex: number, centerIndex: number) => {
98          hilog.info(DOMAIN_NUMBER, TAG, 'first' + firstIndex);
99          hilog.info(DOMAIN_NUMBER, TAG, 'last' + lastIndex);
100          hilog.info(DOMAIN_NUMBER, TAG, 'center' + centerIndex);
101        })
102        .onScroll((scrollOffset: number, scrollState: ScrollState) => {
103          hilog.info(DOMAIN_NUMBER, TAG, `onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` +
104            scrollOffset);
105        })
106      }
107      .width('100%')
108      .shadow(ShadowStyle.OUTER_FLOATING_SM)
109    }
110    .height('100%')
111    .shadow(ShadowStyle.OUTER_FLOATING_SM)
112  }
113}