fix(mobile): eager cast discovery

This commit is contained in:
Will 2026-08-07 14:30:53 -05:00
parent 9b6ffb2342
commit ae285cc933
3 changed files with 23 additions and 23 deletions

View file

@ -1,10 +1,23 @@
import 'package:fcast_sender_sdk/fcast_sender_sdk.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'cast_manager_state.freezed.dart';
enum CastDestinationType { googleCast, fCast }
enum CastState { idle, playing, paused, buffering }
enum CastState {
idle,
playing,
paused,
buffering;
static CastState fromPlaybackState(PlaybackState state) => switch (state) {
PlaybackState.playing => CastState.playing,
PlaybackState.paused => CastState.paused,
PlaybackState.buffering => CastState.buffering,
PlaybackState.idle || PlaybackState.ended => CastState.idle,
};
}
typedef CastDestination = (String name, CastDestinationType type, dynamic device);

View file

@ -13,11 +13,13 @@ class CastRepository {
final Map<(String, ProtocolType), (DeviceInfo, int?)> _discoveredDevices = {};
int _currentDeviceGeneration = 0;
Future<void>? _initialized;
late final Future<void> _initialized;
void init() {
_initialized = _initialize();
}
Future<void> connect(DeviceInfo deviceInfo) async {
await _ensureInitialized();
_activeDevice?.disconnect();
final device = _castContext!.createDeviceFromInfo(info: deviceInfo);
_activeDevice = device;
@ -68,18 +70,11 @@ class CastRepository {
void seekTo(Duration position) => _activeDevice?.seek(timeSeconds: position.inSeconds.toDouble());
Future<List<(DeviceInfo, int?)>> listDestinations() async {
final isFirstScan = _initialized == null;
await _ensureInitialized();
if (isFirstScan) {
await Future.delayed(const Duration(seconds: 3));
}
await _initialized;
return _discoveredDevices.values.toList(growable: false);
}
Future<void> _ensureInitialized() => _initialized ??= _initialize();
Future<void> _initialize() async {
await FCastSenderSdkLib.init();
_castContext = CastContext();
@ -96,5 +91,6 @@ class CastRepository {
});
await discoverer.init();
await Future.delayed(const Duration(seconds: 3));
}
}

View file

@ -41,6 +41,7 @@ class CastService {
CastService(this._castRepository, this._sessionsApiService, this._assetApiRepository) {
_castRepository.onConnectionState = _onConnectionState;
_castRepository.onDeviceEvent = _onDeviceEvent;
_castRepository.init();
}
void _onConnectionState(DeviceConnectionState state) {
@ -71,17 +72,7 @@ class CastService {
}
void _handlePlaybackState(PlaybackState state) {
switch (state) {
case PlaybackState.playing:
onCastState?.call(CastState.playing);
case PlaybackState.paused:
onCastState?.call(CastState.paused);
case PlaybackState.buffering:
onCastState?.call(CastState.buffering);
case PlaybackState.idle:
case PlaybackState.ended:
onCastState?.call(CastState.idle);
}
onCastState?.call(CastState.fromPlaybackState(state));
}
Future<void> connect(dynamic device) async {