Skip to content

Commit

Permalink
TW-746: add progress for video download
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Nov 3, 2023
1 parent 177ff6f commit 967a07e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 75 deletions.
14 changes: 7 additions & 7 deletions lib/data/model/download_file_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class DownloadFileResponse extends Response<dynamic> with EquatableMixin {

@override
List<Object?> get props => [
super.statusCode,
super.statusMessage,
super.data,
super.extra,
super.headers,
super.isRedirect,
super.requestOptions,
statusCode,
statusMessage,
data,
extra,
headers,
isRedirect,
requestOptions,
savePath,
onReceiveProgress,
requestOptions,
Expand Down
33 changes: 24 additions & 9 deletions lib/pages/chat/events/event_video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/pages/chat/events/download_video_state.dart';
import 'package:fluffychat/pages/chat/events/message_content_style.dart';
import 'package:fluffychat/pages/chat_details/chat_details_page_view/media/chat_details_media_style.dart';
import 'package:fluffychat/presentation/mixins/handle_video_download_mixin.dart';
import 'package:fluffychat/presentation/mixins/play_video_action_mixin.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/utils/twake_snackbar.dart';
Expand Down Expand Up @@ -29,8 +30,6 @@ class EventVideoPlayer extends StatefulWidget {

final bool showDuration;

final DownloadVideoEventCallback? handleDownloadVideoEvent;

final String? thumbnailCacheKey;

final Map<EventId, ImageData>? thumbnailCacheMap;
Expand All @@ -43,7 +42,6 @@ class EventVideoPlayer extends StatefulWidget {
Key? key,
this.width,
this.height,
this.handleDownloadVideoEvent,
this.rounded = true,
this.showDuration = false,
this.thumbnailCacheMap,
Expand All @@ -56,14 +54,25 @@ class EventVideoPlayer extends StatefulWidget {
}

class EventVideoPlayerState extends State<EventVideoPlayer>
with PlayVideoActionMixin {
with HandleVideoDownloadMixin, PlayVideoActionMixin {
final _downloadStateNotifier = ValueNotifier(DownloadVideoState.initial);
String? path;
final downloadProgressNotifier = ValueNotifier(0.0);

void _downloadAction() async {
_downloadStateNotifier.value = DownloadVideoState.loading;
try {
path = await widget.handleDownloadVideoEvent?.call(widget.event);
path = await handleDownloadVideoEvent(
event: widget.event,
playVideoAction: (path) => playVideoAction(
context,
path,
eventId: widget.event.eventId,
),
progressCallback: (count, total) {
downloadProgressNotifier.value = count / total;
},
);
_downloadStateNotifier.value = DownloadVideoState.done;
} on MatrixConnectionException catch (e) {
_downloadStateNotifier.value = DownloadVideoState.failed;
Expand Down Expand Up @@ -130,10 +139,16 @@ class EventVideoPlayerState extends State<EventVideoPlayer>
SizedBox(
width: MessageContentStyle.videoCenterButtonSize,
height: MessageContentStyle.videoCenterButtonSize,
child: CircularProgressIndicator(
strokeWidth: 2,
color:
LinagoraRefColors.material().primary[100],
child: ValueListenableBuilder(
valueListenable: downloadProgressNotifier,
builder: (context, progress, child) {
return CircularProgressIndicator(
strokeWidth: 2,
color: LinagoraRefColors.material()
.primary[100],
value: progress,
);
},
),
),
],
Expand Down
17 changes: 1 addition & 16 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:fluffychat/pages/chat/events/redacted_content.dart';
import 'package:fluffychat/pages/chat/events/sending_image_info_widget.dart';
import 'package:fluffychat/pages/chat/events/sending_video_widget.dart';
import 'package:fluffychat/pages/chat/events/unknown_content.dart';
import 'package:fluffychat/presentation/mixins/play_video_action_mixin.dart';
import 'package:fluffychat/presentation/model/file/display_image_info.dart';
import 'package:fluffychat/utils/extension/image_size_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
Expand All @@ -29,7 +28,7 @@ import 'map_bubble.dart';
import 'message_download_content.dart';
import 'sticker.dart';

class MessageContent extends StatelessWidget with PlayVideoActionMixin {
class MessageContent extends StatelessWidget {
final Event event;
final Color textColor;
final Widget endOfBubbleWidget;
Expand Down Expand Up @@ -92,16 +91,6 @@ class MessageContent extends StatelessWidget with PlayVideoActionMixin {
return _MessageVideoBuilder(
event: event,
onFileTapped: controller.onFileTapped,
handleDownloadVideoEvent: (event) {
return controller.handleDownloadVideoEvent(
event: event,
playVideoAction: (path) => playVideoAction(
context,
path,
eventId: event.eventId,
),
);
},
);
case MessageTypes.File:
return Column(
Expand Down Expand Up @@ -328,12 +317,9 @@ class _MessageVideoBuilder extends StatelessWidget {

final void Function(Event event) onFileTapped;

final DownloadVideoEventCallback handleDownloadVideoEvent;

const _MessageVideoBuilder({
required this.event,
required this.onFileTapped,
required this.handleDownloadVideoEvent,
});

@override
Expand Down Expand Up @@ -365,7 +351,6 @@ class _MessageVideoBuilder extends StatelessWidget {
}
return EventVideoPlayer(
event,
handleDownloadVideoEvent: handleDownloadVideoEvent,
width: displayImageInfo.size.width,
height: displayImageInfo.size.height,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class _VideoItem extends StatelessWidget {
Widget build(BuildContext context) {
return EventVideoPlayer(
event,
handleDownloadVideoEvent: handleDownloadVideoEvent,
rounded: false,
showDuration: true,
thumbnailCacheKey: event.eventId,
Expand Down
6 changes: 1 addition & 5 deletions lib/utils/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ extension StringCasingExtension on String {
if (length < maxCharacters) return this;
return substring(0, maxCharacters);
}

String substringToHighlight(String highlightText, {int prefixLength = 0}) {
if (prefixLength < 0) return this;
final index = toLowerCase().indexOf(highlightText.toLowerCase());
Expand All @@ -306,8 +306,4 @@ extension StringCasingExtension on String {
}
return this;
}

String getFileNameFromPath() {
return split('/').last;
}
}
95 changes: 61 additions & 34 deletions lib/widgets/mxc_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,40 +271,67 @@ class _MxcImageState extends State<MxcImage>
borderRadius: widget.rounded
? BorderRadius.circular(12.0)
: BorderRadius.zero,
child: filePath != null && filePath!.isNotEmpty
? Image.file(
File(filePath!),
width: widget.width,
height: widget.height,
cacheWidth: needResize ? widget.width?.toInt() : null,
cacheHeight: needResize ? widget.height?.toInt() : null,
fit: widget.fit,
filterQuality: FilterQuality.medium,
errorBuilder: (context, __, ___) {
_isCached = false;
filePath = null;
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
return placeholder(context);
},
)
: data != null
? Image.memory(
data,
width: widget.width,
height: widget.height,
cacheWidth: needResize ? widget.width?.toInt() : null,
cacheHeight: needResize ? widget.height?.toInt() : null,
fit: widget.fit,
filterQuality: FilterQuality.medium,
errorBuilder: (context, __, ___) {
_isCached = false;
_imageData = null;
WidgetsBinding.instance
.addPostFrameCallback(_tryLoad);
return placeholder(context);
},
)
: const SizedBox.shrink(),
child: _ImageWidget(
filePath: filePath,
data: data,
width: widget.width,
height: widget.height,
fit: widget.fit,
needResize: needResize,
imageErrorWidgetBuilder: (context, __, ___) {
_isCached = false;
_imageData = null;
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
return placeholder(context);
},
),
);
}
}

class _ImageWidget extends StatelessWidget {
final String? filePath;
final Uint8List? data;
final double? width;
final double? height;
final bool needResize;
final BoxFit? fit;
final ImageErrorWidgetBuilder imageErrorWidgetBuilder;

const _ImageWidget({
this.filePath,
this.data,
this.width,
this.height,
required this.needResize,
this.fit,
required this.imageErrorWidgetBuilder,
});

@override
Widget build(BuildContext context) {
return filePath != null && filePath!.isNotEmpty
? Image.file(
File(filePath!),
width: width,
height: height,
cacheWidth: needResize ? width?.toInt() : null,
cacheHeight: needResize ? height?.toInt() : null,
fit: fit,
filterQuality: FilterQuality.medium,
errorBuilder: imageErrorWidgetBuilder,
)
: data != null
? Image.memory(
data!,
width: width,
height: height,
cacheWidth: needResize ? width?.toInt() : null,
cacheHeight: needResize ? height?.toInt() : null,
fit: fit,
filterQuality: FilterQuality.medium,
errorBuilder: imageErrorWidgetBuilder,
)
: const SizedBox.shrink();
}
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1566,8 +1566,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "feat/support-get-store-fileEntity"
resolved-ref: c4d139570f8126d4cd8b6f24fcae5f09f70a1329
ref: "twake-supported-0.22.4"
resolved-ref: "1cb0e2d896f9adf8787398a7bebd5c0f6e7345e5"
url: "[email protected]:linagora/matrix-dart-sdk.git"
source: git
version: "0.22.4"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dependencies:
matrix:
git:
url: [email protected]:linagora/matrix-dart-sdk.git
ref: feat/support-get-store-fileEntity
ref: twake-supported-0.22.4
matrix_homeserver_recommendations: ^0.3.0
matrix_link_text: ^2.0.0
native_imaging: ^0.1.0
Expand Down

0 comments on commit 967a07e

Please sign in to comment.