• 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 deviceManager from '@ohos.distributedHardware.deviceManager'
17import router from '@ohos.router'
18import { BUNDLE, ABILITY } from '../model/Const'
19import DeviceDialog from '../common/DeviceDialog'
20import DistributedObjectModel from '../model/DistributedObjectModel'
21import Logger from '../model/Logger'
22import Note from '../model/Note'
23import NoteDataSource from '../common/BasicDataSource'
24import NoteItem from '../common/NoteItem'
25import RemoteDeviceModel from '../model/RemoteDeviceModel'
26import TitleBar from '../common/TitleBar'
27const TAG: string = 'Index'
28
29@Entry
30@Component
31struct Index {
32  private dialogController?: CustomDialogController
33  private selectedIndex: number = 0
34  private sessionId: string = ''
35  @StorageLink('sessionId') globalSessionId: string = ''
36  @StorageLink('objectModel') globalObject: DistributedObjectModel = new DistributedObjectModel()
37  @State devices: Array<deviceManager.DeviceInfo> = []
38  @State isOnline: boolean = false
39  @State notes: Array<Note> = []
40  private noteDataSource : NoteDataSource = new NoteDataSource()
41
42  onPageShow() {
43    this.noteDataSource['dataArray'] = this.globalObject.distributedObject.documents
44    this.noteDataSource.notifyDataReload()
45    Logger.info(TAG, `this.sessionId = ${this.sessionId}`)
46    Logger.info(TAG, `globalSessionId = ${this.globalSessionId}`)
47    if (this.sessionId !== this.globalSessionId) {
48      this.sessionId = this.globalSessionId
49      this.share()
50    }
51  }
52
53  share() {
54    Logger.info(TAG, `sessionId = ${this.sessionId}`)
55    this.globalObject.setChangeCallback(() => {
56      this.noteDataSource['dataArray'] = this.globalObject.distributedObject.documents
57      this.noteDataSource.notifyDataReload()
58    })
59    this.globalObject.setStatusCallback((session, networkId, status) => {
60      Logger.info(TAG, `StatusCallback,${status}`)
61      if (status === 'online') {
62        this.isOnline = true
63      } else {
64        this.isOnline = false
65      }
66    })
67    this.globalObject.distributedObject.setSessionId(this.sessionId)
68    AppStorage.SetOrCreate('objectModel', this.globalObject)
69  }
70
71  clearSelectState() {
72    this.devices = []
73    this.dialogController.close()
74    this.dialogController = undefined
75  }
76
77  onSelectedDevice = (selectedIndex) => {
78    this.selectedIndex = selectedIndex
79    Logger.info(TAG, 'start ability ......')
80    if (RemoteDeviceModel === null || RemoteDeviceModel.discoverDevices.length <= 0) {
81      Logger.info(TAG, `start ability device:${JSON.stringify(this.devices)}`)
82      this.startAbility(this.devices[this.selectedIndex].deviceId)
83      this.clearSelectState()
84      return
85    }
86    Logger.info(TAG, 'start ability, needAuth')
87    RemoteDeviceModel.authenticateDevice(this.devices[this.selectedIndex], (device) => {
88      Logger.info(TAG, 'auth and online finished')
89      this.startAbility(device.deviceId)
90    })
91    Logger.info(TAG, 'start ability2 ......')
92    this.clearSelectState()
93  }
94
95  startAbility(deviceId) {
96    this.globalObject = new DistributedObjectModel()
97    this.sessionId = this.globalObject.genSessionId()
98    AppStorage.SetOrCreate('sessionId', this.sessionId)
99    this.noteDataSource['dataArray'] = []
100    this.noteDataSource.notifyDataReload()
101    this.globalObject.off()
102    this.share()
103    Logger.info(TAG, `startAbility deviceId:${deviceId}`)
104    let context = getContext(this) as any
105    context.startAbility({
106      bundleName: BUNDLE,
107      abilityName: ABILITY,
108      deviceId: deviceId,
109      parameters: {
110        sessionId: this.sessionId,
111      }
112    })
113  }
114
115  showDialog = () => {
116    RemoteDeviceModel.registerDeviceListCallback(() => {
117      Logger.info(TAG, 'registerDeviceListCallback, callback entered')
118      this.devices = []
119      this.devices = RemoteDeviceModel.discoverDevices.length > 0 ? RemoteDeviceModel.discoverDevices : RemoteDeviceModel.devices
120      if (this.dialogController) {
121        this.dialogController.close()
122        this.dialogController = undefined
123      }
124      this.dialogController = new CustomDialogController({
125        builder: DeviceDialog({
126          devices: this.devices,
127          onSelectedIndexChange: this.onSelectedDevice
128        }),
129        autoCancel: true
130      })
131      this.dialogController.open()
132    })
133  }
134
135  build() {
136    Column() {
137      TitleBar({ rightBtn: $r('app.media.trans'), onRightBtnClicked: this.showDialog })
138
139      Row() {
140        Text($r('app.string.state'))
141          .fontSize(30)
142        Image(this.isOnline ? $r('app.media.green') : $r('app.media.red'))
143          .size({ width: 30, height: 30 })
144          .objectFit(ImageFit.Contain)
145      }
146      .width('100%')
147      .padding(16)
148
149      List({ space: 10 }) {
150        LazyForEach(this.noteDataSource, (item, index) => {
151          ListItem() {
152            NoteItem({ note: item, index: index })
153          }
154        }, item => item.title)
155      }
156      .width('95%')
157      .margin(10)
158      .layoutWeight(1)
159
160      Row() {
161        Column() {
162          Image($r('app.media.clear'))
163            .size({ width: 40, height: 40 })
164          Text($r('app.string.clear'))
165            .fontColor(Color.Red)
166            .fontSize(20)
167        }.layoutWeight(1)
168        .key('clearNote')
169        .onClick(() => {
170          Logger.info(TAG, 'clear notes')
171          this.noteDataSource['dataArray'] = []
172          this.noteDataSource.notifyDataReload()
173          this.globalObject.clear()
174          AppStorage.SetOrCreate('sessionId', this.sessionId)
175        })
176
177        Column() {
178          Image($r('app.media.add'))
179            .size({ width: 40, height: 40 })
180          Text($r('app.string.add'))
181            .fontColor(Color.Black)
182            .fontSize(20)
183        }.layoutWeight(1)
184        .key('addNote')
185        .onClick(() => {
186          router.push({
187            url: 'pages/Edit',
188            params: {
189              note: new Note('', '', -1),
190              isAdd: true
191            }
192          })
193        })
194      }
195      .width('100%')
196      .padding(10)
197      .backgroundColor('#F0F0F0')
198    }
199    .width('100%')
200    .height('100%')
201    .backgroundColor('#F5F5F5')
202  }
203}