-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
241 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,39 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:iotic/ui/pages/value_unit.dart'; | ||
import 'package:iotic/ui/things/thing_property.dart'; | ||
import '../../data/thing_live_data.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:iotic/ui/things/thing_card_controller.dart'; | ||
import '../../data/repository.dart'; | ||
|
||
class ThingCard extends StatelessWidget { | ||
ThingCard(this._name, this._properties, {super.key}) { | ||
_name = _properties[ReadableThingProperty.name] ?? _name; | ||
var type = _properties[ReadableThingProperty.type]; | ||
_icon = _typeToIcon[type] ?? Icons.device_hub; | ||
if (_properties.containsKey(ReadableThingProperty.powerControl)) { | ||
_hasPowerControl = true; | ||
_powerControl = _properties[ReadableThingProperty.powerControl] != 0; | ||
if (_icon == Icons.device_hub) { | ||
_icon = Icons.electrical_services; | ||
} | ||
} | ||
late final _control = Get.put(ThingCardController(_id), tag: _id); | ||
|
||
dynamic p; | ||
if ((p = _properties[ReadableThingProperty.power]) != null) { | ||
_propertyWidgets | ||
.add(ThingProperty(Icons.electric_bolt, p, powerUnit(p.round()))); | ||
} | ||
if ((p = _properties[ReadableThingProperty.temperature]) != null) { | ||
_propertyWidgets.add(ThingProperty(Icons.thermostat, p, '°C')); | ||
} | ||
} | ||
ThingCard(this._id, {super.key}); | ||
|
||
final String _id; | ||
|
||
String _name; | ||
Map<ReadableThingProperty, dynamic> _properties; | ||
List<ThingProperty> _propertyWidgets = List.empty(growable: true); | ||
IconData _icon = Icons.device_hub; | ||
bool _hasPowerControl = false; | ||
bool _powerControl = false; | ||
final _repo = Get.find<Repository>(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column(children: [ | ||
Card( | ||
child: ListTile( | ||
leading: Icon(_icon, size: 32), | ||
title: Text(_name), | ||
subtitle: _propertyWidgets.isNotEmpty | ||
? Row( | ||
//mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: _propertyWidgets) | ||
child: Obx( | ||
() => ListTile( | ||
leading: Icon(_control.icon.value, size: 32), | ||
title: Text(_control.name.value), | ||
subtitle: _control.propertyWidgets.isNotEmpty | ||
? Row(children: _control.propertyWidgets.values.toList()) | ||
: null, | ||
trailing: _hasPowerControl | ||
trailing: _control.hasPowerControl.value | ||
? Switch( | ||
activeColor: Colors.yellow, | ||
value: _powerControl, | ||
onChanged: (value) {}, | ||
value: _control.powerControl.value, | ||
onChanged: (value) { | ||
_repo.set(_id, WritableThingProperty.powerControl, | ||
value ? 1.0 : 0.0); | ||
}, | ||
) | ||
: null), | ||
) | ||
)) | ||
]); | ||
} | ||
|
||
String powerUnit(int v) { | ||
if (v.abs() < 1000) return "W"; | ||
return "kW"; | ||
} | ||
|
||
static const Map<String?, IconData> _typeToIcon = { | ||
"smartMeter": Icons.electric_meter, | ||
"solarInverter": Icons.solar_power, | ||
"evStation": Icons.ev_station, | ||
null: Icons.device_hub | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:get/get_core/get_core.dart'; | ||
import 'package:get/get_instance/get_instance.dart'; | ||
import 'package:get/get_rx/get_rx.dart'; | ||
import 'package:get/get_state_manager/get_state_manager.dart'; | ||
import 'package:iotic/ui/things/thing_property.dart'; | ||
|
||
import '../../data/repository.dart'; | ||
import '../../data/thing_live_data.dart'; | ||
|
||
class ThingCardController extends GetxController { | ||
ThingCardController(this._id); | ||
|
||
final String _id; | ||
|
||
static final Map<String?, IconData> _typeToIcon = { | ||
"smartMeter": Icons.electric_meter, | ||
"solarInverter": Icons.solar_power, | ||
"evStation": Icons.ev_station, | ||
null: Icons.device_hub | ||
}; | ||
|
||
static String _powerUnit(int v) { | ||
if (v.abs() < 1000) return "W"; | ||
return "kW"; | ||
} | ||
|
||
final properties = <ReadableThingProperty, dynamic>{}.obs; | ||
final name = "".obs; | ||
final icon = Icons.device_hub.obs; | ||
final hasPowerControl = false.obs; | ||
final powerControl = false.obs; | ||
|
||
final propertyWidgets = <ReadableThingProperty, ThingProperty>{}.obs; | ||
|
||
@override | ||
void onReady() { | ||
//assignProperties(_repo.things); | ||
_repo.things.listen((p0) { | ||
assignProperties(p0); | ||
}); | ||
|
||
super.onReady(); | ||
} | ||
|
||
@override | ||
void onClose() { | ||
_repo.things.close(); | ||
super.onClose(); | ||
} | ||
|
||
void assignProperties(Map<String, ThingLiveData> props) { | ||
var p0 = props[_id]?.properties; | ||
if (p0 != null) { | ||
properties.value = p0; | ||
|
||
name.value = p0[ReadableThingProperty.name] ?? _id; | ||
icon.value = | ||
_typeToIcon[p0[ReadableThingProperty.type]] ?? Icons.device_hub; | ||
|
||
hasPowerControl.value = | ||
p0.containsKey(ReadableThingProperty.powerControl); | ||
if (hasPowerControl.value) { | ||
powerControl.value = p0[ReadableThingProperty.powerControl] != 0; | ||
if (icon.value == Icons.device_hub) { | ||
icon.value = Icons.electrical_services; | ||
} | ||
} | ||
|
||
dynamic p; | ||
if ((p = p0[ReadableThingProperty.power]) != null) { | ||
propertyWidgets[ReadableThingProperty.power] = | ||
ThingProperty(Icons.electric_bolt, p, _powerUnit(p.round())); | ||
} | ||
if ((p = p0[ReadableThingProperty.temperature]) != null) { | ||
propertyWidgets[ReadableThingProperty.temperature] = | ||
ThingProperty(Icons.thermostat, p, '°C'); | ||
} | ||
} | ||
} | ||
|
||
final Repository _repo = Get.find<Repository>(); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.