71 lines
1.7 KiB
Dart
71 lines
1.7 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../data/services/receiver_server.dart';
|
|
import '../../../discovery/data/services/discovery_service.dart';
|
|
|
|
class ReceiverState {
|
|
final bool isRunning;
|
|
final int? port;
|
|
final String? ipAddress;
|
|
final Map<String, dynamic>? currentVideo;
|
|
|
|
ReceiverState({
|
|
this.isRunning = false,
|
|
this.port,
|
|
this.ipAddress,
|
|
this.currentVideo,
|
|
});
|
|
|
|
ReceiverState copyWith({
|
|
bool? isRunning,
|
|
int? port,
|
|
String? ipAddress,
|
|
Map<String, dynamic>? currentVideo,
|
|
}) {
|
|
return ReceiverState(
|
|
isRunning: isRunning ?? this.isRunning,
|
|
port: port ?? this.port,
|
|
ipAddress: ipAddress ?? this.ipAddress,
|
|
currentVideo: currentVideo ?? this.currentVideo,
|
|
);
|
|
}
|
|
}
|
|
|
|
final receiverProvider = NotifierProvider<ReceiverNotifier, ReceiverState>(() {
|
|
return ReceiverNotifier();
|
|
});
|
|
|
|
class ReceiverNotifier extends Notifier<ReceiverState> {
|
|
late ReceiverServer _server;
|
|
final _discoveryService = DiscoveryService();
|
|
|
|
@override
|
|
ReceiverState build() {
|
|
_server = ReceiverServer(onPlayRequest: _handlePlayRequest);
|
|
return ReceiverState();
|
|
}
|
|
|
|
Future<void> startServer() async {
|
|
if (state.isRunning) return;
|
|
|
|
final port = await _server.start();
|
|
await _discoveryService.registerService(port);
|
|
|
|
state = state.copyWith(
|
|
isRunning: true,
|
|
port: port,
|
|
// In a real app, use network_info_plus to get the real IP
|
|
ipAddress: 'Checking...',
|
|
);
|
|
}
|
|
|
|
void _handlePlayRequest(Map<String, dynamic> data) {
|
|
state = state.copyWith(currentVideo: data);
|
|
}
|
|
|
|
Future<void> stopServer() async {
|
|
await _server.stop();
|
|
await _discoveryService.stopRegistration();
|
|
state = ReceiverState();
|
|
}
|
|
}
|