• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      AppStorage.setOrCreate('recentMissionsList', this.mRecentMissionsList);
118      return;
119    }
120    animateTo({
121      duration: 200,
122      curve: Curve.EaseInOut,
123      delay: 100,
124      playMode: PlayMode.Normal,
125      tempo: 0.5,
126      iterations: 1,
127      onFinish: () => {
128      }
129    }, () => {
130      AppStorage.setOrCreate('recentMissionsList', this.mRecentMissionsList);
131    });
132    if (this.mRecentMissionsList.length == 0) {
133      this.terminateRecentIfAllClear();
134    }
135  }
136
137  /**
138   * Set recent mission locked status.
139   *
140   * @param {string} missionId - The missionId of current recent mission.
141   * @param {boolean} lockedState - The lockedState of current recent mission.
142   */
143  setRecentMissionLock(missionId: number, lockedState: boolean): void {
144    if (lockedState) {
145      amsMissionManager.lockMission(missionId);
146    } else {
147      amsMissionManager.unlockMission(missionId);
148    }
149  }
150
151  /**
152   * Get mission snapshot
153   *
154   * @param missionId - The missionId of current recent mission.
155   *
156   * @return snapshot - The snapshot of current recent mission.
157   */
158  async getMissionSnapShot(missionId: number, callback?: any) {
159    Log.showDebug(TAG, `getMissionSnapShot missionId: ${missionId}`);
160    let snapShot = await amsMissionManager.getMissionSnapShot(missionId);
161    if (callback != undefined) {
162      callback(missionId, snapShot);
163    } else {
164      return snapShot;
165    }
166  }
167
168  /**
169   * Move the recent mission to front
170   *
171   * @param missionId - The missionId of current recent mission.
172   */
173  async moveMissionToFront(missionId: number): Promise<void> {
174    if (globalThis.recentMode && windowManager.isSplitWindowMode(globalThis.recentMode)) {
175      await amsMissionManager.moveMissionToFront(missionId, globalThis.recentMode);
176    } else {
177      await amsMissionManager.moveMissionToFront(missionId);
178    }
179  }
180
181  /**
182   * Terminate recent if clear all missions.
183   */
184  terminateRecentIfAllClear(): void {
185    Log.showDebug(TAG, 'terminateRecentIfAllClear all recent cleared');
186    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
187  }
188
189  /**
190   * Back to desktop.
191   */
192  backView(): void {
193    Log.showDebug(TAG, 'backView start');
194    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
195  }
196}