1/* 2 * Copyright (c) 2022-2025 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 common from '@ohos.app.ability.common'; 17import consts from '../module/Consts'; 18import dataPreferences from '@ohos.data.preferences'; 19import Logger from '../module/Logger'; 20 21const TAG: string = '[Sample_CustomCommonEvent_HistoryFeature]'; 22 23export default class HistoryFeature { 24 constructor(abilityContext: common.UIAbilityContext) { 25 this.innerContext = abilityContext; 26 } 27 28 async getData() { 29 await this.init(); 30 return new Promise((resolve) => { 31 resolve(this.dataSource); 32 }); 33 } 34 35 private async init() { 36 let prefer = null; 37 await dataPreferences.getPreferences(this.innerContext, consts.DATA_BASE_NAME).then((pref) => { 38 prefer = pref; 39 }); 40 let records: Array<string>; 41 await prefer.get(consts.DATA_BASE_KEY_TOTAL_TIMES, []).then((value: Array<string>) => { 42 records = value; 43 }); 44 this.dataSource = []; 45 for (let item of records) { 46 await prefer.get(item, []).then((detail: Array<string>) => { 47 if (JSON.stringify(detail) !== '[]') { 48 this.dataSource.push(detail); 49 } 50 }).catch((error) => { 51 Logger.info(TAG, `Failed to get value code is ${error.code}`); 52 }); 53 } 54 } 55 56 private dataSource: Array<Array<string>> = []; 57 private innerContext: common.UIAbilityContext = null; 58}