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