Skip to content

Commit

Permalink
TW-1702 handle connectivity status change when downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Aug 26, 2024
1 parent b9d3c60 commit 3934039
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/utils/manager/download_manager/download_manager.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dartz/dartz.dart' hide Task;
import 'package:dio/dio.dart';
import 'package:fluffychat/app_state/failure.dart';
Expand All @@ -11,6 +12,7 @@ import 'package:fluffychat/utils/manager/download_manager/download_file_state.da
import 'package:fluffychat/utils/manager/download_manager/downloading_worker_queue.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/download_file_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/download_file_web_extension.dart';
import 'package:fluffychat/utils/network_connection_service.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/task_queue/task.dart';
import 'package:matrix/matrix.dart';
Expand All @@ -28,6 +30,9 @@ class DownloadManager {

final Map<String, DownloadFileInfo> _eventIdMapDownloadFileInfo = {};

final NetworkConnectionService _connectivityService =
getIt.get<NetworkConnectionService>();

void cancelDownload(String eventId) {
final cancelToken = _eventIdMapDownloadFileInfo[eventId]?.cancelToken;
if (cancelToken != null) {
Expand Down Expand Up @@ -132,6 +137,26 @@ class DownloadManager {
cancelToken: cancelToken,
isFirstPriority: isFirstPriority,
);

_connectivityService.connectivity.onConnectivityChanged
.listen((connectivity) async {
if (connectivity == ConnectivityResult.none) {
Logs().e(
'DownloadManager::download(): No internet connectivity',
);
streamController.add(
Left(
DownloadFileFailureState(
exception: Exception(
'No internet connectivity',
),
),
),
);
cancelDownload(event.eventId);
return;
}
});
}

void _addTaskToWorkerQueue({
Expand Down

0 comments on commit 3934039

Please sign in to comment.