1/**
2 * Copyright (c) 2022 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 AdvancedSettingsController from './advancedSettingsController';
16import DeviceUtil from '../../../utils/DeviceUtil';
17import { MmsSimpleDialog } from '../../../views/MmsDialogs';
18import { MoreMenu } from '../../../views/MmsMenu';
19import router from '@system.router';
20import { SettingItemJump } from '../../../views/SettingItem';
21import WantUtil from '../../../utils/WantUtil';
22import SettingsController from  '../settingsController'
23
24@Entry
25@Component
26struct AdvancedSettings {
27    @StorageLink('AdvancedSettingsController') @Watch('changeSelectState') mAdvancedSettingsCtrl:
28        AdvancedSettingsController = AdvancedSettingsController.getInstance();
29    @StorageLink('SettingsController') @Watch('changeSelectState') mSettingsCtrl:
30        SettingsController = SettingsController.getInstance()
31    private gridColumns: GridRowColumnOption = { sm: 4, md: 8, lg: 12 };
32    private girdSpan: GridColColumnOption = { sm: 4, md: 6, lg: 8 };
33    private gridColOffset: GridColColumnOption = { md: 1, lg: 2 };
34    private gridGutter: string = '12vp';
35    private dialogGridCount: number = 4;
36    dialogAlignment: DialogAlignment = DeviceUtil.isTablet() ? DialogAlignment.Center : DialogAlignment.Bottom;
37    dialogOffset: Offset = DeviceUtil.isTablet() ? { dx: 0, dy: 0 } : { dx: 0, dy: -16 };
38    private restoreDialog = new MmsSimpleDialog({
39        message: $r('app.string.restore_all_default_settings'),
40        primaryButton: {
41            value: $r('app.string.cancel'),
42            action: () => {
43            }
44        },
45        secondaryButton: {
46            value: $r('app.string.restore'),
47            action: () => {
48                this.mAdvancedSettingsCtrl.restoreSettingPageSwitchValue();
49                this.mSettingsCtrl.restoreSettingsPageSwitchValue();
50            }
51        }
52    });
53    @Provide menuItems: Array<any> = [
54        {
55            value: $r('app.string.restore_default_settings'),
56            action: () => {
57                this.restoreDialog.show();
58            },
59            enabled: true
60        }
61    ];
62    @Provide statusTitle: Resource = this.mAdvancedSettingsCtrl.deliveryReportSwitchInText;
63    changeSelectState(){
64        this.statusTitle = this.mAdvancedSettingsCtrl.deliveryReportSwitchInText
65    }
66    deliveryReportsDialogCtrl: CustomDialogController = new CustomDialogController({
67        builder: DeliveryReportsDialog({
68            cancel: () => {
69                this.mAdvancedSettingsCtrl.cancelRestore();
70            },
71            confirm: (isOnOfSms: boolean, isOnOfMms: boolean) => {
72                this.mAdvancedSettingsCtrl.setRestore(isOnOfSms, isOnOfMms);
73            },
74            isOnOfSms: this.mAdvancedSettingsCtrl.checkedValueOfSms,
75            isOnOfMms: this.mAdvancedSettingsCtrl.checkedValueOfMms
76        }),
77        autoCancel: false,
78        alignment: this.dialogAlignment,
79        offset: this.dialogOffset,
80        gridCount: this.dialogGridCount
81    })
82
83    /**
84     * The function executes after a new instance of the custom component is created and before its build function
85     * is executed.
86     * Allows state variables to be changed in the aboutToAppear function, and these changes will take effect in
87     * subsequent executions of the build function.
88     */
89    aboutToAppear() {
90        this.mAdvancedSettingsCtrl.onInit();
91    }
92    /**
93     * Triggers once when this page is displayed. In scenarios such as routing and application access to the foreground
94     * and background, only customized components modified by @Entry take effect.
95     */
96    onPageShow() {
97        this.mAdvancedSettingsCtrl.onShow();
98        WantUtil.getWant();
99    }
100    /**
101     * Triggers once when this page disappears. In scenarios such as routing and application access to the foreground
102     * and background, only customized components modified by @Entry take effect.
103     */
104    onPageHide() {
105    }
106    /**
107     * Function executes before custom component destructor consumption.
108     * Changing state variables in the aboutToDisappear function is not allowed, especially changes to the @Link
109     * variable may cause unstable application behavior.
110     */
111    aboutToDisappear() {
112        this.deliveryReportsDialogCtrl = null;
113    }
114    /**
115     * Triggered when a user clicks the back button. Only the customized component modified by @Entry takes effect.
116     * If true is returned, the page processes the return logic and does not route the page.
117     * If false is returned, the default return logic is used.
118     * If no value is returned, the value is treated as false.
119     */
120    onBackPress() {
121    }
122
123    build() {
124        Column() {
125            // Top Device Title
126            Row() {
127                Image($rawfile('icon/ic_message_back.svg'))
128                    .width($r('app.float.icon_side_length_medium'))
129                    .height($r('app.float.icon_side_length_medium'))
130                    .margin({
131                        left: $r('app.float.action_bar_margin_left')
132                    })
133                    .onClick(() => {
134                        router.back()
135                    })
136
137                Row().width($r('app.float.space_16'))
138
139                Text($r('app.string.advanced'))
140                    .fontSize($r('app.float.action_bar_text_size'))
141                    .fontColor(Color.Black)
142                    .fontWeight(FontWeight.Bold)
143
144                Row().width('100%').flexShrink(1)
145
146                Column() {
147                    MoreMenu()
148                }
149                .margin({ right: $r('app.float.action_bar_margin_right') })
150            }
151            .width('100%')
152            .height($r('app.float.action_bar_height'))
153
154            Scroll() {
155                GridRow({ columns: this.gridColumns, gutter: this.gridGutter }) {
156                    GridCol({ span: this.girdSpan, offset: this.gridColOffset }) {
157                        // All settings
158                        Column({ space: 12 }) {
159                            // Setting items of the first group
160                            Column() {
161                                // Delivery Report
162                                SettingItemJump({
163                                    primaryTitle: $r('app.string.delivery_reports'),
164                                    secondaryTitle: $r('app.string.delivery_reports_hint'),
165                                    showBottomDivider: false,
166                                    OnClick: (event?: ClickEvent) => {
167                                        this.deliveryReportsDialogCtrl.open();
168                                    }
169                                })
170                                // Automatically downloading MMs
171                                SettingItemJump({
172                                    primaryTitle: $r('app.string.auto_retrieve_mms'),
173//                                    statusTitle: this.mAdvancedSettingsCtrl.autoRetrieveMmsSwitchInText,
174                                    visibilityShow: Visibility.None,
175                                    OnClick: (event?: ClickEvent) => {
176                                        this.mAdvancedSettingsCtrl.showAutoRetrieveMmsDialog();
177                                    }
178                                })
179                            }
180                            .width('100%')
181                            .padding({
182                                top: $r('app.float.settings_item_padding_top'),
183                                bottom: $r('app.float.settings_item_padding_bottom'),
184                                left: $r('app.float.settings_item_padding_left'),
185                                right: $r('app.float.settings_item_padding_right')
186                            })
187                            .border({
188                                radius: $r('app.float.settings_items_radius'),
189                                color: $r('sys.color.ohos_id_color_text_field_bg')
190                            })
191                            .backgroundColor($r('sys.color.ohos_id_color_text_field_bg'))
192
193                            // Setting items of the second group
194                            Column() {
195                            }
196                            .width('100%')
197                            .padding({
198                                top: $r('app.float.settings_item_padding_top'),
199                                bottom: $r('app.float.settings_item_padding_bottom'),
200                                left: $r('app.float.settings_item_padding_left'),
201                                right: $r('app.float.settings_item_padding_right')
202                            })
203                            .border({
204                                radius: $r('app.float.settings_items_radius'),
205                                color: $r('sys.color.ohos_id_color_text_field_bg')
206                            })
207                            .backgroundColor($r('sys.color.ohos_id_color_text_field_bg'))
208                            .visibility(Visibility.None)
209
210                            // Setting items of the third group
211                            Column() {
212                                // SMSC
213                                SettingItemJump({
214                                    primaryTitle: $r('app.string.sms_center'),
215                                    showBottomDivider: true,
216                                    OnClick: (event?: ClickEvent) => {
217                                        this.mAdvancedSettingsCtrl.jumpToSmsCenterPage(1);
218                                    }
219                                })
220                                // Managing SIM Card Information
221                                SettingItemJump({
222                                    primaryTitle: $r('app.string.manage_sim_card_messages'),
223                                    OnClick: (event?: ClickEvent) => {
224                                        this.mAdvancedSettingsCtrl.jumpToManageSimPage(1);
225                                    }
226                                })
227                            }
228                            .width('100%')
229                            .padding({
230                                top: $r('app.float.settings_item_padding_top'),
231                                bottom: $r('app.float.settings_item_padding_bottom'),
232                                left: $r('app.float.settings_item_padding_left'),
233                                right: $r('app.float.settings_item_padding_right')
234                            })
235                            .border({
236                                radius: $r('app.float.settings_items_radius'),
237                                color: $r('sys.color.ohos_id_color_text_field_bg')
238                            })
239                            .backgroundColor($r('sys.color.ohos_id_color_text_field_bg'))
240                            .visibility(Visibility.None)
241                        }
242                        .margin({
243                            top: $r('app.float.settings_items_margin_top'),
244                            bottom: $r('app.float.settings_items_margin_bottom')
245                        })
246                    }
247                }
248            }
249            .layoutWeight(1)
250            .align(Alignment.Top)
251        }
252        .width('100%')
253        .height('100%')
254        .padding({ left: $r('app.float.page_padding_left'), right: $r('app.float.page_padding_right') })
255        .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
256    }
257}
258
259/**
260 * Delivery report selection box
261 */
262@CustomDialog
263struct DeliveryReportsDialog {
264    controller: CustomDialogController;
265    cancel: () => void;
266    confirm: (isOnOfSms: boolean, isOnOfMms: boolean) => void;
267    private isOnOfSms: boolean = false;
268    private isOnOfMms: boolean = false;
269
270    build() {
271        Column() {
272            Column() {
273                Text($r('app.string.delivery_reports'))
274                    .width('100%')
275                    .height(56)
276                    .textAlign(TextAlign.Start)
277                    .fontSize(20)
278                    .fontColor($r('sys.color.ohos_id_color_foreground'))
279                    .lineHeight(28)
280                    .fontWeight(FontWeight.Medium)
281                    .fontFamily('HarmonyHeiTi')
282                Stack() {
283                    Text($r('app.string.sms'))
284                        .fontSize(16)
285                        .fontColor($r('sys.color.ohos_id_color_foreground'))
286                        .lineHeight(22)
287                        .fontWeight(FontWeight.Medium)
288                        .fontFamily('HarmonyHeiTi')
289                        .align(Alignment.Start)
290                        .width('100%')
291                    Toggle({ type: ToggleType.Checkbox, isOn: this.isOnOfSms })
292                        .width('100%')
293                        .height(20)
294                        .selectedColor($r('sys.color.ohos_id_color_activated'))
295                        .align(Alignment.End)
296                        .onChange((isOn: boolean) => {
297                            this.isOnOfSms = isOn
298                        })
299                }.margin({ bottom: 5 })
300
301                Divider()
302                    .vertical(false)
303                    .strokeWidth(0.5)
304                    .width('100%')
305                    .color($r('sys.color.ohos_id_color_list_separator'))
306
307                Stack() {
308                    Text($r('app.string.mms'))
309                        .fontSize(16)
310                        .fontColor($r('sys.color.ohos_id_color_foreground'))
311                        .lineHeight(22)
312                        .fontWeight(FontWeight.Medium)
313                        .fontFamily('HarmonyHeiTi')
314                        .align(Alignment.Start)
315                        .width('100%')
316                    Toggle({ type: ToggleType.Checkbox, isOn: this.isOnOfMms })
317                        .width('100%')
318                        .height(20)
319                        .selectedColor($r('sys.color.ohos_id_color_activated'))
320                        .align(Alignment.End)
321                        .onChange((isOn: boolean) => {
322                            this.isOnOfMms = isOn
323                        })
324                }.margin({ top: 5 })
325
326            }
327            .padding({ left: 24, right: 24, bottom: 9 })
328
329            Flex({
330                direction: FlexDirection.Row,
331                justifyContent: FlexAlign.SpaceEvenly,
332                alignItems: ItemAlign.Center
333            }) {
334                Button() {
335                    Text($r('app.string.cancel'))
336                        .textAlign(TextAlign.Center)
337                        .fontSize(16)
338                        .fontColor($r('sys.color.ohos_id_color_activated'))
339                        .fontWeight(FontWeight.Medium)
340                        .fontFamily('HarmonyHeiTi')
341                        .lineHeight(22)
342                }
343                .backgroundColor($r('sys.color.ohos_id_color_background_transparent'))
344                .layoutWeight(1)
345                .height(40)
346                .onClick(() => {
347                    this.controller.close();
348                    this.cancel();
349                })
350
351                Divider().vertical(true).strokeWidth(0.5).height(24).color($r('sys.color.ohos_id_color_list_separator'))
352                Button() {
353                    Text($r('app.string.ok'))
354                        .textAlign(TextAlign.Center)
355                        .fontSize(16)
356                        .fontColor($r('sys.color.ohos_id_color_activated'))
357                        .fontWeight(FontWeight.Medium)
358                        .fontFamily('HarmonyHeiTi')
359                        .lineHeight(22)
360                }
361                .backgroundColor($r('sys.color.ohos_id_color_background_transparent'))
362                .layoutWeight(1)
363                .height(40)
364                .onClick(() => {
365                    this.controller.close();
366                    this.confirm(this.isOnOfSms, this.isOnOfMms);
367                })
368            }
369            .width('100%')
370            .height(56)
371            .padding({ left: 16, right: 16, bottom: 16 })
372        }
373        .width('100%')
374        .borderRadius(16)  // Customdialog need to set borderRadius
375        .backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))
376    }
377}
378