• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 callTabletPage from './dialer/DialerTablet';
16import contactPage from './contacts/ContactList';
17import favoritePage from './favorites/favoriteList';
18import callPage from './phone/dialer/Dialer';
19import IndexPresenter from '../presenter/IndexPresenter';
20import { HiLog, StringUtil } from '../../../../../common';
21import { PermissionManager } from '../../../../../common/src/main/ets/permission/PermissionManager';
22import DialerPresenter from '../presenter/dialer/DialerPresenter';
23import call from '@ohos.telephony.call';
24import ContactListPresenter from '../presenter/contact/ContactListPresenter';
25import CallRecordPresenter from '../presenter/dialer/callRecord/CallRecordPresenter';
26import FavoriteListPresenter from '../presenter/favorite/FavoriteListPresenter';
27import device from '@system.device';
28import emitter from '@ohos.events.emitter';
29
30const TAG = 'Index ';
31
32let storage = LocalStorage.GetShared()
33
34@Entry(storage)
35@Component
36struct Index {
37  private controller: TabsController = new TabsController();
38  @State mPermissionManager: PermissionManager = PermissionManager.getInstance();
39  mIndexPresenter: IndexPresenter = IndexPresenter.getInstance();
40  @StorageLink('mainTabsIndex') @Watch('onIndexChanged') mainTabsIndex: number = 0;
41  @StorageLink('teleNumber') @Watch('teleNumberChange') teleNumber: string = '';
42  mDialerPresenter: DialerPresenter = DialerPresenter.getInstance();
43  @State bottomTabIndex: number = call.hasVoiceCapability() ? 0 : 1;
44  @StorageLink('targetPage') @Watch('targetPageChange') targetPage: {
45    url?: string,
46    pageIndex?: number,
47    params?: any
48  } = {};
49  @LocalStorageProp('breakpoint') curBp: string = 'sm';
50  @State isContactSearch: boolean = false;
51  emitterId: number = 105;
52
53  teleNumberChange() {
54    if (!StringUtil.isEmpty(this.teleNumber)) {
55      this.mDialerPresenter.editPhoneNumber(this.teleNumber);
56      AppStorage.SetOrCreate('showDialBtn', true);
57      this.teleNumber = '';
58    }
59  }
60
61  targetPageChange() {
62    if (this.targetPage && this.targetPage.url) {
63      HiLog.i(TAG, 'targetPageChange in' + this.targetPage);
64      this.mIndexPresenter.goToPage(this.targetPage.url, this.targetPage.pageIndex, this.targetPage.params)
65      this.targetPage = {}
66    }
67  }
68
69  onIndexChanged(): void {
70    HiLog.i(TAG, 'uriTabIndex change:' + this.mainTabsIndex);
71    if (this.mainTabsIndex != this.bottomTabIndex) {
72      if (this.mainTabsIndex == 0 && !call.hasVoiceCapability()) {
73        HiLog.i(TAG, 'not hasVoiceCapability');
74        return;
75      }
76      this.bottomTabIndex = this.mainTabsIndex;
77      this.controller.changeIndex(this.mainTabsIndex);
78      CallRecordPresenter.getInstance().setPageShow(this.bottomTabIndex == 0);
79      ContactListPresenter.getInstance().setPageShow(this.bottomTabIndex == 1);
80      if (this.mainTabsIndex != 2) {
81        FavoriteListPresenter.getInstance().onPageHide()
82      } else {
83        FavoriteListPresenter.getInstance().onPageShow()
84      }
85    }
86  }
87
88  pageTransition() {
89    PageTransitionEnter({ duration: 100 })
90    PageTransitionExit({ duration: 100 })
91  }
92
93  onPageShow() {
94    this.mIndexPresenter.onPageShow();
95    CallRecordPresenter.getInstance().setPageShow(this.bottomTabIndex == 0);
96    ContactListPresenter.getInstance().setPageShow(this.bottomTabIndex == 1);
97    if (this.bottomTabIndex == 2) {
98      FavoriteListPresenter.getInstance().onPageShow();
99    }
100  }
101
102  onPageHide() {
103    ContactListPresenter.getInstance().setPageShow(false);
104    CallRecordPresenter.getInstance().setPageShow(false);
105    FavoriteListPresenter.getInstance().onPageHide();
106  }
107
108  aboutToAppear() {
109    this.mPermissionManager.initPermissions();
110    this.mIndexPresenter.aboutToAppear();
111    this.getInfo();
112    this.onIndexChanged();
113    this.teleNumberChange()
114    let innerEvent = {
115      eventId: this.emitterId,
116      priority: emitter.EventPriority.HIGH
117    };
118    emitter.on(innerEvent, (data) => {
119      this.isContactSearch = data.data['isSearchPage'];
120    })
121  }
122
123  aboutToDisappear() {
124    this.mIndexPresenter.aboutToDisappear();
125    emitter.off(this.emitterId);
126  }
127
128  onBackPress() {
129    if (this.isContactSearch) {
130      ContactListPresenter.getInstance().sendEmitter(false);
131      return true;
132    }
133  }
134
135  getInfo() {
136    device.getInfo({
137      success: function (data) {
138        AppStorage.SetOrCreate('windowHeight', data.windowHeight / data.screenDensity)
139      },
140      fail: function (data, code) {
141        HiLog.i(TAG, 'Failed to obtain device information. Error code:' + code + '; Error information: ' + data);
142      },
143    });
144  }
145
146  build() {
147    if (this.mPermissionManager.isAllPermissionsGranted()) {
148        Flex({
149          direction: this.curBp === 'lg' ? FlexDirection.Row : FlexDirection.Column,
150          alignItems: ItemAlign.Start,
151          justifyContent: FlexAlign.Start
152        }) {
153          if (this.curBp === 'lg') {
154            TabBars({ controller: this.controller, bottomTabIndex: $bottomTabIndex })
155              .visibility(this.isContactSearch ? Visibility.None : Visibility.Visible)
156          }
157          Tabs({
158            barPosition: BarPosition.End,
159            index: this.bottomTabIndex,
160            controller: this.controller
161          }) {
162            if (this.curBp !== 'lg') {
163              TabContent() {
164                callPage()
165              }
166            } else {
167              TabContent() {
168                callTabletPage()
169              }
170            }
171            TabContent() {
172              contactPage()
173            }
174            TabContent() {
175              favoritePage()
176            }
177          }
178          .width(this.curBp === 'lg' ? null : '100%')
179          .height(this.curBp === 'lg' ? '100%' : null)
180          .vertical(false)
181          .barMode(BarMode.Fixed)
182          .barWidth(0)
183          .barHeight(0)
184          .scrollable(false)
185          .animationDuration(0)
186          .layoutWeight(1)
187
188          if (this.curBp !== 'lg') {
189            TabBars({ controller: this.controller, bottomTabIndex: $bottomTabIndex })
190              .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
191              .visibility(this.isContactSearch ? Visibility.None : Visibility.Visible)
192          }
193        }
194        .backgroundColor($r('sys.color.ohos_fa_sub_background'))
195        .width('100%')
196        .height('100%')
197      } else {
198        Column()
199          .width('100%')
200          .height('100%')
201    }
202  }
203}
204
205@Component
206struct TabBars {
207  private tabSrc: number[] = call.hasVoiceCapability() ? [0, 1, 2] : [1];
208  private controller: TabsController;
209  @Link bottomTabIndex: number;
210  @State mIndexPresenter: IndexPresenter = IndexPresenter.getInstance()
211  @LocalStorageProp('breakpoint') curBp: string = 'sm'
212
213  build() {
214    Flex({
215      direction: this.curBp === 'lg' ? FlexDirection.Column : FlexDirection.Row,
216      alignItems: ItemAlign.Center,
217      justifyContent: FlexAlign.Center
218    }) {
219      ForEach(this.tabSrc, item => {
220        Column() {
221          Column() {
222            Image(this.mIndexPresenter.getTabSrc(this.bottomTabIndex, item))
223              .objectFit(ImageFit.Contain)
224              .width($r('app.float.id_card_image_small'))
225              .height($r('app.float.id_card_image_small'))
226              .fillColor(this.mIndexPresenter.getTabTextColor(this.bottomTabIndex, item))
227            Text(this.mIndexPresenter.getTabText(this.bottomTabIndex, item))
228              .fontWeight(FontWeight.Medium)
229              .margin(this.curBp === 'lg' ?
230                { top: $r('app.float.id_card_margin_mid') } : {})
231              .fontSize($r('sys.float.ohos_id_text_size_caption'))
232              .fontColor(this.mIndexPresenter.getTabTextColor(this.bottomTabIndex, item))
233          }
234          .onClick(() => {
235            if (this.bottomTabIndex != item) {
236              this.controller.changeIndex(item);
237              this.bottomTabIndex = item;
238              AppStorage.SetOrCreate('mainTabsIndex', item);
239              if (item == 0) {
240                ContactListPresenter.getInstance().setPageShow(false);
241                CallRecordPresenter.getInstance().setPageShow(true);
242                FavoriteListPresenter.getInstance().onPageHide();
243              } else if (item == 1) {
244                CallRecordPresenter.getInstance().setPageShow(false);
245                ContactListPresenter.getInstance().setPageShow(true);
246                FavoriteListPresenter.getInstance().onPageHide();
247              } else if (item == 2) {
248                CallRecordPresenter.getInstance().setPageShow(false);
249                ContactListPresenter.getInstance().setPageShow(false);
250                FavoriteListPresenter.getInstance().onPageShow();
251              }
252            }
253          })
254          .height($r('app.float.id_card_image_mid'))
255        }
256        .justifyContent(FlexAlign.Center)
257        .height(this.curBp === 'lg' ?
258          '130vp' : $r('app.float.id_item_height_large'))
259        .layoutWeight(this.curBp === 'lg' ? 0 : 1)
260      }, item => item.toString())
261    }
262    .width(this.curBp === 'lg' ?
263      '96vp' : '100%')
264    .height(this.curBp === 'lg' ?
265      '100%' : $r('app.float.id_item_height_large'))
266    .padding(this.curBp === 'lg' ?
267      { left: $r('app.float.id_card_margin_max'), right: $r('app.float.id_card_margin_max') } : {})
268  }
269}