1- import { common } from '@kit.AbilityKit';
21import { TokenIcon } from "../components/TokenIcon";
2+ import { showSelectFilePicker } from "../utils/FileUtils";
33import { AegisIconPack, TokenIconPacks } from "../utils/TokenUtils";
4+ import { fileIo } from "@kit.CoreFileKit";
45
56@Preview
67@CustomDialog
@@ -13,12 +14,12 @@ export struct SelectIconDialog {
1314 @State selected_icon_path: string = '';
1415
1516 @State icon_matched_with_issuer: string[] = [''];
16- @State default_icon_pack_visable : boolean = true;
17- @State aegis_icon_pack_visable : boolean[] = [];
17+ @State default_icon_pack_visible : boolean = true;
18+ @State aegis_icon_pack_visible : boolean[] = [];
1819
1920 private default_icon_data: MyDataSource = new MyDataSource();
21+ private user_icon_data: MyDataSource = new MyDataSource();
2022 private aegis_icon_data: MyDataSource[] = [];
21- private appCtx = AppStorage.get<common.UIAbilityContext>('appContext') as common.UIAbilityContext;
2223
2324 aboutToAppear(): void {
2425 // find issuer's icon
@@ -28,7 +29,7 @@ export struct SelectIconDialog {
2829 }
2930 TokenIconPacks.aegis_icon_packs.forEach((aegis_icon_pack) => {
3031 const aegis_icon = aegis_icon_pack.getIconPathByIssuer(this.issuer);
31- this.aegis_icon_pack_visable .push(false);
32+ this.aegis_icon_pack_visible .push(false);
3233 if (aegis_icon !== '') {
3334 this.icon_matched_with_issuer.push(aegis_icon);
3435 }
@@ -38,6 +39,11 @@ export struct SelectIconDialog {
3839 TokenIconPacks.default_icon_pack.default_icon_pack_path.forEach((path) => {
3940 this.default_icon_data.pushData(path!);
4041 });
42+
43+ TokenIconPacks.user_added_icon_packs.icon_path.forEach((path) => {
44+ this.user_icon_data.pushData(path!);
45+ });
46+
4147 TokenIconPacks.aegis_icon_packs.forEach((aegis_icon_pack) => {
4248 let data_source = new MyDataSource();
4349 for (let icon of aegis_icon_pack.icons) {
@@ -58,25 +64,88 @@ export struct SelectIconDialog {
5864 }
5965
6066 @Builder
61- ItemIcon(path: string, visable : boolean) {
67+ ItemIcon(path: string, visible: boolean, removable : boolean) {
6268 ListItem() {
63- Stack() {
69+ Stack( { alignContent: Alignment.TopStart } ) {
6470 if (path === '') {
6571 Text($r('app.string.dialog_icon_default'))
72+ .margin({ top: 15 })
6673 } else {
6774 TokenIcon({ issuer: '', icon_path: path, bypassColorFilter: true })
6875 }
76+
77+ Button({ type: ButtonType.Circle }) {
78+ SymbolGlyph($r('sys.symbol.plus'))
79+ .fontSize(15)
80+ .fontColor([Color.White])
81+ .fontWeight(FontWeight.Medium)
82+ .rotate({ angle: 45 })
83+ }
84+ .backgroundColor(Color.Red)
85+ .width(20)
86+ .height(20)
87+ .margin({ left: 0, top: 0 })
88+ .visibility(removable ? Visibility.Visible : Visibility.Hidden)
89+ .onClick(() => {
90+ AlertDialog.show({
91+ message: $r('app.string.icon_alert_remove_confirm_msg'),
92+ autoCancel: true,
93+ alignment: DialogAlignment.Center,
94+ primaryButton: {
95+ defaultFocus: false,
96+ value: $r('app.string.dialog_btn_cancel'),
97+ action: () => {
98+ return
99+ }
100+ },
101+ secondaryButton: {
102+ value: $r('app.string.dialog_btn_confirm'),
103+ fontColor: Color.Red,
104+ action: () => {
105+ TokenIconPacks.user_added_icon_packs.removeIcon(path);
106+ this.user_icon_data.removeData(path);
107+ }
108+ }
109+ })
110+ })
111+
69112 Radio({ value: '', group: 'icon' })
70113 .width(20)
71114 .height(20)
72- .margin({ left:30 , top: 30 })
115+ .margin({ left: 20 , top: 20 })
73116 .onChange((checked) => {
74117 if (checked) {
75118 this.selected_icon_path = path;
76119 }
77120 })
78121 }
79- .visibility(visable ? Visibility.Visible : Visibility.Hidden)
122+ .visibility(visible ? Visibility.Visible : Visibility.Hidden)
123+ }
124+ .margin({ left: 10 })
125+ }
126+
127+ @Builder
128+ ItemIconAddButton() {
129+ ListItem() {
130+ Button({ type: ButtonType.Circle }) {
131+ SymbolGlyph($r('sys.symbol.plus'))
132+ .fontSize(30)
133+ .fontWeight(FontWeight.Bold)
134+ .fontColor([Color.Gray])
135+ }
136+ .backgroundColor(Color.Transparent)
137+ .shadow({ radius: 10, color: Color.Gray })
138+ .width(40)
139+ .height(40)
140+ .onClick(() => {
141+ showSelectFilePicker(1, ["PNG|.png", "JPEG|.jpg"]).then((uris) => {
142+ fileIo.open(uris[0]).then((file) => {
143+ TokenIconPacks.user_added_icon_packs.addIcon(file);
144+ this.user_icon_data.pushData(TokenIconPacks.user_added_icon_packs.latest_icon_path);
145+ });
146+
147+ });
148+ })
80149 }
81150 .margin({ left: 10 })
82151 }
@@ -86,25 +155,34 @@ export struct SelectIconDialog {
86155 List({ space: 10 }) {
87156 ListItemGroup({ header: this.itemHead($r('app.string.dialog_icon_recommend')) }) {
88157 ForEach(this.icon_matched_with_issuer, (icon_path: string) => {
89- this.ItemIcon(icon_path, true)
158+ this.ItemIcon(icon_path, true, false)
159+ })
160+ }
161+ ListItemGroup({ header: this.itemHead(TokenIconPacks.user_added_icon_packs.name) }) {
162+ LazyForEach(this.user_icon_data, (path: string) => {
163+ this.ItemIcon(path, true, true)
90164 })
165+ this.ItemIconAddButton()
91166 }
167+ .onClick(() => {
168+ this.default_icon_pack_visible = !this.default_icon_pack_visible;
169+ })
92170 ListItemGroup({ header: this.itemHead(TokenIconPacks.default_icon_pack.name) }) {
93171 LazyForEach(this.default_icon_data, (path: string) => {
94- this.ItemIcon(path, true)
172+ this.ItemIcon(path, true, false )
95173 })
96174 }
97175 .onClick(() => {
98- this.default_icon_pack_visable = !this.default_icon_pack_visable ;
176+ this.default_icon_pack_visible = !this.default_icon_pack_visible ;
99177 })
100178 ForEach(TokenIconPacks.aegis_icon_packs, (aegis_icon_pack: AegisIconPack, index: number) => {
101179 ListItemGroup({ header: this.itemHead(aegis_icon_pack.name) }) {
102180 LazyForEach(this.aegis_icon_data[index], (path: string) => {
103- this.ItemIcon(path, true)
181+ this.ItemIcon(path, true, false )
104182 })
105183 }
106184 .onClick(() => {
107- this.aegis_icon_pack_visable [index] = !this.aegis_icon_pack_visable [index];
185+ this.aegis_icon_pack_visible [index] = !this.aegis_icon_pack_visible [index];
108186 })
109187 }, (aegis_icon_pack: AegisIconPack) => {
110188 return aegis_icon_pack.on_device_path;
@@ -221,7 +299,6 @@ class BasicDataSource implements IDataSource {
221299 }
222300}
223301
224-
225302class MyDataSource extends BasicDataSource {
226303 private dataArray: string[] = [];
227304
@@ -234,7 +311,15 @@ class MyDataSource extends BasicDataSource {
234311 }
235312
236313 public pushData(data: string): void {
237- this.dataArray.push(data);
238- this.notifyDataAdd(this.dataArray.length - 1);
314+ if (this.dataArray.findIndex(_ => _ === data) < 0) {
315+ this.dataArray.push(data);
316+ this.notifyDataAdd(this.dataArray.length - 1);
317+ }
318+ }
319+
320+ public removeData(data: string): void {
321+ const index = this.dataArray.findIndex(_ => _ === data);
322+ this.dataArray = this.dataArray.filter(_ => _ !== data);
323+ this.notifyDataDelete(index);
239324 }
240325}
0 commit comments