1/*
2 * Copyright (c) 2021 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 {Log} from '@ohos/common'
17import ScreenLockService from '../../../../../../features/screenlock/src/main/ets/com/ohos/model/screenLockService'
18import Constants from '../common/constants'
19import {Callback} from '@ohos.base';
20
21const TAG = 'ScreenLock-SlideScreenLockViewModel'
22//Height of notification area.
23const NOTIFICATION_MAXHEIGHT = 500
24const SLIDING_LENGTH = 150
25const DELAY_TIME = 3 * 1000
26
27export default class SlideScreenLockViewModel {
28    startX: number = 0
29    startY: number = 0
30    moveX: number = 0
31    moveY: number = 0
32    slidingLength: number = 0
33    elementAlpha: number = 1
34    elementScale: number = 1
35    backgroundScale: number = 1.1
36    duration: number= 250
37    toggleShow: boolean = false
38
39    ViewModelInit(): void{
40        Log.showDebug(TAG, `ViewModelInit`);
41        ScreenLockService.setUnlockAnimation((callback: Callback<void>) => {
42            this.elementAlpha = 0
43            this.elementScale = 0.85
44            this.backgroundScale = 1
45            setTimeout(() => {
46                callback()
47                this.elementAlpha = 1
48                this.elementScale = 1
49                this.backgroundScale = 1.1
50            }, 250);
51        })
52        this.slidingLength = SLIDING_LENGTH
53    }
54
55    unlockScreen(): void{
56        ScreenLockService.unlockScreen()
57    }
58
59    touchEvent(event: TouchEvent) {
60        Log.showInfo(TAG, `Touch Event ${event.type} at Point ${event.touches[0].x}, ${event.touches[0].y}`)
61        if (event.type == Constants.TOUCHTYPE_DOWN) {
62            this.startX = event.touches[0].screenX
63            this.startY = event.touches[0].screenY
64        } else if (event.type == Constants.TOUCHTYPE_MOVE) {
65            this.moveX = event.touches[0].screenX - this.startX
66            this.moveY = event.touches[0].screenY - this.startY
67        } else if (event.type == Constants.TOUCHTYPE_UP) {
68            Log.showInfo(TAG, `Touch Event slidingLength: ${this.slidingLength}`)
69            if (Math.abs(this.moveY) > this.slidingLength) {
70                this.unlockScreen()
71            }
72            this.moveX = 0
73            this.moveY = 0
74        }
75    }
76
77    toggleDisplay(batteryCharging?) {
78        Log.showInfo(TAG, `toggleDisplay charging:${batteryCharging}`);
79        this.toggleShow = true;
80        setTimeout(() => {
81            this.toggleShow = false;
82            Log.showInfo(TAG, `toggleDisplay setTimeout: ${this.toggleShow}`);
83        }, DELAY_TIME);
84    }
85
86    onPageShow(): void {
87        this.elementAlpha = 1
88        this.elementScale = 1
89        this.backgroundScale = 1.1
90        AppStorage.SetOrCreate('slidestatus', false);
91    }
92}