1/*
2 * Copyright (c) 2023-2023 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 print from '@ohos.print';
17import { Log, PrinterExtensionInfo } from '@ohos/common';
18import AppStorageHelper from '../Common/Adapter/AppStorageHelper';
19import {AppStorageKeyName} from '@ohos/common';
20import {PrintExtensionModel} from '../Model/PrintExtensionModel';
21
22const TAG = '[PrintExtensionController]:';
23
24/**
25 * PrintExtensionController
26 *
27 */
28export class PrintExtensionController {
29    private mPrintExtensionModel : PrintExtensionModel = new PrintExtensionModel()
30
31    /**
32     * init
33     *
34     */
35    public init() : void {
36        Log.info(TAG, 'PrintExtensionController init')
37        AppStorageHelper.createValue<Array<PrinterExtensionInfo>>(this.getModel().getPrintExtensions, AppStorageKeyName.PRINT_EXTENSION_LIST_NAME)
38    }
39
40    /**
41     * get all printExtension
42     */
43    public getAllPrintExtension() : void {
44        Log.info(TAG, 'queryPrinterCapability')
45        print.queryAllPrinterExtensionInfos().then((data)=> {
46            Log.info(TAG, 'start connect Printer success data : ' )
47            this.getModel().dealGetAllPrintExtension(data)
48        }).catch((err)=> {
49            Log.error(TAG, 'failed to get AllPrintExtension because ' + JSON.stringify(err));
50        });
51    }
52
53    /**
54     * get model
55     *
56     * @return PrintExtensionModel
57     */
58    public getModel() : PrintExtensionModel {
59        return this.mPrintExtensionModel
60    }
61}