cast dialog now shows connected device at top of list with a list header. Discovered devices are also cached for app session.

This commit is contained in:
bwees 2025-05-20 14:55:38 -05:00
parent 9e9b8fdb64
commit 8a31c68ddd
No known key found for this signature in database
4 changed files with 73 additions and 35 deletions

View File

@ -655,6 +655,7 @@
"common_create_new_album": "Create new album",
"common_server_error": "Please check your network connection, make sure the server is reachable and app/server versions are compatible.",
"completed": "Completed",
"connected_device": "Connected device",
"confirm": "Confirm",
"confirm_admin_password": "Confirm Admin Password",
"confirm_delete_face": "Are you sure you want to delete {name} face from the asset?",
@ -762,6 +763,7 @@
"disallow_edits": "Disallow edits",
"discord": "Discord",
"discover": "Discover",
"discovered_devices": "Discovered devices",
"dismiss_all_errors": "Dismiss all errors",
"dismiss_error": "Dismiss error",
"display_options": "Display options",

View File

@ -10,6 +10,8 @@ final castProvider = StateNotifierProvider<CastNotifier, CastManagerState>(
class CastNotifier extends StateNotifier<CastManagerState> {
final GCastService _gCastService;
List<(String, CastDestinationType, dynamic)> discovered = List.empty();
CastNotifier(this._gCastService)
: super(
CastManagerState(
@ -60,7 +62,11 @@ class CastNotifier extends StateNotifier<CastManagerState> {
}
Future<List<(String, CastDestinationType, dynamic)>> getDevices() async {
return _gCastService.getDevices();
if (discovered.isEmpty) {
discovered = await _gCastService.getDevices();
}
return discovered;
}
void play() {

View File

@ -190,6 +190,6 @@ class GCastService implements ICastDestinationService {
device
),
)
.toList(growable: true);
.toList(growable: false);
}
}

View File

@ -48,47 +48,77 @@ class CastDialog extends ConsumerWidget {
).tr();
}
final devices = snapshot.data!;
final connected =
devices.where((d) => isCurrentDevice(d.$1)).toList();
final others =
devices.where((d) => !isCurrentDevice(d.$1)).toList();
final List<dynamic> sectionedList = [];
if (connected.isNotEmpty) {
sectionedList.add("connected_device".tr());
sectionedList.addAll(connected);
}
if (others.isNotEmpty) {
sectionedList.add("discovered_devices".tr());
sectionedList.addAll(others);
}
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemCount: sectionedList.length,
itemBuilder: (context, index) {
final found = snapshot.data![index];
final deviceName = found.$1;
final type = found.$2;
final deviceObj = found.$3;
final item = sectionedList[index];
return ListTile(
title: Text(
deviceName,
style: TextStyle(
if (item is String) {
// It's a section header
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
item,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
).tr(),
);
} else {
final (deviceName, type, deviceObj) =
item as (String, CastDestinationType, dynamic);
return ListTile(
title: Text(
deviceName,
style: TextStyle(
color: isCurrentDevice(deviceName)
? context.colorScheme.primary
: null,
),
),
leading: Icon(
type == CastDestinationType.googleCast
? Icons.cast
: Icons.cast_connected,
color: isCurrentDevice(deviceName)
? context.colorScheme.primary
: null,
),
),
leading: Icon(
type == CastDestinationType.googleCast
? Icons.cast
: Icons.cast_connected,
color: isCurrentDevice(deviceName)
? context.colorScheme.primary
: null,
),
trailing: isCurrentDevice(deviceName)
? Icon(Icons.check, color: context.colorScheme.primary)
: isDeviceConnecting(deviceName)
? const CircularProgressIndicator()
: null,
onTap: () {
// dont accept taps if the device is already connected or is connecting now
if (isDeviceConnecting(deviceName) ||
castManager.isCasting) {
return;
}
ref.read(castProvider.notifier).connect(type, deviceObj);
},
);
trailing: isCurrentDevice(deviceName)
? Icon(Icons.check, color: context.colorScheme.primary)
: isDeviceConnecting(deviceName)
? const CircularProgressIndicator()
: null,
onTap: () {
if (isDeviceConnecting(deviceName)) {
return;
}
ref.read(castProvider.notifier).disconnect();
ref.read(castProvider.notifier).connect(type, deviceObj);
},
);
}
},
);
},