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

View File

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

View File

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

View File

@ -48,47 +48,77 @@ class CastDialog extends ConsumerWidget {
).tr(); ).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( return ListView.builder(
shrinkWrap: true, shrinkWrap: true,
itemCount: snapshot.data!.length, itemCount: sectionedList.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final found = snapshot.data![index]; final item = sectionedList[index];
final deviceName = found.$1;
final type = found.$2;
final deviceObj = found.$3;
return ListTile( if (item is String) {
title: Text( // It's a section header
deviceName, return Padding(
style: TextStyle( 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) color: isCurrentDevice(deviceName)
? context.colorScheme.primary ? context.colorScheme.primary
: null, : null,
), ),
), trailing: isCurrentDevice(deviceName)
leading: Icon( ? Icon(Icons.check, color: context.colorScheme.primary)
type == CastDestinationType.googleCast : isDeviceConnecting(deviceName)
? Icons.cast ? const CircularProgressIndicator()
: Icons.cast_connected, : null,
color: isCurrentDevice(deviceName) onTap: () {
? context.colorScheme.primary if (isDeviceConnecting(deviceName)) {
: null, return;
), }
trailing: isCurrentDevice(deviceName) ref.read(castProvider.notifier).disconnect();
? Icon(Icons.check, color: context.colorScheme.primary) ref.read(castProvider.notifier).connect(type, deviceObj);
: 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);
},
);
}, },
); );
}, },