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/entity_widgets/controls/media_player_widget.dart
2018-11-12 20:47:49 +02:00

180 lines
5.4 KiB
Dart

part of '../../main.dart';
class MediaPlayerWidget extends StatelessWidget {
void _setPower(MediaPlayerEntity entity) {
TheLogger.debug('WAT?');
}
@override
Widget build(BuildContext context) {
final EntityModel entityModel = EntityModel.of(context);
final MediaPlayerEntity entity = entityModel.entity;
return Column(
children: <Widget>[
Stack(
alignment: AlignmentDirectional.topEnd,
children: <Widget>[
_buildImage(entity),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
color: Colors.black45,
child: _buildState(entity),
),
),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: MediaPlayerProgressWidget()
)
],
),
_buildControls(entity)
]
);
}
Widget _buildControls(MediaPlayerEntity entity) {
List<Widget> result = [];
if (entity.supportTurnOn) {
result.add(
IconButton(
icon: Icon(Icons.power_settings_new),
onPressed: () => _setPower(entity),
iconSize: Sizes.iconSize,
)
);
}
return Row(
children: result,
mainAxisAlignment: MainAxisAlignment.center,
);
}
Widget _buildState(MediaPlayerEntity entity) {
TextStyle style = TextStyle(
fontSize: 14.0,
color: Colors.white,
fontWeight: FontWeight.normal,
height: 1.2
);
List<Widget> states = [];
states.add(Text("${entity.displayName}", style: style));
String state = entity.state;
if (state == null || state == "off" || state == "unavailable" || state == "idle") {
states.add(Text("${entity.state}", style: style.apply(fontSizeDelta: 4.0),));
}
if (entity.attributes['media_title'] != null) {
states.add(Text("${entity.attributes['media_title']}", style: style.apply(fontSizeDelta: 6.0, fontWeightDelta: 50),));
}
if (entity.attributes['media_content_type'] == "music") {
states.add(Text("${entity.attributes['media_artist'] ?? entity.attributes['app_name']}", style: style.apply(fontSizeDelta: 4.0),));
} else if (entity.attributes['app_name'] != null) {
states.add(Text("${entity.attributes['app_name']}", style: style.apply(fontSizeDelta: 4.0),));
}
return Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: states,
),
);
}
Widget _buildImage(MediaPlayerEntity entity) {
String state = entity.state;
if (homeAssistantWebHost != null && entity.entityPicture != null && state != "off" && state != "unavailable" && state != "idle") {
return Container(
color: Colors.black,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image(
image: CachedNetworkImageProvider("$homeAssistantWebHost${entity.entityPicture}"),
height: 240.0,
width: 320.0,
fit: BoxFit.contain,
)
],
),
);
} else {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:movie"),
size: 150.0,
color: EntityColors.stateColor("$state"),
)
],
);
/*return Container(
color: Colors.blue,
height: 80.0,
);*/
}
}
}
class MediaPlayerProgressWidget extends StatefulWidget {
@override
_MediaPlayerProgressWidgetState createState() => _MediaPlayerProgressWidgetState();
}
class _MediaPlayerProgressWidgetState extends State<MediaPlayerProgressWidget> {
Timer _timer;
@override
Widget build(BuildContext context) {
final EntityModel entityModel = EntityModel.of(context);
final MediaPlayerEntity entity = entityModel.entity;
double progress;
try {
DateTime lastUpdated = DateTime.parse(
entity.attributes["media_position_updated_at"]).toLocal();
Duration duration = Duration(seconds: entity._getIntAttributeValue("media_duration") ?? 1);
Duration position = Duration(seconds: entity._getIntAttributeValue("media_position") ?? 0);
int currentPosition = position.inSeconds;
if (entity.state == "playing") {
_timer?.cancel();
_timer = Timer(Duration(seconds: 1), () {
setState(() {
});
});
int differenceInSeconds = DateTime
.now()
.difference(lastUpdated)
.inSeconds;
currentPosition = currentPosition + differenceInSeconds;
} else {
_timer?.cancel();
}
progress = currentPosition / duration.inSeconds;
return LinearProgressIndicator(
value: progress,
backgroundColor: Colors.black45,
valueColor: AlwaysStoppedAnimation<Color>(EntityColors.stateColor("on")),
);
} catch (e) {
_timer?.cancel();
progress = 0.0;
}
return LinearProgressIndicator(
value: progress,
backgroundColor: Colors.black45,
);
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
}