18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ciimport callTabletPage from './dialer/DialerTablet';
168779efd5Sopenharmony_ciimport contactPage from './contacts/ContactList';
178779efd5Sopenharmony_ciimport favoritePage from './favorites/favoriteList';
188779efd5Sopenharmony_ciimport callPage from './phone/dialer/Dialer';
198779efd5Sopenharmony_ciimport IndexPresenter from '../presenter/IndexPresenter';
208779efd5Sopenharmony_ciimport { HiLog, StringUtil } from '../../../../../common';
218779efd5Sopenharmony_ciimport { PermissionManager } from '../../../../../common/src/main/ets/permission/PermissionManager';
228779efd5Sopenharmony_ciimport DialerPresenter from '../presenter/dialer/DialerPresenter';
238779efd5Sopenharmony_ciimport call from '@ohos.telephony.call';
248779efd5Sopenharmony_ciimport ContactListPresenter from '../presenter/contact/ContactListPresenter';
258779efd5Sopenharmony_ciimport CallRecordPresenter from '../presenter/dialer/callRecord/CallRecordPresenter';
268779efd5Sopenharmony_ciimport FavoriteListPresenter from '../presenter/favorite/FavoriteListPresenter';
278779efd5Sopenharmony_ciimport device from '@system.device';
288779efd5Sopenharmony_ciimport emitter from '@ohos.events.emitter';
298779efd5Sopenharmony_ci
308779efd5Sopenharmony_ciconst TAG = 'Index ';
318779efd5Sopenharmony_ci
328779efd5Sopenharmony_cilet storage = LocalStorage.GetShared()
338779efd5Sopenharmony_ci
348779efd5Sopenharmony_ci@Entry(storage)
358779efd5Sopenharmony_ci@Component
368779efd5Sopenharmony_cistruct Index {
378779efd5Sopenharmony_ci  private controller: TabsController = new TabsController();
388779efd5Sopenharmony_ci  @State mPermissionManager: PermissionManager = PermissionManager.getInstance();
398779efd5Sopenharmony_ci  mIndexPresenter: IndexPresenter = IndexPresenter.getInstance();
408779efd5Sopenharmony_ci  @StorageLink('mainTabsIndex') @Watch('onIndexChanged') mainTabsIndex: number = 0;
418779efd5Sopenharmony_ci  @StorageLink('teleNumber') @Watch('teleNumberChange') teleNumber: string = '';
428779efd5Sopenharmony_ci  mDialerPresenter: DialerPresenter = DialerPresenter.getInstance();
438779efd5Sopenharmony_ci  @State bottomTabIndex: number = call.hasVoiceCapability() ? 0 : 1;
448779efd5Sopenharmony_ci  @StorageLink('targetPage') @Watch('targetPageChange') targetPage: {
458779efd5Sopenharmony_ci    url?: string,
468779efd5Sopenharmony_ci    pageIndex?: number,
478779efd5Sopenharmony_ci    params?: any
488779efd5Sopenharmony_ci  } = {};
498779efd5Sopenharmony_ci  @LocalStorageProp('breakpoint') curBp: string = 'sm';
508779efd5Sopenharmony_ci  @State isContactSearch: boolean = false;
518779efd5Sopenharmony_ci  emitterId: number = 105;
528779efd5Sopenharmony_ci
538779efd5Sopenharmony_ci  teleNumberChange() {
548779efd5Sopenharmony_ci    if (!StringUtil.isEmpty(this.teleNumber)) {
558779efd5Sopenharmony_ci      this.mDialerPresenter.editPhoneNumber(this.teleNumber);
568779efd5Sopenharmony_ci      AppStorage.SetOrCreate('showDialBtn', true);
578779efd5Sopenharmony_ci      this.teleNumber = '';
588779efd5Sopenharmony_ci    }
598779efd5Sopenharmony_ci  }
608779efd5Sopenharmony_ci
618779efd5Sopenharmony_ci  targetPageChange() {
628779efd5Sopenharmony_ci    if (this.targetPage && this.targetPage.url) {
638779efd5Sopenharmony_ci      HiLog.i(TAG, 'targetPageChange in' + this.targetPage);
648779efd5Sopenharmony_ci      this.mIndexPresenter.goToPage(this.targetPage.url, this.targetPage.pageIndex, this.targetPage.params)
658779efd5Sopenharmony_ci      this.targetPage = {}
668779efd5Sopenharmony_ci    }
678779efd5Sopenharmony_ci  }
688779efd5Sopenharmony_ci
698779efd5Sopenharmony_ci  onIndexChanged(): void {
708779efd5Sopenharmony_ci    HiLog.i(TAG, 'uriTabIndex change:' + this.mainTabsIndex);
718779efd5Sopenharmony_ci    if (this.mainTabsIndex != this.bottomTabIndex) {
728779efd5Sopenharmony_ci      if (this.mainTabsIndex == 0 && !call.hasVoiceCapability()) {
738779efd5Sopenharmony_ci        HiLog.i(TAG, 'not hasVoiceCapability');
748779efd5Sopenharmony_ci        return;
758779efd5Sopenharmony_ci      }
768779efd5Sopenharmony_ci      this.bottomTabIndex = this.mainTabsIndex;
778779efd5Sopenharmony_ci      this.controller.changeIndex(this.mainTabsIndex);
788779efd5Sopenharmony_ci      CallRecordPresenter.getInstance().setPageShow(this.bottomTabIndex == 0);
798779efd5Sopenharmony_ci      ContactListPresenter.getInstance().setPageShow(this.bottomTabIndex == 1);
808779efd5Sopenharmony_ci      if (this.mainTabsIndex != 2) {
818779efd5Sopenharmony_ci        FavoriteListPresenter.getInstance().onPageHide()
828779efd5Sopenharmony_ci      } else {
838779efd5Sopenharmony_ci        FavoriteListPresenter.getInstance().onPageShow()
848779efd5Sopenharmony_ci      }
858779efd5Sopenharmony_ci    }
868779efd5Sopenharmony_ci  }
878779efd5Sopenharmony_ci
888779efd5Sopenharmony_ci  pageTransition() {
898779efd5Sopenharmony_ci    PageTransitionEnter({ duration: 100 })
908779efd5Sopenharmony_ci    PageTransitionExit({ duration: 100 })
918779efd5Sopenharmony_ci  }
928779efd5Sopenharmony_ci
938779efd5Sopenharmony_ci  onPageShow() {
948779efd5Sopenharmony_ci    this.mIndexPresenter.onPageShow();
958779efd5Sopenharmony_ci    CallRecordPresenter.getInstance().setPageShow(this.bottomTabIndex == 0);
968779efd5Sopenharmony_ci    ContactListPresenter.getInstance().setPageShow(this.bottomTabIndex == 1);
978779efd5Sopenharmony_ci    if (this.bottomTabIndex == 2) {
988779efd5Sopenharmony_ci      FavoriteListPresenter.getInstance().onPageShow();
998779efd5Sopenharmony_ci    }
1008779efd5Sopenharmony_ci  }
1018779efd5Sopenharmony_ci
1028779efd5Sopenharmony_ci  onPageHide() {
1038779efd5Sopenharmony_ci    ContactListPresenter.getInstance().setPageShow(false);
1048779efd5Sopenharmony_ci    CallRecordPresenter.getInstance().setPageShow(false);
1058779efd5Sopenharmony_ci    FavoriteListPresenter.getInstance().onPageHide();
1068779efd5Sopenharmony_ci  }
1078779efd5Sopenharmony_ci
1088779efd5Sopenharmony_ci  aboutToAppear() {
1098779efd5Sopenharmony_ci    this.mPermissionManager.initPermissions();
1108779efd5Sopenharmony_ci    this.mIndexPresenter.aboutToAppear();
1118779efd5Sopenharmony_ci    this.getInfo();
1128779efd5Sopenharmony_ci    this.onIndexChanged();
1138779efd5Sopenharmony_ci    this.teleNumberChange()
1148779efd5Sopenharmony_ci    let innerEvent = {
1158779efd5Sopenharmony_ci      eventId: this.emitterId,
1168779efd5Sopenharmony_ci      priority: emitter.EventPriority.HIGH
1178779efd5Sopenharmony_ci    };
1188779efd5Sopenharmony_ci    emitter.on(innerEvent, (data) => {
1198779efd5Sopenharmony_ci      this.isContactSearch = data.data['isSearchPage'];
1208779efd5Sopenharmony_ci    })
1218779efd5Sopenharmony_ci  }
1228779efd5Sopenharmony_ci
1238779efd5Sopenharmony_ci  aboutToDisappear() {
1248779efd5Sopenharmony_ci    this.mIndexPresenter.aboutToDisappear();
1258779efd5Sopenharmony_ci    emitter.off(this.emitterId);
1268779efd5Sopenharmony_ci  }
1278779efd5Sopenharmony_ci
1288779efd5Sopenharmony_ci  onBackPress() {
1298779efd5Sopenharmony_ci    if (this.isContactSearch) {
1308779efd5Sopenharmony_ci      ContactListPresenter.getInstance().sendEmitter(false);
1318779efd5Sopenharmony_ci      return true;
1328779efd5Sopenharmony_ci    }
1338779efd5Sopenharmony_ci  }
1348779efd5Sopenharmony_ci
1358779efd5Sopenharmony_ci  getInfo() {
1368779efd5Sopenharmony_ci    device.getInfo({
1378779efd5Sopenharmony_ci      success: function (data) {
1388779efd5Sopenharmony_ci        AppStorage.SetOrCreate('windowHeight', data.windowHeight / data.screenDensity)
1398779efd5Sopenharmony_ci      },
1408779efd5Sopenharmony_ci      fail: function (data, code) {
1418779efd5Sopenharmony_ci        HiLog.i(TAG, 'Failed to obtain device information. Error code:' + code + '; Error information: ' + data);
1428779efd5Sopenharmony_ci      },
1438779efd5Sopenharmony_ci    });
1448779efd5Sopenharmony_ci  }
1458779efd5Sopenharmony_ci
1468779efd5Sopenharmony_ci  build() {
1478779efd5Sopenharmony_ci    if (this.mPermissionManager.isAllPermissionsGranted()) {
1488779efd5Sopenharmony_ci        Flex({
1498779efd5Sopenharmony_ci          direction: this.curBp === 'lg' ? FlexDirection.Row : FlexDirection.Column,
1508779efd5Sopenharmony_ci          alignItems: ItemAlign.Start,
1518779efd5Sopenharmony_ci          justifyContent: FlexAlign.Start
1528779efd5Sopenharmony_ci        }) {
1538779efd5Sopenharmony_ci          if (this.curBp === 'lg') {
1548779efd5Sopenharmony_ci            TabBars({ controller: this.controller, bottomTabIndex: $bottomTabIndex })
1558779efd5Sopenharmony_ci              .visibility(this.isContactSearch ? Visibility.None : Visibility.Visible)
1568779efd5Sopenharmony_ci          }
1578779efd5Sopenharmony_ci          Tabs({
1588779efd5Sopenharmony_ci            barPosition: BarPosition.End,
1598779efd5Sopenharmony_ci            index: this.bottomTabIndex,
1608779efd5Sopenharmony_ci            controller: this.controller
1618779efd5Sopenharmony_ci          }) {
1628779efd5Sopenharmony_ci            if (this.curBp !== 'lg') {
1638779efd5Sopenharmony_ci              TabContent() {
1648779efd5Sopenharmony_ci                callPage()
1658779efd5Sopenharmony_ci              }
1668779efd5Sopenharmony_ci            } else {
1678779efd5Sopenharmony_ci              TabContent() {
1688779efd5Sopenharmony_ci                callTabletPage()
1698779efd5Sopenharmony_ci              }
1708779efd5Sopenharmony_ci            }
1718779efd5Sopenharmony_ci            TabContent() {
1728779efd5Sopenharmony_ci              contactPage()
1738779efd5Sopenharmony_ci            }
1748779efd5Sopenharmony_ci            TabContent() {
1758779efd5Sopenharmony_ci              favoritePage()
1768779efd5Sopenharmony_ci            }
1778779efd5Sopenharmony_ci          }
1788779efd5Sopenharmony_ci          .width(this.curBp === 'lg' ? null : '100%')
1798779efd5Sopenharmony_ci          .height(this.curBp === 'lg' ? '100%' : null)
1808779efd5Sopenharmony_ci          .vertical(false)
1818779efd5Sopenharmony_ci          .barMode(BarMode.Fixed)
1828779efd5Sopenharmony_ci          .barWidth(0)
1838779efd5Sopenharmony_ci          .barHeight(0)
1848779efd5Sopenharmony_ci          .scrollable(false)
1858779efd5Sopenharmony_ci          .animationDuration(0)
1868779efd5Sopenharmony_ci          .layoutWeight(1)
1878779efd5Sopenharmony_ci
1888779efd5Sopenharmony_ci          if (this.curBp !== 'lg') {
1898779efd5Sopenharmony_ci            TabBars({ controller: this.controller, bottomTabIndex: $bottomTabIndex })
1908779efd5Sopenharmony_ci              .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
1918779efd5Sopenharmony_ci              .visibility(this.isContactSearch ? Visibility.None : Visibility.Visible)
1928779efd5Sopenharmony_ci          }
1938779efd5Sopenharmony_ci        }
1948779efd5Sopenharmony_ci        .backgroundColor($r('sys.color.ohos_fa_sub_background'))
1958779efd5Sopenharmony_ci        .width('100%')
1968779efd5Sopenharmony_ci        .height('100%')
1978779efd5Sopenharmony_ci      } else {
1988779efd5Sopenharmony_ci        Column()
1998779efd5Sopenharmony_ci          .width('100%')
2008779efd5Sopenharmony_ci          .height('100%')
2018779efd5Sopenharmony_ci    }
2028779efd5Sopenharmony_ci  }
2038779efd5Sopenharmony_ci}
2048779efd5Sopenharmony_ci
2058779efd5Sopenharmony_ci@Component
2068779efd5Sopenharmony_cistruct TabBars {
2078779efd5Sopenharmony_ci  private tabSrc: number[] = call.hasVoiceCapability() ? [0, 1, 2] : [1];
2088779efd5Sopenharmony_ci  private controller: TabsController;
2098779efd5Sopenharmony_ci  @Link bottomTabIndex: number;
2108779efd5Sopenharmony_ci  @State mIndexPresenter: IndexPresenter = IndexPresenter.getInstance()
2118779efd5Sopenharmony_ci  @LocalStorageProp('breakpoint') curBp: string = 'sm'
2128779efd5Sopenharmony_ci
2138779efd5Sopenharmony_ci  build() {
2148779efd5Sopenharmony_ci    Flex({
2158779efd5Sopenharmony_ci      direction: this.curBp === 'lg' ? FlexDirection.Column : FlexDirection.Row,
2168779efd5Sopenharmony_ci      alignItems: ItemAlign.Center,
2178779efd5Sopenharmony_ci      justifyContent: FlexAlign.Center
2188779efd5Sopenharmony_ci    }) {
2198779efd5Sopenharmony_ci      ForEach(this.tabSrc, item => {
2208779efd5Sopenharmony_ci        Column() {
2218779efd5Sopenharmony_ci          Column() {
2228779efd5Sopenharmony_ci            Image(this.mIndexPresenter.getTabSrc(this.bottomTabIndex, item))
2238779efd5Sopenharmony_ci              .objectFit(ImageFit.Contain)
2248779efd5Sopenharmony_ci              .width($r('app.float.id_card_image_small'))
2258779efd5Sopenharmony_ci              .height($r('app.float.id_card_image_small'))
2268779efd5Sopenharmony_ci              .fillColor(this.mIndexPresenter.getTabTextColor(this.bottomTabIndex, item))
2278779efd5Sopenharmony_ci            Text(this.mIndexPresenter.getTabText(this.bottomTabIndex, item))
2288779efd5Sopenharmony_ci              .fontWeight(FontWeight.Medium)
2298779efd5Sopenharmony_ci              .margin(this.curBp === 'lg' ?
2308779efd5Sopenharmony_ci                { top: $r('app.float.id_card_margin_mid') } : {})
2318779efd5Sopenharmony_ci              .fontSize($r('sys.float.ohos_id_text_size_caption'))
2328779efd5Sopenharmony_ci              .fontColor(this.mIndexPresenter.getTabTextColor(this.bottomTabIndex, item))
2338779efd5Sopenharmony_ci          }
2348779efd5Sopenharmony_ci          .onClick(() => {
2358779efd5Sopenharmony_ci            if (this.bottomTabIndex != item) {
2368779efd5Sopenharmony_ci              this.controller.changeIndex(item);
2378779efd5Sopenharmony_ci              this.bottomTabIndex = item;
2388779efd5Sopenharmony_ci              AppStorage.SetOrCreate('mainTabsIndex', item);
2398779efd5Sopenharmony_ci              if (item == 0) {
2408779efd5Sopenharmony_ci                ContactListPresenter.getInstance().setPageShow(false);
2418779efd5Sopenharmony_ci                CallRecordPresenter.getInstance().setPageShow(true);
2428779efd5Sopenharmony_ci                FavoriteListPresenter.getInstance().onPageHide();
2438779efd5Sopenharmony_ci              } else if (item == 1) {
2448779efd5Sopenharmony_ci                CallRecordPresenter.getInstance().setPageShow(false);
2458779efd5Sopenharmony_ci                ContactListPresenter.getInstance().setPageShow(true);
2468779efd5Sopenharmony_ci                FavoriteListPresenter.getInstance().onPageHide();
2478779efd5Sopenharmony_ci              } else if (item == 2) {
2488779efd5Sopenharmony_ci                CallRecordPresenter.getInstance().setPageShow(false);
2498779efd5Sopenharmony_ci                ContactListPresenter.getInstance().setPageShow(false);
2508779efd5Sopenharmony_ci                FavoriteListPresenter.getInstance().onPageShow();
2518779efd5Sopenharmony_ci              }
2528779efd5Sopenharmony_ci            }
2538779efd5Sopenharmony_ci          })
2548779efd5Sopenharmony_ci          .height($r('app.float.id_card_image_mid'))
2558779efd5Sopenharmony_ci        }
2568779efd5Sopenharmony_ci        .justifyContent(FlexAlign.Center)
2578779efd5Sopenharmony_ci        .height(this.curBp === 'lg' ?
2588779efd5Sopenharmony_ci          '130vp' : $r('app.float.id_item_height_large'))
2598779efd5Sopenharmony_ci        .layoutWeight(this.curBp === 'lg' ? 0 : 1)
2608779efd5Sopenharmony_ci      }, item => item.toString())
2618779efd5Sopenharmony_ci    }
2628779efd5Sopenharmony_ci    .width(this.curBp === 'lg' ?
2638779efd5Sopenharmony_ci      '96vp' : '100%')
2648779efd5Sopenharmony_ci    .height(this.curBp === 'lg' ?
2658779efd5Sopenharmony_ci      '100%' : $r('app.float.id_item_height_large'))
2668779efd5Sopenharmony_ci    .padding(this.curBp === 'lg' ?
2678779efd5Sopenharmony_ci      { left: $r('app.float.id_card_margin_max'), right: $r('app.float.id_card_margin_max') } : {})
2688779efd5Sopenharmony_ci  }
2698779efd5Sopenharmony_ci}