Skip to content

Commit

Permalink
feat: major data model refactoring
Browse files Browse the repository at this point in the history
Moving away from JSON document storage to more object models
  • Loading branch information
nickelskevin committed Oct 2, 2022
1 parent 258c34a commit bb4e37c
Show file tree
Hide file tree
Showing 13 changed files with 833 additions and 163 deletions.
61 changes: 61 additions & 0 deletions flutter/iot_receiver/lib/forms/device_owner_form.dart
Original file line number Diff line number Diff line change
@@ -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<FormBuilderState> formKey,
}) : _formKey = formKey,
super(key: key);

final GlobalKey<FormBuilderState> _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();
},
),
);
}
}
4 changes: 4 additions & 0 deletions flutter/iot_receiver/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ 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;
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;
Expand Down Expand Up @@ -76,9 +78,11 @@ class _MyAppState extends State<MyApp> {
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,
Expand Down
20 changes: 20 additions & 0 deletions flutter/iot_receiver/lib/models/hro2_device_owner.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json)
: hrO2Device = HrO2Device.fromJson(json['hrO2Device']),
deviceOwnerAtsign = json['deviceOwnerAtsign'];

Map<String, dynamic> toJson() => {
'hrO2Device': hrO2Device.toJson(),
'deviceOwnerAtsign': deviceOwnerAtsign,
};
}
8 changes: 3 additions & 5 deletions flutter/iot_receiver/lib/screens/data_owners_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class _DataOwnersScreenState extends State<DataOwnersScreen> {

@override
Widget build(BuildContext context) {
_hrO2DataService.getDataOwners();
return Scaffold(
appBar: NewGradientAppBar(
title: const AutoSizeText(
Expand All @@ -37,7 +36,7 @@ class _DataOwnersScreenState extends State<DataOwnersScreen> {
drawer: const HRo2DrawerWidget(),
body: Builder(
builder: (context) => FutureBuilder<List<HrO2DataOwner>>(
future: _hrO2DataService.getDataOwnerList(),
future: _hrO2DataService.getDataOwners(),
builder: (BuildContext context,
AsyncSnapshot<List<HrO2DataOwner>> snapshot) {
List<Widget> children;
Expand Down Expand Up @@ -93,8 +92,7 @@ class _DataOwnersScreenState extends State<DataOwnersScreen> {
},
onDismissed: (_) async {
hrO2DataOwnerList.remove(dataOwner);
await _hrO2DataService
.putDataOwnerList(hrO2DataOwnerList);
await _hrO2DataService.putDataOwner(dataOwner);
setState(() {});
},
child: ListTile(
Expand All @@ -103,7 +101,7 @@ class _DataOwnersScreenState extends State<DataOwnersScreen> {
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),
Expand Down
164 changes: 164 additions & 0 deletions flutter/iot_receiver/lib/screens/device_owners_screen.dart
Original file line number Diff line number Diff line change
@@ -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<DeviceOwnersScreen> createState() => _DeviceOwnersScreenState();
}

class _DeviceOwnersScreenState extends State<DeviceOwnersScreen> {
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<List<HrO2DeviceOwner>>(
future: _hrO2DataService.getDeviceOwners(),
builder: (BuildContext context,
AsyncSnapshot<List<HrO2DeviceOwner>> snapshot) {
List<Widget> children;
if (snapshot.hasData) {
List<HrO2DeviceOwner>? hrO2DeviceOwnerList = snapshot.data;
children = <Widget>[
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 = <Widget>[
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 = <Widget>[
// 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),
),
);
}
}
4 changes: 2 additions & 2 deletions flutter/iot_receiver/lib/screens/devices_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _DevicesScreenState extends State<DevicesScreen> {
drawer: const HRo2DrawerWidget(),
body: Builder(
builder: (context) => FutureBuilder<List<HrO2Device>>(
future: _hrO2DataService.getDeviceList(),
future: _hrO2DataService.getDevices(),
builder: (BuildContext context,
AsyncSnapshot<List<HrO2Device>> snapshot) {
List<Widget> children;
Expand Down Expand Up @@ -88,7 +88,7 @@ class _DevicesScreenState extends State<DevicesScreen> {
},
onDismissed: (_) async {
hrO2Devices.remove(device);
await _hrO2DataService.putDeviceList(hrO2Devices);
await _hrO2DataService.deleteDevice(device);
setState(() {});
},
child: ListTile(
Expand Down
7 changes: 2 additions & 5 deletions flutter/iot_receiver/lib/screens/receivers_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class _ReceiversScreenState extends State<ReceiversScreen> {

@override
Widget build(BuildContext context) {
_hrO2DataService.getReceivers();

return Scaffold(
appBar: NewGradientAppBar(
title: const AutoSizeText(
Expand All @@ -38,7 +36,7 @@ class _ReceiversScreenState extends State<ReceiversScreen> {
drawer: const HRo2DrawerWidget(),
body: Builder(
builder: (context) => FutureBuilder<List<HrO2Receiver>>(
future: _hrO2DataService.getReceiverList(),
future: _hrO2DataService.getReceivers(),
builder: (BuildContext context,
AsyncSnapshot<List<HrO2Receiver>> snapshot) {
List<Widget> children;
Expand Down Expand Up @@ -93,8 +91,7 @@ class _ReceiversScreenState extends State<ReceiversScreen> {
},
onDismissed: (_) async {
hrO2ReceiverList.remove(receiver);
await _hrO2DataService
.putReceiverList(hrO2ReceiverList);
await _hrO2DataService.putReceiver(receiver);
setState(() {});
},
child: ListTile(
Expand Down
Loading

0 comments on commit bb4e37c

Please sign in to comment.