Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tw 1702/hotfix error handling on download #1814

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ prime
.pub-cache/
.pub/
/build/
**/*.mocks.dart

# Web related
docs/build/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class ChatDetailsFilesView extends StatelessWidget {
return ValueListenableBuilder(
valueListenable: controller.downloadFileStateNotifier,
builder: (context, DownloadPresentationState state, child) {
if (state is DownloadingPresentationState) {
if (state is DownloadingPresentationState ||
state is DownloadErrorPresentationState) {
return ChatDetailsDownloadingFileTile(
mimeType: controller.event.mimeType,
fileType: filetype,
Expand All @@ -37,6 +38,7 @@ class ChatDetailsFilesView extends StatelessWidget {
controller.downloadManager
.cancelDownload(controller.event.eventId);
},
hasError: state is DownloadErrorPresentationState,
);
} else if (state is DownloadedPresentationState) {
return ChatDetailsDownloadedFileTile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ChatDetailsFilesViewWeb extends StatelessWidget {
return ValueListenableBuilder(
valueListenable: controller.downloadFileStateNotifier,
builder: (context, DownloadPresentationState state, child) {
if (state is DownloadingPresentationState) {
if (state is DownloadingPresentationState ||
state is DownloadErrorPresentationState) {
return ChatDetailsFileTileRowDownloadingWeb(
mimeType: controller.event.mimeType,
fileType: filetype,
Expand All @@ -36,6 +37,7 @@ class ChatDetailsFilesViewWeb extends StatelessWidget {
controller.downloadManager
.cancelDownload(controller.event.eventId);
},
hasError: state is DownloadErrorPresentationState,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ChatDetailsDownloadingFileTile extends StatelessWidget {
required this.fileType,
required this.sizeString,
required this.downloadFileStateNotifier,
this.hasError = false,
});

final GestureTapCallback onTap;
Expand All @@ -22,13 +23,15 @@ class ChatDetailsDownloadingFileTile extends StatelessWidget {
final String? fileType;
final String? sizeString;
final ValueNotifier<DownloadPresentationState> downloadFileStateNotifier;
final bool hasError;

@override
Widget build(BuildContext context) {
return InkWell(
hoverColor: LinagoraSysColors.material().surfaceVariant,
onTap: onTap,
child: ChatDetailsFileRowDownloadingWrapper(
hasError: hasError,
mimeType: mimeType,
fileType: fileType,
downloadFileStateNotifier: downloadFileStateNotifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ChatDetailsFileTileRowDownloadingWeb extends StatelessWidget {
required this.sizeString,
required this.onTap,
required this.downloadFileStateNotifier,
this.hasError = false,
});

final GestureTapCallback onTap;
Expand All @@ -26,13 +27,15 @@ class ChatDetailsFileTileRowDownloadingWeb extends StatelessWidget {
final String? fileType;
final DateTime sentDate;
final ValueNotifier<DownloadPresentationState> downloadFileStateNotifier;
final bool hasError;

@override
Widget build(BuildContext context) {
return InkWell(
hoverColor: LinagoraSysColors.material().surfaceVariant,
onTap: onTap,
child: ChatDetailsFileRowDownloadingWrapper(
hasError: hasError,
mimeType: mimeType,
fileType: fileType,
downloadFileStateNotifier: downloadFileStateNotifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class ChatDetailsFileRowDownloadingWrapper extends StatelessWidget {
final String? mimeType;
final String? fileType;
final ValueNotifier<DownloadPresentationState> downloadFileStateNotifier;
final bool hasError;

const ChatDetailsFileRowDownloadingWrapper({
super.key,
required this.child,
required this.mimeType,
required this.fileType,
required this.downloadFileStateNotifier,
this.hasError = false,
});

final style = const MessageFileTileStyle();
Expand Down Expand Up @@ -50,11 +52,14 @@ class ChatDetailsFileRowDownloadingWrapper extends StatelessWidget {
width: style.iconSize,
height: style.iconSize,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
color: style.iconBackgroundColor(
hasError: hasError,
context: context,
),
shape: BoxShape.circle,
),
),
if (downloadProgress != 0)
if (downloadProgress != 0 && !hasError)
SizedBox(
width: style.circularProgressLoadingSize,
height: style.circularProgressLoadingSize,
Expand All @@ -66,11 +71,18 @@ class ChatDetailsFileRowDownloadingWrapper extends StatelessWidget {
Container(
width: style.downloadIconSize,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
color: style.iconBackgroundColor(
hasError: hasError,
context: context,
),
shape: BoxShape.circle,
),
child: Icon(
downloadProgress == 0 ? Icons.arrow_downward : Icons.close,
hasError
? Icons.error_outline
: downloadProgress == 0
? Icons.arrow_downward
: Icons.close,
key: ValueKey(downloadProgress),
color: Theme.of(context).colorScheme.surface,
size: style.downloadIconSize,
Expand Down
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
20 changes: 15 additions & 5 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 Down Expand Up @@ -71,10 +72,13 @@ mixin DownloadFileOnMobileMixin<T extends StatefulWidget> on State<T> {
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 +93,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
12 changes: 10 additions & 2 deletions lib/widgets/mixins/download_file_on_web_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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/download_manager/download_manager.dart';
import 'package:fluffychat/widgets/twake_app.dart';
Expand Down Expand Up @@ -44,8 +45,15 @@ mixin DownloadFileOnWebMixin<T extends StatefulWidget> on State<T> {
void setupDownloadingProcess(Either<Failure, Success> resultEvent) {
resultEvent.fold(
(failure) {
Logs().e('$T::onDownloadingProcess(): $failure');
downloadFileStateNotifier.value = const NotDownloadPresentationState();
Logs().e('MessageDownloadContentWeb::onDownloadingProcess(): $failure');
if (failure is DownloadFileFailureState &&
failure.exception is CancelDownloadingException) {
downloadFileStateNotifier.value =
const NotDownloadPresentationState();
} else {
downloadFileStateNotifier.value =
DownloadErrorPresentationState(error: failure);
}
},
(success) {
if (success is DownloadingFileState) {
Expand Down
Loading