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 */
15import { BlockTag } from '../model/BlockTag';
16import { tagDesRule } from '../utils';
17import { formatDate } from '../utils/timeUtil';
18
19import { Block } from './Block';
20import { IconPalette } from './IconPalette';
21
22@Component
23export struct IconBlock {
24  @Prop title: string;
25  @Link isEnabled: boolean;
26  @Link icon: ResourceStr;
27  @StorageLink('Block') listIconBlockTags: Array<BlockTag> = [];
28  @State listIconBlockTagsTemp: Array<BlockTag> = [];
29  build() {
30    Block({
31      title: this.title,
32      isEnabled: $isEnabled
33    }) {
34      IconPalette({
35        icon: this.icon,
36        title: this.title,
37        onChange: (icon,index) => {
38          this.icon = icon;
39          if (this.isEnabled) {
40            let time = formatDate(new Date(Date.now()))
41            this.listIconBlockTagsTemp.push(new BlockTag(time, tagDesRule(this.title, (index + 1).toString())))
42            setTimeout(() => {
43              if (AppStorage.get('Block')) {
44                this.listIconBlockTags = this.listIconBlockTags.concat(this.listIconBlockTagsTemp);
45                this.listIconBlockTagsTemp = [];
46              } else {
47                if (this.listIconBlockTagsTemp) {
48                  this.listIconBlockTags = this.listIconBlockTags.concat(this.listIconBlockTagsTemp);
49                }
50                if (this.listIconBlockTags) {
51                  this.listIconBlockTagsTemp = [];
52                }
53              }
54            }, 200)
55          }
56        },
57        isEnabled: this.isEnabled,
58      })
59    }
60  }
61}
62
63@Preview
64@Component
65struct IconBlockPreview {
66  @State isEnabled: boolean = true
67  @State icon: ResourceStr = '#fd5d77'
68
69  build() {
70    IconBlock({
71      title: '标题',
72      isEnabled: $isEnabled,
73      icon: $icon
74    })
75  }
76}