Change Next, Prev Button clicked handler
authorHeonjae Jang <heonjae.jang@samsung.com>
Fri, 2 Jun 2017 06:14:58 +0000 (15:14 +0900)
committerHeonjae Jang <heonjae.jang@samsung.com>
Fri, 2 Jun 2017 06:23:18 +0000 (15:23 +0900)
Change-Id: Idebdd18db7f2d23e5c7706d2ea8a701d1bceb299
Signed-off-by: Heonjae Jang <heonjae.jang@samsung.com>
TVMediaHub/TVMediaHub.Tizen/Controls/MediaHubButton.xaml.cs
TVMediaHub/TVMediaHub.Tizen/ViewModels/VideoTabViewModel.cs
TVMediaHub/TVMediaHub.Tizen/Views/VideoPlayer.xaml
TVMediaHub/TVMediaHub.Tizen/Views/VideoPlayer.xaml.cs

index 5c3b221..165ccd8 100755 (executable)
@@ -35,11 +35,6 @@ namespace TVMediaHub.Tizen.Controls
         int effectDuration = 200;
 
         /// <summary>
-        /// A flag that whether the button is handling clicked event or not
-        /// </summary>
-        private static bool isButtonRunning = false;
-
-        /// <summary>
         /// Gets or sets MediaHubButtonIcon's image source
         /// </summary>
         public string MediaHubButtonIcon
@@ -101,37 +96,45 @@ namespace TVMediaHub.Tizen.Controls
         /// </summary>
         private void InitializeButton()
         {
-            Btn.Clicked += async (s, e) =>
+            Btn.Clicked += (s, e) =>
             {
-                if (isButtonRunning)
-                {
-                    return;
-                }
-
-                isButtonRunning = true;
-
                 Clicked?.Invoke(this, EventArgs.Empty);
-
-                try
-                {
-                    BtnIcon.Source = PressedImage;
-#pragma warning disable CS4014
-                    BG.ScaleTo(0.1, Convert.ToUInt32(effectDuration), Easing.CubicInOut);
-#pragma warning restore CS4014
-                    await BG.FadeTo(0, Convert.ToUInt32(effectDuration), Easing.CubicInOut);
-
-                    BtnIcon.Source = NormalImage;
-#pragma warning disable CS4014
-                    BG.ScaleTo(1, Convert.ToUInt32(effectDuration), Easing.CubicInOut);
-#pragma warning restore CS4014
-                    await BG.FadeTo(1, Convert.ToUInt32(effectDuration), Easing.CubicInOut);
-                }
-                catch (Exception err)
-                {
-                    DbgPort.D("Error : " + err.Message);
-                }
-
-                isButtonRunning = false;
+                var BGScale = BG.Scale;
+                var BGOpacity = BG.Opacity;
+
+                this.AbortAnimation("ClickAnimation");
+                BtnIcon.Source = PressedImage;
+                this.Animate("ClickAnimation", (v) =>
+                 {
+                     BG.Scale = BGScale - (BGScale - 0.1) * v;
+                     BG.Opacity = BGOpacity - (BGOpacity - 0) * v;
+                 },
+                 length: Convert.ToUInt32(effectDuration),
+                 easing: Easing.CubicInOut,
+                 finished: (progress, isCanceled) =>
+                 {
+                     BtnIcon.Source = NormalImage;
+                     if (!isCanceled)
+                     {
+                         this.Animate("ClickAnimation", (v) =>
+                         {
+                             BG.Scale = 0.1 + 0.9 * v;
+                             BG.Opacity = v;
+                         },
+                         length: Convert.ToUInt32(effectDuration),
+                         easing: Easing.CubicInOut,
+                         finished: (progress2, isCanceled2) =>
+                         {
+                             BG.Scale = 1;
+                             BGOpacity = 1;
+                         });
+                     }
+                     else
+                     {
+                         BG.Scale = 1;
+                         BGOpacity = 1;
+                     }
+                 });
             };
         }
 
index 33b1e61..14821b5 100755 (executable)
@@ -100,6 +100,16 @@ namespace TVMediaHub.Tizen.ViewModels
         public ICommand SelectAllContentCommand { get; set; }
 
         /// <summary>
+        /// A command for changing current video to next
+        /// </summary>
+        public ICommand SetNextVideoCommand { get; set; }
+
+        /// <summary>
+        /// A command for changing current video to previous
+        /// </summary>
+        public ICommand SetPrevVideoCommand { get; set; }
+
+        /// <summary>
         /// A command to set current video information
         /// </summary>
         public ICommand SetCurrentVideoInfo
