Skip to content

Commit

Permalink
Simplify a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsRefsgaard committed Aug 24, 2023
1 parent e25e9c8 commit 9057857
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 139 deletions.
247 changes: 109 additions & 138 deletions lib/ui/pages/devices_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ class DevicesPageState extends State<DevicesPage> {
super.dispose();
}

@override
void initState() {
super.initState();
for (var element in physicalDevices) {
element.deviceEvents.listen((event) {
setState(() {});
});
}
}

@override
Widget build(BuildContext context) {
RPLocalizations locale = RPLocalizations.of(context)!;
Expand Down Expand Up @@ -80,10 +70,11 @@ class DevicesPageState extends State<DevicesPage> {
flex: 4,
child: CustomScrollView(
slivers: [
...smartphoneDeviceListWidget(),
...smartphoneDeviceListWidget(locale),
if (physicalDevices.isNotEmpty)
...physicalDevicesListWidget(),
if (onlineServices.isNotEmpty) ...onlineServicesListWidget(),
...physicalDevicesListWidget(locale),
if (onlineServices.isNotEmpty)
...onlineServicesListWidget(locale),
],
),
),
Expand All @@ -93,16 +84,14 @@ class DevicesPageState extends State<DevicesPage> {
);
}

List<Widget> smartphoneDeviceListWidget() {
RPLocalizations locale = RPLocalizations.of(context)!;
return [
DevicesPageListTitle(locale: locale, type: DevicesPageTypes.phone),
SliverList(
delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
return Center(
child: StudiesCard(
child: Column(
children: cardListBuilder(
List<Widget> smartphoneDeviceListWidget(RPLocalizations locale) => [
DevicesPageListTitle(locale: locale, type: DevicesPageTypes.phone),
SliverList(
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
return Center(
child: StudiesCard(
child: cardListBuilder(
smartphoneDevice[index].icon!,
smartphoneDevice[index].phoneInfo['name']!,
(
Expand All @@ -111,79 +100,66 @@ class DevicesPageState extends State<DevicesPage> {
),
),
),
),
);
}, childCount: smartphoneDevice.length),
), // List of smartphone(s)
];
}

List<Widget> physicalDevicesListWidget() {
RPLocalizations locale = RPLocalizations.of(context)!;

return [
DevicesPageListTitle(locale: locale, type: DevicesPageTypes.devices),
SliverList(
delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
RPLocalizations locale = RPLocalizations.of(context)!;
DeviceModel device = physicalDevices[index];
var children = cardListBuilder(
device.icon!,
locale.translate(device.name!),
(device.id, device.batteryLevel ?? 0),
enableFeedback: true,
onTap: () => physicalDeviceClicked(device),
trailing: device.getDeviceStatusIcon is String
? Text(
locale
.translate(device.getDeviceStatusIcon)
.toUpperCase(),
style: aboutCardTitleStyle.copyWith(
color: Theme.of(context).primaryColor))
: device.getDeviceStatusIcon);

return devicesPageCardStream(
device.deviceEvents, children, DeviceStatus.unknown);
}, childCount: physicalDevices.length),
)
];
}

List<Widget> onlineServicesListWidget() {
RPLocalizations locale = RPLocalizations.of(context)!;

return [
DevicesPageListTitle(locale: locale, type: DevicesPageTypes.services),
SliverList(
delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
RPLocalizations locale = RPLocalizations.of(context)!;
);
}, childCount: smartphoneDevice.length),
), // List of smartphone(s)
];

