Skip to content

Commit 32591b5

Browse files
committed
Use Isolate to move files
1 parent dd6896c commit 32591b5

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

lib/creation/creation_bloc.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ class CreationBloc extends Bloc<dynamic, CreationState> {
5656
}
5757

5858
Future<void> pickFiles(List<File> files) async {
59+
emit(state.copyWith(isLoading: true));
5960
final project = await _project;
6061
final media = files.map((file) => Media(projectId: project.id, path: file.path));
6162
emit(state.copyWith(
6263
media: await project.attachMedia(media),
64+
isLoading: false,
6365
));
6466
}
6567

lib/creator/movie_project.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ class MovieProjectImpl extends MovieProject {
132132
@override
133133
Future<void> dispose({required bool deleteDraft}) async {
134134
if (!deleteDraft) return;
135-
for (final media in _media) {
136-
File(media.path).delete().ignore();
137-
}
138135
_media.clear();
139136
fileManager.deleteDraft(projectId).ignore();
140137
await draftMovieManager.deleteDraft(projectId);

lib/file_manager.dart

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:io';
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter_bloc/flutter_bloc.dart';
56
import '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

Comments
 (0)