View as widget refactoring
This commit is contained in:
@ -1,77 +1,30 @@
|
||||
part of 'main.dart';
|
||||
|
||||
class View {
|
||||
List<Entity> childEntitiesAsBadges;
|
||||
Map<String, CardSkeleton> childEntitiesAsCards;
|
||||
|
||||
int count;
|
||||
List<Entity> entities;
|
||||
class View extends StatefulWidget {
|
||||
final String displayName;
|
||||
final List<Entity> childEntities;
|
||||
final int count;
|
||||
|
||||
View({
|
||||
Key key,
|
||||
this.count,
|
||||
this.entities
|
||||
}) {
|
||||
childEntitiesAsBadges = [];
|
||||
childEntitiesAsCards = {};
|
||||
_composeEntities();
|
||||
}
|
||||
|
||||
Widget buildWidget(BuildContext context) {
|
||||
return ViewWidget(
|
||||
badges: childEntitiesAsBadges,
|
||||
cards: childEntitiesAsCards,
|
||||
);
|
||||
}
|
||||
|
||||
void _composeEntities() {
|
||||
entities.forEach((Entity entity){
|
||||
if (!entity.isGroup) {
|
||||
if (entity.isBadge) {
|
||||
childEntitiesAsBadges.add(entity);
|
||||
} else {
|
||||
String groupIdToAdd = "${entity.domain}.${entity.domain}$count";
|
||||
if (childEntitiesAsCards[groupIdToAdd] == null) {
|
||||
childEntitiesAsCards[groupIdToAdd] = CardSkeleton(
|
||||
displayName: entity.domain,
|
||||
);
|
||||
}
|
||||
childEntitiesAsCards[groupIdToAdd].childEntities.add(entity);
|
||||
}
|
||||
} else {
|
||||
childEntitiesAsCards[entity.entityId] = CardSkeleton(
|
||||
displayName: entity.displayName,
|
||||
);
|
||||
childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ViewWidget extends StatefulWidget {
|
||||
final List<Entity> badges;
|
||||
final Map<String, CardSkeleton> cards;
|
||||
final String displayName;
|
||||
|
||||
const ViewWidget({
|
||||
Key key,
|
||||
this.badges,
|
||||
this.cards,
|
||||
@required this.childEntities,
|
||||
@required this.count,
|
||||
this.displayName
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
return ViewWidgetState();
|
||||
return ViewState();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ViewWidgetState extends State<ViewWidget> {
|
||||
class ViewState extends State<View> {
|
||||
|
||||
StreamSubscription _refreshDataSubscription;
|
||||
Completer _refreshCompleter;
|
||||
List<Entity> _childEntitiesAsBadges;
|
||||
Map<String, Entity> _childEntitiesAsCards;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -81,6 +34,28 @@ class ViewWidgetState extends State<ViewWidget> {
|
||||
_refreshCompleter.complete();
|
||||
}
|
||||
});
|
||||
_childEntitiesAsCards = {};
|
||||
_childEntitiesAsBadges = [];
|
||||
_composeEntities();
|
||||
}
|
||||
|
||||
void _composeEntities() {
|
||||
widget.childEntities.forEach((Entity entity){
|
||||
if (!entity.isGroup) {
|
||||
if (entity.isBadge) {
|
||||
_childEntitiesAsBadges.add(entity);
|
||||
} else {
|
||||
String groupIdToAdd = "${entity.domain}.${entity.domain}${widget.count}";
|
||||
if (_childEntitiesAsCards[groupIdToAdd] == null) {
|
||||
_childEntitiesAsCards[groupIdToAdd] = entity;
|
||||
}
|
||||
_childEntitiesAsCards[groupIdToAdd].childEntities.add(entity);
|
||||
}
|
||||
} else {
|
||||
_childEntitiesAsCards[entity.entityId] = entity;
|
||||
_childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@ -98,22 +73,23 @@ class ViewWidgetState extends State<ViewWidget> {
|
||||
List<Widget> _buildChildren(BuildContext context) {
|
||||
List<Widget> result = [];
|
||||
|
||||
if (widget.badges.isNotEmpty) {
|
||||
if (_childEntitiesAsBadges.isNotEmpty) {
|
||||
result.insert(0,
|
||||
Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 10.0,
|
||||
runSpacing: 1.0,
|
||||
children: _buildBadges(context, widget.badges),
|
||||
children: _buildBadges(context),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
widget.cards.forEach((String id, CardSkeleton skeleton){
|
||||
_childEntitiesAsCards.forEach((String id, Entity groupEntity){
|
||||
result.add(
|
||||
HACard(
|
||||
entities: skeleton.childEntities,
|
||||
friendlyName: skeleton.displayName,
|
||||
entities: groupEntity.childEntities,
|
||||
friendlyName: groupEntity.displayName,
|
||||
hidden: groupEntity.isHidden
|
||||
)
|
||||
);
|
||||
});
|
||||
@ -121,9 +97,9 @@ class ViewWidgetState extends State<ViewWidget> {
|
||||
return result;
|
||||
}
|
||||
|
||||
List<Widget> _buildBadges(BuildContext context, List<Entity> badges) {
|
||||
List<Widget> _buildBadges(BuildContext context) {
|
||||
List<Widget> result = [];
|
||||
badges.forEach((Entity entity) {
|
||||
_childEntitiesAsBadges.forEach((Entity entity) {
|
||||
result.add(entity.buildBadgeWidget(context));
|
||||
});
|
||||
return result;
|
||||
@ -144,15 +120,4 @@ class ViewWidgetState extends State<ViewWidget> {
|
||||
_refreshDataSubscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class CardSkeleton {
|
||||
String displayName;
|
||||
List<Entity> childEntities;
|
||||
|
||||
CardSkeleton({Key key, this.displayName, this.childEntities}) {
|
||||
childEntities = [];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user