• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 distributedObject from '@ohos.data.distributedDataObject'
17import featureAbility from '@ohos.ability.featureAbility';
18
19function grantPermission() {
20    console.info('grantPermission');
21    let context = featureAbility.getContext();
22    context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) {
23        console.info(`result.requestCode=${result.requestCode}`)
24
25    })
26    console.info('end grantPermission');
27}
28export default class DistributedDataModel {
29    documentList = [];
30    distributedObject; // distributed proxy
31    imgSrc = "common/red.png";
32    #callback;
33    #statusCallback;
34
35    constructor() {
36        this.distributedObject = distributedObject.createDistributedObject({
37            documentList: this.documentList,
38            documentSize: 0
39        });
40        this.share();
41    }
42
43    clearCallback() {
44        this.distributedObject.off("change");
45        this.#callback = undefined;
46        this.distributedObject.off("status");
47        this.#statusCallback = undefined;
48    }
49
50    setCallback(callback) {
51        if (this.#callback == callback) {
52            console.info("same callback");
53            return;
54        }
55        console.info("start off");
56        if (this.#callback != undefined) {
57            this.distributedObject.off("change", this.#callback);
58        }
59        this.#callback = callback;
60        console.info("start watch change");
61        this.distributedObject.on("change", this.#callback);
62    }
63
64    setStatusCallback(callback) {
65        if (this.#statusCallback == callback) {
66            console.info("same callback");
67            return;
68        }
69        console.info("start off");
70        if (this.#statusCallback != undefined) {
71            this.distributedObject.off("status", this.#statusCallback);
72        }
73        this.#statusCallback = callback;
74        console.info("start watch change");
75        this.distributedObject.on("status", this.#statusCallback);
76    }
77
78    share() {
79        console.info("start share");
80        if (this.distributedObject.__sessionId == undefined) {
81            grantPermission()
82            this.distributedObject.setSessionId("123456")
83        }
84    }
85
86    update(index, title, content) {
87        console.info("doUpdate " + title + index);
88        this.documentList = this.distributedObject.documentList;
89        this.documentList[index] = {
90            index: index, title: title, content: content
91        };
92        this.distributedObject.documentList = this.documentList;
93        console.info("update my documentList " + JSON.stringify(this.documentList));
94    }
95
96    add(title, content) {
97        console.info("doAdd " + title + content);
98        console.info("documentList " + JSON.stringify(this.documentList));
99        this.documentList = this.distributedObject.documentList;
100        this.documentList[this.distributedObject.documentSize] = {
101            index: this.distributedObject.documentSize, title: title, content: content
102        };
103        this.distributedObject.documentList = this.documentList;
104        this.distributedObject.documentSize++;
105        console.info("add my documentList " + JSON.stringify(this.documentList));
106    }
107
108
109    clear() {
110        console.info("doClear ");
111        this.documentList = [];
112        this.distributedObject.documentList = this.documentList;
113        this.distributedObject.documentSize = 0;
114        console.info("doClear finish");
115    }
116}
117
118export var g_dataModel = new DistributedDataModel();