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