Resolves #26 Entity view page

This commit is contained in:
estevez 2018-09-29 16:19:01 +03:00
parent 067ccfde02
commit c0a9b89d40
8 changed files with 219 additions and 27 deletions

View File

@ -1,18 +1,24 @@
part of 'main.dart'; part of 'main.dart';
class Entity { class Entity {
static Map<String, Color> stateIconColors = { static const STATE_ICONS_COLORS = {
"on": Colors.amber, "on": Colors.amber,
"off": Color.fromRGBO(68, 115, 158, 1.0), "off": Color.fromRGBO(68, 115, 158, 1.0),
"unavailable": Colors.black12, "unavailable": Colors.black12,
"unknown": Colors.black12, "unknown": Colors.black12,
"playing": Colors.amber "playing": Colors.amber
}; };
static const RIGTH_WIDGET_PADDING = 14.0;
static const LEFT_WIDGET_PADDING = 8.0;
static const EXTENDED_WIDGET_HEIGHT = 50.0;
static const WIDGET_HEIGHT = 34.0;
Map _attributes; Map _attributes;
String _domain; String _domain;
String _entityId; String _entityId;
String _state; String _state;
String _entityPicture; String _entityPicture;
DateTime _lastUpdated;
String get displayName => _attributes["friendly_name"] ?? (_attributes["name"] ?? "_"); String get displayName => _attributes["friendly_name"] ?? (_attributes["name"] ?? "_");
String get domain => _domain; String get domain => _domain;
@ -34,6 +40,7 @@ class Entity {
String get entityPicture => _attributes["entity_picture"]; String get entityPicture => _attributes["entity_picture"];
String get unitOfMeasurement => _attributes["unit_of_measurement"] ?? ""; String get unitOfMeasurement => _attributes["unit_of_measurement"] ?? "";
List get childEntities => _attributes["entity_id"] ?? []; List get childEntities => _attributes["entity_id"] ?? [];
String get lastUpdated => _getLastUpdatedFormatted();
Entity(Map rawData) { Entity(Map rawData) {
update(rawData); update(rawData);
@ -48,25 +55,34 @@ class Entity {
_domain = rawData["entity_id"].split(".")[0]; _domain = rawData["entity_id"].split(".")[0];
_entityId = rawData["entity_id"]; _entityId = rawData["entity_id"];
_state = rawData["state"]; _state = rawData["state"];
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
}
String _getLastUpdatedFormatted() {
if (_lastUpdated == null) {
return "-";
} else {
return formatDate(_lastUpdated, [yy, '-', M, '-', d, ' ', HH, ':', nn, ':', ss]);
}
}
void openEntityPage() {
eventBus.fire(new ShowEntityPageEvent(this));
} }
Widget buildWidget() { Widget buildWidget() {
return SizedBox( return SizedBox(
height: 34.0, height: Entity.WIDGET_HEIGHT,
child: Row( child: Row(
children: <Widget>[ children: <Widget>[
Padding( GestureDetector(
padding: EdgeInsets.fromLTRB(8.0, 0.0, 12.0, 0.0), child: _buildIconWidget(),
child: MaterialDesignIcons.createIconWidgetFromEntityData(this, 28.0, Entity.stateIconColors[_state] ?? Colors.blueGrey), onTap: openEntityPage,
), ),
Expanded( Expanded(
child: Text( child: GestureDetector(
"${this.displayName}", child: _buildNameWidget(),
overflow: TextOverflow.fade, onTap: openEntityPage,
softWrap: false,
style: TextStyle(
fontSize: 16.0
),
), ),
), ),
_buildActionWidget() _buildActionWidget()
@ -75,18 +91,78 @@ class Entity {
); );
} }
Widget buildExtendedWidget() {
return Row(
children: <Widget>[
_buildIconWidget(),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: _buildNameWidget(),
),
_buildExtendedActionWidget()
],
),
_buildLastUpdatedWidget()
],
),
)
],
);
}
Widget _buildIconWidget() {
return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
child: MaterialDesignIcons.createIconWidgetFromEntityData(this, 28.0, Entity.STATE_ICONS_COLORS[_state] ?? Colors.blueGrey),
);
}
Widget _buildLastUpdatedWidget() {
return Text(
'${this.lastUpdated}',
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 12.0,
color: Colors.black26
),
);
}
Widget _buildNameWidget() {
return Text(
"${this.displayName}",
overflow: TextOverflow.fade,
softWrap: false,
style: TextStyle(
fontSize: 16.0
),
);
}
Widget _buildActionWidget() { Widget _buildActionWidget() {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 14.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGTH_WIDGET_PADDING, 0.0),
child: Text( child: GestureDetector(
"${_state}${this.unitOfMeasurement}", child: Text(
textAlign: TextAlign.right, "$_state${this.unitOfMeasurement}",
style: new TextStyle( textAlign: TextAlign.right,
fontSize: 16.0, style: new TextStyle(
) fontSize: 16.0,
)
),
onTap: openEntityPage,
) )
); );
} }
Widget _buildExtendedActionWidget() {
return _buildActionWidget();
}
} }
class SwitchEntity extends Entity { class SwitchEntity extends Entity {
@ -129,6 +205,30 @@ class InputEntity extends Entity {
InputEntity(Map rawData) : super(rawData); InputEntity(Map rawData) : super(rawData);
@override
Widget buildExtendedWidget() {
return Column(
children: <Widget>[
SizedBox(
height: Entity.EXTENDED_WIDGET_HEIGHT,
child: Row(
children: <Widget>[
_buildIconWidget(),
Expanded(
child: _buildNameWidget(),
),
_buildLastUpdatedWidget()
],
),
),
SizedBox(
height: Entity.EXTENDED_WIDGET_HEIGHT,
child: _buildExtendedActionWidget(),
)
],
);
}
@override @override
Widget _buildActionWidget() { Widget _buildActionWidget() {
if (this.isSlider) { if (this.isSlider) {
@ -146,7 +246,7 @@ class InputEntity extends Entity {
eventBus.fire(new StateChangedEvent(_entityId, (value.roundToDouble() / 10).toString(), true)); eventBus.fire(new StateChangedEvent(_entityId, (value.roundToDouble() / 10).toString(), true));
}, },
onChangeEnd: (value) { onChangeEnd: (value) {
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "${_state}"})); eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "$_state"}));
}, },
), ),
), ),
@ -164,9 +264,25 @@ class InputEntity extends Entity {
), ),
); );
} else { } else {
//TODO draw box instead of slider return super._buildActionWidget();
return Text("Not implemented");
} }
} }
@override
Widget _buildExtendedActionWidget() {
return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, Entity.RIGTH_WIDGET_PADDING, 0.0),
child: Container(
child: TextField(
controller: TextEditingController(
text: _state,
),
onChanged: (value) {
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "$value"}));
},
),
)
);
}
} }

