From bb4e37c6a0233453d6b9bed907fe725d56349bcc Mon Sep 17 00:00:00 2001 From: nickelskevin Date: Sun, 2 Oct 2022 16:14:05 -0700 Subject: [PATCH] feat: major data model refactoring Moving away from JSON document storage to more object models --- .../lib/forms/device_owner_form.dart | 61 +++ flutter/iot_receiver/lib/main.dart | 4 + .../lib/models/hro2_device_owner.dart | 20 + .../lib/screens/data_owners_screen.dart | 8 +- .../lib/screens/device_owners_screen.dart | 164 ++++++ .../lib/screens/devices_screen.dart | 4 +- .../lib/screens/receivers_screen.dart | 7 +- .../lib/services/hro2_data_service.dart | 501 +++++++++++++----- .../lib/widgets/hro2_drawer_widget.dart | 20 +- .../lib/widgets/new_data_owner_dialog.dart | 5 +- .../lib/widgets/new_device_dialog.dart | 2 +- .../lib/widgets/new_device_owner_dialog.dart | 195 +++++++ .../lib/widgets/new_receiver_dialog.dart | 5 +- 13 files changed, 833 insertions(+), 163 deletions(-) create mode 100644 flutter/iot_receiver/lib/forms/device_owner_form.dart create mode 100644 flutter/iot_receiver/lib/models/hro2_device_owner.dart create mode 100644 flutter/iot_receiver/lib/screens/device_owners_screen.dart create mode 100644 flutter/iot_receiver/lib/widgets/new_device_owner_dialog.dart diff --git a/flutter/iot_receiver/lib/forms/device_owner_form.dart b/flutter/iot_receiver/lib/forms/device_owner_form.dart new file mode 100644 index 00000000..dd403914 --- /dev/null +++ b/flutter/iot_receiver/lib/forms/device_owner_form.dart @@ -0,0 +1,61 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_form_builder/flutter_form_builder.dart'; +import 'package:form_builder_validators/form_builder_validators.dart'; +import 'package:auto_size_text/auto_size_text.dart'; + +// Some Form templates to reuse in New and Edit for devices + +FormBuilderDropdown deviceOwnerDeviceSelector(BuildContext context, items) { + return FormBuilderDropdown( + name: "device_selector", + items: items, + decoration: const InputDecoration( + // labelText: 'Select Device', + // labelStyle: TextStyle(fontWeight: FontWeight.bold), + hintText: 'Select Device', + ), + ); +} + +FormBuilderTextField deviceOwnerAtsignForm( + BuildContext context, String initialValue) { + return FormBuilderTextField( + initialValue: initialValue.toString(), + name: '@deviceOwner', + decoration: const InputDecoration( + labelText: 'Device Owner\'s atSign', + // fillColor: Colors.white, + // focusColor: Colors.lightGreenAccent, + labelStyle: TextStyle(fontWeight: FontWeight.bold), + ), + validator: FormBuilderValidators.required(), + style: const TextStyle(fontSize: 20, letterSpacing: 5)); +} + +class DeviceOwnerSubmitForm extends StatelessWidget { + const DeviceOwnerSubmitForm({ + Key? key, + required GlobalKey formKey, + }) : _formKey = formKey, + super(key: key); + + final GlobalKey _formKey; + + @override + Widget build(BuildContext context) { + return Expanded( + child: MaterialButton( + child: const AutoSizeText( + "Reset", + style: TextStyle(color: Colors.black), + maxLines: 1, + maxFontSize: 30, + minFontSize: 10, + ), + onPressed: () { + _formKey.currentState!.reset(); + }, + ), + ); + } +} diff --git a/flutter/iot_receiver/lib/main.dart b/flutter/iot_receiver/lib/main.dart index 64a7668e..ec081385 100644 --- a/flutter/iot_receiver/lib/main.dart +++ b/flutter/iot_receiver/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:at_app_flutter/at_app_flutter.dart' show AtEnv; import 'package:at_client_mobile/at_client_mobile.dart'; import 'package:iot_receiver/screens/data_owners_screen.dart'; +import 'package:iot_receiver/screens/device_owners_screen.dart'; import 'package:iot_receiver/screens/devices_screen.dart'; import 'package:iot_receiver/screens/receivers_screen.dart'; import 'package:at_utils/at_logger.dart' show AtSignLogger; @@ -9,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:iot_receiver/models/iot_model.dart'; import 'package:iot_receiver/widgets/new_data_owner_dialog.dart'; import 'package:iot_receiver/widgets/new_device_dialog.dart'; +import 'package:iot_receiver/widgets/new_device_owner_dialog.dart'; import 'package:iot_receiver/widgets/new_receiver_dialog.dart'; import 'package:path_provider/path_provider.dart' show getApplicationSupportDirectory; @@ -76,9 +78,11 @@ class _MyAppState extends State { ReceiversScreen.id: (_) => const ReceiversScreen(), DevicesScreen.id: (_) => const DevicesScreen(), DataOwnersScreen.id: (_) => const DataOwnersScreen(), + DeviceOwnersScreen.id: (_) => const DeviceOwnersScreen(), NewHrO2Device.id: (_) => const NewHrO2Device(), NewHrO2Receiver.id: (_) => const NewHrO2Receiver(), NewHrO2DataOwner.id: (_) => const NewHrO2DataOwner(), + NewHrO2DeviceOwner.id: (_) => const NewHrO2DeviceOwner(), //Next.id: (_) => const Next(), }, initialRoute: OnboardingScreen.id, diff --git a/flutter/iot_receiver/lib/models/hro2_device_owner.dart b/flutter/iot_receiver/lib/models/hro2_device_owner.dart new file mode 100644 index 00000000..eba602ac --- /dev/null +++ b/flutter/iot_receiver/lib/models/hro2_device_owner.dart @@ -0,0 +1,20 @@ +import 'package:iot_receiver/models/hro2_device.dart'; + +class HrO2DeviceOwner { + HrO2Device hrO2Device; + String deviceOwnerAtsign; + + HrO2DeviceOwner({ + required this.hrO2Device, + required this.deviceOwnerAtsign, + }); + + HrO2DeviceOwner.fromJson(Map json) + : hrO2Device = HrO2Device.fromJson(json['hrO2Device']), + deviceOwnerAtsign = json['deviceOwnerAtsign']; + + Map toJson() => { + 'hrO2Device': hrO2Device.toJson(), + 'deviceOwnerAtsign': deviceOwnerAtsign, + }; +} diff --git a/flutter/iot_receiver/lib/screens/data_owners_screen.dart b/flutter/iot_receiver/lib/screens/data_owners_screen.dart index 256b5459..ebcf64b8 100644 --- a/flutter/iot_receiver/lib/screens/data_owners_screen.dart +++ b/flutter/iot_receiver/lib/screens/data_owners_screen.dart @@ -21,7 +21,6 @@ class _DataOwnersScreenState extends State { @override Widget build(BuildContext context) { - _hrO2DataService.getDataOwners(); return Scaffold( appBar: NewGradientAppBar( title: const AutoSizeText( @@ -37,7 +36,7 @@ class _DataOwnersScreenState extends State { drawer: const HRo2DrawerWidget(), body: Builder( builder: (context) => FutureBuilder>( - future: _hrO2DataService.getDataOwnerList(), + future: _hrO2DataService.getDataOwners(), builder: (BuildContext context, AsyncSnapshot> snapshot) { List children; @@ -93,8 +92,7 @@ class _DataOwnersScreenState extends State { }, onDismissed: (_) async { hrO2DataOwnerList.remove(dataOwner); - await _hrO2DataService - .putDataOwnerList(hrO2DataOwnerList); + await _hrO2DataService.putDataOwner(dataOwner); setState(() {}); }, child: ListTile( @@ -103,7 +101,7 @@ class _DataOwnersScreenState extends State { color: Colors.blue, width: 1), borderRadius: BorderRadius.circular(10)), title: Text( - hrO2DataOwnerList[index].dataOwnerAtsign), + "${hrO2DataOwnerList[index].dataOwnerAtsign}[${hrO2DataOwnerList[index].hrO2Device.deviceAtsign}]"), subtitle: Text( "${hrO2DataOwnerList[index].hrO2Device.deviceAtsign} ${hrO2DataOwnerList[index].hrO2Device.sensorName.isNotEmpty ? hrO2DataOwnerList[index].hrO2Device.sensorName : ""}"), // trailing: const Icon(Icons.navigate_next), diff --git a/flutter/iot_receiver/lib/screens/device_owners_screen.dart b/flutter/iot_receiver/lib/screens/device_owners_screen.dart new file mode 100644 index 00000000..e8833509 --- /dev/null +++ b/flutter/iot_receiver/lib/screens/device_owners_screen.dart @@ -0,0 +1,164 @@ +import 'package:at_utils/at_logger.dart'; +import 'package:flutter/material.dart'; +import 'package:auto_size_text/auto_size_text.dart'; +import 'package:iot_receiver/models/hro2_device_owner.dart'; +import 'package:iot_receiver/services/hro2_data_service.dart'; +import 'package:iot_receiver/widgets/hro2_drawer_widget.dart'; +import 'package:iot_receiver/widgets/new_device_owner_dialog.dart'; +import 'package:new_gradient_app_bar/new_gradient_app_bar.dart'; + +final AtSignLogger _logger = AtSignLogger('DeviceOwnersScreen'); + +class DeviceOwnersScreen extends StatefulWidget { + const DeviceOwnersScreen({Key? key}) : super(key: key); + static const String id = '/device_owners_screen'; + @override + State createState() => _DeviceOwnersScreenState(); +} + +class _DeviceOwnersScreenState extends State { + final Hro2DataService _hrO2DataService = Hro2DataService(); + + @override + Widget build(BuildContext context) { + _hrO2DataService.getDeviceOwners(); + return Scaffold( + appBar: NewGradientAppBar( + title: const AutoSizeText( + 'Device Owners', + minFontSize: 5, + maxFontSize: 50, + ), + gradient: const LinearGradient(colors: [ + Color.fromARGB(255, 173, 83, 78), + Color.fromARGB(255, 108, 169, 197) + ]), + ), + drawer: const HRo2DrawerWidget(), + body: Builder( + builder: (context) => FutureBuilder>( + future: _hrO2DataService.getDeviceOwners(), + builder: (BuildContext context, + AsyncSnapshot> snapshot) { + List children; + if (snapshot.hasData) { + List? hrO2DeviceOwnerList = snapshot.data; + children = [ + const Text( + "The following deviceOwners have been created.", + overflow: TextOverflow.visible, + ), + const SizedBox( + height: 20, + ), + ListView.builder( + scrollDirection: Axis.vertical, + shrinkWrap: true, + // padding: const EdgeInsets.symmetric( + // vertical: 5, horizontal: 20), + itemCount: hrO2DeviceOwnerList!.length, + itemBuilder: (BuildContext context, int index) { + final HrO2DeviceOwner deviceOwner = + hrO2DeviceOwnerList[index]; + const align = Align( + alignment: Alignment.centerRight, + child: Padding( + padding: EdgeInsets.only(right: 16), + child: Icon(Icons.delete), + )); + return Dismissible( + key: Key(deviceOwner.deviceOwnerAtsign), + background: Container( + color: Colors.red, + child: align, + ), + confirmDismiss: (direction) async { + if (direction == DismissDirection.startToEnd) { + return false; + } else { + bool delete = true; + final snackbarController = + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Delete ${deviceOwner.deviceOwnerAtsign} ?'), + action: SnackBarAction( + label: 'Cancel', + onPressed: () => delete = false), + ), + ); + await snackbarController.closed; + return delete; + } + }, + onDismissed: (_) async { + hrO2DeviceOwnerList.remove(deviceOwner); + await _hrO2DataService + .putDeviceOwner(deviceOwner); + setState(() {}); + }, + child: ListTile( + shape: RoundedRectangleBorder( + side: const BorderSide( + color: Colors.blue, width: 1), + borderRadius: BorderRadius.circular(10)), + title: Text( + hrO2DeviceOwnerList[index].deviceOwnerAtsign), + subtitle: Text( + "${hrO2DeviceOwnerList[index].hrO2Device.deviceAtsign} ${hrO2DeviceOwnerList[index].hrO2Device.sensorName.isNotEmpty ? hrO2DeviceOwnerList[index].hrO2Device.sensorName : ""}"), + // trailing: const Icon(Icons.navigate_next), + ), + ); + }), + ]; + } else if (snapshot.hasError) { + _logger.severe(snapshot.error); + children = [ + const Icon( + Icons.error_outline, + color: Colors.red, + size: 60, + ), + Padding( + padding: const EdgeInsets.only(top: 16), + child: Text( + '${snapshot.error}, please click the + button below to add a deviceOwner.'), + ), + ]; + } else { + children = [ + // DoOnboardWidget( + // // futurePreference: widget.futurePreference, + // ), + ]; + } + return Padding( + padding: + const EdgeInsets.symmetric(horizontal: 20, vertical: 10), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: children, + ), + ); + // child: + })), + floatingActionButton: FloatingActionButton( + backgroundColor: Colors.green, + onPressed: () async { + var newDeviceOwner = await Navigator.push( + context, + MaterialPageRoute(builder: (context) => const NewHrO2DeviceOwner()), + ); + if (newDeviceOwner == null) { + } else { + setState(() { + // deviceOwners.add(newDeviceOwner); + // saveDeviceOwners(deviceOwners); + }); + } + }, + child: const Icon(Icons.add), + ), + ); + } +} diff --git a/flutter/iot_receiver/lib/screens/devices_screen.dart b/flutter/iot_receiver/lib/screens/devices_screen.dart index 62e74056..d0b331d8 100644 --- a/flutter/iot_receiver/lib/screens/devices_screen.dart +++ b/flutter/iot_receiver/lib/screens/devices_screen.dart @@ -34,7 +34,7 @@ class _DevicesScreenState extends State { drawer: const HRo2DrawerWidget(), body: Builder( builder: (context) => FutureBuilder>( - future: _hrO2DataService.getDeviceList(), + future: _hrO2DataService.getDevices(), builder: (BuildContext context, AsyncSnapshot> snapshot) { List children; @@ -88,7 +88,7 @@ class _DevicesScreenState extends State { }, onDismissed: (_) async { hrO2Devices.remove(device); - await _hrO2DataService.putDeviceList(hrO2Devices); + await _hrO2DataService.deleteDevice(device); setState(() {}); }, child: ListTile( diff --git a/flutter/iot_receiver/lib/screens/receivers_screen.dart b/flutter/iot_receiver/lib/screens/receivers_screen.dart index ec88e344..268e32fe 100644 --- a/flutter/iot_receiver/lib/screens/receivers_screen.dart +++ b/flutter/iot_receiver/lib/screens/receivers_screen.dart @@ -21,8 +21,6 @@ class _ReceiversScreenState extends State { @override Widget build(BuildContext context) { - _hrO2DataService.getReceivers(); - return Scaffold( appBar: NewGradientAppBar( title: const AutoSizeText( @@ -38,7 +36,7 @@ class _ReceiversScreenState extends State { drawer: const HRo2DrawerWidget(), body: Builder( builder: (context) => FutureBuilder>( - future: _hrO2DataService.getReceiverList(), + future: _hrO2DataService.getReceivers(), builder: (BuildContext context, AsyncSnapshot> snapshot) { List children; @@ -93,8 +91,7 @@ class _ReceiversScreenState extends State { }, onDismissed: (_) async { hrO2ReceiverList.remove(receiver); - await _hrO2DataService - .putReceiverList(hrO2ReceiverList); + await _hrO2DataService.putReceiver(receiver); setState(() {}); }, child: ListTile( diff --git a/flutter/iot_receiver/lib/services/hro2_data_service.dart b/flutter/iot_receiver/lib/services/hro2_data_service.dart index 15c921ba..0c6de52a 100644 --- a/flutter/iot_receiver/lib/services/hro2_data_service.dart +++ b/flutter/iot_receiver/lib/services/hro2_data_service.dart @@ -3,6 +3,7 @@ import 'package:at_client_mobile/at_client_mobile.dart'; import 'package:at_utils/at_logger.dart'; import 'package:iot_receiver/models/hro2_data_owner.dart'; import 'package:iot_receiver/models/hro2_device.dart'; +import 'package:iot_receiver/models/hro2_device_owner.dart'; import 'package:iot_receiver/models/hro2_receiver.dart'; class Hro2DataService { @@ -30,9 +31,11 @@ class Hro2DataService { } Future deleteAllData() async { - List keyStrings = AppConstants() as List; - for (var keyString in keyStrings) { - deleteAllForKey(keyString); + var keyStrings = AppConstants().listAllKeys; + for (var key in keyStrings) { + AtKey atKey = AtKey()..key = key; + await delete(atKey); + _logger.info('deleteAllData deleting ${atKey.toString()}'); } return true; } @@ -42,11 +45,10 @@ class Hro2DataService { var atClient = _atClient; var keys = await atClient.getAtKeys(regex: AppConstants.deviceKey); for (var element in keys) { - var receiverData = await atClient.get(element); - _logger.info('getDevices got ${receiverData.value}'); + var data = await atClient.get(element); + _logger.info('getDevices got ${data.value}'); try { - HrO2Device hrO2Device = - HrO2Device.fromJson(jsonDecode(receiverData.value)); + HrO2Device hrO2Device = HrO2Device.fromJson(jsonDecode(data.value)); hrO2DeviceList.add(hrO2Device); } catch (error) { // found some dirty data, consider deleting @@ -57,49 +59,70 @@ class Hro2DataService { return hrO2DeviceList; } - Future> getDeviceList() async { - AtKey atKey = AtKey()..key = AppConstants.deviceListKey; - var data = await _atClient.get(atKey); - _logger.info('getDeviceList got ${data.value}'); - List hrO2DeviceList; - hrO2DeviceList = (json.decode(data.value) as List) - .map((i) => HrO2Device.fromJson(i)) - .toList(); - _logger.info('getDeviceList returning $hrO2DeviceList'); - return hrO2DeviceList; - } - - Future putDeviceList(List hrO2DeviceList) async { - AtKey atKey = AtKey()..key = AppConstants.deviceListKey; - var value = jsonEncode(hrO2DeviceList); + Future putDevice(HrO2Device hrO2Device) async { + AtKey atKey = AtKey()..key = AppConstants.deviceKey; + var value = jsonEncode(hrO2Device); var response = await _atClient.put(atKey, value); _logger.info('putDeviceList success = $response'); return response; } - Future addDeviceToList(HrO2Device hrO2Device) async { - List deviceList = []; - deviceList = await getDeviceList().onError((error, stackTrace) async { - return deviceList; - }); - deviceList.add(hrO2Device); - AtKey atKey = AtKey()..key = AppConstants.deviceListKey; - var value = jsonEncode(deviceList); - var response = await _atClient.put(atKey, value); - _logger.info('addDeviceToList success = $response'); - return response; + Future deleteDevice(HrO2Device hrO2Device) async { + List keys = await _atClient.getAtKeys(regex: AppConstants.deviceKey); + _logger.info('deleteDevice processing ${keys.length} items'); + for (var key in keys) { + var data = await _atClient.get(key); + if (data.value == hrO2Device) { + _logger.info('deleteDevice deleting $key'); + delete(key); + } + } + return true; } + // Future> getDeviceList() async { + // AtKey atKey = AtKey()..key = AppConstants.deviceListKey; + // var data = await _atClient.get(atKey); + // _logger.info('getDeviceList got ${data.value}'); + // List hrO2DeviceList; + // hrO2DeviceList = (json.decode(data.value) as List) + // .map((i) => HrO2Device.fromJson(i)) + // .toList(); + // _logger.info('getDeviceList returning $hrO2DeviceList'); + // return hrO2DeviceList; + // } + + // Future putDeviceList(List hrO2DeviceList) async { + // AtKey atKey = AtKey()..key = AppConstants.deviceListKey; + // var value = jsonEncode(hrO2DeviceList); + // var response = await _atClient.put(atKey, value); + // _logger.info('putDeviceList success = $response'); + // return response; + // } + + // Future addDeviceToList(HrO2Device hrO2Device) async { + // List deviceList = []; + // deviceList = await getDeviceList().onError((error, stackTrace) async { + // return deviceList; + // }); + // deviceList.add(hrO2Device); + // AtKey atKey = AtKey()..key = AppConstants.deviceListKey; + // var value = jsonEncode(deviceList); + // var response = await _atClient.put(atKey, value); + // _logger.info('addDeviceToList success = $response'); + // return response; + // } + Future> getReceivers() async { List hrO2ReceiverList = []; var atClient = _atClient; - var keys = await atClient.getAtKeys(regex: AppConstants.deviceReceiverKey); + var keys = await atClient.getAtKeys(regex: AppConstants.receiverKey); for (var element in keys) { - var receiverData = await atClient.get(element); - _logger.info('getReceivers got ${receiverData.value}'); + var data = await atClient.get(element); + _logger.info('getReceivers got ${data.value}'); try { HrO2Receiver hrO2Receiver = - HrO2Receiver.fromJson(jsonDecode(receiverData.value)); + HrO2Receiver.fromJson(jsonDecode(data.value)); hrO2ReceiverList.add(hrO2Receiver); } catch (error) { // found some dirty data, consider deleting @@ -107,67 +130,123 @@ class Hro2DataService { // await atClient.delete(element); } } + _logger.info('getReceivers returning ${hrO2ReceiverList.toString()}'); return hrO2ReceiverList; } - Future> getReceiverList() async { - AtKey atKey = AtKey()..key = AppConstants.receiverListKey; - var data = await _atClient.get(atKey); - _logger.info('getReceiverList got ${data.value}'); - List hrO2ReceiverList; - hrO2ReceiverList = (json.decode(data.value) as List) - .map((i) => HrO2Receiver.fromJson(i)) - .toList(); - _logger.info('getReceiverList returning $hrO2ReceiverList'); - return hrO2ReceiverList; + Future putReceiver(HrO2Receiver hrO2Receiver) async { + AtKey atKey = AtKey()..key = AppConstants.receiverKey; + var value = jsonEncode(hrO2Receiver); + var success = await _atClient.put(atKey, value); + _logger.info('putReceiver success = $success'); + // now, share the list with the device + List receiverList = await getReceivers(); + atKey.key = AppConstants.receiverListKey; + atKey.sharedWith = hrO2Receiver.hrO2Device.deviceAtsign; + var receiverSuccess = await _atClient.put(atKey, jsonEncode(receiverList)); + _logger.info('putReceiver success = $receiverSuccess'); + return success && receiverSuccess; } - Future putReceiverList(List hrO2ReceiverList) async { - AtKey atKey = AtKey()..key = AppConstants.receiverListKey; - var value = jsonEncode(hrO2ReceiverList); - var response = await _atClient.put(atKey, value); - _logger.info('putReceiverList success = $response'); - return response; + Future deleteReceiver(HrO2Receiver hrO2Receiver) async { + List keys = + await _atClient.getAtKeys(regex: AppConstants.receiverKey); + _logger.info('deleteDevice processing ${keys.length} items'); + for (var key in keys) { + var data = await _atClient.get(key); + if (data.value == hrO2Receiver) { + _logger.info('deleteReceiver deleting $key'); + delete(key); + } + } + return true; } - Future addReceiverToList(HrO2Receiver hrO2Receiver) async { - List receiverList = []; - receiverList = await getReceiverList().onError((error, stackTrace) async { - return receiverList; - }); - receiverList.add(hrO2Receiver); - AtKey atKey = AtKey()..key = AppConstants.receiverListKey; - var receiverListJson = jsonEncode(receiverList); - var receiverSuccess = await AtClientManager.getInstance() - .atClient - .put(atKey, receiverListJson); - _logger.info('addReceiverToList success = $receiverSuccess'); - AtKey dataOwnerKey = AtKey() - ..key = AppConstants.deviceReceiverKey - ..sharedWith = hrO2Receiver.receiverAtsign; - var sharedReceiverJson = jsonEncode(hrO2Receiver); - var sharedReceiverSuccess = await AtClientManager.getInstance() - .atClient - .put(dataOwnerKey, sharedReceiverJson); - _logger.info('shareDeviceSuccess success = $sharedReceiverSuccess'); - atKey.sharedWith = hrO2Receiver.hrO2Device.deviceAtsign; - var deviceReceiverSuccess = await AtClientManager.getInstance() - .atClient - .put(atKey, receiverListJson); - _logger.info('deviceDataOwnerSuccess success = $deviceReceiverSuccess'); - return receiverSuccess && deviceReceiverSuccess; - } + // Future> getReceiverList() async { + // AtKey atKey = AtKey()..key = AppConstants.receiverListKey; + // var data = await _atClient.get(atKey); + // _logger.info('getReceiverList got ${data.value}'); + // List hrO2ReceiverList; + // hrO2ReceiverList = (json.decode(data.value) as List) + // .map((i) => HrO2Receiver.fromJson(i)) + // .toList(); + // _logger.info('getReceiverList returning $hrO2ReceiverList'); + // return hrO2ReceiverList; + // } + + // Future putReceiverList(List hrO2ReceiverList) async { + // AtKey atKey = AtKey()..key = AppConstants.receiverListKey; + // var value = jsonEncode(hrO2ReceiverList); + // var response = await _atClient.put(atKey, value); + // _logger.info('putReceiverList success = $response'); + // return response; + // } + + // Future addReceiver(HrO2Receiver hrO2Receiver) async { + // List receiverList = []; + // receiverList = await getReceiverList().onError((error, stackTrace) async { + // return receiverList; + // }); + // receiverList.add(hrO2Receiver); + // AtKey atKey = AtKey()..key = AppConstants.receiverListKey; + // var receiverListJson = jsonEncode(receiverList); + // var receiverSuccess = await AtClientManager.getInstance() + // .atClient + // .put(atKey, receiverListJson); + // _logger.info('addReceiverToList success = $receiverSuccess'); + // AtKey dataOwnerKey = AtKey() + // ..key = AppConstants.deviceReceiverKey + // ..sharedWith = hrO2Receiver.receiverAtsign; + // var sharedReceiverJson = jsonEncode(hrO2Receiver); + // var sharedReceiverSuccess = await AtClientManager.getInstance() + // .atClient + // .put(dataOwnerKey, sharedReceiverJson); + // _logger.info('shareDeviceSuccess success = $sharedReceiverSuccess'); + // atKey.sharedWith = hrO2Receiver.hrO2Device.deviceAtsign; + // var deviceReceiverSuccess = await AtClientManager.getInstance() + // .atClient + // .put(atKey, receiverListJson); + // _logger.info('deviceDataOwnerSuccess success = $deviceReceiverSuccess'); + // return receiverSuccess && deviceReceiverSuccess; + // } + + // Future addReceiverToList(HrO2Receiver hrO2Receiver) async { + // List receiverList = []; + // receiverList = await getReceiverList().onError((error, stackTrace) async { + // return receiverList; + // }); + // receiverList.add(hrO2Receiver); + // AtKey atKey = AtKey()..key = AppConstants.receiverListKey; + // var receiverListJson = jsonEncode(receiverList); + // var receiverSuccess = await AtClientManager.getInstance() + // .atClient + // .put(atKey, receiverListJson); + // _logger.info('addReceiverToList success = $receiverSuccess'); + // AtKey dataOwnerKey = AtKey() + // ..key = AppConstants.deviceReceiverKey + // ..sharedWith = hrO2Receiver.receiverAtsign; + // var sharedReceiverJson = jsonEncode(hrO2Receiver); + // var sharedReceiverSuccess = await AtClientManager.getInstance() + // .atClient + // .put(dataOwnerKey, sharedReceiverJson); + // _logger.info('shareDeviceSuccess success = $sharedReceiverSuccess'); + // atKey.sharedWith = hrO2Receiver.hrO2Device.deviceAtsign; + // var deviceReceiverSuccess = await AtClientManager.getInstance() + // .atClient + // .put(atKey, receiverListJson); + // _logger.info('deviceDataOwnerSuccess success = $deviceReceiverSuccess'); + // return receiverSuccess && deviceReceiverSuccess; + // } Future> getDataOwners() async { List hrO2DataOwnerList = []; - var keys = - await _atClient.getAtKeys(regex: AppConstants.deviceDataOwnerKey); + var keys = await _atClient.getAtKeys(regex: AppConstants.dataOwnerKey); for (var element in keys) { - var dataOwnerData = await _atClient.get(element); - _logger.info('getDataOwners got ${dataOwnerData.value}'); + var data = await _atClient.get(element); + _logger.info('getDataOwners got ${data.value}'); try { HrO2DataOwner hrO2DataOwner = - HrO2DataOwner.fromJson(jsonDecode(dataOwnerData.value)); + HrO2DataOwner.fromJson(jsonDecode(data.value)); hrO2DataOwnerList.add(hrO2DataOwner); } catch (error) { // found some dirty data, consider deleting @@ -178,81 +257,219 @@ class Hro2DataService { return hrO2DataOwnerList; } - Future> getDataOwnerList() async { - AtKey atKey = AtKey()..key = AppConstants.dataOwnerListKey; - var data = await _atClient.get(atKey); - _logger.info('getDataOwnerList got ${data.value}'); - List hrO2DataOwnerList; - hrO2DataOwnerList = (json.decode(data.value) as List) - .map((i) => HrO2DataOwner.fromJson(i)) - .toList(); - _logger.info('getDataOwnerList returning $hrO2DataOwnerList'); - return hrO2DataOwnerList; + Future putDataOwner(HrO2DataOwner hrO2DataOwner) async { + AtKey atKey = AtKey()..key = AppConstants.dataOwnerKey; + var success = await _atClient.put(atKey, jsonEncode(hrO2DataOwner)); + _logger.info('putDataOwner success = $success'); + List owners = await getDataOwners(); + atKey.key = AppConstants.dataOwnerListKey; + atKey.sharedWith = hrO2DataOwner.hrO2Device.deviceAtsign; + var dataOwnerSuccess = await _atClient.put(atKey, jsonEncode(owners)); + _logger.info('putDataOwner success = $dataOwnerSuccess'); + return success && dataOwnerSuccess; } - Future> getDeviceDataOwnerList() async { - AtKey atKey = AtKey() - ..key = AppConstants.deviceDataOwnerKey - ..sharedBy = "@mwcmanager"; - var data = await _atClient.get(atKey); - _logger.info('getDeviceDataOwner got ${data.value}'); - HrO2DataOwner hrO2DataOwner = - HrO2DataOwner.fromJson(json.decode(data.value)); - List hrO2DataOwnerList = []; - hrO2DataOwnerList.add(hrO2DataOwner); - _logger.info('getDeviceDataOwnerList returning $hrO2DataOwnerList'); - return hrO2DataOwnerList; + // Future> getDataOwnerList() async { + // AtKey atKey = AtKey()..key = AppConstants.dataOwnerListKey; + // var data = await _atClient.get(atKey); + // _logger.info('getDataOwnerList got ${data.value}'); + // List hrO2DataOwnerList; + // hrO2DataOwnerList = (json.decode(data.value) as List) + // .map((i) => HrO2DataOwner.fromJson(i)) + // .toList(); + // _logger.info('getDataOwnerList returning $hrO2DataOwnerList'); + // return hrO2DataOwnerList; + // } + +// Future> getDeviceDataOwnerList() async { +// AtKey atKey = AtKey() +// ..key = AppConstants.deviceDataOwnerKey +// ..sharedBy = "@mwcmanager"; +// var data = await _atClient.get(atKey); +// _logger.info('getDeviceDataOwner got ${data.value}'); +// HrO2DataOwner hrO2DataOwner = +// HrO2DataOwner.fromJson(json.decode(data.value)); +// List hrO2DataOwnerList = []; +// hrO2DataOwnerList.add(hrO2DataOwner); +// _logger.info('getDeviceDataOwnerList returning $hrO2DataOwnerList'); +// return hrO2DataOwnerList; +// } + +// Future putDataOwnerList(List hrO2DataOwnerList) async { +// AtKey atKey = AtKey()..key = AppConstants.dataOwnerListKey; +// var value = jsonEncode(hrO2DataOwnerList); +// var response = await _atClient.put(atKey, value); +// _logger.info('putDataOwnerList success = $response'); +// return response; +// } + +// Future addDataOwnerToList(HrO2DataOwner hrO2DataOwner) async { +// List dataOwnerList = []; +// dataOwnerList = await getDataOwnerList().onError((error, stackTrace) async { +// return dataOwnerList; +// }); +// dataOwnerList.add(hrO2DataOwner); +// AtKey listKey = AtKey()..key = AppConstants.dataOwnerListKey; +// var dataOwnerListJson = jsonEncode(dataOwnerList); +// var deviceOwnerSuccess = await AtClientManager.getInstance() +// .atClient +// .put(listKey, dataOwnerListJson); +// _logger.info('addDataOwnerToList success = $deviceOwnerSuccess'); +// // share information to the data owner +// var sharedDataOwnerJson = jsonEncode(hrO2DataOwner); +// AtKey dataOwnerKey = AtKey() +// ..key = AppConstants.deviceDataOwnerKey +// ..sharedWith = hrO2DataOwner.dataOwnerAtsign; +// var sharedDataOwnerSuccess = await AtClientManager.getInstance() +// .atClient +// .put(dataOwnerKey, sharedDataOwnerJson); +// _logger.info('shareDeviceSuccess success = $sharedDataOwnerSuccess'); +// // Share the list to the device +// dataOwnerKey.sharedWith = hrO2DataOwner.hrO2Device.deviceAtsign; +// var deviceDataOwnerSuccess = await AtClientManager.getInstance() +// .atClient +// .put(listKey, dataOwnerListJson); +// _logger.info('deviceDataOwnerSuccess success = $deviceDataOwnerSuccess'); +// return deviceOwnerSuccess && +// sharedDataOwnerSuccess && +// deviceDataOwnerSuccess; +// } + + Future> getDeviceOwners() async { + List hrO2DeviceOwnerList = []; + var keys = await _atClient.getAtKeys(regex: AppConstants.deviceOwnerKey); + for (var element in keys) { + var deviceOwnerData = await _atClient.get(element); + _logger.info('getDeviceOwners got ${deviceOwnerData.value}'); + try { + HrO2DeviceOwner hrO2DeviceOwner = + HrO2DeviceOwner.fromJson(jsonDecode(deviceOwnerData.value)); + hrO2DeviceOwnerList.add(hrO2DeviceOwner); + } catch (error) { + // found some dirty data, consider deleting + _logger.severe('getDeviceOwners error $error for ${element.key}'); + // await atClient.delete(element); + } + } + return hrO2DeviceOwnerList; } - Future putDataOwnerList(List hrO2DataOwnerList) async { - AtKey atKey = AtKey()..key = AppConstants.dataOwnerListKey; - var value = jsonEncode(hrO2DataOwnerList); - var response = await _atClient.put(atKey, value); - _logger.info('putDataOwnerList success = $response'); + Future putDeviceOwner(HrO2DeviceOwner hrO2DeviceOwner) async { + AtKey atKey = AtKey() + ..key = AppConstants.deviceOwnerKey + ..sharedWith = hrO2DeviceOwner.deviceOwnerAtsign; + var response = await _atClient.put(atKey, jsonEncode(hrO2DeviceOwner)); + _logger.info('putDeviceOwner success = $response'); return response; } - Future addDataOwnerToList(HrO2DataOwner hrO2DataOwner) async { - List dataOwnerList = []; - dataOwnerList = await getDataOwnerList().onError((error, stackTrace) async { - return dataOwnerList; - }); - dataOwnerList.add(hrO2DataOwner); - AtKey listKey = AtKey()..key = AppConstants.dataOwnerListKey; - var dataOwnerListJson = jsonEncode(dataOwnerList); - var deviceOwnerSuccess = await AtClientManager.getInstance() - .atClient - .put(listKey, dataOwnerListJson); - _logger.info('addDataOwnerToList success = $deviceOwnerSuccess'); -// share information to the data owner - var sharedDataOwnerJson = jsonEncode(hrO2DataOwner); - AtKey dataOwnerKey = AtKey() - ..key = AppConstants.deviceDataOwnerKey - ..sharedWith = hrO2DataOwner.dataOwnerAtsign; - var sharedDataOwnerSuccess = await AtClientManager.getInstance() - .atClient - .put(dataOwnerKey, sharedDataOwnerJson); - _logger.info('shareDeviceSuccess success = $sharedDataOwnerSuccess'); - // Share the list to the device - dataOwnerKey.sharedWith = hrO2DataOwner.hrO2Device.deviceAtsign; - var deviceDataOwnerSuccess = await AtClientManager.getInstance() - .atClient - .put(listKey, dataOwnerListJson); - _logger.info('deviceDataOwnerSuccess success = $deviceDataOwnerSuccess'); - return deviceOwnerSuccess && - sharedDataOwnerSuccess && - deviceDataOwnerSuccess; + Future deleteDeviceOwner(HrO2DeviceOwner hrO2DeviceOwner) async { + List keys = + await _atClient.getAtKeys(regex: AppConstants.deviceOwnerKey); + _logger.info('deleteDeviceOwner processing ${keys.length} items'); + for (var key in keys) { + var data = await _atClient.get(key); + if (data.value == hrO2DeviceOwner) { + _logger.info('deleteReceiver deleting $key'); + delete(key); + } + } + return true; } + +// Future> getDeviceOwnerList() async { +// AtKey atKey = AtKey()..key = AppConstants.deviceOwnerListKey; +// var data = await _atClient.get(atKey); +// _logger.info('getDeviceOwnerList got ${data.value}'); +// List hrO2DeviceOwnerList; +// hrO2DeviceOwnerList = (json.decode(data.value) as List) +// .map((i) => HrO2DeviceOwner.fromJson(i)) +// .toList(); +// _logger.info('getDeviceOwnerList returning $hrO2DeviceOwnerList'); +// return hrO2DeviceOwnerList; +// } + +// Future> getDeviceDeviceOwnerList() async { +// AtKey atKey = AtKey() +// ..key = AppConstants.deviceOwnerKey +// ..sharedBy = "@mwcmanager"; +// var data = await _atClient.get(atKey); +// _logger.info('getDeviceOwner got ${data.value}'); +// HrO2DeviceOwner hrO2DeviceOwner = +// HrO2DeviceOwner.fromJson(json.decode(data.value)); +// List hrO2DeviceOwnerList = []; +// hrO2DeviceOwnerList.add(hrO2DeviceOwner); +// _logger.info('getDeviceOwnerList returning $hrO2DeviceOwnerList'); +// return hrO2DeviceOwnerList; +// } + +// Future putDeviceOwnerList( +// List hrO2DeviceOwnerList) async { +// AtKey atKey = AtKey()..key = AppConstants.dataOwnerListKey; +// var value = jsonEncode(hrO2DeviceOwnerList); +// var response = await _atClient.put(atKey, value); +// _logger.info('putDeviceOwnerList success = $response'); +// return response; +// } + +// Future addDeviceOwnerToList(HrO2DeviceOwner hrO2DeviceOwner) async { +// List dataOwnerList = []; +// dataOwnerList = +// await getDeviceOwnerList().onError((error, stackTrace) async { +// return dataOwnerList; +// }); +// dataOwnerList.add(hrO2DeviceOwner); +// AtKey listKey = AtKey()..key = AppConstants.dataOwnerListKey; +// var dataOwnerListJson = jsonEncode(dataOwnerList); +// var deviceOwnerSuccess = await AtClientManager.getInstance() +// .atClient +// .put(listKey, dataOwnerListJson); +// _logger.info('addDeviceOwnerToList success = $deviceOwnerSuccess'); +// // share information to the data owner +// var sharedDeviceOwnerJson = jsonEncode(hrO2DeviceOwner); +// AtKey dataOwnerKey = AtKey() +// ..key = AppConstants.deviceDeviceOwnerKey +// ..sharedWith = hrO2DeviceOwner.dataOwnerAtsign; +// var sharedDeviceOwnerSuccess = await AtClientManager.getInstance() +// .atClient +// .put(dataOwnerKey, sharedDeviceOwnerJson); +// _logger.info('shareDeviceSuccess success = $sharedDeviceOwnerSuccess'); +// // Share the list to the device +// dataOwnerKey.sharedWith = hrO2DeviceOwner.hrO2Device.deviceAtsign; +// var deviceOwnerSuccess = await AtClientManager.getInstance() +// .atClient +// .put(listKey, dataOwnerListJson); +// _logger +// .info('deviceDeviceOwnerSuccess success = $deviceDeviceOwnerSuccess'); +// return deviceOwnerSuccess && +// sharedDeviceOwnerSuccess && +// deviceDeviceOwnerSuccess; +// } } class AppConstants { static const String libraryNamespace = 'iot_receiver'; static const String deviceListKey = 'device_list.$libraryNamespace'; static const String deviceKey = 'device.$libraryNamespace'; + static const String receiverKey = 'receiver.$libraryNamespace'; + static const String dataOwnerKey = 'data_owner.$libraryNamespace'; + static const String deviceOwnerKey = 'device_owner.$libraryNamespace'; static const String receiverListKey = 'receiver_list.$libraryNamespace'; static const String dataOwnerListKey = 'data_owner_list.$libraryNamespace'; + static const String deviceOwnerListKey = + 'device_owner_list.$libraryNamespace'; static const String deviceDataOwnerKey = 'device_data_owner.$libraryNamespace'; static const String deviceReceiverKey = 'device_receiver.$libraryNamespace'; static const int responseTimeLimit = 30; + List listAllKeys = [ + deviceListKey, + deviceKey, + receiverKey, + dataOwnerKey, + receiverListKey, + dataOwnerListKey, + deviceDataOwnerKey, + deviceReceiverKey, + ]; } diff --git a/flutter/iot_receiver/lib/widgets/hro2_drawer_widget.dart b/flutter/iot_receiver/lib/widgets/hro2_drawer_widget.dart index c0b55bc5..744a0ea7 100644 --- a/flutter/iot_receiver/lib/widgets/hro2_drawer_widget.dart +++ b/flutter/iot_receiver/lib/widgets/hro2_drawer_widget.dart @@ -2,8 +2,10 @@ import 'package:at_client_mobile/at_client_mobile.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:iot_receiver/screens/data_owners_screen.dart'; +import 'package:iot_receiver/screens/device_owners_screen.dart'; import 'package:iot_receiver/screens/onboarding_screen.dart'; import 'package:iot_receiver/widgets/new_data_owner_dialog.dart'; +import 'package:iot_receiver/widgets/new_device_owner_dialog.dart'; import '../services/hro2_data_service.dart'; import '../screens/devices_screen.dart'; import '../screens/home_screen.dart'; @@ -71,9 +73,16 @@ class _HRo2DrawerWidgetState extends State { ), if (isAdmin) ListTile( - title: const Text('Add a new data owner'), + title: const Text('See all device owners'), onTap: () { - Navigator.of(context).pushNamed(NewHrO2DataOwner.id); + Navigator.of(context).pushNamed(DeviceOwnersScreen.id); + }, + ), + if (isAdmin) + ListTile( + title: const Text('Add a new device owner'), + onTap: () { + Navigator.of(context).pushNamed(NewHrO2DeviceOwner.id); }, ), if (isAdmin) @@ -83,6 +92,13 @@ class _HRo2DrawerWidgetState extends State { Navigator.of(context).pushNamed(DataOwnersScreen.id); }, ), + if (isAdmin) + ListTile( + title: const Text('Add a new data owner'), + onTap: () { + Navigator.of(context).pushNamed(NewHrO2DataOwner.id); + }, + ), ListTile( title: const Text('Change atSign'), onTap: () async { diff --git a/flutter/iot_receiver/lib/widgets/new_data_owner_dialog.dart b/flutter/iot_receiver/lib/widgets/new_data_owner_dialog.dart index 93d42ceb..1aa5e0d9 100644 --- a/flutter/iot_receiver/lib/widgets/new_data_owner_dialog.dart +++ b/flutter/iot_receiver/lib/widgets/new_data_owner_dialog.dart @@ -83,7 +83,7 @@ class _NewHrO2DataOwnerState extends State { child: Column(children: [ Builder( builder: (context) => FutureBuilder>( - future: _hrO2DataService.getDeviceList(), + future: _hrO2DataService.getDevices(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasData) { @@ -134,8 +134,7 @@ class _NewHrO2DataOwnerState extends State { dataOwnerAtsign: dataOwnerAtsign, hrO2Device: device, ); - await _hrO2DataService - .addDataOwnerToList(newDataOwner); + await _hrO2DataService.putDataOwner(newDataOwner); if (mounted) { Navigator.of(context) .pushNamed(DataOwnersScreen.id); diff --git a/flutter/iot_receiver/lib/widgets/new_device_dialog.dart b/flutter/iot_receiver/lib/widgets/new_device_dialog.dart index 577ef8c2..e5806a78 100644 --- a/flutter/iot_receiver/lib/widgets/new_device_dialog.dart +++ b/flutter/iot_receiver/lib/widgets/new_device_dialog.dart @@ -87,7 +87,7 @@ class _NewHrO2DeviceState extends State { sensorName.isNotEmpty ? sensorName : '', deviceUuid: UniqueKey().toString(), ); - await _hrO2DataService.addDeviceToList(newDevice); + await _hrO2DataService.putDevice(newDevice); if (mounted) { Navigator.of(context) .pushNamed(DevicesScreen.id); diff --git a/flutter/iot_receiver/lib/widgets/new_device_owner_dialog.dart b/flutter/iot_receiver/lib/widgets/new_device_owner_dialog.dart new file mode 100644 index 00000000..b0370157 --- /dev/null +++ b/flutter/iot_receiver/lib/widgets/new_device_owner_dialog.dart @@ -0,0 +1,195 @@ +import 'dart:io'; +import 'package:flutter/material.dart'; +import 'package:flutter_form_builder/flutter_form_builder.dart'; +import 'package:auto_size_text/auto_size_text.dart'; +import 'package:iot_receiver/forms/device_owner_form.dart'; +import 'package:iot_receiver/models/hro2_device.dart'; +import 'package:iot_receiver/models/hro2_device_owner.dart'; +import 'package:iot_receiver/screens/device_owners_screen.dart'; +import 'package:iot_receiver/services/hro2_data_service.dart'; +import 'package:new_gradient_app_bar/new_gradient_app_bar.dart'; + +class NewHrO2DeviceOwner extends StatefulWidget { + const NewHrO2DeviceOwner({Key? key}) : super(key: key); + static const String id = '/new_deviceOwner'; + + @override + State createState() => _NewHrO2DeviceOwnerState(); +} + +class _NewHrO2DeviceOwnerState extends State { + final _formKey = GlobalKey(); + final Hro2DataService _hrO2DataService = Hro2DataService(); + + @override + Widget build(BuildContext context) { + double width = MediaQuery.of(context).size.width; + double height = MediaQuery.of(context).size.height; + int gridRows = 1; + if (width > height) { + gridRows = 2; + } else { + gridRows = 1; + } + + return Scaffold( + appBar: NewGradientAppBar( + title: const AutoSizeText( + 'New DeviceOwner', + minFontSize: 5, + maxFontSize: 50, + ), + gradient: const LinearGradient(colors: [ + Color.fromARGB(255, 173, 83, 78), + Color.fromARGB(255, 108, 169, 197) + ]), + actions: [ + PopupMenuButton( + color: const Color.fromARGB(255, 108, 169, 197), + //padding: const EdgeInsets.symmetric(horizontal: 10), + icon: const Icon( + Icons.menu, + size: 20, + ), + onSelected: (String result) { + switch (result) { + case 'CLOSE': + exit(0); + default: + } + }, + itemBuilder: (BuildContext context) => >[ + const PopupMenuItem( + height: 20, + value: 'CLOSE', + child: Text( + 'CLOSE', + style: TextStyle( + fontSize: 15, + letterSpacing: 5, + backgroundColor: Color.fromARGB(255, 108, 169, 197), + color: Colors.black), + ), + ), + ], + ), + ], + ), + body: Container( + decoration: backgroundGradient(gridRows), + child: SingleChildScrollView( + child: FormBuilder( + key: _formKey, + child: Column(children: [ + Builder( + builder: (context) => FutureBuilder>( + future: _hrO2DataService.getDevices(), + builder: (BuildContext context, + AsyncSnapshot> snapshot) { + if (snapshot.hasData) { + return deviceOwnerDeviceSelector( + context, + snapshot.data + ?.map((item) => + DropdownMenuItem( + value: item, + child: Text(item.deviceAtsign), + )) + .toList()); + } else { + return const Text("loading"); + } + })), + deviceOwnerAtsignForm(context, ''), + Row( + children: [ + const SizedBox(width: 20), + Expanded( + child: BackButton( + onPressed: () { + Navigator.pop(context); + }, + ), + ), + const Spacer(), + DeviceOwnerSubmitForm(formKey: _formKey), + const Spacer(), + Expanded( + child: MaterialButton( + child: const AutoSizeText( + "Submit", + style: TextStyle(color: Colors.black), + maxLines: 1, + maxFontSize: 30, + minFontSize: 10, + ), + onPressed: () async { + _formKey.currentState!.save(); + if (_formKey.currentState!.validate()) { + HrO2Device device = _formKey.currentState! + .fields['device_selector']!.value; + String deviceOwnerAtsign = _formKey + .currentState!.fields['@deviceOwner']!.value; + var newDeviceOwner = HrO2DeviceOwner( + deviceOwnerAtsign: deviceOwnerAtsign, + hrO2Device: device, + ); + await _hrO2DataService + .putDeviceOwner(newDeviceOwner); + if (mounted) { + Navigator.of(context) + .pushNamed(DeviceOwnersScreen.id); + } + } else { + Navigator.pop(context, null); + } + }, + ), + ), + const SizedBox(width: 20) + ], + ), + // Row( + // children: [ + // SizedBox( + // width: _width, + // height: _height, + // ) + // ], + // ) + ])), + ), + )); + } +} + +BoxDecoration backgroundGradient(int gridRows) { + return BoxDecoration( + color: Colors.white70, + gradient: gridRows > 1 + ? const LinearGradient( + begin: Alignment.topLeft, + end: Alignment.bottomRight, + colors: [ + Color.fromARGB(255, 240, 181, 178), + Color.fromARGB(255, 171, 200, 224) + ], + ) + : const LinearGradient( + begin: Alignment.topLeft, + end: Alignment.bottomRight, + colors: [ + Color.fromARGB(255, 240, 181, 178), + Color.fromARGB(255, 171, 200, 224) + ], + ), + image: const DecorationImage( + opacity: .15, + fit: BoxFit.cover, + alignment: Alignment.center, + image: AssetImage( + 'assets/images/blood-pressure.png', + ), + ), + ); +} diff --git a/flutter/iot_receiver/lib/widgets/new_receiver_dialog.dart b/flutter/iot_receiver/lib/widgets/new_receiver_dialog.dart index 4aa39dd4..ecf92c67 100644 --- a/flutter/iot_receiver/lib/widgets/new_receiver_dialog.dart +++ b/flutter/iot_receiver/lib/widgets/new_receiver_dialog.dart @@ -87,7 +87,7 @@ class _NewHrO2ReceiverState extends State { ), Builder( builder: (context) => FutureBuilder>( - future: _hrO2DataService.getDeviceDataOwnerList(), + future: _hrO2DataService.getDataOwners(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasData) { @@ -150,8 +150,7 @@ class _NewHrO2ReceiverState extends State { hrO2Device: device, sendHR: sendHr, sendO2: sendO2); - await _hrO2DataService - .addReceiverToList(newReceiver); + await _hrO2DataService.putReceiver(newReceiver); if (mounted) { Navigator.of(context) .pushNamed(ReceiversScreen.id);