1/*
2 * Copyright (c) 2023 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 update from '@ohos.update';
17import { UpgradeCallResult } from '@ohos/common/src/main/ets/const/update_const';
18import { TitleBar } from '@ohos/common/src/main/ets/component/TitleBar';
19import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils';
20import { VersionPageInfo } from '@ohos/common/src/main/ets/manager/UpgradeInterface';
21import { UpgradeAdapter } from '@ohos/ota/src/main/ets/UpgradeAdapter';
22import { ChangelogContent } from '@ohos/ota/src/main/ets/components/ChangelogContent';
23import { OtaUpdateManager } from '@ohos/ota/src/main/ets/manager/OtaUpdateManager';
24import { VersionUtils } from '@ohos/ota/src/main/ets/util/VersionUtils';
25
26/**
27 * 当前版本页面
28 *
29 * @since 2022-06-06
30 */
31@Entry
32@Component
33struct CurrentVersion {
34
35  @State private versionArray: Array<VersionPageInfo> = null;
36
37  aboutToAppear() {
38    globalThis.currentVersionThis = this;
39  }
40
41  public onLanguageChange(): void {
42    this.initCurrentVersionPageInfo();
43  }
44
45  private async initCurrentVersionPageInfo(): Promise<void> {
46    let upgradeData = await OtaUpdateManager.getInstance().getCurrentVersionInfo();
47    let componentDescription = await OtaUpdateManager.getInstance().getCurrentVersionDescription();
48    if (upgradeData.callResult == UpgradeCallResult.OK) {
49      if (!upgradeData.data) {
50        return;
51      }
52      let components: update.VersionComponent[] = VersionUtils.sortComponents(
53        upgradeData.data?.versionComponents);
54      this.versionArray = components.map((component: update.VersionComponent) => {
55        return UpgradeAdapter.getInstance()
56          .getPageInstance()?.getCurrentVersionPageInfo(components, componentDescription?.data);
57      }).filter((versionPageInfo: VersionPageInfo) => {
58        return versionPageInfo != null;
59      });
60    }
61  }
62
63  onPageShow(): void {
64    this.logInfo('onPageShow CurrentVersionPage');
65    globalThis.currentPage = 'pages/currentVersion';
66    this.initCurrentVersionPageInfo();
67  }
68
69  onPageHide(): void {
70    this.logInfo('onPageHide CurrentVersionPage');
71  }
72
73
74  onBackPress() {
75    return false;
76  }
77
78  build() {
79    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) {
80      Column() {
81        TitleBar({ title: $r('app.string.title_current_version'), onBack: this.onBackPress.bind(this) })
82      }.flexShrink(0)
83
84      Scroll() {
85        Column() {
86          Column() {
87            Column() {
88              Image($r('app.media.logo'))
89                .height($r('app.float.progress_logo_other_height'))
90                .width($r('app.float.progress_logo_other_width'))
91            }
92            .padding({
93              top: $r('app.float.progress_logo_other_padding_top'),
94              bottom: $r('app.float.progress_logo_other_padding_bottom')
95            })
96          }.flexShrink(0)
97
98          if (this.versionArray) {
99            Column() {
100              ForEach(this.versionArray, (pageInfo: VersionPageInfo) => {
101                Text(pageInfo.version)
102                  .fontSize($r('app.float.text_size_version_name'))
103                  .maxLines(5)
104                  .textOverflow({ overflow: TextOverflow.Ellipsis })
105                  .fontWeight(FontWeight.Medium)
106                  .width('100%')
107                  .textAlign(this.versionArray.length > 1 ? TextAlign.Start : TextAlign.Center)
108                  .padding({
109                    right: $r('app.float.version_padding'),
110                    left: $r('app.float.version_padding'),
111                    bottom: this.versionArray.length > 1 ? $r('app.float.current_version_name_margin_bottom') :
112                    $r('app.float.current_version_name_margin_bottom_single')
113                  })
114                ChangelogContent({
115                  isCurrentPage: true,
116                  isNeedFold: this.versionArray?.length > 1,
117                  description: JSON.stringify([pageInfo.changelog])
118                })
119              })
120            }.flexShrink(0).margin({ bottom: $r('app.float.current_version_changelog_margin_bottom') })
121          }
122        }.width('100%').flexShrink(0)
123      }.width('100%')
124      .scrollable(ScrollDirection.Vertical)
125      .scrollBar(BarState.Auto)
126    }
127    .width('100%')
128    .height('100%')
129    .backgroundColor($r('app.color.page_background'))
130  }
131
132  private logInfo(message: string): void {
133    LogUtils.info('CurrentVersion', message);
134  }
135}