• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 */
15import router from '@ohos.router';
16import dataAbility from '@ohos.data.dataAbility'
17import { BASE_URI, COLUMNS, DA_HELPER } from '../model/DaHelperConst'
18import { getListFromResultSet } from '../model/BookDataModel'
19import { BookView } from '../component/BookView'
20import { BookDataSource } from '../model/BookDataSource'
21
22const TAG = 'DataAbility.Query'
23
24@Entry
25@Component
26struct Query {
27  @State bookList: BookDataSource = new BookDataSource()
28
29  query(queryStr) {
30    let predicates = new dataAbility.DataAbilityPredicates()
31    predicates.contains('name', queryStr)
32      .or()
33      .contains('introduction', queryStr)
34    DA_HELPER.query(BASE_URI, COLUMNS, predicates, (err, resultSet) => {
35      this.bookList['dataArray'] = getListFromResultSet(resultSet)
36      this.bookList.notifyDataReload()
37    })
38  }
39  aboutToAppear() {
40    console.info(TAG + ' aboutToAppear')
41  }
42  build() {
43    Column() {
44      Row() {
45        Image($r('app.media.ic_back'))
46          .width(40).height(40)
47          .objectFit(ImageFit.Contain)
48          .onClick(() => {
49            router.back()
50          })
51        Text($r('app.string.title'))
52          .fontColor(Color.White)
53          .fontSize(20)
54          .layoutWeight(1)
55      }
56      .height(50).width('100%')
57      .backgroundColor('#0D9FFB')
58      .padding({ left: 10, right: 10 })
59
60      Stack({ alignContent: Alignment.End }) {
61        TextInput({ placeholder: $r('app.string.search_placeholder') })
62          .height('85%').width('94%')
63          .padding({ left: 15 })
64          .margin('3%')
65          .backgroundColor('#F0F0F0')
66          .border({ radius: 20 })
67          .onChange((value: string) => {
68            console.info(TAG + ' query str=' + value)
69            if (value !== '') {
70              this.query(value)
71            }else{
72              this.bookList['dataArray'] = []
73              this.bookList.notifyDataReload()
74            }
75          })
76        Image($r('app.media.search_gray'))
77          .height(40).width(40)
78          .margin({ right: '5%' })
79          .objectFit(ImageFit.Contain)
80      }
81      .width('100%').height(50)
82      .backgroundColor('#F5F5F5')
83
84      List() {
85        LazyForEach(this.bookList, item => {
86          ListItem() {
87            BookView({ book: item, deleteCallback: null })
88          }
89        }, item => item.id.toString())
90      }
91      .width('100%')
92      .layoutWeight(1)
93      .divider({ strokeWidth: 1, color: Color.Gray, startMargin: 10, endMargin: 10 })
94    }
95  }
96}