Skip to content

Commit

Permalink
TW-1702: DownloadErrorPresentationState added handle errors (#1714)
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z authored Apr 25, 2024
1 parent 090e0fd commit cb0bb96
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
14 changes: 12 additions & 2 deletions lib/pages/chat/events/message_download_content.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/download_manager/download_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/download_file_extension.dart';
Expand Down Expand Up @@ -95,7 +96,14 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
event.fold(
(failure) {
Logs().e('MessageDownloadContent::onDownloadingProcess(): $failure');
downloadFileStateNotifier.value = const NotDownloadPresentationState();
if (failure is DownloadFileFailureState &&
failure.exception is CancelDownloadingException) {
downloadFileStateNotifier.value =
const NotDownloadPresentationState();
} else {
downloadFileStateNotifier.value =
DownloadErrorPresentationState(error: failure);
}
streamSubscription?.cancel();
},
(success) {
Expand Down Expand Up @@ -159,7 +167,8 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
style: const MessageFileTileStyle(),
),
);
} else if (state is DownloadingPresentationState) {
} else if (state is DownloadingPresentationState ||
state is DownloadErrorPresentationState) {
return DownloadFileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
Expand All @@ -173,6 +182,7 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>
const NotDownloadPresentationState();
downloadManager.cancelDownload(widget.event.eventId);
},
hasError: state is DownloadErrorPresentationState,
);
}

Expand Down
16 changes: 13 additions & 3 deletions lib/pages/chat/events/message_download_content_web.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/utils/matrix_sdk_extensions/event_extension.dart';
Expand Down Expand Up @@ -59,8 +60,15 @@ class _MessageDownloadContentWebState extends State<MessageDownloadContentWeb>
void setupDownloadingProcess(Either<Failure, Success> event) {
event.fold(
(failure) {
Logs().e('MessageDownloadContent::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 Expand Up @@ -111,7 +119,8 @@ class _MessageDownloadContentWebState extends State<MessageDownloadContentWeb>
return ValueListenableBuilder(
valueListenable: downloadFileStateNotifier,
builder: (context, DownloadPresentationState state, child) {
if (state is DownloadingPresentationState) {
if (state is DownloadingPresentationState ||
state is DownloadErrorPresentationState) {
return DownloadFileTileWidget(
mimeType: widget.event.mimeType,
fileType: filetype,
Expand All @@ -125,6 +134,7 @@ class _MessageDownloadContentWebState extends State<MessageDownloadContentWeb>
const NotDownloadPresentationState();
downloadManager.cancelDownload(widget.event.eventId);
},
hasError: state is DownloadErrorPresentationState,
);
} else if (state is FileWebDownloadedPresentationState) {
return InkWell(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ class DownloadingPresentationState extends DownloadPresentationState {
@override
List<Object?> get props => [receive, total];
}

class DownloadErrorPresentationState extends DownloadPresentationState {
final dynamic error;

const DownloadErrorPresentationState({required this.error});

@override
List<Object?> get props => [error];
}
5 changes: 5 additions & 0 deletions lib/utils/matrix_sdk_extensions/download_file_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ extension DownloadFileExtension on Event {
Logs().i("downloadOrRetrieveAttachment: duplicate request");
} else {
Logs().e("downloadOrRetrieveAttachment: $e");
downloadStreamController?.add(
Left(
DownloadFileFailureState(exception: e),
),
);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ extension DownloadFileWebExtension on Event {
Logs().i("_handleDownloadFileWeb: user cancel the download");
}
Logs().e("_handleDownloadFileWeb: $e");
downloadStreamController.add(
Left(
DownloadFileFailureState(exception: e),
),
);
}
return null;
}
Expand Down
22 changes: 16 additions & 6 deletions lib/widgets/file_widget/download_file_tile_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DownloadFileTileWidget extends StatelessWidget {
this.sizeString,
required this.downloadFileStateNotifier,
this.onCancelDownload,
this.hasError = false,
});

final TwakeMimeType mimeType;
Expand All @@ -26,6 +27,7 @@ class DownloadFileTileWidget extends StatelessWidget {
final String? fileType;
final ValueNotifier<DownloadPresentationState> downloadFileStateNotifier;
final VoidCallback? onCancelDownload;
final bool hasError;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -63,11 +65,14 @@ class DownloadFileTileWidget 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 @@ -81,13 +86,18 @@ class DownloadFileTileWidget extends StatelessWidget {
child: 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
8 changes: 8 additions & 0 deletions lib/widgets/file_widget/message_file_tile_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ class MessageFileTileStyle extends FileTileWidgetStyle {
double get downloadIconSize => 28;

EdgeInsets get marginDownloadIcon => const EdgeInsets.all(4);

Color iconBackgroundColor({
required bool hasError,
required BuildContext context,
}) =>
hasError
? Theme.of(context).colorScheme.error
: Theme.of(context).colorScheme.primary;
}

0 comments on commit cb0bb96

Please sign in to comment.