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 { Log } from '@ohos/common'; 17import { windowManager } from '@ohos/common'; 18import { amsMissionManager } from '@ohos/common'; 19import { layoutConfigManager } from '@ohos/common'; 20import { RecentMissionInfo } from '@ohos/common'; 21import { RecentMissionsModel } from '@ohos/common'; 22import RecentModeFeatureConfig from '../common/layoutconfig/RecentModeFeatureConfig'; 23 24const TAG = 'RecentMissionsViewModel'; 25 26/** 27 * Class RecentMissionsViewModel. 28 */ 29export class RecentMissionsViewModel { 30 private mRecentMissionsModel: RecentMissionsModel; 31 private mRecentModeFeatureConfig: RecentModeFeatureConfig; 32 private mRecentMissionsLimit: number; 33 private mRecentMissionsList: RecentMissionInfo[] = []; 34 35 private constructor() { 36 Log.showInfo(TAG, 'constructor start'); 37 this.mRecentMissionsModel = RecentMissionsModel.getInstance(); 38 let config = layoutConfigManager.getModeConfig(RecentModeFeatureConfig.RECENT_MISSIONS_MODE_CONFIG); 39 if (config instanceof RecentModeFeatureConfig) { 40 this.mRecentModeFeatureConfig = <RecentModeFeatureConfig> config; 41 this.mRecentMissionsLimit = this.mRecentModeFeatureConfig.getRecentMissionsLimit(); 42 } 43 } 44 45 /** 46 * Delete recent missions. 47 * 48 */ 49 private async deleteRecentMissions(): Promise<void> { 50 await amsMissionManager.clearAllMissions(); 51 } 52 53 /** 54 * Get the recent mission view model object. 55 * 56 * @return {object} recent mission view model singleton 57 */ 58 static getInstance(): RecentMissionsViewModel { 59 if (globalThis.RecentMissionsViewModelInstance == null) { 60 globalThis.RecentMissionsViewModelInstance = new RecentMissionsViewModel(); 61 } 62 return globalThis.RecentMissionsViewModelInstance; 63 } 64 65 /** 66 * Get the recent mission row type. 67 * 68 * @return {string} row type 69 */ 70 getRecentMissionsRowType(): string { 71 return this.mRecentModeFeatureConfig.getRecentMissionsRowType(); 72 } 73 74 /** 75 * Callback function of getRecentMissionsList. 76 */ 77 async getRecentMissionsList(): Promise<void> { 78 Log.showDebug(TAG, 'getRecentMissionsList start'); 79 this.mRecentMissionsList = await amsMissionManager.getRecentMissionsList(); 80 const snapShotTime = new Date().toString(); 81 for (let item of this.mRecentMissionsList) { 82 item.snapShotTime = snapShotTime; 83 } 84 if (globalThis.recentMode && windowManager.isSplitWindowMode(globalThis.recentMode)) { 85 this.mRecentMissionsList.forEach((item, index) => { 86 if (item.missionId == globalThis.splitMissionId) { 87 this.mRecentMissionsList.splice(index, 1); 88 return; 89 } 90 }); 91 } 92 Log.showDebug(TAG, `getRecentMissionsList length: ${this.mRecentMissionsList.length}`); 93 AppStorage.SetOrCreate('recentMissionsList', this.mRecentMissionsList); 94 } 95 96 /** 97 * Delete recent mission. 98 * 99 * @param {boolean} isClickDelBtn - The flag of click delete button. 100 * @param {number} missionId - The missionId of current recent mission. 101 */ 102 async deleteRecentMission(isClickDelBtn: boolean, missionId: number): Promise<void> { 103 Log.showDebug(TAG, `deleteRecentMissions missionId: ${missionId}`); 104 if (!isClickDelBtn && missionId != -1) { 105 await amsMissionManager.clearMission(missionId); 106 this.mRecentMissionsList = AppStorage.Get('recentMissionsList'); 107 this.mRecentMissionsList = this.mRecentMissionsList == null ? this.mRecentMissionsList : this.mRecentMissionsList.filter((item) => { 108 return item.missionId != missionId; 109 }); 110 } else { 111 await this.deleteRecentMissions(); 112 this.mRecentMissionsList = AppStorage.Get('recentMissionsList'); 113 this.mRecentMissionsList = this.mRecentMissionsList == null ? this.mRecentMissionsList : this.mRecentMissionsList.filter((item) => { 114 return item.lockedState === true; 115 }); 116 if (this.mRecentMissionsList && this.mRecentMissionsList.length) { 117 windowManager.minimizeAllApps(); 118 } 119 return; 120 } 121 animateTo({ 122 duration: 200, 123 curve: Curve.EaseInOut, 124 delay: 100, 125 playMode: PlayMode.Normal, 126 tempo: 0.5, 127 iterations: 1, 128 onFinish: () => { 129 } 130 }, () => { 131 AppStorage.SetOrCreate('recentMissionsList', this.mRecentMissionsList); 132 }); 133 if (this.mRecentMissionsList.length == 0) { 134 this.terminateRecentIfAllClear(); 135 } 136 } 137 138 /** 139 * Set recent mission locked status. 140 * 141 * @param {string} missionId - The missionId of current recent mission. 142 * @param {boolean} lockedState - The lockedState of current recent mission. 143 */ 144 setRecentMissionLock(missionId: number, lockedState: boolean): void { 145 if (lockedState) { 146 amsMissionManager.lockMission(missionId); 147 } else { 148 amsMissionManager.unlockMission(missionId); 149 } 150 } 151 152 /** 153 * Get mission snapshot 154 * 155 * @param missionId - The missionId of current recent mission. 156 * 157 * @return snapshot - The snapshot of current recent mission. 158 */ 159 async getMissionSnapShot(missionId: number, callback?: any) { 160 Log.showDebug(TAG, `getMissionSnapShot missionId: ${missionId}`); 161 let snapShot = await amsMissionManager.getMissionSnapShot(missionId); 162 if (callback != undefined) { 163 callback(missionId, snapShot); 164 } else { 165 return snapShot; 166 } 167 } 168 169 /** 170 * Move the recent mission to front 171 * 172 * @param missionId - The missionId of current recent mission. 173 */ 174 async moveMissionToFront(missionId: number): Promise<void> { 175 if (globalThis.recentMode && windowManager.isSplitWindowMode(globalThis.recentMode)) { 176 await amsMissionManager.moveMissionToFront(missionId, globalThis.recentMode); 177 } else { 178 await amsMissionManager.moveMissionToFront(missionId); 179 } 180 } 181 182 /** 183 * Terminate recent if clear all missions. 184 */ 185 terminateRecentIfAllClear(): void { 186 Log.showDebug(TAG, 'terminateRecentIfAllClear all recent cleared'); 187 windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME); 188 } 189 190 /** 191 * Back to desktop. 192 */ 193 backView(): void { 194 Log.showDebug(TAG, 'backView start'); 195 windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME); 196 } 197}