From: k.stepaniuk Date: Thu, 26 Nov 2020 17:00:48 +0000 (+0100) Subject: Fixing review & animation cases X-Git-Tag: accepted/tizen/unified/20210915.100113~134 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4e875ce91bf4de4c30e1f53bb3480a09821c4d01;p=profile%2Fiot%2Fapps%2Fdotnet%2Ffitness.git Fixing review & animation cases Signed-off-by: k.stepaniuk --- diff --git a/Fitness/ViewModels/ExercisingViewModel.cs b/Fitness/ViewModels/ExercisingViewModel.cs index e264c38..cc4d4f0 100644 --- a/Fitness/ViewModels/ExercisingViewModel.cs +++ b/Fitness/ViewModels/ExercisingViewModel.cs @@ -1,7 +1,6 @@ +using Fitness.Models; using System; using System.Windows.Input; -using Fitness.Models; -using Fitness.Services; using Tizen.NUI.Binding; namespace Fitness.ViewModels @@ -13,7 +12,6 @@ namespace Fitness.ViewModels PauseResumeWorkout = new Command(TriggerPauseResumeWorkout); EndWorkout = new Command(ExecuteEndWorkout); State = WorkoutState.Playing; - //TriggerPauseResumeWorkout(); } /// diff --git a/Fitness/Views/Behaviors/VisibilitySetter.cs b/Fitness/Views/Behaviors/VisibilitySetter.cs index 2fa9c71..2f856c6 100644 --- a/Fitness/Views/Behaviors/VisibilitySetter.cs +++ b/Fitness/Views/Behaviors/VisibilitySetter.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tizen.NUI.BaseComponents; +using Tizen.NUI.BaseComponents; using Tizen.NUI.Binding; namespace Fitness.Views.Behaviors @@ -16,22 +11,19 @@ namespace Fitness.Views.Behaviors typeof(bool), typeof(VisibilitySetter), true, - propertyChanged: OnStyleChanged); + propertyChanged: OnVisibilityChanged); - public static void OnStyleChanged(BindableObject bindable, object oldValue, object newValue) + public static void OnVisibilityChanged(BindableObject bindable, object oldValue, object newValue) { if (newValue is bool isVisible && bindable is View view) { if (isVisible) { view.Show(); - view.HeightResizePolicy = Tizen.NUI.ResizePolicyType.FillToParent; - //view.SizeHeight = 500; } else { view.Hide(); - view.SizeHeight = 0; } } } diff --git a/Fitness/Views/ExercisingView.xaml.cs b/Fitness/Views/ExercisingView.xaml.cs index 9b6f869..31eecec 100644 --- a/Fitness/Views/ExercisingView.xaml.cs +++ b/Fitness/Views/ExercisingView.xaml.cs @@ -14,7 +14,9 @@ namespace Fitness.Views public Size Size; } - private const int TransitionTime = 2_000; + private CancellationTokenSource source; + private bool isInitialized = false; + private const int TransitionTime = 1_000; private (Coordinates Preview, Coordinates Camera) playing; private (Coordinates Preview, Coordinates Camera) pause = ( @@ -29,7 +31,6 @@ namespace Fitness.Views Size = new Size(392, 220), } ); - private bool isInitialized = false; public static readonly BindableProperty IsPlayingProperty = BindableProperty.Create( @@ -43,7 +44,7 @@ namespace Fitness.Views { InitializeComponent(); - PlayingView.Relayout += OnPlayingViewRelayout; + PlayingView.PreviewStub.Relayout += OnPlayingViewRelayout; } public static void OnIsPlayingChanged(BindableObject bindable, object oldValue, object newValue) @@ -56,78 +57,95 @@ namespace Fitness.Views private async Task TriggerStates(bool isPlaying) { - bool shouldAnimate = PlayingView?.CameraPosition != null; - if (isPlaying) + if (isInitialized) { - if (shouldAnimate) + source?.Cancel(); + source?.Dispose(); + source = new CancellationTokenSource(); + + if (isPlaying) { await Task.WhenAll( - Animate(Preview, pause.Preview, playing.Preview), - Animate(Camera, pause.Camera, playing.Camera)); - } - - PauseView.Hide(); - PlayingView.Show(); - } - else - { - PlayingView.Hide(); - PauseView.Show(); + Animate(Preview, pause.Preview, playing.Preview, source.Token), + Animate(Camera, pause.Camera, playing.Camera, source.Token)); - if (shouldAnimate) + PauseView.Hide(); + PlayingView.Show(); + } + else { + PlayingView.Hide(); + PauseView.Show(); + await Task.WhenAll( - Animate(Preview, playing.Preview, pause.Preview), - Animate(Camera, playing.Camera, pause.Camera)); + Animate(Preview, playing.Preview, pause.Preview, source.Token), + Animate(Camera, playing.Camera, pause.Camera, source.Token)); } + + SetPositionAndSize(isPlaying); } } private void OnPlayingViewRelayout(object sender, System.EventArgs e) { UpdatePositionAndSize(); + isInitialized = true; + } + + private void UpdatePositionAndSize() + { playing.Camera = new Coordinates() { - Position = Camera.Position, - Size = Camera.Size, + Position = GetPosition(PlayingView.CameraStub), + Size = PlayingView.CameraStub.Size, }; playing.Preview = new Coordinates() { - Position = Preview.Position, - Size = Preview.Size, + Position = GetPosition(PlayingView.PreviewStub), + Size = PlayingView.PreviewStub.Size, }; - //Tizen.Log.Debug("demo", $"camera:"); - //show(playing.Camera); - //show(pause.Camera); - //Tizen.Log.Debug("demo", $""); - if (isInitialized == false) - { - isInitialized = true; - TriggerStates((bool)GetValue(IsPlayingProperty)); - } + SetPositionAndSize((bool)GetValue(IsPlayingProperty)); } - //void show(Coordinates c) - //{ - // Tizen.Log.Debug("demo", $"pos: {c.Position.X} {c.Position.Y}"); - // Tizen.Log.Debug("demo", $"size: {c.Size.Width} {c.Size.Height}"); - //} - - private void UpdatePositionAndSize() + private void SetPositionAndSize(bool isPlaying) { - if (PlayingView.CameraPosition != null) + Camera.Size = playing.Camera.Size; + Preview.Size = playing.Preview.Size; + + if (isPlaying) + { + Camera.Position = playing.Camera.Position; + Camera.ScaleX = 1; + Camera.ScaleY = 1; + Preview.Position = playing.Preview.Position; + Preview.ScaleX = 1; + Preview.ScaleY = 1; + + PauseView.Hide(); + PlayingView.Show(); + } + else { - Camera.Position = PlayingView.CameraPosition; - Camera.Size = PlayingView.CameraSize; + var cameraScale = pause.Camera.Size.Width / playing.Camera.Size.Width; + Camera.Position = pause.Camera.Position; + Camera.PivotPoint = new Position(0, 0); + Camera.ScaleX = cameraScale; + Camera.ScaleY = cameraScale; + + var previewScale = pause.Preview.Size.Width / playing.Preview.Size.Width; + Preview.Position = pause.Preview.Position; + Preview.PivotPoint = new Position(0, 0); + Preview.ScaleX = previewScale; + Preview.ScaleY = previewScale; - Preview.Position = PlayingView.PreviewPosition; - Preview.Size = PlayingView.PreviewSize; + PlayingView.Hide(); + PauseView.Show(); } } - private static Task Animate(View view, Coordinates from, Coordinates to) + private static Task Animate(View view, Coordinates from, Coordinates to, CancellationToken token) { Task Scale() { @@ -136,22 +154,32 @@ namespace Fitness.Views var animation = new Animation(TransitionTime); animation.AnimateTo(view, "ScaleX", scale, new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear)); animation.AnimateTo(view, "ScaleY", scale, new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear)); - return animation.PlayAndDispose(CancellationToken.None); + return animation.PlayAndDispose(token); } Task Move() { - var path = new Path(); - path.AddPoint(from.Position); - path.AddPoint(to.Position); - path.GenerateControlPoints(0f); + var frames = new KeyFrames(); + frames.Add(0.0f, from.Position); + frames.Add(1.0f, to.Position); var animation = new Animation(TransitionTime); - animation.AnimatePath(view, path, Vector3.Zero, new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear)); - return animation.PlayAndDispose(CancellationToken.None); + animation.AnimateBetween(view, nameof(from.Position), frames); + return animation.PlayAndDispose(token); } return Task.WhenAll(Scale(), Move()); } + + private static Position GetPosition(View view) + { + Position position = view.Position; + if (view.GetParent() is View parent) + { + position += GetPosition(parent); + } + + return position; + } } } diff --git a/Fitness/Views/PlayingView.xaml.cs b/Fitness/Views/PlayingView.xaml.cs index d78b4f7..f14605a 100644 --- a/Fitness/Views/PlayingView.xaml.cs +++ b/Fitness/Views/PlayingView.xaml.cs @@ -1,58 +1,10 @@ -using Tizen.NUI; -using Tizen.NUI.BaseComponents; - -namespace Fitness.Views +namespace Fitness.Views { public partial class PlayingView { - public Position PreviewPosition { get; private set; } - - public Position CameraPosition { get; private set; } - - public Size PreviewSize { get; private set; } - - public Size CameraSize { get; private set; } - public PlayingView() { InitializeComponent(); - PreviewStub.Relayout += (s, e) => UpdatePositionAndSize(); - } - - private void OnCameraStubRelayout(object sender, System.EventArgs e) - { - var pos = GetPosition(CameraStub); - CameraPosition = new Position(pos.X, pos.Y); - CameraSize = CameraStub.Size; - } - - private void OnPreviewStubRelayout(object sender, System.EventArgs e) - { - var pos = GetPosition(PreviewStub); - PreviewPosition = new Position(pos.X, pos.Y); - PreviewSize = PreviewStub.Size; - } - - private void UpdatePositionAndSize() - { - var pos = GetPosition(PreviewStub); - PreviewPosition = new Position(pos.X, pos.Y); - PreviewSize = PreviewStub.Size; - - pos = GetPosition(CameraStub); - CameraPosition = new Position(pos.X, pos.Y); - CameraSize = CameraStub.Size; - } - - private static (int X, int Y) GetPosition(View view) - { - (int X, int Y) parentPosition = (0, 0); - if (view.GetParent() is View parent) - { - parentPosition = GetPosition(parent); - } - - return ((int)view.Position.X + parentPosition.X, (int)view.Position.Y + parentPosition.Y); } } } diff --git a/Fitness/res/layout/ExercisingView.xaml b/Fitness/res/layout/ExercisingView.xaml index e350fe3..c1a58a1 100644 --- a/Fitness/res/layout/ExercisingView.xaml +++ b/Fitness/res/layout/ExercisingView.xaml @@ -5,9 +5,6 @@ xmlns:vm="clr-namespace:Fitness.ViewModels" xmlns:ctrl="clr-namespace:Fitness.Controls" xmlns:views="clr-namespace:Fitness.Views" - xmlns:nui="clr-namespace:Tizen.NUI.Components;assembly=Tizen.NUI.Components" - xmlns:behaviors="clr-namespace:Fitness.Views.Behaviors" - xmlns:styles="clr-namespace:Fitness.Views.Styles" xmlns:converters="clr-namespace:Fitness.Views.Converters" x:Name="Root" BackgroundColor="#EEEFF1"