Skip to content

Commit

Permalink
Minor improvement (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k authored Dec 24, 2024
1 parent 3d691cc commit a4e3df4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
63 changes: 28 additions & 35 deletions app/lib/ui/flow/media_preview/media_preview_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {

if (widget.medias[currentIndex].type.isVideo &&
widget.medias[currentIndex].sources.contains(AppMediaSource.local)) {
runPostFrame(
() => _initializeVideoControllerWithListener(
runPostFrame(() {
_initializeVideoControllerWithListener(
path: widget.medias[currentIndex].path,
),
);
);
_notifier.updateInitializedVideoPath(widget.medias[currentIndex].path);
});
}
}

Expand Down Expand Up @@ -130,13 +131,15 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {
if (_videoPlayerController != null) {
_videoPlayerController?.removeListener(_observeVideoController);
_notifier.updateVideoInitialized(false);
_notifier.updateInitializedVideoPath(null);
_videoPlayerController?.dispose();
_videoPlayerController = null;
}
if (next != null &&
next.type.isVideo &&
next.sources.contains(AppMediaSource.local)) {
_initializeVideoControllerWithListener(path: next.path);
_notifier.updateInitializedVideoPath(next.path);
}
});
}
Expand Down Expand Up @@ -233,18 +236,26 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {
_provider.select(
(state) => (
initialized: state.isVideoInitialized,
buffring: state.isVideoBuffering
buffring: state.isVideoBuffering,
initializedVideoPath: state.initializedVideoPath,
),
),
);

