aves_mio0.31/lib/widgets/cast/fast_cast_controller.dart
2026-07-18 13:39:22 +02:00

112 lines
3.3 KiB
Dart

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:aves/model/entry/entry.dart';
import '../cast_controller.dart';
class FastCastBackend extends CastBackendBase {
FastCastBackend(super.controller);
static const _channel = MethodChannel('google_cast');
// FAST non usa timer Dart → tutto gestito da CAF
@override
Duration? getNextDelay(AvesEntry entry) => null;
// ------------------------------------------------------------
// START CAST (slideshow veloce)
// ------------------------------------------------------------
@override
Future<void> startCast(List<AvesEntry> entries, int index) =>
startSlideshow(entries, index);
@override
Future<void> startSlideshow(List<AvesEntry> entries, int index) async {
controller.castIconState.value = CastIconState.connecting;
controller.isCastingNotifier.value = true;
final json = _buildQueueJson(
entries: entries,
startIndex: index,
mode: "slideshow",
);
await _channel.invokeMethod(
"fast.loadQueue",
json,
);
controller.castIconState.value = CastIconState.connected;
}
// ------------------------------------------------------------
// FULLSCREEN = slideshow veloce
// ------------------------------------------------------------
@override
Future<void> startFullscreen(List<AvesEntry> entries, int index) =>
startSlideshow(entries, index);
// ------------------------------------------------------------
// START MANUAL QUEUE (slideshow lento)
// ------------------------------------------------------------
@override
Future<void> startManualQueue(List<AvesEntry> entries, int index) async {
controller.castIconState.value = CastIconState.connecting;
controller.isCastingNotifier.value = true;
final json = _buildQueueJson(
entries: entries,
startIndex: index,
mode: "manual",
);
await _channel.invokeMethod(
"fast.loadQueue",
json,
);
controller.castIconState.value = CastIconState.connected;
}
// ------------------------------------------------------------
// NEXT / PREV
// ------------------------------------------------------------
@override
Future<void> nextManual() =>
_channel.invokeMethod("fast.next");
@override
Future<void> prevManual() =>
_channel.invokeMethod("fast.prev");
// ------------------------------------------------------------
// STOP
// ------------------------------------------------------------
@override
Future<void> stopCast() async {
await _channel.invokeMethod("fast.stop");
controller.castIconState.value = CastIconState.idle;
controller.isCastingNotifier.value = false;
}
// ------------------------------------------------------------
// COSTRUZIONE QUEUE JSON (CAF)
// ------------------------------------------------------------
Map<String, dynamic> _buildQueueJson({
required List<AvesEntry> entries,
required int startIndex,
required String mode, // "slideshow" / "manual"
}) {
return {
"mode": mode,
"startIndex": startIndex,
"items": entries.map((e) {
return {
"id": e.id.toString(),
"url": e.bestUri,
"isVideo": e.isVideo,
"title": e.bestTitle ?? "",
};
}).toList(),
};
}
}