Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix: preview video state & similar controls

Prateek Sunal18ca1545783f
diff --git a/mobile/lib/models/preview/preview_item_status.dart b/mobile/lib/models/preview/preview_item_status.dart
index 5f99bc5356..507dd213ee 100644
--- a/mobile/lib/models/preview/preview_item_status.dart
+++ b/mobile/lib/models/preview/preview_item_status.dart
@@ -1,8 +1,12 @@
enum PreviewItemStatus {
+ // queued
inQueue,
+ retry,
+ // in progress
compressing,
uploading,
- retry,
- uploaded,
+ // error
failed,
+ // done
+ uploaded,
}
diff --git a/mobile/lib/ui/viewer/file/preview_status_widget.dart b/mobile/lib/ui/viewer/file/preview_status_widget.dart
new file mode 100644
index 0000000000..e292154d65
--- /dev/null
+++ b/mobile/lib/ui/viewer/file/preview_status_widget.dart
@@ -0,0 +1,162 @@
+import "dart:async";
+
+import "package:flutter/material.dart";
+import "package:photos/core/event_bus.dart";
+import "package:photos/events/preview_updated_event.dart";
+import "package:photos/models/file/file.dart";
+import "package:photos/models/preview/preview_item.dart";
+import "package:photos/models/preview/preview_item_status.dart";
+import "package:photos/services/filedata/filedata_service.dart";
+import "package:photos/services/preview_video_store.dart";
+import "package:photos/theme/colors.dart";
+
+class PreviewStatusWidget extends StatefulWidget {
+ const PreviewStatusWidget({
+ super.key,
+ required bool showControls,
+ required this.file,
+ this.isPreviewPlayer = false,
+ }) : _showControls = showControls;
+
+ final bool _showControls;
+ final EnteFile file;
+ final bool isPreviewPlayer;
+
+ @override
+ State<PreviewStatusWidget> createState() => _PreviewStatusWidgetState();
+}
+
+class _PreviewStatusWidgetState extends State<PreviewStatusWidget> {
+ StreamSubscription? previewSubscription;
+ late PreviewItem? preview =
+ PreviewVideoStore.instance.previews[widget.file.uploadedFileID];
+ late bool isVideoStreamingEnabled;
+
+ @override
+ void initState() {
+ super.initState();
+
+ isVideoStreamingEnabled =
+ PreviewVideoStore.instance.isVideoStreamingEnabled;
+ if (!isVideoStreamingEnabled) {
+ return;
+ }
+ previewSubscription =
+ Bus.instance.on<PreviewUpdatedEvent>().listen((event) {
+ final newPreview = event.items[widget.file.uploadedFileID];
+ if (newPreview != preview) {
+ setState(() {
+ preview = event.items[widget.file.uploadedFileID];
+ });
+ }
+ });
+ }
+
+ @override
+ void dispose() {
+ previewSubscription?.cancel();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ if (!isVideoStreamingEnabled) {
+ return const SizedBox();
+ }
+ final bool isPreviewAvailable = FileDataService.instance.previewIds
+ ?.containsKey(widget.file.uploadedFileID!) ??
+ false;
+
+ if (isPreviewAvailable && widget.file.localID != null) {
+ return const SizedBox();
+ }
+ final isInProgress = preview?.status == PreviewItemStatus.compressing ||
+ preview?.status == PreviewItemStatus.uploading;
+ final isInQueue = preview?.status == PreviewItemStatus.inQueue ||
+ preview?.status == PreviewItemStatus.retry;
+ final isFailed = preview?.status == PreviewItemStatus.failed;
+
+ return Align(
+ alignment: Alignment.centerRight,
+ child: AnimatedOpacity(
+ duration: const Duration(
+ milliseconds: 200,
+ ),
+ curve: Curves.easeInQuad,
+ opacity: widget._showControls ? 1 : 0,
+ child: Padding(
+ padding: const EdgeInsets.only(
+ right: 10,
+ bottom: 4,
+ ),
+ child: Container(
+ padding: const EdgeInsets.symmetric(
+ horizontal: 8,
+ vertical: 4,
+ ),
+ decoration: BoxDecoration(
+ color: isPreviewAvailable ? Colors.green : null,
+ borderRadius: const BorderRadius.all(
+ Radius.circular(200),
+ ),
+ border: isPreviewAvailable
+ ? null
+ : Border.all(
+ color: strokeFaintDark,
+ width: 1,
+ ),
+ ),
+ child: Row(
+ mainAxisAlignment: MainAxisAlignment.end,
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ !isInProgress
+ ? Icon(
+ !isPreviewAvailable
+ ? isInQueue
+ ? Icons.history_outlined
+ : !isFailed
+ ? Icons.block_outlined
+ : Icons.error_outline
+ : Icons.play_arrow,
+ size: 16,
+ )
+ : const SizedBox(
+ width: 12,
+ height: 12,
+ child: CircularProgressIndicator(
+ valueColor: AlwaysStoppedAnimation(
+ Colors.white,
+ ),
+ backgroundColor: Colors.transparent,
+ strokeWidth: 2,
+ ),
+ ),
+ SizedBox(
+ width: !isInProgress || isPreviewAvailable ? 2 : 6,
+ ),
+ Text(
+ !isPreviewAvailable
+ ? isInProgress
+ ? "Processing"
+ : isInQueue
+ ? "Queued"
+ : !isFailed
+ ? "Ineligible"
+ : "Failed"
+ : widget.isPreviewPlayer
+ ? "Play original"
+ : "Play stream",
+ style: const TextStyle(
+ fontSize: 12,
+ fontWeight: FontWeight.w600,
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/lib/ui/viewer/file/preview_video_widget.dart b/mobile/lib/ui/viewer/file/preview_video_widget.dart
index f1dfe8e7f2..fa8d487526 100644
--- a/mobile/lib/ui/viewer/file/preview_video_widget.dart
+++ b/mobile/lib/ui/viewer/file/preview_video_widget.dart
@@ -247,7 +247,7 @@ class _PreviewVideoWidgetState extends State<PreviewVideoWidget> {
looping: true,
allowMuting: true,
allowFullScreen: false,
- customControls: const VideoControls(),
+ customControls: VideoControls(file: widget.file),
);
return Container(
color: Colors.black,
diff --git a/mobile/lib/ui/viewer/file/video_control.dart b/mobile/lib/ui/viewer/file/video_control.dart
index 3ce9b66e84..51c68d08cb 100644
--- a/mobile/lib/ui/viewer/file/video_control.dart
+++ b/mobile/lib/ui/viewer/file/video_control.dart
@@ -1,14 +1,21 @@
import 'dart:async';
import 'package:chewie/chewie.dart';
-import 'package:chewie/src/material/material_progress_bar.dart';
import 'package:flutter/material.dart';
-import 'package:photos/ente_theme_data.dart';
-import 'package:photos/utils/date_time_util.dart';
+import "package:photos/models/file/file.dart";
+import "package:photos/theme/colors.dart";
+import "package:photos/theme/ente_theme.dart";
+import "package:photos/ui/common/loading_widget.dart";
+import "package:photos/ui/viewer/file/preview_status_widget.dart";
+import "package:photos/utils/debouncer.dart";
import 'package:video_player/video_player.dart';
class VideoControls extends StatefulWidget {
- const VideoControls({Key? key}) : super(key: key);
+ const VideoControls({
+ super.key,
+ required this.file,
+ });
+ final EnteFile file;
@override
State<StatefulWidget> createState() {
@@ -58,20 +65,39 @@ class _VideoControlsState extends State<VideoControls> {
absorbing: _hideStuff,
child: Stack(
children: <Widget>[
- Column(
- children: [
- _latestValue != null &&
- !_latestValue!.isPlaying &&
- _latestValue!.isBuffering
- ? const Center(
- child: CircularProgressIndicator(),
- )
- : _buildHitArea(),
- ],
- ),
+ if (_latestValue != null &&
+ !_latestValue!.isPlaying &&
+ _latestValue!.isBuffering)
+ const Align(
+ alignment: Alignment.center,
+ child: Center(
+ child: EnteLoadingWidget(
+ size: 32,
+ color: fillBaseDark,
+ padding: 0,
+ ),
+ ),
+ )
+ else
+ Positioned.fill(child: _buildHitArea()),
Align(
alignment: Alignment.bottomCenter,
- child: _buildBottomBar(context),
+ child: SafeArea(
+ top: false,
+ left: false,
+ right: false,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ PreviewStatusWidget(
+ showControls: !_hideStuff,
+ file: widget.file,
+ isPreviewPlayer: true,
+ ),
+ _buildBottomBar(context),
+ ],
+ ),
+ ),
),
],
),
@@ -110,100 +136,53 @@ class _VideoControlsState extends State<VideoControls> {
Widget _buildBottomBar(
BuildContext context,
) {
- final iconColor = Theme.of(context).textTheme.labelLarge!.color;
-
return Container(
padding: const EdgeInsets.only(bottom: 60),
+ height: 100,
child: AnimatedOpacity(
opacity: _hideStuff ? 0.0 : 1.0,
duration: const Duration(milliseconds: 300),
- child: Container(
- height: barHeight,
- color: Colors.transparent,
- child: Row(
- children: <Widget>[
- _buildCurrentPosition(iconColor),
- chewieController!.isLive ? const SizedBox() : _buildProgressBar(),
- _buildTotalDuration(iconColor),
- ],
- ),
+ child: _SeekBarAndDuration(
+ controller: controller,
+ latestValue: _latestValue,
+ updateDragging: (bool value) {
+ setState(() {
+ _dragging = value;
+ });
+ },
),
),
);
}
- Expanded _buildHitArea() {
- return Expanded(
- child: GestureDetector(
- onTap: () {
- if (_latestValue != null) {
- if (_displayTapped) {
- setState(() {
- _hideStuff = true;
- });
- } else {
- _cancelAndRestartTimer();
- }
- } else {
- _playPause();
-
+ Widget _buildHitArea() {
+ return GestureDetector(
+ onTap: () {
+ if (_latestValue != null) {
+ if (_displayTapped) {
setState(() {
- _hideStuff = true;
+ _hideStuff = !_hideStuff;
});
+ } else {
+ _cancelAndRestartTimer();
}
- },
- child: Container(
- color: Colors.transparent,
- child: Center(
- child: AnimatedOpacity(
- opacity:
- _latestValue != null && !_hideStuff && !_dragging ? 1.0 : 0.0,
- duration: const Duration(milliseconds: 300),
- child: GestureDetector(
- onTap: _playPause,
- child: Padding(
- padding: const EdgeInsets.all(12.0),
- child: Icon(
- _latestValue!.isPlaying ? Icons.pause : Icons.play_arrow,
- color: Colors.white, // same for both themes
- size: 64.0,
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- );
- }
-
- Widget _buildCurrentPosition(Color? iconColor) {
- final position =
- _latestValue != null ? _latestValue!.position : Duration.zero;
-
- return Container(
- margin: const EdgeInsets.only(left: 20.0, right: 16.0),
- child: Text(
- formatDuration(position),
- style: const TextStyle(
- fontSize: 12.0,
- color: Colors.white,
- ),
- ),
- );
- }
-
- Widget _buildTotalDuration(Color? iconColor) {
- final duration =
- _latestValue != null ? _latestValue!.duration : Duration.zero;
+ } else {
+ _playPause();
- return Padding(
- padding: const EdgeInsets.only(right: 20.0),
- child: Text(
- formatDuration(duration),
- style: const TextStyle(
- fontSize: 12.0,
- color: Colors.white,
+ setState(() {
+ _hideStuff = true;
+ });
+ }
+ },
+ behavior: HitTestBehavior.opaque,
+ child: AnimatedOpacity(
+ opacity: _latestValue != null && !_hideStuff && !_dragging ? 1.0 : 0.0,
+ duration: const Duration(milliseconds: 300),
+ child: Center(
+ child: _PlayPauseButton(
+ _playPause,
+ _latestValue!.isPlaying,
+ ),
),
),
);
@@ -275,34 +254,235 @@ class _VideoControlsState extends State<VideoControls> {
_latestValue = controller.value;
});
}
+}
- Widget _buildProgressBar() {
- return Expanded(
- child: Padding(
- padding: const EdgeInsets.only(right: 16.0),
- child: MaterialVideoProgressBar(
- controller,
- onDragStart: () {
- setState(() {
- _dragging = true;
- });
+class _SeekBarAndDuration extends StatelessWidget {
+ final VideoPlayerController? controller;
+ final VideoPlayerValue? latestValue;
+ final Function(bool) updateDragging;
- _hideTimer?.cancel();
- },
- onDragEnd: () {
+ const _SeekBarAndDuration({
+ required this.controller,
+ required this.latestValue,
+ required this.updateDragging,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ return Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: 8,
+ ),
+ child: Container(
+ padding: const EdgeInsets.fromLTRB(
+ 16,
+ 4,
+ 16,
+ 4,
+ ),
+ decoration: BoxDecoration(
+ color: Colors.black.withOpacity(0.3),
+ borderRadius: const BorderRadius.all(
+ Radius.circular(8),
+ ),
+ border: Border.all(
+ color: strokeFaintDark,
+ width: 1,
+ ),
+ ),
+ child: Row(
+ children: [
+ if (latestValue?.position == null)
+ Text(
+ "0:00",
+ style: getEnteTextTheme(
+ context,
+ ).mini.copyWith(
+ color: textBaseDark,
+ ),
+ )
+ else
+ Text(
+ _secondsToDuration(latestValue!.position.inSeconds),
+ style: getEnteTextTheme(
+ context,
+ ).mini.copyWith(
+ color: textBaseDark,
+ ),
+ ),
+ Expanded(
+ child: _SeekBar(controller!, updateDragging),
+ ),
+ Text(
+ _secondsToDuration(
+ latestValue?.duration.inSeconds ?? 0,
+ ),
+ style: getEnteTextTheme(
+ context,
+ ).mini.copyWith(
+ color: textBaseDark,
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+
+ /// Returns the duration in the format "h:mm:ss" or "m:ss".
+ String _secondsToDuration(int totalSeconds) {
+ final hours = totalSeconds ~/ 3600;
+ final minutes = (totalSeconds % 3600) ~/ 60;
+ final seconds = totalSeconds % 60;
+
+ if (hours > 0) {
+ return '${hours.toString().padLeft(1, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
+ } else {
+ return '${minutes.toString().padLeft(1, '0')}:${seconds.toString().padLeft(2, '0')}';
+ }
+ }
+}
+
+class _SeekBar extends StatefulWidget {
+ final VideoPlayerController controller;
+ final Function(bool) updateDragging;
+ const _SeekBar(
+ this.controller,
+ this.updateDragging,
+ );
+
+ @override
+ State<_SeekBar> createState() => _SeekBarState();
+}
+
+class _SeekBarState extends State<_SeekBar> {
+ double _sliderValue = 0.0;
+ final _debouncer = Debouncer(
+ const Duration(milliseconds: 300),
+ executionInterval: const Duration(milliseconds: 300),
+ );
+ bool _controllerWasPlaying = false;
+
+ @override
+ void initState() {
+ super.initState();
+ widget.controller.addListener(updateSlider);
+ }
+
+ void updateSlider() {
+ if (widget.controller.value.isInitialized) {
+ setState(() {
+ _sliderValue = widget.controller.value.position.inSeconds.toDouble();
+ });
+ }
+ }
+
+ @override
+ void dispose() {
+ _debouncer.cancelDebounceTimer();
+ widget.controller.removeListener(updateSlider);
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+ return SliderTheme(
+ data: SliderTheme.of(context).copyWith(
+ trackHeight: 1.0,
+ thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 8.0),
+ overlayShape: const RoundSliderOverlayShape(overlayRadius: 14.0),
+ activeTrackColor: colorScheme.primary300,
+ inactiveTrackColor: fillMutedDark,
+ thumbColor: backgroundElevatedLight,
+ overlayColor: fillMutedDark,
+ ),
+ child: Slider(
+ min: 0.0,
+ max: widget.controller.value.duration.inSeconds.toDouble(),
+ value: _sliderValue,
+ onChangeStart: (value) async {
+ widget.updateDragging(true);
+ _controllerWasPlaying = widget.controller.value.isPlaying;
+ if (_controllerWasPlaying) {
+ await widget.controller.pause();
+ }
+ },
+ onChanged: (value) {
+ if (mounted) {
setState(() {
- _dragging = false;
+ _sliderValue = value;
});
+ }
+
+ _debouncer.run(() async {
+ await widget.controller.seekTo(Duration(seconds: value.toInt()));
+ });
+ },
+ divisions: 4500,
+ onChangeEnd: (value) async {
+ await widget.controller.seekTo(Duration(seconds: value.toInt()));
+
+ if (_controllerWasPlaying) {
+ await widget.controller.play();
+ }
+ widget.updateDragging(false);
+ },
+ allowedInteraction: SliderInteraction.tapAndSlide,
+ ),
+ );
+ }
+}
- _startHideTimer();
+class _PlayPauseButton extends StatefulWidget {
+ final void Function() playPause;
+ final bool isPlaying;
+ const _PlayPauseButton(
+ this.playPause,
+ this.isPlaying,
+ );
+
+ @override
+ State<_PlayPauseButton> createState() => _PlayPauseButtonState();
+}
+
+class _PlayPauseButtonState extends State<_PlayPauseButton> {
+ @override
+ Widget build(BuildContext context) {
+ return Container(
+ width: 54,
+ height: 54,
+ decoration: BoxDecoration(
+ color: Colors.black.withOpacity(0.3),
+ shape: BoxShape.circle,
+ border: Border.all(
+ color: strokeFaintDark,
+ width: 1,
+ ),
+ ),
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: widget.playPause,
+ child: AnimatedSwitcher(
+ duration: const Duration(milliseconds: 250),
+ transitionBuilder: (Widget child, Animation<double> animation) {
+ return ScaleTransition(scale: animation, child: child);
},
- colors: chewieController!.materialProgressColors ??
- ChewieProgressColors(
- playedColor: Theme.of(context).colorScheme.greenAlternative,
- handleColor: Colors.white,
- bufferedColor: Colors.white,
- backgroundColor: Theme.of(context).disabledColor,
- ),
+ switchInCurve: Curves.easeInOutQuart,
+ switchOutCurve: Curves.easeInOutQuart,
+ child: widget.isPlaying
+ ? const Icon(
+ Icons.pause,
+ size: 32,
+ key: ValueKey("pause"),
+ color: Colors.white,
+ )
+ : const Icon(
+ Icons.play_arrow,
+ size: 36,
+ key: ValueKey("play"),
+ color: Colors.white,
+ ),
),
),
);
diff --git a/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart b/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart
index cf71cd66a9..5bddea1753 100644
--- a/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart
+++ b/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart
@@ -16,6 +16,7 @@ import "package:photos/services/files_service.dart";
import "package:photos/theme/colors.dart";
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/actions/file/file_actions.dart";
+import "package:photos/ui/viewer/file/preview_status_widget.dart";
import "package:photos/utils/debouncer.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/utils/file_util.dart";
@@ -361,6 +362,15 @@ class __VideoWidgetState extends State<_VideoWidget> {
),
)
: const SizedBox.shrink(),
+ ValueListenableBuilder(
+ valueListenable: showControlsNotifier,
+ builder: (context, value, _) {
+ return PreviewStatusWidget(
+ showControls: value,
+ file: widget.file,
+ );
+ },
+ ),
_SeekBarAndDuration(
controller: widget.controller,
isSeekingNotifier: _isSeekingNotifier,
diff --git a/mobile/lib/ui/viewer/file/video_widget_native.dart b/mobile/lib/ui/viewer/file/video_widget_native.dart
index a788bb3398..e41c99e01d 100644
--- a/mobile/lib/ui/viewer/file/video_widget_native.dart
+++ b/mobile/lib/ui/viewer/file/video_widget_native.dart
@@ -22,6 +22,7 @@ import "package:photos/ui/actions/file/file_actions.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/viewer/file/native_video_player_controls/play_pause_button.dart";
import "package:photos/ui/viewer/file/native_video_player_controls/seek_bar.dart";
+import "package:photos/ui/viewer/file/preview_status_widget.dart";
import "package:photos/ui/viewer/file/thumbnail_widget.dart";
import "package:photos/utils/debouncer.dart";
import "package:photos/utils/dialog_util.dart";
@@ -276,6 +277,15 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
+ ValueListenableBuilder(
+ valueListenable: _showControls,
+ builder: (context, value, _) {
+ return PreviewStatusWidget(
+ showControls: value,
+ file: widget.file,
+ );
+ },
+ ),
_VideoDescriptionAndSwitchToMediaKitButton(
file: widget.file,
showControls: _showControls,
diff --git a/mobile/lib/utils/file_uploader.dart b/mobile/lib/utils/file_uploader.dart
index 4cc5bb0574..82e5a74b87 100644
--- a/mobile/lib/utils/file_uploader.dart
+++ b/mobile/lib/utils/file_uploader.dart
@@ -749,9 +749,6 @@ class FileUploader {
throw SyncStopRequestedError();
}
- if (PreviewVideoStore.instance.isVideoStreamingEnabled) {
- _uploadPreview(file);
- }
EnteFile remoteFile;
if (isUpdatedFile) {
remoteFile = await _updateFile(
@@ -821,6 +818,9 @@ class FileUploader {
}
await FilesDB.instance.update(remoteFile);
}
+ if (PreviewVideoStore.instance.isVideoStreamingEnabled) {
+ _uploadPreview(file);
+ }
await UploadLocksDB.instance.deleteMultipartTrack(lockKey);
if (!_isBackground) {