part of 'main.dart'; class HomeAssistantUI { List views; HomeAssistantUI() { views = []; } Widget build(BuildContext context) { return TabBarView( children: _buildViews(context) ); } List _buildViews(BuildContext context) { List result = []; views.forEach((view) { result.add( view.build(context) ); }); return result; } } class HAView { List cards = []; List badges = []; Entity linkedEntity; String name; String id; String iconName; int count; HAView({ this.name, this.id, this.count, this.iconName, List childEntities }) { _fillView(childEntities ?? []); } void _fillView(List childEntities) { List autoGeneratedCards = []; childEntities.forEach((entity) { if (entity.isBadge) { badges.add(entity); TheLogger.debug("----Badge: ${entity.entityId}"); } else { if (!entity.isGroup) { String groupIdToAdd = "${entity.domain}.${entity.domain}$count"; if (autoGeneratedCards.every((HACard card) => card.id != groupIdToAdd )) { HACard card = HACard( id: groupIdToAdd, name: entity.domain ); TheLogger.debug("----Creating card: $groupIdToAdd"); card.entities.add(entity); autoGeneratedCards.add(card); } else { autoGeneratedCards.firstWhere((card) => card.id == groupIdToAdd).entities.add(entity); } } else { TheLogger.debug("----Card: ${entity.entityId}"); HACard card = HACard( name: entity.displayName, id: entity.entityId, linkedEntity: entity ); card.entities.addAll(entity.childEntities); cards.add(card); } } }); cards.addAll(autoGeneratedCards); } Widget build(BuildContext context) { return NewViewWidget( view: this, ); } } class NewViewWidget extends StatefulWidget { final HAView view; const NewViewWidget({ Key key, this.view }) : super(key: key); @override State createState() { return NewViewWidgetState(); } } class NewViewWidgetState extends State { StreamSubscription _refreshDataSubscription; Completer _refreshCompleter; @override void initState() { super.initState(); _refreshDataSubscription = eventBus.on().listen((event) { if ((_refreshCompleter != null) && (!_refreshCompleter.isCompleted)) { _refreshCompleter.complete(); } }); } @override Widget build(BuildContext context) { return RefreshIndicator( color: Colors.amber, child: ListView( physics: const AlwaysScrollableScrollPhysics(), children: _buildChildren(context), ), onRefresh: () => _refreshData(), ); } List _buildChildren(BuildContext context) { List result = []; if (widget.view.badges.isNotEmpty) { result.insert(0, Wrap( alignment: WrapAlignment.center, spacing: 10.0, runSpacing: 1.0, children: _buildBadges(context), ) ); } widget.view.cards.forEach((HACard card){ result.add( card.build(context) ); }); return result; } List _buildBadges(BuildContext context) { List result = []; widget.view.badges.forEach((Entity entity) { if (!entity.isHidden) { result.add(entity.buildBadgeWidget(context)); } }); return result; } Future _refreshData() { if ((_refreshCompleter != null) && (!_refreshCompleter.isCompleted)) { TheLogger.debug("Previous data refresh is still in progress"); } else { _refreshCompleter = Completer(); eventBus.fire(RefreshDataEvent()); } return _refreshCompleter.future; } @override void dispose() { _refreshDataSubscription.cancel(); super.dispose(); } } class HACard { List entities = []; Entity linkedEntity; String name; String id; HACard({ this.name, this.id, this.linkedEntity }); Widget build(BuildContext context) { return NewCardWidget( card: this, ); } } class NewCardWidget extends StatelessWidget { final HACard card; const NewCardWidget({ Key key, this.card }) : super(key: key); @override Widget build(BuildContext context) { if ((card.linkedEntity!= null) && (card.linkedEntity.isHidden)) { return Container(width: 0.0, height: 0.0,); } List body = []; body.add(_buildCardHeader()); body.addAll(_buildCardBody(context)); return Card( child: new Column(mainAxisSize: MainAxisSize.min, children: body) ); } Widget _buildCardHeader() { var result; if ((card.name != null) && (card.name.trim().length > 0)) { result = new ListTile( title: Text("${card.name}", textAlign: TextAlign.left, overflow: TextOverflow.ellipsis, style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)), ); } else { result = new Container(width: 0.0, height: 0.0); } return result; } List _buildCardBody(BuildContext context) { List result = []; card.entities.forEach((Entity entity) { if (!entity.isHidden) { result.add( Padding( padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), child: entity.buildDefaultWidget(context), )); } }); return result; } }