add singleton trick

This commit is contained in:
Salvatore Giordano
2020-11-26 11:44:14 +01:00
parent eca446bcea
commit ebcaed8771
2 changed files with 8 additions and 7 deletions
+6 -3
View File
@@ -2,10 +2,10 @@ import 'dart:async';
import 'package:synchronized/synchronized.dart'; import 'package:synchronized/synchronized.dart';
import 'package:video_compress/video_compress.dart'; import 'package:video_compress/video_compress.dart';
class CompressVideoService { class ICompressVideoService {
static final CompressVideoService instance = CompressVideoService._(); static final ICompressVideoService instance = ICompressVideoService._();
final _lock = Lock(); final _lock = Lock();
CompressVideoService._(); ICompressVideoService._();
Future<MediaInfo> compressVideo(String path) async { Future<MediaInfo> compressVideo(String path) async {
return _lock.synchronized(() { return _lock.synchronized(() {
@@ -15,3 +15,6 @@ class CompressVideoService {
}); });
} }
} }
ICompressVideoService get CompressVideoService =>
ICompressVideoService.instance;
+2 -4
View File
@@ -866,8 +866,7 @@ class MessageInputState extends State<MessageInput> {
if (file.size > _kMaxAttachmentSize) { if (file.size > _kMaxAttachmentSize) {
if (medium.mediaType == MediaType.video) { if (medium.mediaType == MediaType.video) {
final mediaInfo = final mediaInfo = await CompressVideoService.compressVideo(file.path);
await CompressVideoService.instance.compressVideo(file.path);
if (mediaInfo.filesize / (1024 * 1024) > _kMaxAttachmentSize) { if (mediaInfo.filesize / (1024 * 1024) > _kMaxAttachmentSize) {
Scaffold.of(context).showSnackBar( Scaffold.of(context).showSnackBar(
@@ -1561,8 +1560,7 @@ class MessageInputState extends State<MessageInput> {
if (file.size > _kMaxAttachmentSize) { if (file.size > _kMaxAttachmentSize) {
if (attachmentType == 'video') { if (attachmentType == 'video') {
final mediaInfo = final mediaInfo = await CompressVideoService.compressVideo(file.path);
await CompressVideoService.instance.compressVideo(file.path);
file = PlatformFile( file = PlatformFile(
name: mediaInfo.title, name: mediaInfo.title,
size: mediaInfo.filesize, size: mediaInfo.filesize,