From 67e98ed31359f509a9e5fecf9d430286e1d71be4 Mon Sep 17 00:00:00 2001
From: Alex <alex.tran1502@gmail.com>
Date: Wed, 12 Jun 2024 12:43:01 -0500
Subject: [PATCH] fix(mobile): video player not updating state (#10220)

* fix(mobile): video player not updating state

* unused code
---
 mobile/lib/pages/common/video_viewer.page.dart     |  8 ++++----
 .../video_player_controls_provider.dart            | 14 ++++++++++++++
 .../asset_viewer/custom_video_player_controls.dart |  2 ++
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/mobile/lib/pages/common/video_viewer.page.dart b/mobile/lib/pages/common/video_viewer.page.dart
index 4c6f7344f7..527411ec89 100644
--- a/mobile/lib/pages/common/video_viewer.page.dart
+++ b/mobile/lib/pages/common/video_viewer.page.dart
@@ -75,9 +75,7 @@ class VideoViewerPage extends HookConsumerWidget {
     // Also sets the error if there is an error in the playback
     void updateVideoPlayback() {
       final videoPlayback = VideoPlaybackValue.fromController(controller);
-      if (!loopVideo) {
-        ref.read(videoPlaybackValueProvider.notifier).value = videoPlayback;
-      }
+      ref.read(videoPlaybackValueProvider.notifier).value = videoPlayback;
       final state = videoPlayback.state;
 
       // Enable the WakeLock while the video is playing
@@ -110,7 +108,9 @@ class VideoViewerPage extends HookConsumerWidget {
         }
 
         // Subscribes to listener
-        controller.addListener(updateVideoPlayback);
+        Future.microtask(() {
+          controller.addListener(updateVideoPlayback);
+        });
         return () {
           // Removes listener when we dispose
           controller.removeListener(updateVideoPlayback);
diff --git a/mobile/lib/providers/asset_viewer/video_player_controls_provider.dart b/mobile/lib/providers/asset_viewer/video_player_controls_provider.dart
index d935358936..d15b26ea20 100644
--- a/mobile/lib/providers/asset_viewer/video_player_controls_provider.dart
+++ b/mobile/lib/providers/asset_viewer/video_player_controls_provider.dart
@@ -93,4 +93,18 @@ class VideoPlayerControls extends StateNotifier<VideoPlaybackControls> {
       pause: !state.pause,
     );
   }
+
+  void restart() {
+    state = VideoPlaybackControls(
+      position: 0,
+      mute: state.mute,
+      pause: true,
+    );
+
+    state = VideoPlaybackControls(
+      position: 0,
+      mute: state.mute,
+      pause: false,
+    );
+  }
 }
diff --git a/mobile/lib/widgets/asset_viewer/custom_video_player_controls.dart b/mobile/lib/widgets/asset_viewer/custom_video_player_controls.dart
index ebef229dd6..a34fcb9baf 100644
--- a/mobile/lib/widgets/asset_viewer/custom_video_player_controls.dart
+++ b/mobile/lib/widgets/asset_viewer/custom_video_player_controls.dart
@@ -64,6 +64,8 @@ class CustomVideoPlayerControls extends HookConsumerWidget {
       final state = ref.read(videoPlaybackValueProvider).state;
       if (state == VideoPlaybackState.playing) {
         ref.read(videoPlayerControlsProvider.notifier).pause();
+      } else if (state == VideoPlaybackState.completed) {
+        ref.read(videoPlayerControlsProvider.notifier).restart();
       } else {
         ref.read(videoPlayerControlsProvider.notifier).play();
       }