WIP #530 Badges refactoring
This commit is contained in:
parent
4fbf58e707
commit
13508ee92f
29
lib/cards/badges.dart
Normal file
29
lib/cards/badges.dart
Normal file
@ -0,0 +1,29 @@
|
||||
part of '../main.dart';
|
||||
|
||||
class Badges extends StatelessWidget {
|
||||
final BadgesData badges;
|
||||
|
||||
const Badges({Key key, this.badges}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<EntityWrapper> entitiesToShow = badges.getEntitiesToShow();
|
||||
|
||||
if (entitiesToShow.isNotEmpty) {
|
||||
return Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 10.0,
|
||||
runSpacing: 1.0,
|
||||
children: entitiesToShow.map((entity) =>
|
||||
EntityModel(
|
||||
entityWrapper: entity,
|
||||
child: BadgeWidget(),
|
||||
handleTap: true,
|
||||
)).toList(),
|
||||
);
|
||||
}
|
||||
return Container(height: 0.0, width: 0.0,);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -75,6 +75,9 @@ class CardData {
|
||||
case CardType.MEDIA_CONTROL:
|
||||
return MediaControlCardData(rawData);
|
||||
break;
|
||||
case CardType.BADGES:
|
||||
return BadgesData(rawData);
|
||||
break;
|
||||
default:
|
||||
return CardData(null);
|
||||
}
|
||||
@ -172,6 +175,64 @@ class CardData {
|
||||
|
||||
}
|
||||
|
||||
class BadgesData extends CardData {
|
||||
|
||||
String title;
|
||||
String icon;
|
||||
bool showHeaderToggle;
|
||||
|
||||
@override
|
||||
Widget buildCardWidget() {
|
||||
return Badges(badges: this);
|
||||
}
|
||||
|
||||
BadgesData(rawData) : super(rawData) {
|
||||
if (rawData['badges'] is List) {
|
||||
rawData['badges'].forEach((dynamic rawBadge) {
|
||||
if (rawBadge is String && HomeAssistant().entities.isExist(rawBadge)) {
|
||||
entities.add(EntityWrapper(entity: HomeAssistant().entities.get(rawBadge)));
|
||||
} else if (rawBadge is Map && rawBadge.containsKey('entity') && HomeAssistant().entities.isExist(rawBadge['entity'])) {
|
||||
entities.add(
|
||||
EntityWrapper(
|
||||
entity: HomeAssistant().entities.get(rawBadge['entity']),
|
||||
overrideName: rawBadge["name"],
|
||||
overrideIcon: rawBadge["icon"],
|
||||
)
|
||||
);
|
||||
} else if (rawBadge is Map && rawBadge.containsKey('entities')) {
|
||||
_parseEntities(rawBadge);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _parseEntities(rawData) {
|
||||
var rawEntities = rawData['entities'] ?? [];
|
||||
rawEntities.forEach((rawEntity) {
|
||||
if (rawEntity is String) {
|
||||
if (HomeAssistant().entities.isExist(rawEntity)) {
|
||||
entities.add(EntityWrapper(
|
||||
entity: HomeAssistant().entities.get(rawEntity),
|
||||
stateFilter: rawData['state_filter'] ?? [],
|
||||
));
|
||||
}
|
||||
} else if (HomeAssistant().entities.isExist('${rawEntity['entity']}')) {
|
||||
Entity e = HomeAssistant().entities.get(rawEntity["entity"]);
|
||||
entities.add(
|
||||
EntityWrapper(
|
||||
entity: e,
|
||||
overrideName: rawEntity["name"],
|
||||
overrideIcon: rawEntity["icon"],
|
||||
stateFilter: rawEntity['state_filter'] ?? (rawData['state_filter'] ?? []),
|
||||
uiAction: EntityUIAction(rawEntityData: rawEntity)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class EntitiesCardData extends CardData {
|
||||
|
||||
String title;
|
||||
|
@ -63,6 +63,7 @@ class CardType {
|
||||
static const UNKNOWN = "unknown";
|
||||
static const HISTORY_GRAPH = "history-graph";
|
||||
static const PICTURE_GLANCE = "picture-glance";
|
||||
static const BADGES = "badges";
|
||||
}
|
||||
|
||||
class Sizes {
|
||||
|
@ -4,7 +4,6 @@ class BadgeWidget extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final entityModel = EntityModel.of(context);
|
||||
double iconSize = 26.0;
|
||||
Widget badgeIcon;
|
||||
String onBadgeTextValue;
|
||||
Color iconColor = HAClientTheme().getBadgeColor(entityModel.entityWrapper.entity.domain);
|
||||
@ -14,11 +13,9 @@ class BadgeWidget extends StatelessWidget {
|
||||
badgeIcon = entityModel.entityWrapper.entity.state == "below_horizon"
|
||||
? Icon(
|
||||
MaterialDesignIcons.getIconDataFromIconCode(0xf0dc),
|
||||
size: iconSize,
|
||||
)
|
||||
: Icon(
|
||||
MaterialDesignIcons.getIconDataFromIconCode(0xf5a8),
|
||||
size: iconSize,
|
||||
);
|
||||
break;
|
||||
}
|
||||
@ -28,7 +25,6 @@ class BadgeWidget extends StatelessWidget {
|
||||
{
|
||||
badgeIcon = EntityIcon(
|
||||
padding: EdgeInsets.all(0.0),
|
||||
size: iconSize,
|
||||
color: Theme.of(context).textTheme.body1.color
|
||||
);
|
||||
break;
|
||||
@ -38,7 +34,6 @@ class BadgeWidget extends StatelessWidget {
|
||||
{
|
||||
badgeIcon = EntityIcon(
|
||||
padding: EdgeInsets.all(0.0),
|
||||
size: iconSize,
|
||||
color: Theme.of(context).textTheme.body1.color
|
||||
);
|
||||
onBadgeTextValue = entityModel.entityWrapper.entity.displayState;
|
||||
@ -46,27 +41,13 @@ class BadgeWidget extends StatelessWidget {
|
||||
}
|
||||
default:
|
||||
{
|
||||
double stateFontSize;
|
||||
if (entityModel.entityWrapper.entity.displayState.length <= 3) {
|
||||
stateFontSize = 18.0;
|
||||
} else if (entityModel.entityWrapper.entity.displayState.length <= 4) {
|
||||
stateFontSize = 15.0;
|
||||
} else if (entityModel.entityWrapper.entity.displayState.length <= 6) {
|
||||
stateFontSize = 10.0;
|
||||
} else if (entityModel.entityWrapper.entity.displayState.length <= 10) {
|
||||
stateFontSize = 8.0;
|
||||
}
|
||||
onBadgeTextValue = entityModel.entityWrapper.unitOfMeasurement;
|
||||
badgeIcon = Center(
|
||||
child: Text(
|
||||
"${entityModel.entityWrapper.entity.displayState}",
|
||||
overflow: TextOverflow.fade,
|
||||
softWrap: false,
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.body1.copyWith(
|
||||
fontSize: stateFontSize
|
||||
)
|
||||
),
|
||||
badgeIcon = Text(
|
||||
"${entityModel.entityWrapper.entity.displayState}",
|
||||
overflow: TextOverflow.fade,
|
||||
softWrap: false,
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.body1
|
||||
);
|
||||
break;
|
||||
}
|
||||
@ -85,8 +66,6 @@ class BadgeWidget extends StatelessWidget {
|
||||
softWrap: false,
|
||||
overflow: TextOverflow.fade),
|
||||
decoration: new BoxDecoration(
|
||||
// Circle shape
|
||||
//shape: BoxShape.circle,
|
||||
color: iconColor,
|
||||
borderRadius: BorderRadius.circular(9.0),
|
||||
));
|
||||
@ -95,9 +74,9 @@ class BadgeWidget extends StatelessWidget {
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
|
||||
width: 50.0,
|
||||
height: 50.0,
|
||||
//margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
|
||||
width: 45,
|
||||
height: 45,
|
||||
decoration: new BoxDecoration(
|
||||
// Circle shape
|
||||
shape: BoxShape.circle,
|
||||
@ -112,20 +91,24 @@ class BadgeWidget extends StatelessWidget {
|
||||
overflow: Overflow.visible,
|
||||
children: <Widget>[
|
||||
Positioned(
|
||||
width: 46.0,
|
||||
height: 46.0,
|
||||
width: 45,
|
||||
height: 45,
|
||||
top: 0.0,
|
||||
left: 0.0,
|
||||
child: badgeIcon,
|
||||
child: FittedBox(
|
||||
fit: BoxFit.contain,
|
||||
alignment: Alignment.center,
|
||||
child: badgeIcon,
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
//width: 50.0,
|
||||
bottom: -9.0,
|
||||
left: -10.0,
|
||||
right: -10.0,
|
||||
child: Center(
|
||||
child: onBadgeText,
|
||||
))
|
||||
bottom: -9.0,
|
||||
left: -10.0,
|
||||
right: -10.0,
|
||||
child: Center(
|
||||
child: onBadgeText,
|
||||
)
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
@ -142,7 +125,9 @@ class BadgeWidget extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: () =>
|
||||
eventBus.fire(new ShowEntityPageEvent(entityId: entityModel.entityWrapper.entity.entityId)));
|
||||
onTap: () => entityModel.entityWrapper.handleTap(),
|
||||
onDoubleTap: () => entityModel.entityWrapper.handleDoubleTap(),
|
||||
onLongPress: () => entityModel.entityWrapper.handleHold(),
|
||||
);
|
||||
}
|
||||
}
|
@ -210,14 +210,6 @@ class Entity {
|
||||
);
|
||||
}
|
||||
|
||||
Widget buildBadgeWidget(BuildContext context) {
|
||||
return EntityModel(
|
||||
entityWrapper: EntityWrapper(entity: this),
|
||||
child: BadgeWidget(),
|
||||
handleTap: true,
|
||||
);
|
||||
}
|
||||
|
||||
String getAttribute(String attributeName) {
|
||||
if (attributes != null) {
|
||||
return attributes["$attributeName"].toString();
|
||||
|
@ -153,6 +153,7 @@ part 'entities/media_player/widgets/media_player_progress_bar.widget.dart';
|
||||
part 'pages/whats_new.page.dart';
|
||||
part 'pages/fullscreen.page.dart';
|
||||
part 'popups.dart';
|
||||
part 'cards/badges.dart';
|
||||
|
||||
EventBus eventBus = new EventBus();
|
||||
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
|
||||
|
@ -2,7 +2,6 @@ part of 'main.dart';
|
||||
|
||||
class HAView {
|
||||
List<CardData> cards = [];
|
||||
List<Entity> badges = [];
|
||||
Entity linkedEntity;
|
||||
String name;
|
||||
String id;
|
||||
@ -16,28 +15,16 @@ class HAView {
|
||||
iconName = rawData['icon'];
|
||||
isPanel = rawData['panel'] ?? false;
|
||||
|
||||
if (rawData['badges'] != null && rawData['badges'] is List) {
|
||||
rawData['badges'].forEach((entity) {
|
||||
if (entity is String) {
|
||||
if (HomeAssistant().entities.isExist(entity)) {
|
||||
Entity e = HomeAssistant().entities.get(entity);
|
||||
badges.add(e);
|
||||
}
|
||||
} else {
|
||||
String eId = '${entity['entity']}';
|
||||
if (HomeAssistant().entities.isExist(eId)) {
|
||||
Entity e = HomeAssistant().entities.get(eId);
|
||||
badges.add(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (rawData['badges'] != null && !isPanel) {
|
||||
cards.add(CardData.parse({
|
||||
'type': CardType.BADGES,
|
||||
'badges': rawData['badges']
|
||||
}));
|
||||
}
|
||||
|
||||
(rawData["cards"] ?? []).forEach((rawCardData) {
|
||||
(rawData['cards'] ?? []).forEach((rawCardData) {
|
||||
cards.add(CardData.parse(rawCardData));
|
||||
});
|
||||
|
||||
//cards.addAll(_createLovelaceCards(rawData["cards"] ?? [], 1));
|
||||
}
|
||||
|
||||
Widget buildTab() {
|
||||
|
@ -44,13 +44,10 @@ class ViewWidget extends StatelessWidget {
|
||||
} else {
|
||||
cardsContainer = Container();
|
||||
}
|
||||
return ListView(
|
||||
shrinkWrap: true,
|
||||
return SingleChildScrollView(
|
||||
scrollDirection: Axis.vertical,
|
||||
padding: EdgeInsets.all(0),
|
||||
children: <Widget>[
|
||||
_buildBadges(context),
|
||||
cardsContainer
|
||||
]
|
||||
child: cardsContainer
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -63,18 +60,4 @@ class ViewWidget extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildBadges(BuildContext context) {
|
||||
if (this.view.badges.isNotEmpty) {
|
||||
return Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 10.0,
|
||||
runSpacing: 1.0,
|
||||
children: this.view.badges.map((badge) =>
|
||||
badge.buildBadgeWidget(context)).toList(),
|
||||
);
|
||||
} else {
|
||||
return Container(width: 0, height: 0,);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user