@@ -116,6 +126,15 @@ namespace TVMediaHub.Tizen.ViewModels
         /// Gets or sets the MediaInformation of current video
         /// </summary>
         public MediaInformation CurrentVideo { get; set; }
+        public int CurrentVideoIndex { get; set; }
+        public List<MediaInformation> PlayList { get; set; }
+
+        public bool IsLastVideo {
+            get
+            {
+                return (PlayList.Count - 1) == CurrentVideoIndex;
+            }
+        }
 
         /// <summary>
         /// Gets or sets the DeleteStatus
@@ -163,6 +182,8 @@ namespace TVMediaHub.Tizen.ViewModels
             InitializeCommands();
             VideoList = new ObservableCollection<GroupItem>();
             SelectedList = new List<MediaInformation>();
+            PlayList = new List<MediaInformation>();
+
             OnPropertyChanged("VideoList");
             MediaHubImpl.GetInstance.VideoProviderInstance.SetContentUpdatedEventListener((sender, arg) =>
             {
@@ -288,6 +309,19 @@ namespace TVMediaHub.Tizen.ViewModels
 
                 OnPropertyChanged("SelectedCount");
             });
+
+            SetNextVideoCommand = new Command(() =>
+            {
+                SetNextVideo();
+            });
+
+            SetPrevVideoCommand = new Command(() =>
+            {
+                SetPrevVideo();
+            });
+            OnPropertyChanged("SetNextVideoCommand");
+            OnPropertyChanged("SetPrevVideoCommand");
+
             OnPropertyChanged("ChangeTabStatusCommand");
             OnPropertyChanged("ChangeSortOptionCommand");
         }
@@ -347,6 +381,14 @@ namespace TVMediaHub.Tizen.ViewModels
                 //await Task.Delay(1);
                 VideoList.Add(group);
             }
+
+            foreach (var group in VideoList)
+            {
+                foreach (var item in group.Contents)
+                {
+                    PlayList.Add(item.Information);
+                }
+            }
         }
 
         /// <summary>
@@ -356,10 +398,37 @@ namespace TVMediaHub.Tizen.ViewModels
         private void SetCurrentVideo(MediaInformation videoContent)
         {
             CurrentVideo = videoContent;
+            CurrentVideoIndex = PlayList.IndexOf(CurrentVideo);
             OnPropertyChanged("CurrentVideo");
         }
 
         /// <summary>
+        /// A method for setting the next video
+        /// </summary>
+        private void SetPrevVideo()
+        {
+            if (CurrentVideoIndex != 0)
+            {
+                CurrentVideoIndex--;
+                CurrentVideo = PlayList[CurrentVideoIndex];
+                OnPropertyChanged("CurrentVideo");
+            }
+        }
+
+        /// <summary>
+        /// A method for setting the next video
+        /// </summary>
+        private void SetNextVideo()
+        {
+            if (CurrentVideoIndex != (PlayList.Count - 1))
+            {
+                CurrentVideoIndex++;
+                CurrentVideo = PlayList[CurrentVideoIndex];
+                OnPropertyChanged("CurrentVideo");
+            }
+        }
+
+        /// <summary>
         /// A method for setting latest played time of the media file.
         /// </summary>
         /// <param name="videoContent">A played media file</param>
index 54a7975..94c1978 100755 (executable)
@@ -6,7 +6,9 @@
                   xmlns:Utils="clr-namespace:TVMediaHub.Tizen.Utils"
                   xmlns:ViewModels="clr-namespace:TVMediaHub.Tizen.ViewModels"
                   xmlns:Controls="clr-namespace:TVMediaHub.Tizen.Controls"
-                  CurrentVideo="{Binding CurrentVideo}">
+                  CurrentVideo="{Binding CurrentVideo}"
+                  SetNextVideoCommand="{Binding SetNextVideoCommand}"
+                  SetPrevVideoCommand="{Binding SetPrevVideoCommand}">
     <RelativeLayout x:Name="MainLayout">
         <Image x:Name="Thumbnail"
                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"
index 58b951d..a4846b9 100755 (executable)
@@ -24,6 +24,7 @@ using Xamarin.Forms;
 using Extension = Tizen.Xamarin.Forms.Extension;
 using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
 using System.Threading;
+using System.Windows.Input;
 
 namespace TVMediaHub.Tizen.Views
 {
@@ -44,6 +45,14 @@ namespace TVMediaHub.Tizen.Views
             AUTO,
         };
 
