-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectDevicePage.qml
313 lines (283 loc) · 12.3 KB
/
SelectDevicePage.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Skruuvi - Reader for Ruuvi sensors
Copyright (C) 2023 Miika Malin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see [http://www.gnu.org/licenses/].
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
Page {
property int leftMargin: Theme.horizontalPageMargin
property int rightMargin: Theme.horizontalPageMargin
function clearBluetoothIcons() {
for (var i = 0; i < deviceModel.count; i++) {
deviceModel.setProperty(i, "showBluetoothIcon", false);
}
}
SilicaFlickable {
anchors.fill: parent
PullDownMenu {
MenuItem {
text: "Scan"
onClicked: {
clearBluetoothIcons();
busyIndicator.visible = true;
busyIndicator.running = true;
ld.startDiscovery();
}
}
}
PageHeader {
id: pHeader
title: "Select Ruuvi device"
anchors.top: parent.top
}
Image {
id: skruuviLogo
source: "images/skruuvi-logo.png"
width: parent.width - (leftMargin + rightMargin)
fillMode: Image.PreserveAspectFit
//height: 0.2667 * skruuviLogo.width
anchors.top: pHeader.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
BusyIndicator {
id: busyIndicator
size: BusyIndicatorSize.Medium
anchors.centerIn: deviceList
visible: false // Initially hidden
}
SilicaListView {
id: deviceList
width: parent.width - (leftMargin + rightMargin)
height: parent.height - skruuviLogo.height - pHeader.height
anchors.top: skruuviLogo.bottom
anchors.bottom: parent.bottom
clip: true
header: PageHeader {
id: listHeader
title: "Devices"
}
model: ListModel {
id: deviceModel
}
VerticalScrollDecorator {}
// Populate the list with known devices
Component.onCompleted: {
// Fetch devices from the database
var devices = db.getDevices();
// Add the fetched devices to the model
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
deviceModel.append({
deviceName: device.deviceName,
deviceAddress: device.deviceAddress,
showBluetoothIcon: false
});
}
}
Label {
width: parent.width
text: "No known devices, use the pull down menu to scan for new Ruuvi devices"
font.pixelSize: Theme.fontSizeLarge
color: Theme.highlightColor
//anchors.centerIn: parent
visible: deviceModel.count === 0
wrapMode: Text.Wrap
anchors {
left: parent.left
leftMargin: leftMargin
right: parent.right
rightMargin: rightMargin
verticalCenter: parent.verticalCenter
}
}
// Define how the ListItems looks like
delegate: ListItem {
id: listItem
contentHeight: Theme.itemSizeMedium
width: parent.width
Item {
width: parent.width
height: Theme.itemSizeMedium
ListView.onAdd: AddAnimation {
target: listItem
}
ListView.onRemove: RemoveAnimation {
target: listItem
}
Image {
id: icon
source: "images/ruuvitag-enclosure-bottom-768x768.jpg"
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: leftMargin
}
Image {
source: "image://theme/icon-s-bluetooth"
width: Theme.iconSizeSmall
height: Theme.iconSizeSmall
visible: showBluetoothIcon
anchors {
right: parent.right
rightMargin: Theme.paddingMedium
verticalCenter: parent.verticalCenter
}
}
Label {
id: topLabel
text: model.deviceName
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: -topLabel.height / 2
anchors.left: icon.right
anchors.leftMargin: Theme.paddingMedium
}
Label {
anchors.top: topLabel.bottom
text: model.deviceAddress
font.pixelSize: Theme.fontSizeSmall
anchors.left: icon.right
anchors.leftMargin: Theme.paddingMedium
}
}
menu: ContextMenu {
id: deviceMenu
closeOnActivation: true
MenuItem {
text: "Show data"
onClicked: {
var selectedDevice = {
deviceName: model.deviceName,
deviceAddress: model.deviceAddress
}
// On default show last 24h data
var currentTime = Math.floor(Date.now() / 1000);
pageStack.push(Qt.resolvedUrl("plotDataPage.qml"),
{
startTime: currentTime - 86400,
endTime: currentTime,
selectedDevice: selectedDevice
})
}
}
MenuItem {
text: "Fetch data"
onClicked: {
var selectedDevice = {
deviceName: model.deviceName,
deviceAddress: model.deviceAddress
}
// Handle button click to navigate to the page displaying data fetch setup
pageStack.push(Qt.resolvedUrl("GetDataPage.qml"), {selectedDevice: selectedDevice})
}
}
MenuItem {
id: renameMenuItem
text: "Rename device"
onClicked: {
// Prevent the menu from closing when Rename device is clicked
deviceMenu.closeOnActivation = false
var selectedDevice = {
deviceName: model.deviceName,
deviceAddress: model.deviceAddress
}
renameFieldItem.visible = true
// Set the textfield as active
newDeviceName.forceActiveFocus()
}
}
MenuItem {
id: renameFieldItem
visible: false
clip: true
height: newDeviceName.height
TextField {
width: parent.width - renameOkButton.width
id: newDeviceName
placeholderText: "Enter new name"
label: "New name"
EnterKey.enabled: text.length > 0
EnterKey.iconSource: "image://theme/icon-m-accept"
EnterKey.onClicked: {
var newName = newDeviceName.text;
// Update the device name in the database
db.renameDevice(deviceAddress, newName);
// Update the name in the listView
if (newName !== "") {
deviceModel.setProperty(index, "deviceName", newName);
}
// Restore menu item activation and hide text field and button
deviceMenu.closeOnActivation = true
deviceMenu.close()
renameFieldItem.visible = false
}
}
IconButton {
id: renameOkButton
anchors.left: newDeviceName.right
icon.source: "image://theme/icon-m-accept"
onClicked: {
var newName = newDeviceName.text;
// Update the device name in the database
db.renameDevice(deviceAddress, newName);
// Update the name in the listView
if (newName !== "") {
deviceModel.setProperty(index, "deviceName", newName);
}
// Restore menu item activation and hide text field and button
deviceMenu.closeOnActivation = true
deviceMenu.close()
renameFieldItem.visible = false
}
}
}
MenuItem {
text: "Remove device"
onClicked: listItem.remorseDelete(function() {
// Remove device and device data from database
db.removeDevice(deviceAddress)
// Remove device from the list
deviceModel.remove(index)
})
}
}
}
}
}
Connections {
target: ld
onDeviceFound: {
// Check if a device with the same address already exists in the model
var existingDeviceIndex = -1;
for (var i = 0; i < deviceModel.count; i++) {
if (deviceModel.get(i).deviceAddress === deviceAddress) {
existingDeviceIndex = i;
// Update the existing device to show the Bluetooth icon
deviceModel.setProperty(existingDeviceIndex, "showBluetoothIcon", true);
break;
}
}
if (existingDeviceIndex === -1) {
// Add the found device to the model
deviceModel.append({
deviceName: deviceName,
deviceAddress: deviceAddress,
showBluetoothIcon: true
});
}
}
onDiscoveryStopped: {
busyIndicator.visible = false;
}
}
}