11import 'dart:io' ;
22
3+ import 'package:flutter/foundation.dart' ;
34import 'package:flutter/material.dart' ;
45import 'package:flutter_bloc/flutter_bloc.dart' ;
56import 'package:intl/intl.dart' ;
@@ -83,21 +84,35 @@ class FileManagerImpl implements FileManager {
8384
8485 @override
8586 Future <Iterable <Media >> copyToDraftDir (int projectId, Iterable <Media > media) async {
86- final result = < Media > [];
87+ return (await compute (_copyToDraftDir, [
88+ projectId,
89+ media.map ((e) => e.path).toList (growable: false ),
90+ ]))
91+ .map ((path) => Media (
92+ projectId: projectId,
93+ path: path,
94+ ));
95+ }
96+
97+ /// Isolate
98+ /// @see [copyToDraftDir]
99+ Future <List <String >> _copyToDraftDir (List <dynamic > args) async {
100+ int projectId = args[0 ];
101+ List <String > media = args[1 ];
102+
103+ final result = < String > [];
87104
88105 final dir = draftDir (projectId);
89106 await dir.create ();
90107 for (final source in media) {
91- final newPath = await _moveFile (
92- source: source.path ,
93- newPath: _buildPath (dir, source.path ),
108+ final newPath = await moveFile (
109+ source: source,
110+ newPath: _buildPath (dir, source),
94111 );
95112 if (newPath == null ) continue ;
96- result.add (Media (
97- projectId: projectId,
98- // We care here only about relative path
99- path: basename (newPath),
100- ));
113+
114+ // We care here only about relative path
115+ result.add (basename (newPath));
101116 }
102117
103118 return result;
@@ -114,7 +129,7 @@ class FileManagerImpl implements FileManager {
114129 var index = 0 ;
115130 final dir = draftDir (projectId);
116131 for (final source in media) {
117- final newPath = await _moveFile (
132+ final newPath = await moveFile (
118133 source: _buildPath (dir, source.path),
119134 newPath: _buildPath (dir, 'image${format .format (index ++)}.jpg' ),
120135 );
@@ -140,10 +155,21 @@ class FileManagerImpl implements FileManager {
140155 }
141156
142157 /// @return `null` when [source] can't be moved to the new path [newPath]
143- Future <String ?> _moveFile ({
158+ Future <String ?> moveFile ({
144159 required String source,
145160 required String newPath,
146161 }) async {
162+ return await compute (_moveFile, [source, newPath]);
163+ }
164+
165+ /// Isolate
166+ /// @see [moveFile]
167+ ///
168+ /// @return `null` when [source] can't be moved to the new path [newPath]
169+ Future <String ?> _moveFile (List <dynamic > args) async {
170+ String source = args[0 ];
171+ String newPath = args[1 ];
172+
147173 if (source == newPath) return newPath;
148174
149175 Logger .d ('Move file from "$source " to "$newPath "' );
0 commit comments