var service = onlineServices[index];
var children = cardListBuilder(
service.icon!,
locale.translate(service.name!),
null,
trailing: service.getServiceStatusIcon is String
? Text(
locale
.translate(service.getServiceStatusIcon)
.toUpperCase(),
style: aboutCardTitleStyle.copyWith(
color: Theme.of(context).primaryColor))
: service.getServiceStatusIcon,
isThreeLine: false,
);
List<Widget> physicalDevicesListWidget(RPLocalizations locale) => [
DevicesPageListTitle(locale: locale, type: DevicesPageTypes.devices),
SliverList(
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
DeviceModel device = physicalDevices[index];
return devicesPageCardStream(
device.deviceEvents,
() => cardListBuilder(
device.icon!,
locale.translate(device.name!),
(device.id, device.batteryLevel ?? 0),
enableFeedback: true,
onTap: () => physicalDeviceClicked(device),
trailing: device.getDeviceStatusIcon is String
? Text(
locale
.translate(device.getDeviceStatusIcon)
.toUpperCase(),
style: aboutCardTitleStyle.copyWith(
color: Theme.of(context).primaryColor))
: device.getDeviceStatusIcon),
DeviceStatus.unknown);
}, childCount: physicalDevices.length),
),
];

return devicesPageCardStream<DeviceStatus>(
onlineServices[index].deviceEvents,
children,
DeviceStatus.unknown);
}, childCount: onlineServices.length),
)
];
}
List<Widget> onlineServicesListWidget(RPLocalizations locale) => [
DevicesPageListTitle(locale: locale, type: DevicesPageTypes.services),
SliverList(
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
DeviceModel service = onlineServices[index];
return devicesPageCardStream(
service.deviceEvents,
() => cardListBuilder(
service.icon!,
locale.translate(service.name!),
null,
trailing: service.getServiceStatusIcon is String
? Text(
locale
.translate(service.getServiceStatusIcon)
.toUpperCase(),
style: aboutCardTitleStyle.copyWith(
color: Theme.of(context).primaryColor))
: service.getServiceStatusIcon,
isThreeLine: false,
),
DeviceStatus.unknown);
}, childCount: onlineServices.length),
),
];

List<Widget> cardListBuilder(
Widget cardListBuilder(
Icon leading,
String title,
(String, int)? subtitle, {
Expand All @@ -192,55 +168,50 @@ class DevicesPageState extends State<DevicesPage> {
enableFeedback = false,
isThreeLine = true,
}) =>
[
ListTile(
enableFeedback: enableFeedback,
isThreeLine: isThreeLine,
leading: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [leading],
),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(title),
],
),
subtitle: subtitle != null
? Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(subtitle.$1),
BatteryPercentage(batteryLevel: subtitle.$2),
],
)
: null,
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (trailing != null) trailing,
],
),
onTap: onTap,
)
];
ListTile(
enableFeedback: enableFeedback,
isThreeLine: isThreeLine,
leading: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [leading],
),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(title),
],
),
subtitle: subtitle != null
? Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(subtitle.$1),
BatteryPercentage(batteryLevel: subtitle.$2),
],
)
: null,
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (trailing != null) trailing,
],
),
onTap: onTap,
);

Widget devicesPageCardStream<T>(
Stream<T> stream, List<Widget> children, T? initialData) {
return Center(
child: StudiesCard(
child: StreamBuilder<T>(
stream: stream,
initialData: initialData,
builder: (context, AsyncSnapshot<T> snapshot) => Column(
children: children,
Stream<T> stream, Widget Function() childBuilder, T? initialData) =>
Center(
child: StudiesCard(
child: StreamBuilder<T>(
stream: stream,
initialData: initialData,
builder: (context, AsyncSnapshot<T> snapshot) => childBuilder(),
),
),
),
);
}
);

void physicalDeviceClicked(DeviceModel device) async {
if (await FlutterBluePlus.isAvailable == false) {
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/pages/devices_page.list_title.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class DevicesPageListTitle extends StatelessWidget {
return SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 8),
child: Text(locale.translate("pages.devices.${type.name}.title").toUpperCase(),
child: Text(
locale.translate("pages.devices.${type.name}.title").toUpperCase(),
style: dataCardTitleStyle.copyWith(
color: Theme.of(context).primaryColor)),
),
Expand Down

0 comments on commit 9057857

Please sign in to comment.