if (!state.initialized) {
return Stack(
return Hero(
tag: media,
child: Stack(
alignment: Alignment.center,
children: [
Hero(
tag: media,
child: Image(
if (state.initialized &&
media.path == state.initializedVideoPath)
AspectRatio(
aspectRatio: _videoPlayerController!.value.aspectRatio,
child: VideoPlayer(_videoPlayerController!),
),
if (!state.initialized ||
media.path != state.initializedVideoPath)
Image(
image: AppMediaImageProvider(media: media),
width: double.infinity,
fit: BoxFit.contain,
Expand Down Expand Up @@ -279,23 +290,9 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {
}
},
),
),
AppCircularProgressIndicator(
color: context.colorScheme.onPrimary,
),
],
);
}
return Hero(
tag: media,
child: Stack(
alignment: Alignment.center,
children: [
AspectRatio(
aspectRatio: _videoPlayerController!.value.aspectRatio,
child: VideoPlayer(_videoPlayerController!),
),
if (state.buffring)
if (state.buffring ||
!state.initialized &&
media.path == state.initializedVideoPath)
AppCircularProgressIndicator(
color: context.colorScheme.onPrimary,
),
Expand Down Expand Up @@ -380,11 +377,7 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {

Widget _videoActions(BuildContext context) => Consumer(
builder: (context, ref, child) {
final ({
bool showActions,
bool isPlaying,
Duration position,
}) state = ref.watch(
final state = ref.watch(
_provider.select(
(state) => (
showActions: state.showActions &&
Expand All @@ -395,14 +388,15 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {
true &&
state.isVideoInitialized,
isPlaying: state.isVideoPlaying,
isInitialized: state.isVideoInitialized,
position: state.videoPosition,
),
),
);

return VideoActions(
showActions: state.showActions,
isPlaying: state.isPlaying,
isPlaying: state.isPlaying || !state.isInitialized,
onBackward: () {
_videoPlayerController
?.seekTo(state.position - const Duration(seconds: 10));
Expand Down Expand Up @@ -436,8 +430,7 @@ class _MediaPreviewState extends ConsumerState<MediaPreview> {
.elementAtOrNull(state.currentIndex)
?.type
.isVideo ==
true &&
state.isVideoInitialized,
true,
duration: state.videoMaxDuration,
position: state.videoPosition
),
Expand Down
5 changes: 5 additions & 0 deletions app/lib/ui/flow/media_preview/media_preview_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ class MediaPreviewStateNotifier extends StateNotifier<MediaPreviewState> {
state = state.copyWith(isVideoInitialized: isInitialized);
}

void updateInitializedVideoPath(String? path) {
state = state.copyWith(initializedVideoPath: path);
}

void updateVideoMaxDuration(Duration maxDuration) {
if (state.videoMaxDuration == maxDuration) return;
state = state.copyWith(videoMaxDuration: maxDuration);
Expand Down Expand Up @@ -470,6 +474,7 @@ class MediaPreviewState with _$MediaPreviewState {
@Default(0.0) double swipeDownPercentage,
@Default(Duration.zero) Duration videoPosition,
@Default(Duration.zero) Duration videoMaxDuration,
String? initializedVideoPath,
@Default(false) bool isVideoPlaying,
@Default({}) Map<String, UploadMediaProcess> uploadMediaProcesses,
@Default({}) Map<String, DownloadMediaProcess> downloadMediaProcesses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mixin _$MediaPreviewState {
double get swipeDownPercentage => throw _privateConstructorUsedError;
Duration get videoPosition => throw _privateConstructorUsedError;
Duration get videoMaxDuration => throw _privateConstructorUsedError;
String? get initializedVideoPath => throw _privateConstructorUsedError;
bool get isVideoPlaying => throw _privateConstructorUsedError;
Map<String, UploadMediaProcess> get uploadMediaProcesses =>
throw _privateConstructorUsedError;
Expand Down Expand Up @@ -62,6 +63,7 @@ abstract class $MediaPreviewStateCopyWith<$Res> {
double swipeDownPercentage,
Duration videoPosition,
Duration videoMaxDuration,
String? initializedVideoPath,
bool isVideoPlaying,
Map<String, UploadMediaProcess> uploadMediaProcesses,
Map<String, DownloadMediaProcess> downloadMediaProcesses});
Expand Down Expand Up @@ -97,6 +99,7 @@ class _$MediaPreviewStateCopyWithImpl<$Res, $Val extends MediaPreviewState>
Object? swipeDownPercentage = null,
Object? videoPosition = null,
Object? videoMaxDuration = null,
Object? initializedVideoPath = freezed,
Object? isVideoPlaying = null,
Object? uploadMediaProcesses = null,
Object? downloadMediaProcesses = null,
Expand Down Expand Up @@ -148,6 +151,10 @@ class _$MediaPreviewStateCopyWithImpl<$Res, $Val extends MediaPreviewState>
? _value.videoMaxDuration
: videoMaxDuration // ignore: cast_nullable_to_non_nullable
as Duration,
initializedVideoPath: freezed == initializedVideoPath
? _value.initializedVideoPath
: initializedVideoPath // ignore: cast_nullable_to_non_nullable
as String?,
isVideoPlaying: null == isVideoPlaying
? _value.isVideoPlaying
: isVideoPlaying // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -200,6 +207,7 @@ abstract class _$$MediaPreviewStateImplCopyWith<$Res>
double swipeDownPercentage,
Duration videoPosition,
Duration videoMaxDuration,
String? initializedVideoPath,
bool isVideoPlaying,
Map<String, UploadMediaProcess> uploadMediaProcesses,
Map<String, DownloadMediaProcess> downloadMediaProcesses});
Expand Down Expand Up @@ -234,6 +242,7 @@ class __$$MediaPreviewStateImplCopyWithImpl<$Res>
Object? swipeDownPercentage = null,
Object? videoPosition = null,
Object? videoMaxDuration = null,
Object? initializedVideoPath = freezed,
Object? isVideoPlaying = null,
Object? uploadMediaProcesses = null,
Object? downloadMediaProcesses = null,
Expand Down Expand Up @@ -285,6 +294,10 @@ class __$$MediaPreviewStateImplCopyWithImpl<$Res>
? _value.videoMaxDuration
: videoMaxDuration // ignore: cast_nullable_to_non_nullable
as Duration,
initializedVideoPath: freezed == initializedVideoPath
? _value.initializedVideoPath
: initializedVideoPath // ignore: cast_nullable_to_non_nullable
as String?,
isVideoPlaying: null == isVideoPlaying
? _value.isVideoPlaying
: isVideoPlaying // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -318,6 +331,7 @@ class _$MediaPreviewStateImpl implements _MediaPreviewState {
this.swipeDownPercentage = 0.0,
this.videoPosition = Duration.zero,
this.videoMaxDuration = Duration.zero,
this.initializedVideoPath,
this.isVideoPlaying = false,
final Map<String, UploadMediaProcess> uploadMediaProcesses = const {},
final Map<String, DownloadMediaProcess> downloadMediaProcesses =
Expand Down Expand Up @@ -368,6 +382,8 @@ class _$MediaPreviewStateImpl implements _MediaPreviewState {
@JsonKey()
final Duration videoMaxDuration;
@override
final String? initializedVideoPath;
@override
@JsonKey()
final bool isVideoPlaying;
final Map<String, UploadMediaProcess> _uploadMediaProcesses;
Expand All @@ -392,7 +408,7 @@ class _$MediaPreviewStateImpl implements _MediaPreviewState {

@override
String toString() {
return 'MediaPreviewState(googleAccount: $googleAccount, dropboxAccount: $dropboxAccount, error: $error, actionError: $actionError, medias: $medias, currentIndex: $currentIndex, showActions: $showActions, isVideoInitialized: $isVideoInitialized, isVideoBuffering: $isVideoBuffering, isImageZoomed: $isImageZoomed, swipeDownPercentage: $swipeDownPercentage, videoPosition: $videoPosition, videoMaxDuration: $videoMaxDuration, isVideoPlaying: $isVideoPlaying, uploadMediaProcesses: $uploadMediaProcesses, downloadMediaProcesses: $downloadMediaProcesses)';
return 'MediaPreviewState(googleAccount: $googleAccount, dropboxAccount: $dropboxAccount, error: $error, actionError: $actionError, medias: $medias, currentIndex: $currentIndex, showActions: $showActions, isVideoInitialized: $isVideoInitialized, isVideoBuffering: $isVideoBuffering, isImageZoomed: $isImageZoomed, swipeDownPercentage: $swipeDownPercentage, videoPosition: $videoPosition, videoMaxDuration: $videoMaxDuration, initializedVideoPath: $initializedVideoPath, isVideoPlaying: $isVideoPlaying, uploadMediaProcesses: $uploadMediaProcesses, downloadMediaProcesses: $downloadMediaProcesses)';
}

@override
Expand Down Expand Up @@ -424,6 +440,8 @@ class _$MediaPreviewStateImpl implements _MediaPreviewState {
other.videoPosition == videoPosition) &&
(identical(other.videoMaxDuration, videoMaxDuration) ||
other.videoMaxDuration == videoMaxDuration) &&
(identical(other.initializedVideoPath, initializedVideoPath) ||
other.initializedVideoPath == initializedVideoPath) &&
(identical(other.isVideoPlaying, isVideoPlaying) ||
other.isVideoPlaying == isVideoPlaying) &&
const DeepCollectionEquality()
Expand All @@ -448,6 +466,7 @@ class _$MediaPreviewStateImpl implements _MediaPreviewState {
swipeDownPercentage,
videoPosition,
videoMaxDuration,
initializedVideoPath,
isVideoPlaying,
const DeepCollectionEquality().hash(_uploadMediaProcesses),
const DeepCollectionEquality().hash(_downloadMediaProcesses));
Expand Down Expand Up @@ -477,6 +496,7 @@ abstract class _MediaPreviewState implements MediaPreviewState {
final double swipeDownPercentage,
final Duration videoPosition,
final Duration videoMaxDuration,
final String? initializedVideoPath,
final bool isVideoPlaying,
final Map<String, UploadMediaProcess> uploadMediaProcesses,
final Map<String, DownloadMediaProcess> downloadMediaProcesses}) =
Expand Down Expand Up @@ -509,6 +529,8 @@ abstract class _MediaPreviewState implements MediaPreviewState {
@override
Duration get videoMaxDuration;
@override
String? get initializedVideoPath;
@override
bool get isVideoPlaying;
@override
Map<String, UploadMediaProcess> get uploadMediaProcesses;
Expand Down

0 comments on commit a4e3df4

Please sign in to comment.