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 } 98 99 onPageHide() { 100 ContactListPresenter.getInstance().setPageShow(false); 101 CallRecordPresenter.getInstance().setPageShow(false); 102 } 103 104 aboutToAppear() { 105 this.mPermissionManager.initPermissions(); 106 this.mIndexPresenter.aboutToAppear(); 107 this.getInfo(); 108 this.onIndexChanged(); 109 this.teleNumberChange() 110 let innerEvent = { 111 eventId: this.emitterId, 112 priority: emitter.EventPriority.HIGH 113 }; 114 emitter.on(innerEvent, (data) => { 115 this.isContactSearch = data.data['isSearchPage']; 116 }) 117 } 118 119 aboutToDisappear() { 120 this.mIndexPresenter.aboutToDisappear(); 121 emitter.off(this.emitterId); 122 } 123 124 onBackPress() { 125 if (this.isContactSearch) { 126 ContactListPresenter.getInstance().sendEmitter(false); 127 return true; 128 } 129 } 130 131 getInfo() { 132 device.getInfo({ 133 success: function (data) { 134 AppStorage.SetOrCreate("windowHeight", data.windowHeight / data.screenDensity) 135 }, 136 fail: function (data, code) { 137 HiLog.i(TAG, 'Failed to obtain device information. Error code:' + code + '; Error information: ' + data); 138 }, 139 }); 140 } 141 142 build() { 143 Column() { 144 if (this.mPermissionManager.isAllPermissionsGranted()) { 145 Flex({ 146 direction: this.curBp === 'lg' ? FlexDirection.Row : FlexDirection.Column, 147 alignItems: ItemAlign.Start, 148 justifyContent: FlexAlign.Start 149 }) { 150 if (this.curBp === 'lg') { 151 Column() { 152 TabBars({ controller: this.controller, bottomTabIndex: $bottomTabIndex }) 153 } 154 .height('100%') 155 .zIndex(3) 156 .visibility(this.isContactSearch ? Visibility.None : Visibility.Visible) 157 } 158 Column() { 159 Tabs({ 160 barPosition: this.curBp === 'lg' ? BarPosition.Start : BarPosition.End, 161 index: this.bottomTabIndex, 162 controller: this.controller 163 }) { 164 if (this.curBp !== 'lg') { 165 TabContent() { 166 callPage() 167 } 168 } else { 169 TabContent() { 170 callTabletPage() 171 } 172 } 173 TabContent() { 174 contactPage() 175 } 176 177 TabContent() { 178 favoritePage() 179 } 180 } 181 .width('100%') 182 .vertical(false) 183 .barMode(BarMode.Fixed) 184 .barWidth(0) 185 .barHeight(0) 186 .scrollable(false) 187 .animationDuration(0) 188 } 189 .layoutWeight(this.curBp === 'lg' ? 1 : 0) 190 .flexShrink(this.curBp === 'lg' ? 0 : 1) 191 .height(this.curBp === 'lg' ? '100%' : null) 192 .width(this.curBp === 'lg' ? 0 : '100%') 193 194 if (this.curBp !== 'lg') { 195 Column() { 196 TabBars({ controller: this.controller, bottomTabIndex: $bottomTabIndex }) 197 } 198 .backgroundColor($r("sys.color.ohos_id_color_sub_background")) 199 .width('100%') 200 .height($r("app.float.id_item_height_large")) 201 .flexShrink(0) 202 .visibility(this.isContactSearch ? Visibility.None : Visibility.Visible) 203 } 204 } 205 .backgroundColor($r("sys.color.ohos_fa_sub_background")) 206 .width('100%') 207 .height('100%') 208 } 209 } 210 .width('100%') 211 .height('100%') 212 } 213} 214 215@Component 216struct TabBars { 217 private tabSrc: number[] = call.hasVoiceCapability() ? [0, 1, 2] : [1]; 218 private controller: TabsController; 219 @Link bottomTabIndex: number; 220 @State mIndexPresenter: IndexPresenter = IndexPresenter.getInstance() 221 @LocalStorageProp('breakpoint') curBp: string = 'sm' 222 223 build() { 224 Flex({ 225 direction: this.curBp === 'lg' ? FlexDirection.Column : FlexDirection.Row, 226 alignItems: ItemAlign.Center, 227 justifyContent: FlexAlign.Center 228 }) { 229 ForEach(this.tabSrc, item => { 230 Column() { 231 Column() { 232 Image(this.mIndexPresenter.getTabSrc(this.bottomTabIndex, item)) 233 .objectFit(ImageFit.Contain) 234 .width($r("app.float.id_card_image_small")) 235 .height($r("app.float.id_card_image_small")) 236 .fillColor(this.mIndexPresenter.getTabTextColor(this.bottomTabIndex, item)) 237 Text(this.mIndexPresenter.getTabText(this.bottomTabIndex, item)) 238 .fontWeight(FontWeight.Medium) 239 .margin(this.curBp === 'lg' ? 240 { top: $r("app.float.id_card_margin_mid") } : {}) 241 .fontSize($r("sys.float.ohos_id_text_size_caption")) 242 .fontColor(this.mIndexPresenter.getTabTextColor(this.bottomTabIndex, item)) 243 } 244 .onClick(() => { 245 if (this.bottomTabIndex != item) { 246 this.controller.changeIndex(item); 247 this.bottomTabIndex = item; 248 AppStorage.SetOrCreate("mainTabsIndex", item); 249 if (item == 0) { 250 ContactListPresenter.getInstance().setPageShow(false); 251 CallRecordPresenter.getInstance().setPageShow(true); 252 FavoriteListPresenter.getInstance().onPageHide(); 253 } else if (item == 1) { 254 CallRecordPresenter.getInstance().setPageShow(false); 255 ContactListPresenter.getInstance().setPageShow(true); 256 FavoriteListPresenter.getInstance().onPageHide(); 257 } else if (item == 2) { 258 CallRecordPresenter.getInstance().setPageShow(false); 259 ContactListPresenter.getInstance().setPageShow(false); 260 FavoriteListPresenter.getInstance().onPageShow(); 261 } 262 } 263 }) 264 .height($r("app.float.id_card_image_mid")) 265 } 266 .justifyContent(FlexAlign.Center) 267 .height(this.curBp === 'lg' ? 268 '130vp' : $r("app.float.id_item_height_large")) 269 .layoutWeight(this.curBp === 'lg' ? 0 : 1) 270 }, item => item.toString()) 271 } 272 .width(this.curBp === 'lg' ? 273 '96vp' : '100%') 274 .height(this.curBp === 'lg' ? 275 '100%' : $r("app.float.id_item_height_large")) 276 .padding(this.curBp === 'lg' ? 277 { left: $r("app.float.id_card_margin_max"), right: $r("app.float.id_card_margin_max") } : {}) 278 } 279}