Skip to content

Commit

Permalink
TW-1702: regression fix + tests added for download on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z committed May 28, 2024
1 parent 3d348cd commit 6cd553c
Show file tree
Hide file tree
Showing 5 changed files with 1,343 additions and 19 deletions.
24 changes: 12 additions & 12 deletions lib/utils/manager/download_manager/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class DownloadManager {

final workingQueue = getIt.get<DownloadWorkerQueue>();

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

void cancelDownload(String eventId) {
final cancelToken = _eventIdMapDownloadFileInfo[eventId]?.cancelToken;
final cancelToken = eventIdMapDownloadFileInfo[eventId]?.cancelToken;
if (cancelToken != null) {
try {
cancelToken.cancel();
_eventIdMapDownloadFileInfo[eventId]?.downloadStateStreamController.add(
eventIdMapDownloadFileInfo[eventId]?.downloadStateStreamController.add(
Left(
DownloadFileFailureState(
exception: CancelDownloadingException(),
Expand All @@ -44,7 +44,7 @@ class DownloadManager {
Logs().e(
'DownloadManager::cancelDownload(): $e',
);
_eventIdMapDownloadFileInfo[eventId]?.downloadStateStreamController.add(
eventIdMapDownloadFileInfo[eventId]?.downloadStateStreamController.add(
Left(
DownloadFileFailureState(exception: e),
),
Expand All @@ -60,7 +60,7 @@ class DownloadManager {
) {
final streamController = StreamController<Either<Failure, Success>>();

_eventIdMapDownloadFileInfo[event.eventId] = DownloadFileInfo(
eventIdMapDownloadFileInfo[event.eventId] = DownloadFileInfo(
eventId: event.eventId,
cancelToken: CancelToken(),
downloadStateStreamController: streamController,
Expand All @@ -69,25 +69,25 @@ class DownloadManager {
}

Stream<Either<Failure, Success>>? getDownloadStateStream(String eventId) {
return _eventIdMapDownloadFileInfo[eventId]?.downloadStream;
return eventIdMapDownloadFileInfo[eventId]?.downloadStream;
}

Future<void> clear(String eventId) async {
try {
await _eventIdMapDownloadFileInfo[eventId]
await eventIdMapDownloadFileInfo[eventId]
?.downloadStateStreamController
.close();
} catch (e) {
Logs().e(
'DownloadManager::_clear(): $e',
);
_eventIdMapDownloadFileInfo[eventId]?.downloadStateStreamController.add(
eventIdMapDownloadFileInfo[eventId]?.downloadStateStreamController.add(
Left(
DownloadFileFailureState(exception: e),
),
);
} finally {
_eventIdMapDownloadFileInfo.remove(eventId);
eventIdMapDownloadFileInfo.remove(eventId);
Logs().i(
'DownloadManager::clear with $eventId successfully',
);
Expand All @@ -100,14 +100,14 @@ class DownloadManager {
bool isFirstPriority = false,
}) async {
_initDownloadFileInfo(event);
final streamController = _eventIdMapDownloadFileInfo[event.eventId]
final streamController = eventIdMapDownloadFileInfo[event.eventId]
?.downloadStateStreamController;
final cancelToken = _eventIdMapDownloadFileInfo[event.eventId]?.cancelToken;
final cancelToken = eventIdMapDownloadFileInfo[event.eventId]?.cancelToken;
if (streamController == null || cancelToken == null) {
Logs().e(
'DownloadManager::download(): streamController or cancelToken is null',
);
_eventIdMapDownloadFileInfo[event.eventId]
eventIdMapDownloadFileInfo[event.eventId]
?.downloadStateStreamController
.add(
Left(
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/manager/storage_directory_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class StorageDirectoryManager {

static StorageDirectoryManager get instance => _instance;

factory StorageDirectoryManager() => _instance;

Future<String> getFileStoreDirectory() async {
try {
try {
Expand Down
25 changes: 18 additions & 7 deletions lib/widgets/mixins/download_file_on_mobile_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/presentation/model/chat/downloading_state_presentation_model.dart';
import 'package:fluffychat/utils/exception/downloading_exception.dart';
import 'package:fluffychat/utils/manager/download_manager/download_file_state.dart';
import 'package:fluffychat/utils/manager/storage_directory_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/download_file_extension.dart';
Expand All @@ -17,6 +18,8 @@ import 'package:matrix/matrix.dart';
mixin DownloadFileOnMobileMixin<T extends StatefulWidget> on State<T> {
final downloadManager = getIt.get<DownloadManager>();

final storageDirectoryManager = StorageDirectoryManager();

final downloadFileStateNotifier = ValueNotifier<DownloadPresentationState>(
const NotDownloadPresentationState(),
);
Expand Down Expand Up @@ -66,15 +69,17 @@ mixin DownloadFileOnMobileMixin<T extends StatefulWidget> on State<T> {
}

Future<void> checkFileInDownloadsInApp() async {
final filePath =
await StorageDirectoryManager.instance.getFilePathInAppDownloads(
final filePath = await storageDirectoryManager.getFilePathInAppDownloads(
eventId: event.eventId,
fileName: event.filename,
);
final file = File(filePath);
updateStateIfFileExists(File(filePath));
}

Future<void> updateStateIfFileExists(File file) async {
if (await file.exists() && await file.length() == event.getFileSize()) {
downloadFileStateNotifier.value = DownloadedPresentationState(
filePath: filePath,
filePath: file.path,
);
return;
}
Expand All @@ -89,9 +94,15 @@ mixin DownloadFileOnMobileMixin<T extends StatefulWidget> on State<T> {
void setupDownloadingProcess(Either<Failure, Success> resultEvent) {
resultEvent.fold(
(failure) {
Logs()
.e('$T::setupDownloadingProcess::onDownloadingProcess(): $failure');
downloadFileStateNotifier.value = const NotDownloadPresentationState();
Logs().e('MessageDownloadContent::onDownloadingProcess(): $failure');
if (failure is DownloadFileFailureState &&
failure.exception is CancelDownloadingException) {
downloadFileStateNotifier.value =
const NotDownloadPresentationState();
} else {
downloadFileStateNotifier.value =
DownloadErrorPresentationState(error: failure);
}
streamSubscription?.cancel();
},
(success) {
Expand Down
Loading

0 comments on commit 6cd553c

Please sign in to comment.