48
lib/entity.page.dart Normal file
View File

@ -0,0 +1,48 @@
part of 'main.dart';
class EntityViewPage extends StatefulWidget {
EntityViewPage({Key key, this.entity}) : super(key: key);
Entity entity;
@override
_EntityViewPageState createState() => new _EntityViewPageState();
}
class _EntityViewPageState extends State<EntityViewPage> {
String _title;
Entity _entity;
@override
void initState() {
super.initState();
_entity = widget.entity;
_prepareData();
}
_prepareData() async {
_title = _entity.displayName;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
Navigator.pop(context);
}),
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text(_title),
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: ListView(
children: <Widget>[
_entity.buildExtendedWidget()
],
),
),
);
}
}

View File

@ -40,6 +40,7 @@ class EntityCollection {
return ButtonEntity(rawEntityData); return ButtonEntity(rawEntityData);
} }
case "input_text":
case "input_number": { case "input_number": {
return InputEntity(rawEntityData); return InputEntity(rawEntityData);
} }

View File

@ -10,11 +10,6 @@ class LogViewPage extends StatefulWidget {
} }
class _LogViewPageState extends State<LogViewPage> { class _LogViewPageState extends State<LogViewPage> {
String _hassioDomain = "";
String _hassioPort = "8123";
String _hassioPassword = "";
String _socketProtocol = "wss";
String _authType = "access_token";
String _logData; String _logData;
@override @override

View File

@ -10,10 +10,12 @@ import 'package:flutter/widgets.dart';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:date_format/date_format.dart';
part 'settings.page.dart'; part 'settings.page.dart';
part 'home_assistant.class.dart'; part 'home_assistant.class.dart';
part 'log.page.dart'; part 'log.page.dart';
part 'entity.page.dart';
part 'utils.class.dart'; part 'utils.class.dart';
part 'mdi.class.dart'; part 'mdi.class.dart';
part 'entity.class.dart'; part 'entity.class.dart';
@ -86,6 +88,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
StreamSubscription _stateSubscription; StreamSubscription _stateSubscription;
StreamSubscription _settingsSubscription; StreamSubscription _settingsSubscription;
StreamSubscription _serviceCallSubscription; StreamSubscription _serviceCallSubscription;
StreamSubscription _showEntityPageSubscription;
bool _isLoading = true; bool _isLoading = true;
Map<String, Color> _badgeColors = { Map<String, Color> _badgeColors = {
@ -152,6 +155,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_serviceCallSubscription = eventBus.on<ServiceCallEvent>().listen((event) { _serviceCallSubscription = eventBus.on<ServiceCallEvent>().listen((event) {
_callService(event.domain, event.service, event.entityId, event.additionalParams); _callService(event.domain, event.service, event.entityId, event.additionalParams);
}); });
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
_showEntityPageSubscription = eventBus.on<ShowEntityPageEvent>().listen((event) {
_showEntityPage(event.entity);
});
} }
_refreshData() async { _refreshData() async {
@ -192,6 +200,15 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
}).catchError((e) => _setErrorState(e)); }).catchError((e) => _setErrorState(e));
} }
void _showEntityPage(Entity entity) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EntityViewPage(entity: entity),
)
);
}
List<Widget> _buildViews() { List<Widget> _buildViews() {
List<Widget> result = []; List<Widget> result = [];
if ((_entities != null) && (!_homeAssistant.uiBuilder.isEmpty)) { if ((_entities != null) && (!_homeAssistant.uiBuilder.isEmpty)) {
@ -602,6 +619,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
if (_stateSubscription != null) _stateSubscription.cancel(); if (_stateSubscription != null) _stateSubscription.cancel();
if (_settingsSubscription != null) _settingsSubscription.cancel(); if (_settingsSubscription != null) _settingsSubscription.cancel();
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel(); if (_serviceCallSubscription != null) _serviceCallSubscription.cancel();
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
_homeAssistant.closeConnection(); _homeAssistant.closeConnection();
super.dispose(); super.dispose();
} }

View File

@ -64,3 +64,9 @@ class ServiceCallEvent {
ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams); ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams);
} }
class ShowEntityPageEvent {
Entity entity;
ShowEntityPageEvent(this.entity);
}

View File

@ -87,6 +87,13 @@ packages:
url: "https://github.com/MarkOSullivan94/dart_config.git" url: "https://github.com/MarkOSullivan94/dart_config.git"
source: git source: git
version: "0.5.0" version: "0.5.0"
date_format:
dependency: "direct main"
description:
name: date_format
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
event_bus: event_bus:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -16,6 +16,7 @@ dependencies:
flutter_launcher_icons: ^0.6.1 flutter_launcher_icons: ^0.6.1
cached_network_image: ^0.4.1 cached_network_image: ^0.4.1
url_launcher: ^3.0.3 url_launcher: ^3.0.3
date_format: ^1.0.5
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.