Last updated time in duration

This commit is contained in:
estevez
2018-09-30 10:15:32 +03:00
parent 9e83a3e447
commit 7dd8f65af7
2 changed files with 38 additions and 9 deletions

View File

@ -15,7 +15,7 @@ class Entity {
static const ICON_SIZE = 28.0; static const ICON_SIZE = 28.0;
static const STATE_FONT_SIZE = 16.0; static const STATE_FONT_SIZE = 16.0;
static const NAME_FONT_SIZE = 16.0; static const NAME_FONT_SIZE = 16.0;
static const SMALL_FONT_SIZE = 12.0; static const SMALL_FONT_SIZE = 14.0;
Map _attributes; Map _attributes;
String _domain; String _domain;
@ -63,7 +63,28 @@ class Entity {
if (_lastUpdated == null) { if (_lastUpdated == null) {
return "-"; return "-";
} else { } else {
return formatDate(_lastUpdated, [yy, '-', M, '-', d, ' ', HH, ':', nn, ':', ss]); DateTime now = DateTime.now();
Duration d = now.difference(_lastUpdated);
String text;
int v;
if (d.inDays == 0) {
if (d.inHours == 0) {
if (d.inMinutes == 0) {
text = "seconds ago";
v = d.inSeconds;
} else {
text = "minutes ago";
v = d.inMinutes;
}
} else {
text = "hours ago";
v = d.inHours;
}
} else {
text = "days ago";
v = d.inDays;
}
return "$v $text";
} }
} }
@ -92,6 +113,10 @@ class Entity {
); );
} }
Widget buildAdditionalWidget() {
return _buildLastUpdatedWidget();
}
Widget _buildIconWidget() { Widget _buildIconWidget() {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0), padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
@ -100,12 +125,15 @@ class Entity {
} }
Widget _buildLastUpdatedWidget() { Widget _buildLastUpdatedWidget() {
return Text( return Padding(
'${this.lastUpdated}', padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, Entity.SMALL_FONT_SIZE, 0.0, 0.0),
textAlign: TextAlign.left, child: Text(
style: TextStyle( '${this.lastUpdated}',
fontSize: Entity.SMALL_FONT_SIZE, textAlign: TextAlign.left,
color: Colors.black26 style: TextStyle(
fontSize: Entity.SMALL_FONT_SIZE,
color: Colors.black26
),
), ),
); );
} }

View File

@ -46,7 +46,8 @@ class _EntityViewPageState extends State<EntityViewPage> {
padding: EdgeInsets.all(10.0), padding: EdgeInsets.all(10.0),
child: ListView( child: ListView(
children: <Widget>[ children: <Widget>[
_entity.buildWidget(false) _entity.buildWidget(false),
_entity.buildAdditionalWidget()
], ],
), ),
), ),