Resolves #258 Timer support
This commit is contained in:
@ -10,7 +10,7 @@ class EntityColor {
|
||||
static const _stateColors = {
|
||||
EntityState.on: Colors.amber,
|
||||
"auto": Colors.amber,
|
||||
EntityState.idle: Colors.amber,
|
||||
EntityState.active: Colors.amber,
|
||||
EntityState.playing: Colors.amber,
|
||||
"above_horizon": Colors.amber,
|
||||
EntityState.home: Colors.amber,
|
||||
@ -19,6 +19,7 @@ class EntityColor {
|
||||
EntityState.closed: Color.fromRGBO(68, 115, 158, 1.0),
|
||||
"below_horizon": Color.fromRGBO(68, 115, 158, 1.0),
|
||||
"default": Color.fromRGBO(68, 115, 158, 1.0),
|
||||
EntityState.idle: Color.fromRGBO(68, 115, 158, 1.0),
|
||||
"heat": Colors.redAccent,
|
||||
"cool": Colors.lightBlue,
|
||||
EntityState.unavailable: Colors.black26,
|
||||
|
@ -6,14 +6,20 @@ class SimpleEntityState extends StatelessWidget {
|
||||
final TextAlign textAlign;
|
||||
final EdgeInsetsGeometry padding;
|
||||
final int maxLines;
|
||||
final String customValue;
|
||||
|
||||
const SimpleEntityState({Key key, this.maxLines: 10, this.expanded: true, this.textAlign: TextAlign.right, this.padding: const EdgeInsets.fromLTRB(0.0, 0.0, Sizes.rightWidgetPadding, 0.0)}) : super(key: key);
|
||||
const SimpleEntityState({Key key, this.maxLines: 10, this.expanded: true, this.textAlign: TextAlign.right, this.padding: const EdgeInsets.fromLTRB(0.0, 0.0, Sizes.rightWidgetPadding, 0.0), this.customValue}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final entityModel = EntityModel.of(context);
|
||||
String state = entityModel.entityWrapper.entity.displayState ?? "";
|
||||
state = state.replaceAll("\n", "").replaceAll("\t", " ").trim();
|
||||
String state;
|
||||
if (customValue == null) {
|
||||
state = entityModel.entityWrapper.entity.displayState ?? "";
|
||||
state = state.replaceAll("\n", "").replaceAll("\t", " ").trim();
|
||||
} else {
|
||||
state = customValue;
|
||||
}
|
||||
TextStyle textStyle = TextStyle(
|
||||
fontSize: Sizes.stateFontSize,
|
||||
);
|
||||
|
65
lib/entity_widgets/state/timer_state.dart
Normal file
65
lib/entity_widgets/state/timer_state.dart
Normal file
@ -0,0 +1,65 @@
|
||||
part of '../../main.dart';
|
||||
|
||||
class TimerState extends StatefulWidget {
|
||||
//final bool expanded;
|
||||
//final TextAlign textAlign;
|
||||
//final EdgeInsetsGeometry padding;
|
||||
//final int maxLines;
|
||||
|
||||
const TimerState({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_TimerStateState createState() => _TimerStateState();
|
||||
|
||||
}
|
||||
|
||||
class _TimerStateState extends State<TimerState> {
|
||||
|
||||
Timer timer;
|
||||
Duration remaining = Duration(seconds: 0);
|
||||
|
||||
void checkState(TimerEntity entity) {
|
||||
if (entity.state == EntityState.active) {
|
||||
//Logger.d("Timer is active");
|
||||
if (timer == null || !timer.isActive) {
|
||||
timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
||||
setState(() {
|
||||
try {
|
||||
int passed = DateTime
|
||||
.now()
|
||||
.difference(entity._lastUpdated)
|
||||
.inSeconds;
|
||||
remaining = Duration(seconds: entity.duration.inSeconds - passed);
|
||||
} catch (e) {
|
||||
Logger.e("Error calculating ${entity.entityId} remaining time: ${e.toString()}");
|
||||
remaining = Duration(seconds: 0);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
timer?.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
EntityModel model = EntityModel.of(context);
|
||||
TimerEntity entity = model.entityWrapper.entity;
|
||||
checkState(entity);
|
||||
if (entity.state != EntityState.active) {
|
||||
return SimpleEntityState();
|
||||
} else {
|
||||
return SimpleEntityState(
|
||||
customValue: "${remaining.toString().split('.')[0]}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user