twonly-app/lib/src/channels/video_compression.channel.dart
otsmr c85914672e
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run
Improve: Video compression with progress updates
2026-03-13 01:35:04 +01:00

45 lines
1.2 KiB
Dart

import 'package:flutter/services.dart';
import 'package:twonly/src/utils/log.dart';
abstract class VideoCompressionChannel {
static const MethodChannel _channel =
MethodChannel('eu.twonly/videoCompression');
static void Function(double)? _currentProgressCallback;
static bool _handlerSetup = false;
static void _setupProgressHandler() {
if (_handlerSetup) return;
_channel.setMethodCallHandler((call) async {
if (call.method == 'onProgress') {
// ignore: avoid_dynamic_calls
final progress = call.arguments['progress'] as int;
_currentProgressCallback?.call(progress / 100.0);
}
});
_handlerSetup = true;
}
static Future<String?> compressVideo({
required String inputPath,
required String outputPath,
void Function(double progress)? onProgress,
}) async {
try {
_setupProgressHandler();
_currentProgressCallback = onProgress;
await _channel.invokeMethod('compressVideo', {
'input': inputPath,
'output': outputPath,
});
return outputPath;
} on PlatformException catch (e) {
Log.error('Failed to compress video: $e');
return null;
} finally {
_currentProgressCallback = null;
}
}
}