1/*
2 * Copyright (c) 2021-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 */
15
16import Constants from '../common/constants'
17import CapsuleViewModel, { VIEW_MODEL_ID, CallState } from '../viewmodel/CapsuleViewModel'
18import Log from '../../../../../../../common/src/main/ets/default/Log'
19import StyleConfigurationCommon, { CommonStyle
20} from '../../../../../../../common/src/main/ets/default/StyleConfiguration'
21import StyleConfiguration, { CapsuleComponentStyle } from '../common/StyleConfiguration'
22
23const TAG = "CapsuleIcon";
24
25@Component
26export default struct CapsuleIcon {
27  @StorageLink(VIEW_MODEL_ID) mCapsuleViewModel: CapsuleViewModel = CapsuleViewModel.getInstance();
28  startX: number = 0;
29  startY: number = 0;
30  moveX: number = 0;
31  moveY: number = 0;
32  @State style: CapsuleComponentStyle = StyleConfiguration.getCapsuleComponentStyle();
33  @State styleCommon: CommonStyle = StyleConfigurationCommon.getCommonStyle();
34
35  aboutToAppear() {
36    this.mCapsuleViewModel.initViewModel();
37    Log.showInfo(TAG, 'aboutToAppear, text: ' + this.mCapsuleViewModel.mText);
38  }
39
40  aboutToDisappear() {
41    Log.showInfo(TAG, 'aboutToDisappear');
42  }
43
44  build() {
45    if (this.mCapsuleViewModel.mIsBackground) {
46      Row() {
47        Row().width(this.styleCommon.statusBarMarginLeftRight).height('100%')
48        Row() {
49          Row().width(this.style.greenCapsuleTextMarginLeftRight).height('100%')
50          Image($r("app.media.ic_statusbar_phone"))
51            .width(this.style.greenCapsulePhoneWidth)
52            .height(this.style.greenCapsulePhoneHeight)
53            .objectFit(ImageFit.Contain)
54          Row().width($r("app.float.green_capsule_phone_text_left")).height('100%')
55          Text(this.mCapsuleViewModel.mText)
56            .fontSize(this.styleCommon.statusBarFontSize)
57            .fontWeight(FontWeight.Regular)
58            .fontColor(this.style.greenCapsuleTextColor)
59            .maxLines(this.style.maxLines)
60            .textOverflow({ overflow: TextOverflow.Ellipsis })
61          Row().width(this.style.greenCapsuleTextMarginLeftRight).height('100%')
62        }
63        .alignItems(VerticalAlign.Center)
64        .height(this.style.greenCapsuleHeight)
65        .backgroundColor(this.style.greenCapsuleBackgroundColor)
66        .borderRadius(this.style.greenCapsuleRadius)
67        .onTouch(this.touchEvent.bind(this))
68
69        Row().width(this.styleCommon.statusBarMarginLeftRight).height('100%')
70      }.margin({
71        left: $r("app.float.green_capsule_phone_margin_left"),
72        right: $r("app.float.green_capsule_phone_margin_left")
73      })
74    }
75  }
76
77  touchEvent(event: TouchEvent) {
78    Log.showDebug(TAG, `touchEventtouchEventtouchEvent`);
79    if (event.type == Constants.TOUCH_TYPE_DOWN) {
80      this.startX = event.touches[0].screenX;
81      this.startY = event.touches[0].screenY;
82      this.moveX = 0;
83      this.moveY = 0;
84      Log.showDebug(TAG, `touchStart2=======startX: ${this.startX}, startY: ${this.startY}`);
85    } else if (event.type == Constants.TOUCH_TYPE_MOVE) {
86      this.moveX = event.touches[0].screenX - this.startX;
87      this.moveY = event.touches[0].screenY - this.startY;
88    } else if (event.type == Constants.TOUCH_TYPE_UP) {
89      Log.showDebug(TAG, `touchEnd2, moveX: ${this.moveX}, moveY: ${this.moveY}`);
90      if (this.moveX < 5 && this.moveX > -5 && this.moveY < 5 && this.moveY > -5) {
91        event.stopPropagation();
92        this.mCapsuleViewModel.onClickEvent();
93        this.mCapsuleViewModel.mIsBackground = false;
94      };
95    };
96  }
97}