+        enum PlayStatus
+        {
+            Play = 0,
+            Rewind,
+            FastFoward,
+            Pause
+        }
+
         /// <summary>
         /// The media duration in Milliseconds.
         /// </summary>
@@ -91,30 +100,9 @@ namespace TVMediaHub.Tizen.Views
         private int timerDueTime = 5000;
 
         /// <summary>
-        /// Gets or sets whether play status is back forward or back rewind
+        /// Gets or sets current play status
         /// </summary>
-        private bool IsInPlayBackRewFwd
-        {
-            set
-            {
-                if (value == false)
-                {
-                    PlayBackForwardIndex = 0;
-                    PlayBackRewindIndex = 0;
-                }
-            }
-
-            get
-            {
-                if (PlayBackForwardIndex == 0 &&
-                    PlayBackRewindIndex == 0)
-                {
-                    return false;
-                }
-
-                return true;
-            }
-        }
+        private PlayStatus CurrentPlayStatus { get; set; }
 
         /// <summary>
         /// The value for play back forward index
@@ -178,6 +166,34 @@ namespace TVMediaHub.Tizen.Views
         }
 
         /// <summary>
+        /// Identifies the SetNextVideoCommand bindable property
+        /// </summary>
+        public static readonly BindableProperty SetNextVideoCommandProperty = BindableProperty.Create("SetNextVideoCommand", typeof(ICommand), typeof(VideoPlayer), null);
+
+        /// <summary>
+        /// A Command for setting current video to next video
+        /// </summary>
+        public ICommand SetNextVideoCommand
+        {
+            get { return (ICommand)GetValue(SetNextVideoCommandProperty); }
+            set { SetValue(SetNextVideoCommandProperty, value); }
+        }
+
+        /// <summary>
+        /// Identifies the SetPrevVideoCommand bindable property
+        /// </summary>
+        public static readonly BindableProperty SetPrevVideoCommandProperty = BindableProperty.Create("SetPrevVideoCommand", typeof(ICommand), typeof(VideoPlayer), null);
+
+        /// <summary>
+        /// A Command for setting current video to previous video
+        /// </summary>
+        public ICommand SetPrevVideoCommand
+        {
+            get { return (ICommand)GetValue(SetPrevVideoCommandProperty); }
+            set { SetValue(SetPrevVideoCommandProperty, value); }
+        }
+
+        /// <summary>
         /// A constructor
         /// </summary>
         public VideoPlayer()
@@ -185,7 +201,7 @@ namespace TVMediaHub.Tizen.Views
             BindingContext = VideoTabViewModelLocator.ViewModel;
             InitializeComponent();
             InitializeFontSize();
-
+            PropertyChanged += VideoPlayePropertyChanged;
             mediaView = new Extension.MediaView();
 
             mediaView.NativeViewCreated += (s, e) =>
@@ -217,16 +233,45 @@ namespace TVMediaHub.Tizen.Views
                }
 
 #pragma warning disable CS4014
-                GradientDim.FadeTo(0, 667, Easing.CubicInOut);
+               GradientDim.FadeTo(0, 667, Easing.CubicInOut);
                ControlArea.TranslateTo(0, 93, 667, Easing.CubicInOut);
                ControlArea.FadeTo(0, 667, Easing.CubicInOut);
                TitleLabel.TranslateTo(0, -50, 667, Easing.CubicInOut);
                await TitleLabel.FadeTo(0, 667, Easing.CubicInOut);
 
                App.AppMainPage.Navigation.PopAsync();
-                //Program.TransitionToMain(0);
 #pragma warning restore CS4014
-            };
+           };
+        }
+
+        /// <summary>
+        /// This method is called when the properties is changed
+        /// </summary>
+        /// <param name="sender">The source of the event</param>
+        /// <param name="e">A propertyChanged event argument</param>
+        private void VideoPlayePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
+        {
+            if (e.PropertyName.Equals("CurrentVideo"))
+            {
+                if (playerInstance.State == PlayerState.Playing || playerInstance.State == PlayerState.Paused)
+                {
+                    try
+                    {
+                        playerInstance.Stop();
+                        playerInstance.Unprepare();
+                        playerDisplayInstance = new Display((MediaView)mediaView.NativeView);
+                        playerInstance.Display = playerDisplayInstance;
+                        videoMediaSource = new MediaUriSource(CurrentVideo.FilePath);
+                        playerInstance.SetSource(videoMediaSource);
+                    }
+                    catch (Exception ex)
+                    {
+                        DbgPort.D(ex.Message);
+                        return;
+                    }
+                    PrepareAsync();
+                }
+            }
         }
 
         /// <summary>
