Reverting views refactoring
This commit is contained in:
@ -4,13 +4,11 @@ class HACard extends StatelessWidget {
|
|||||||
|
|
||||||
final List<Entity> entities;
|
final List<Entity> entities;
|
||||||
final String friendlyName;
|
final String friendlyName;
|
||||||
final bool hidden;
|
|
||||||
|
|
||||||
const HACard({
|
const HACard({
|
||||||
Key key,
|
Key key,
|
||||||
this.entities,
|
this.entities,
|
||||||
this.friendlyName,
|
this.friendlyName
|
||||||
this.hidden
|
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -18,13 +16,9 @@ class HACard extends StatelessWidget {
|
|||||||
List<Widget> body = [];
|
List<Widget> body = [];
|
||||||
body.add(_buildCardHeader());
|
body.add(_buildCardHeader());
|
||||||
body.addAll(_buildCardBody(context));
|
body.addAll(_buildCardBody(context));
|
||||||
if (hidden) {
|
return Card(
|
||||||
return Container(height: 0.0,);
|
child: new Column(mainAxisSize: MainAxisSize.min, children: body)
|
||||||
} else {
|
);
|
||||||
return Card(
|
|
||||||
child: new Column(mainAxisSize: MainAxisSize.min, children: body)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildCardHeader() {
|
Widget _buildCardHeader() {
|
||||||
|
@ -62,7 +62,6 @@ class Entity {
|
|||||||
String get unitOfMeasurement => attributes["unit_of_measurement"] ?? "";
|
String get unitOfMeasurement => attributes["unit_of_measurement"] ?? "";
|
||||||
List get childEntityIds => attributes["entity_id"] ?? [];
|
List get childEntityIds => attributes["entity_id"] ?? [];
|
||||||
String get lastUpdated => _getLastUpdatedFormatted();
|
String get lastUpdated => _getLastUpdatedFormatted();
|
||||||
bool get isHidden => attributes["hidden"] ?? false;
|
|
||||||
|
|
||||||
Entity(Map rawData) {
|
Entity(Map rawData) {
|
||||||
update(rawData);
|
update(rawData);
|
||||||
|
@ -781,12 +781,14 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
|
|||||||
Color _tmpColor;
|
Color _tmpColor;
|
||||||
bool _changedHere = false;
|
bool _changedHere = false;
|
||||||
String _tmpEffect;
|
String _tmpEffect;
|
||||||
|
String _tmpFlash;
|
||||||
|
|
||||||
void _resetState(LightEntity entity) {
|
void _resetState(LightEntity entity) {
|
||||||
_tmpBrightness = entity.brightness;
|
_tmpBrightness = entity.brightness;
|
||||||
_tmpColorTemp = entity.colorTemp;
|
_tmpColorTemp = entity.colorTemp;
|
||||||
_tmpColor = entity.color;
|
_tmpColor = entity.color;
|
||||||
_tmpEffect = null;
|
_tmpEffect = null;
|
||||||
|
_tmpFlash = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setBrightness(LightEntity entity, double value) {
|
void _setBrightness(LightEntity entity, double value) {
|
||||||
@ -844,6 +846,18 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _setFlash(LightEntity entity, String value) {
|
||||||
|
setState(() {
|
||||||
|
_tmpFlash = value;
|
||||||
|
_changedHere = true;
|
||||||
|
if (_tmpFlash != null) {
|
||||||
|
eventBus.fire(new ServiceCallEvent(
|
||||||
|
entity.domain, "turn_on", entity.entityId,
|
||||||
|
{"flash": "$value"}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final entityModel = EntityModel.of(context);
|
final entityModel = EntityModel.of(context);
|
||||||
|
@ -13,8 +13,8 @@ class ViewBuilder{
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget buildWidget(BuildContext context) {
|
Widget buildWidget(BuildContext context) {
|
||||||
return TabBarView(
|
return ViewBuilderWidget(
|
||||||
children: _views
|
entities: _views
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class ViewBuilder{
|
|||||||
entitiesForView.add(entityCollection.get(entityId));
|
entitiesForView.add(entityCollection.get(entityId));
|
||||||
});
|
});
|
||||||
return View(
|
return View(
|
||||||
childEntities: entitiesForView,
|
entities: entitiesForView,
|
||||||
count: 0
|
count: 0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ class ViewBuilder{
|
|||||||
});
|
});
|
||||||
result.add(View(
|
result.add(View(
|
||||||
count: counter,
|
count: counter,
|
||||||
childEntities: entitiesForView
|
entities: entitiesForView
|
||||||
));
|
));
|
||||||
/*} catch (error) {
|
/*} catch (error) {
|
||||||
TheLogger.log("Error","Error parsing view: $viewId");
|
TheLogger.log("Error","Error parsing view: $viewId");
|
||||||
@ -76,4 +76,30 @@ class ViewBuilder{
|
|||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ViewBuilderWidget extends StatelessWidget {
|
||||||
|
|
||||||
|
final List<View> entities;
|
||||||
|
|
||||||
|
const ViewBuilderWidget({
|
||||||
|
Key key,
|
||||||
|
this.entities
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return TabBarView(
|
||||||
|
children: _buildChildren(context)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _buildChildren(BuildContext context) {
|
||||||
|
List<Widget> result = [];
|
||||||
|
entities.forEach((View view){
|
||||||
|
result.add(view.buildWidget(context));
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,30 +1,77 @@
|
|||||||
part of 'main.dart';
|
part of 'main.dart';
|
||||||
|
|
||||||
class View extends StatefulWidget {
|
class View {
|
||||||
final String displayName;
|
List<Entity> childEntitiesAsBadges;
|
||||||
final List<Entity> childEntities;
|
Map<String, CardSkeleton> childEntitiesAsCards;
|
||||||
final int count;
|
|
||||||
|
int count;
|
||||||
|
List<Entity> entities;
|
||||||
|
|
||||||
View({
|
View({
|
||||||
Key key,
|
Key key,
|
||||||
@required this.childEntities,
|
this.count,
|
||||||
@required this.count,
|
this.entities
|
||||||
|
}) {
|
||||||
|
childEntitiesAsBadges = [];
|
||||||
|
childEntitiesAsCards = {};
|
||||||
|
_composeEntities();
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildWidget(BuildContext context) {
|
||||||
|
return ViewWidget(
|
||||||
|
badges: childEntitiesAsBadges,
|
||||||
|
cards: childEntitiesAsCards,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _composeEntities() {
|
||||||
|
entities.forEach((Entity entity){
|
||||||
|
if (!entity.isGroup) {
|
||||||
|
if (entity.isBadge) {
|
||||||
|
childEntitiesAsBadges.add(entity);
|
||||||
|
} else {
|
||||||
|
String groupIdToAdd = "${entity.domain}.${entity.domain}$count";
|
||||||
|
if (childEntitiesAsCards[groupIdToAdd] == null) {
|
||||||
|
childEntitiesAsCards[groupIdToAdd] = CardSkeleton(
|
||||||
|
displayName: entity.domain,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
childEntitiesAsCards[groupIdToAdd].childEntities.add(entity);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
childEntitiesAsCards[entity.entityId] = CardSkeleton(
|
||||||
|
displayName: entity.displayName,
|
||||||
|
);
|
||||||
|
childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ViewWidget extends StatefulWidget {
|
||||||
|
final List<Entity> badges;
|
||||||
|
final Map<String, CardSkeleton> cards;
|
||||||
|
final String displayName;
|
||||||
|
|
||||||
|
const ViewWidget({
|
||||||
|
Key key,
|
||||||
|
this.badges,
|
||||||
|
this.cards,
|
||||||
this.displayName
|
this.displayName
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() {
|
State<StatefulWidget> createState() {
|
||||||
return ViewState();
|
return ViewWidgetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ViewState extends State<View> {
|
class ViewWidgetState extends State<ViewWidget> {
|
||||||
|
|
||||||
StreamSubscription _refreshDataSubscription;
|
StreamSubscription _refreshDataSubscription;
|
||||||
Completer _refreshCompleter;
|
Completer _refreshCompleter;
|
||||||
List<Entity> _childEntitiesAsBadges;
|
|
||||||
Map<String, Entity> _childEntitiesAsCards;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -34,28 +81,6 @@ class ViewState extends State<View> {
|
|||||||
_refreshCompleter.complete();
|
_refreshCompleter.complete();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
_childEntitiesAsCards = {};
|
|
||||||
_childEntitiesAsBadges = [];
|
|
||||||
_composeEntities();
|
|
||||||
}
|
|
||||||
|
|
||||||
void _composeEntities() {
|
|
||||||
widget.childEntities.forEach((Entity entity){
|
|
||||||
if (!entity.isGroup) {
|
|
||||||
if (entity.isBadge) {
|
|
||||||
_childEntitiesAsBadges.add(entity);
|
|
||||||
} else {
|
|
||||||
String groupIdToAdd = "${entity.domain}.${entity.domain}${widget.count}";
|
|
||||||
if (_childEntitiesAsCards[groupIdToAdd] == null) {
|
|
||||||
_childEntitiesAsCards[groupIdToAdd] = entity;
|
|
||||||
}
|
|
||||||
_childEntitiesAsCards[groupIdToAdd].childEntities.add(entity);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_childEntitiesAsCards[entity.entityId] = entity;
|
|
||||||
_childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -73,23 +98,22 @@ class ViewState extends State<View> {
|
|||||||
List<Widget> _buildChildren(BuildContext context) {
|
List<Widget> _buildChildren(BuildContext context) {
|
||||||
List<Widget> result = [];
|
List<Widget> result = [];
|
||||||
|
|
||||||
if (_childEntitiesAsBadges.isNotEmpty) {
|
if (widget.badges.isNotEmpty) {
|
||||||
result.insert(0,
|
result.insert(0,
|
||||||
Wrap(
|
Wrap(
|
||||||
alignment: WrapAlignment.center,
|
alignment: WrapAlignment.center,
|
||||||
spacing: 10.0,
|
spacing: 10.0,
|
||||||
runSpacing: 1.0,
|
runSpacing: 1.0,
|
||||||
children: _buildBadges(context),
|
children: _buildBadges(context, widget.badges),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_childEntitiesAsCards.forEach((String id, Entity groupEntity){
|
widget.cards.forEach((String id, CardSkeleton skeleton){
|
||||||
result.add(
|
result.add(
|
||||||
HACard(
|
HACard(
|
||||||
entities: groupEntity.childEntities,
|
entities: skeleton.childEntities,
|
||||||
friendlyName: groupEntity.displayName,
|
friendlyName: skeleton.displayName,
|
||||||
hidden: groupEntity.isHidden
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -97,9 +121,9 @@ class ViewState extends State<View> {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Widget> _buildBadges(BuildContext context) {
|
List<Widget> _buildBadges(BuildContext context, List<Entity> badges) {
|
||||||
List<Widget> result = [];
|
List<Widget> result = [];
|
||||||
_childEntitiesAsBadges.forEach((Entity entity) {
|
badges.forEach((Entity entity) {
|
||||||
result.add(entity.buildBadgeWidget(context));
|
result.add(entity.buildBadgeWidget(context));
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
@ -120,4 +144,15 @@ class ViewState extends State<View> {
|
|||||||
_refreshDataSubscription.cancel();
|
_refreshDataSubscription.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class CardSkeleton {
|
||||||
|
String displayName;
|
||||||
|
List<Entity> childEntities;
|
||||||
|
|
||||||
|
CardSkeleton({Key key, this.displayName, this.childEntities}) {
|
||||||
|
childEntities = [];
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user