Skip to content

Commit b4bdb78

Browse files
Fix #66: ios - file is downloaded but the state is not updated properly
Signed-off-by: huynguyennovem <huynguyennovem@gmail.com>
1 parent a5ffc95 commit b4bdb78

5 files changed

Lines changed: 27 additions & 24 deletions

File tree

ios/Runner/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
</dict>
6161
<key>FDMaximumConcurrentTasks</key>
6262
<integer>5</integer>
63+
<key>UISupportsDocumentBrowser</key>
64+
<true/>
6365
<key>LSSupportsOpeningDocumentsInPlace</key>
6466
<true/>
6567
<key>UIFileSharingEnabled</key>

lib/provider/file_provider.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class FileProvider extends ChangeNotifier {
2929
required SharedFileState newFileState,
3030
required String savedDir,
3131
}) {
32-
final oldFile = _files.firstWhere((file) => fileName == file.name);
32+
if(_files.isEmpty) return;
33+
final oldFile = _files.firstWhere((file) => fileName.trim() == file.name?.trim());
3334
final oldIndex = _files.indexOf(oldFile);
3435
final updatedFile = oldFile.copyWith(state: newFileState, savedDir: savedDir);
3536
_files[oldIndex] = updatedFile;
36-
3737
notifyListeners();
3838
}
3939
}

lib/service/download_service.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DownloadService {
4646
String? externalStorageDirPath;
4747
// fix https://github.com/huynguyennovem/netshare/issues/67:
4848
// file name has space > need to encode the uri
49-
String downloadFileUrl = fileUrl;
49+
String encodedFileUrl = Uri.encodeFull(fileUrl);
5050
if (Platform.isAndroid) {
5151
try {
5252
externalStorageDirPath = await AndroidPathProvider.downloadsPath;
@@ -57,27 +57,28 @@ class DownloadService {
5757
}
5858
} else if (Platform.isIOS) {
5959
externalStorageDirPath = (await getApplicationDocumentsDirectory()).absolute.path;
60-
downloadFileUrl = Uri.encodeFull(fileUrl);
6160
}
6261
if (null != externalStorageDirPath) {
6362
final savedDir = Directory(externalStorageDirPath);
6463
if (!savedDir.existsSync()) {
6564
await savedDir.create();
6665
}
66+
final encodedFileName = path.basename(encodedFileUrl);
67+
final decodedFileName = Uri.decodeFull(encodedFileName);
68+
// use raw file name for beautiful name only (without encoded name)
6769
final taskId = await FlutterDownloader.enqueue(
68-
url: downloadFileUrl,
70+
url: encodedFileUrl,
71+
fileName: decodedFileName,
6972
savedDir: savedDir.path,
7073
saveInPublicStorage: true,
7174
);
72-
debugPrint('Download taskId: $taskId');
7375

7476
// add to stream
75-
final fileName = path.basename(downloadFileUrl);
7677
updateDownloadState(
7778
DownloadEntity(
78-
taskId ?? fileName,
79-
fileName,
80-
downloadFileUrl,
79+
taskId ?? decodedFileName,
80+
decodedFileName,
81+
encodedFileUrl,
8182
savedDir.path,
8283
DownloadManner.flutterDownloader,
8384
DownloadState.downloading,

lib/ui/client/client_widget.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ class _ClientWidgetState extends State<ClientWidget> {
7575
String? fileName;
7676
String? url;
7777
String? savedDir;
78-
if (null != tasks) {
78+
if (null != tasks && tasks.isNotEmpty) {
7979
final task = tasks.firstWhere((element) => taskId == element.taskId);
8080
fileName = task.filename;
8181
url = task.url;
8282
savedDir = task.savedDir;
83+
getIt.get<DownloadService>().updateDownloadState(
84+
DownloadEntity(
85+
taskId,
86+
fileName ?? '',
87+
url,
88+
savedDir,
89+
DownloadManner.flutterDownloader,
90+
state,
91+
)
92+
);
8393
}
84-
getIt.get<DownloadService>().updateDownloadState(
85-
DownloadEntity(
86-
taskId,
87-
fileName ?? '',
88-
url ?? '',
89-
savedDir ?? '',
90-
DownloadManner.flutterDownloader,
91-
state,
92-
)
93-
);
9494
}
9595
});
9696
FlutterDownloader.registerCallback(downloadCallback);
@@ -151,7 +151,7 @@ class _ClientWidgetState extends State<ClientWidget> {
151151
@pragma('vm:entry-point')
152152
static void downloadCallback(
153153
String id,
154-
DownloadTaskStatus status,
154+
int status,
155155
int progress,
156156
) {
157157
debugPrint(
@@ -161,7 +161,7 @@ class _ClientWidgetState extends State<ClientWidget> {
161161
// TODO: 1. Flutter engine issue: can only send basic dart type + restart/hot reload does not work
162162
// (https://github.com/flutter/flutter/issues/119589)
163163
// can only send basic dart type -> Fix: convert status entity to int
164-
IsolateNameServer.lookupPortByName('downloader_send_port')?.send([id, status.value, progress]);
164+
IsolateNameServer.lookupPortByName('downloader_send_port')?.send([id, status, progress]);
165165
}
166166

167167
@override

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dependencies:
5252
mime: ^1.0.3
5353
http_parser: ^4.0.2
5454
share_plus: ^6.3.0
55-
flutter_downloader: ^1.10.1+2
55+
flutter_downloader: ^1.11.1
5656
path_provider: ^2.0.11
5757
android_path_provider: ^0.3.0
5858
permission_handler: ^10.2.0

0 commit comments

Comments
 (0)