@@ -335,15 +380,14 @@ namespace TVMediaHub.Tizen.Views
                 int playPosition = playerInstance.GetPlayPosition();
                 SetPlayTime(playPosition);
                 progress.Progress = (double)playPosition / sourceDuration;
-                DbgPort.D("Status : " + playerInstance.State + "/" + prevPlayerStatus + ", temp : " + playPosition + ", progress : " + progress.Progress.ToString());
+                //DbgPort.D("Status : " + playerInstance.State + "/" + prevPlayerStatus + ", temp : " + playPosition + ", progress : " + progress.Progress.ToString());
 
                 if (prevPlayerStatus != playerInstance.State)
                 {
                     if (playerInstance.State == PlayerState.Paused || playerInstance.State == PlayerState.Ready)
                     {
-                        if (IsInPlayBackRewFwd)
+                        if (CurrentPlayStatus == PlayStatus.Rewind || CurrentPlayStatus == PlayStatus.FastFoward)
                         {
-                            IsInPlayBackRewFwd = false;
                             playerInstance.SetPlaybackRate((PlayBackRate[0]));
                         }
 
@@ -360,19 +404,6 @@ namespace TVMediaHub.Tizen.Views
 
                     prevPlayerStatus = playerInstance.State;
                 }
-
-                // TODO : check later, hotfix for VD GBM player
-                if ((PlayBackRewindIndex != 0 && playPosition < 4000)
-                    || PlayBackForwardIndex != 0 && playPosition > (sourceDuration - 6000))
-                {
-                    IsInPlayBackRewFwd = false;
-                    playerInstance.SetPlaybackRate((PlayBackRate[0]));
-
-                    if (SpeedInfo.Opacity == 1)
-                    {
-                        SpeedInfo.FadeTo(0.0, 100);
-                    }
-                }
             });
 
             if (playerInstance.State != PlayerState.Playing &&
@@ -403,24 +434,40 @@ namespace TVMediaHub.Tizen.Views
             playerInstance.Display = playerDisplayInstance;
             playerInstance.PlaybackCompleted += (obj, eventArg) =>
             {
+                PlayBackRewindIndex = 0;
+                PlayBackForwardIndex = 0;
                 DbgPort.D("PlaybackCompleted");
-                try
+
+                if (CurrentPlayStatus == PlayStatus.Rewind)
                 {
-                    playerInstance.Stop();
-                    playerInstance.Unprepare();
+                    try
+                    {
+                        CurrentPlayStatus = PlayStatus.Play;
+                        playerInstance?.SetPlaybackRate(1);
+                        playerInstance?.Start();
+                    }
+                    catch (Exception ex)
+                    {
+                        DbgPort.E("Start Failed : " + ex.Message);
+                        return;
+                    }
                 }
-                catch (Exception ex)
+                else if (VideoTabViewModelLocator.ViewModel.IsLastVideo)
                 {
-                    DbgPort.E("Stop Failed : " + ex.Message);
+                    App.AppMainPage.Navigation.PopAsync();
+                }
+                else
+                {
+                    try
+                    {
+                        CurrentPlayStatus = PlayStatus.Play;
+                        SetNextVideoCommand?.Execute("");
+                    }
+                    catch (Exception ex)
+                    {
+                        DbgPort.E("Stop Failed : " + ex.Message);
+                    }
                 }
-
-                PausePlayBtn.NormalImage = PausePlayBtn.NormalImage.Replace("pause", "play");
-                PausePlayBtn.PressedImage = PausePlayBtn.PressedImage.Replace("pause", "play");
-                PausePlayBtn.MediaHubButtonIcon = PausePlayBtn.NormalImage;
-
-                // If you don't want to back automatically whenever VideoPlayer's play is over, remove below line.
-                // TODO: Remove this later, only for TDC
-                Navigation.PopToRootAsync();
             };
 
             videoMediaSource = new MediaUriSource(CurrentVideo.FilePath);
@@ -530,8 +577,6 @@ namespace TVMediaHub.Tizen.Views
         /// </summary>
         private void PlayVideo()
         {
-            DbgPort.D("ElmSharp.Window is activated");
-
             try
             {
                 playerInstance.Start();
@@ -611,6 +656,11 @@ namespace TVMediaHub.Tizen.Views
         /// <param name="e">An event's argument</param>
         private async void PreviousBtnClicked(object sender, EventArgs e)
         {
+            if (playerInstance.State == PlayerState.Idle || playerInstance.State == PlayerState.Preparing)
+            {
+                return;
+            }
+
             if (ControlArea.Opacity == 0)
             {
                 SetControlAreaState(ControlAreaState.AUTO);
@@ -620,17 +670,25 @@ namespace TVMediaHub.Tizen.Views
 
             SetControlAreaState(ControlAreaState.AUTO);
 
-            try
-            {
-                await playerInstance.SetPlayPositionAsync(0, true);
-            }
-            catch (Exception ex)
+            if (playerInstance.GetPlayPosition() < 1000)
             {
-                DbgPort.E("SetPlayPositionAsync Failed : " + ex.Message);
+                SetPrevVideoCommand?.Execute("");
                 return;
             }
+            else
+            {
+                try
+                {
+                    await playerInstance.SetPlayPositionAsync(0, true);
+                }
+                catch (Exception ex)
+                {
+                    DbgPort.E("SetPlayPositionAsync Failed : " + ex.Message);
+                    return;
+                }
 
-            progress.Progress = 0.0;
+                progress.Progress = 0.0;
+            }
         }
 
         /// <summary>
@@ -673,6 +731,7 @@ namespace TVMediaHub.Tizen.Views
             */
             try
             {
+                CurrentPlayStatus = PlayStatus.Rewind;
                 PlayBackRewindIndex++;
                 playerInstance.SetPlaybackRate((PlayBackRate[PlayBackRewindIndex]) * -1);
                 // TODO : check later, added as hotfix for VD GBM player
@@ -715,6 +774,7 @@ namespace TVMediaHub.Tizen.Views
             if (playerInstance.State == PlayerState.Paused || playerInstance.State == PlayerState.Ready)
             {
                 SetControlAreaState(ControlAreaState.SHOW);
+                CurrentPlayStatus = PlayStatus.Play;
                 PlayVideo();
             }
             else if (playerInstance.State == PlayerState.Playing)
@@ -723,6 +783,7 @@ namespace TVMediaHub.Tizen.Views
 
                 try
                 {
+                    CurrentPlayStatus = PlayStatus.Pause;
                     playerInstance.Pause();
                 }
                 catch (Exception ex)
@@ -751,6 +812,7 @@ namespace TVMediaHub.Tizen.Views
 
             try
             {
+                CurrentPlayStatus = PlayStatus.FastFoward;
                 PlayBackForwardIndex++;
                 playerInstance.SetPlaybackRate(PlayBackRate[PlayBackForwardIndex]);
                 // TODO : check later, added as hotfix for VD GBM player
@@ -776,6 +838,11 @@ namespace TVMediaHub.Tizen.Views
         /// <param name="e">An event's argument</param>
         private async void NextBtnClicked(object sender, EventArgs e)
         {
+            if (playerInstance.State == PlayerState.Idle || playerInstance.State == PlayerState.Preparing)
+            {
+                return;
+            }
+
             if (ControlArea.Opacity == 0)
             {
                 SetControlAreaState(ControlAreaState.AUTO);
@@ -784,14 +851,22 @@ namespace TVMediaHub.Tizen.Views
 
             SetControlAreaState(ControlAreaState.AUTO);
 
-            try
+            if (playerInstance.GetPlayPosition() > sourceDuration - 1000)
             {
-                await playerInstance.SetPlayPositionAsync(sourceDuration - 500, true);
+                SetNextVideoCommand?.Execute("");
             }
-            catch (Exception err)
+            else
             {
-                DbgPort.E("SetPlayPositionAsync Failed : " + err.Message);
-                return;
+                try
+                {
+                    await playerInstance.SetPlayPositionAsync(sourceDuration - 1000, true);
+                }
+                catch (Exception err)
+                {
+                    DbgPort.E("SetPlayPositionAsync Failed : " + err.Message);
+                    return;
+                }
+
             }
 
             progress.Progress = 1.0;
@@ -831,7 +906,7 @@ namespace TVMediaHub.Tizen.Views
             TitleLabel.TranslateTo(0, 0, 667, Easing.CubicInOut);
             TitleLabel.FadeTo(1, 667, Easing.CubicInOut);
 
-            if (IsInPlayBackRewFwd)
+            if (CurrentPlayStatus == PlayStatus.Rewind || CurrentPlayStatus == PlayStatus.FastFoward)
             {
                 SpeedInfo.FadeTo(1.0, 100);
             }