83 lines
2.0 KiB
Dart
83 lines
2.0 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:nsd/nsd.dart';
|
|
import '../../data/services/link_extraction_service.dart';
|
|
|
|
|
|
class SenderState {
|
|
final Service? connectedDevice;
|
|
final bool isExtracting;
|
|
final bool isSending;
|
|
final List<String> history;
|
|
|
|
SenderState({
|
|
this.connectedDevice,
|
|
this.isExtracting = false,
|
|
this.isSending = false,
|
|
this.history = const [],
|
|
});
|
|
|
|
SenderState copyWith({
|
|
Service? connectedDevice,
|
|
bool? isExtracting,
|
|
bool? isSending,
|
|
List<String>? history,
|
|
}) {
|
|
return SenderState(
|
|
connectedDevice: connectedDevice ?? this.connectedDevice,
|
|
isExtracting: isExtracting ?? this.isExtracting,
|
|
isSending: isSending ?? this.isSending,
|
|
history: history ?? this.history,
|
|
);
|
|
}
|
|
}
|
|
|
|
final senderProvider = NotifierProvider<SenderNotifier, SenderState>(() {
|
|
return SenderNotifier();
|
|
});
|
|
|
|
class SenderNotifier extends Notifier<SenderState> {
|
|
final _dio = Dio();
|
|
final _linkExtractionService = LinkExtractionService();
|
|
|
|
@override
|
|
SenderState build() => SenderState();
|
|
|
|
void connectToDevice(Service device) {
|
|
state = state.copyWith(connectedDevice: device);
|
|
}
|
|
|
|
Future<void> sendLink(String url) async {
|
|
if (state.connectedDevice == null) return;
|
|
|
|
state = state.copyWith(isExtracting: true);
|
|
|
|
final videoUrl = await _linkExtractionService.extractVideoUrl(url);
|
|
|
|
if (videoUrl == null) {
|
|
state = state.copyWith(isExtracting: false);
|
|
return;
|
|
}
|
|
|
|
state = state.copyWith(isExtracting: false, isSending: true);
|
|
|
|
try {
|
|
final device = state.connectedDevice!;
|
|
final endpoint = 'http://${device.host}:${device.port}/play';
|
|
|
|
await _dio.post(endpoint, data: {
|
|
'url': videoUrl,
|
|
'title': 'Shared Video',
|
|
'timestamp': DateTime.now().millisecondsSinceEpoch,
|
|
});
|
|
|
|
state = state.copyWith(
|
|
isSending: false,
|
|
history: [url, ...state.history],
|
|
);
|
|
} catch (e) {
|
|
state = state.copyWith(isSending: false);
|
|
}
|
|
}
|
|
}
|