Aves_mio0.88/lib/widgets/collection/actions/entry_action_cast.dart
2026-10-03 12:51:15 +02:00

128 lines
3.1 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:aves/model/entry/entry.dart';
import 'package:aves/services/common/services.dart';
/// Gestione CAST.
/// Versione ultra‑ottimizzata, senza album, senza grouping, senza filter_grids.
/// Funziona con il tuo nuovo modello folder‑based + remote origin.
class EntryActionCast {
final BuildContext context;
final List<AvesEntry> entries;
EntryActionCast(this.context, this.entries);
// ------------------------------------------------------------
// ENTRY POINT
// ------------------------------------------------------------
Future<void> startCast() async {
debugPrint('[CAST ACTION] startCast entries.length=${entries.length}');
for (var i = 0; i < entries.length; i++) {
final e = entries[i];
debugPrint(
'[CAST ACTION] entry[$i] '
'uri=${e.uri} '
'mime=${e.mimeType} '
'title=${e.bestTitle}',
);
}
if (entries.isEmpty) {
_show('Nessun elemento da inviare al Cast');
return;
}
try {
final ok = await _connect();
if (!ok) {
_show('Nessun dispositivo Cast disponibile');
return;
}
await _sendMedia(entries);
_show('Invio al Cast in corso…');
} catch (e) {
_show('Errore Cast: $e');
}
}
// ------------------------------------------------------------
// CONNECTION
// ------------------------------------------------------------
Future<bool> _connect() async {
try {
final connected = await castService.connect();
return connected;
} catch (_) {
return false;
}
}
// ------------------------------------------------------------
// SEND MEDIA
// ------------------------------------------------------------
Future<void> _sendMedia(List<AvesEntry> entries) async {
debugPrint('[CAST ACTION] _sendMedia count=${entries.length}');
for (var i = 0; i < entries.length; i++) {
final entry = entries[i];
debugPrint(
'[CAST ACTION] sendMedia[$i] '
'uri=${entry.uri} '
'mime=${entry.mimeType} '
'title=${entry.bestTitle}',
);
await castService.sendMedia(
uri: entry.uri,
mimeType: entry.mimeType,
title: entry.bestTitle,
);
}
debugPrint('[CAST ACTION] _sendMedia done');
}
// ------------------------------------------------------------
// CONTROLS
// ------------------------------------------------------------
Future<void> next() async {
try {
await castService.next();
} catch (e) {
_show('Errore next: $e');
}
}
Future<void> previous() async {
try {
await castService.previous();
} catch (e) {
_show('Errore previous: $e');
}
}
Future<void> stop() async {
try {
await castService.stop();
_show('Cast terminato');
} catch (e) {
_show('Errore stop: $e');
}
}
// ------------------------------------------------------------
// UTILS
// ------------------------------------------------------------
void _show(String msg) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(msg)),
);
}
}