This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/ui.dart

254 lines
5.6 KiB
Dart
Raw Normal View History

2018-10-25 00:05:29 +03:00
part of 'main.dart';
2018-10-27 00:54:05 +03:00
class HomeAssistantUI {
List<HAView> views;
2018-10-25 00:05:29 +03:00
2018-10-27 00:54:05 +03:00
HomeAssistantUI() {
2018-10-25 00:05:29 +03:00
views = [];
}
Widget build(BuildContext context) {
return TabBarView(
children: _buildViews(context)
);
}
List<Widget> _buildViews(BuildContext context) {
List<Widget> result = [];
views.forEach((view) {
result.add(
view.build(context)
);
});
return result;
}
}
2018-10-27 00:54:05 +03:00
class HAView {
List<HACard> cards = [];
2018-10-25 00:05:29 +03:00
List<Entity> badges = [];
Entity linkedEntity;
String name;
String id;
2018-10-27 00:54:05 +03:00
String iconName;
2018-10-25 00:05:29 +03:00
int count;
2018-10-27 00:54:05 +03:00
HAView({
2018-10-25 00:05:29 +03:00
this.name,
this.id,
2018-10-27 00:54:05 +03:00
this.count,
this.iconName,
List<Entity> childEntities
}) {
_fillView(childEntities ?? []);
}
void _fillView(List<Entity> childEntities) {
List<HACard> autoGeneratedCards = [];
childEntities.forEach((entity) {
if (entity.isBadge) {
badges.add(entity);
2018-10-27 01:24:23 +03:00
TheLogger.debug("----Badge: ${entity.entityId}");
2018-10-27 00:54:05 +03:00
} 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
);
2018-10-27 01:24:23 +03:00
TheLogger.debug("----Creating card: $groupIdToAdd");
2018-10-27 00:54:05 +03:00
card.entities.add(entity);
autoGeneratedCards.add(card);
} else {
autoGeneratedCards.firstWhere((card) => card.id == groupIdToAdd).entities.add(entity);
}
} else {
2018-10-27 01:24:23 +03:00
TheLogger.debug("----Card: ${entity.entityId}");
2018-10-27 00:54:05 +03:00
HACard card = HACard(
name: entity.displayName,
id: entity.entityId,
linkedEntity: entity
);
card.entities.addAll(entity.childEntities);
cards.add(card);
}
}
});
cards.addAll(autoGeneratedCards);
}
2018-10-25 00:05:29 +03:00
Widget build(BuildContext context) {
return NewViewWidget(
view: this,
);
}
}
class NewViewWidget extends StatefulWidget {
2018-10-27 00:54:05 +03:00
final HAView view;
2018-10-25 00:05:29 +03:00
const NewViewWidget({
Key key,
this.view
}) : super(key: key);
@override
State<StatefulWidget> createState() {
return NewViewWidgetState();
}
}
class NewViewWidgetState extends State<NewViewWidget> {
StreamSubscription _refreshDataSubscription;
Completer _refreshCompleter;
@override
void initState() {
super.initState();
_refreshDataSubscription = eventBus.on<RefreshDataFinishedEvent>().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<Widget> _buildChildren(BuildContext context) {
List<Widget> result = [];
if (widget.view.badges.isNotEmpty) {
result.insert(0,
Wrap(
alignment: WrapAlignment.center,
spacing: 10.0,
runSpacing: 1.0,
children: _buildBadges(context),
)
);
}
2018-10-27 00:54:05 +03:00
widget.view.cards.forEach((HACard card){
2018-10-25 00:05:29 +03:00
result.add(
card.build(context)
);
});
return result;
}
List<Widget> _buildBadges(BuildContext context) {
List<Widget> result = [];
widget.view.badges.forEach((Entity entity) {
2018-10-25 00:13:50 +03:00
if (!entity.isHidden) {
result.add(entity.buildBadgeWidget(context));
}
2018-10-25 00:05:29 +03:00
});
return result;
}
Future _refreshData() {
if ((_refreshCompleter != null) && (!_refreshCompleter.isCompleted)) {
2018-10-27 01:24:23 +03:00
TheLogger.debug("Previous data refresh is still in progress");
2018-10-25 00:05:29 +03:00
} else {
_refreshCompleter = Completer();
eventBus.fire(RefreshDataEvent());
}
return _refreshCompleter.future;
}
@override
void dispose() {
_refreshDataSubscription.cancel();
super.dispose();
}
}
2018-10-27 00:54:05 +03:00
class HACard {
2018-10-25 00:05:29 +03:00
List<Entity> entities = [];
Entity linkedEntity;
String name;
String id;
2018-10-27 00:54:05 +03:00
HACard({
2018-10-25 00:05:29 +03:00
this.name,
this.id,
this.linkedEntity
});
Widget build(BuildContext context) {
return NewCardWidget(
card: this,
);
}
}
class NewCardWidget extends StatelessWidget {
2018-10-27 00:54:05 +03:00
final HACard card;
2018-10-25 00:05:29 +03:00
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<Widget> 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<Widget> _buildCardBody(BuildContext context) {
List<Widget> result = [];
card.entities.forEach((Entity entity) {
2018-10-25 00:13:50 +03:00
if (!entity.isHidden) {
result.add(
Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: entity.buildDefaultWidget(context),
));
}
2018-10-25 00:05:29 +03:00
});
return result;
}
}