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/entities/timer/widgets/timer_state.dart

65 lines
1.6 KiB
Dart
Raw Normal View History

2019-08-24 21:22:32 +03:00
part of '../../../main.dart';
2019-03-13 21:33:58 +02:00
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()
2020-04-25 20:38:21 +03:00
.difference(entity.lastUpdatedTimestamp)
2019-03-13 21:33:58 +02:00
.inSeconds;
remaining = Duration(seconds: entity.duration.inSeconds - passed);
2020-05-01 19:24:13 +03:00
} catch (e, stacktrace) {
Logger.e("Error calculating ${entity.entityId} remaining time: $e", stacktrace: stacktrace);
2019-03-13 21:33:58 +02:00
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();
}
}