Hide buttons in exercising view when the screen is inactive.
authorPiotr Czaja/Advanced Frameworks (PLT) /SRPOL/Engineer/Samsung Electronics <p.czaja@samsung.com>
Wed, 25 Aug 2021 13:31:49 +0000 (15:31 +0200)
committerPiotr Czaja <p.czaja@samsung.com>
Tue, 14 Sep 2021 11:01:34 +0000 (13:01 +0200)
Fitness/Services/ScreenActivityService.cs [new file with mode: 0644]
Fitness/Views/ExercisingView.xaml.cs
Fitness/res/layout/ExercisingView.xaml
Fitness/res/layout/PlayingView.xaml

diff --git a/Fitness/Services/ScreenActivityService.cs b/Fitness/Services/ScreenActivityService.cs
new file mode 100644 (file)
index 0000000..1d83aea
--- /dev/null
@@ -0,0 +1,73 @@
+using System;
+using Tizen.NUI;
+
+namespace Fitness.Services
+{
+    /// <summary>
+    /// Manages screen activity.
+    /// </summary>
+    public class ScreenActivityService : IDisposable
+    {
+        private readonly Window win = NUIApplication.GetDefaultWindow();
+        private Timer activityTimer;
+        private uint activatyInterval = 3000;
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="ScreenActivityService"/> class.
+        /// </summary>
+        public ScreenActivityService()
+        {
+            activityTimer = new Timer(activatyInterval);
+            activityTimer.Tick += ActivatyTimer_Tick;
+            win.TouchEvent += Win_TouchEvent;
+        }
+
+        /// <summary>
+        /// Raised when the screen is touched.
+        /// </summary>
+        public event EventHandler ScreenActivated;
+
+        /// <summary>
+        /// Raised when the screen is deactivated.
+        /// </summary>
+        public event EventHandler ScreenDeactivated;
+
+        /// <summary>
+        /// Starts the screen activity timer.
+        /// </summary>
+        public void StartTimer()
+        {
+            activityTimer.Start();
+        }
+
+        /// <summary>
+        /// Stops the screen activity timer.
+        /// </summary>
+        public void StopTimer()
+        {
+            activityTimer.Stop();
+        }
+
+        /// <summary>
+        /// Disposes activity timer.
+        /// </summary>
+        public void Dispose()
+        {
+            activityTimer?.Dispose();
+        }
+
+        private void Win_TouchEvent(object sender, Window.TouchEventArgs e)
+        {
+            if (e.Touch.GetState(0) == PointStateType.Down)
+            {
+                ScreenActivated?.Invoke(this, EventArgs.Empty);
+            }
+        }
+
+        private bool ActivatyTimer_Tick(object source, Timer.TickEventArgs e)
+        {
+            ScreenDeactivated?.Invoke(this, EventArgs.Empty);
+            return false;
+        }
+    }
+}
index bb55f45adc8d7911fa05ea38762f73e2e3955483..c33697550d23251dc964a89e4f6e03eb76a9ddea 100644 (file)
@@ -2,6 +2,7 @@ using System.Linq;
 using System.Threading;
 using System.Threading.Tasks;
 using Fitness.Models;
+using Fitness.Services;
 using Fitness.ViewModels;
 using Tizen.Multimedia;
 using Tizen.NUI;
@@ -24,6 +25,7 @@ namespace Fitness.Views
 
         private const int TransitionTime = 500;
         private const string UnhandledWorkoutStateMessage = "Unhandled workout state.";
+        private ScreenActivityService screenActivityService;
         private CancellationTokenSource source;
         private bool isInitialized = false;
         private (Coordinates Preview, Coordinates Camera) playing;
@@ -48,6 +50,16 @@ namespace Fitness.Views
             Initialize();
         }
 
+        /// <summary>
+        /// Dispose.
+        /// </summary>
+        /// <param name="type">Dispose type.</param>
+        protected override void Dispose(DisposeTypes type)
+        {
+            screenActivityService?.Dispose();
+            base.Dispose(type);
+        }
+
         /// <summary>
         /// OnDisappearing.
         /// </summary>
@@ -58,6 +70,9 @@ namespace Fitness.Views
                 cameraView.Preview -= DetectPreview;
                 cameraView.StopPreview();
             }
+
+            screenActivityService.ScreenActivated -= OnScreenActivated;
+            screenActivityService.ScreenDeactivated -= OnScreenDeactivated;
         }
 
         /// <summary>
@@ -70,6 +85,9 @@ namespace Fitness.Views
                 cameraView.StartPreview();
                 cameraView.Preview += DetectPreview;
             }
+
+            screenActivityService.ScreenActivated += OnScreenActivated;
+            screenActivityService.ScreenDeactivated += OnScreenDeactivated;
         }
 
         private static void OnWorkoutStateChanged(BindableObject bindable, object oldValue, object newValue)
@@ -124,6 +142,7 @@ namespace Fitness.Views
 
             PlayingView.PreviewStub.Relayout += OnPlayingViewRelayout;
             cameraView.Preview += DetectPreview;
+            screenActivityService = new ScreenActivityService();
         }
 
         private void ExecuteNewState(WorkoutState newState, WorkoutState oldState)
@@ -137,6 +156,11 @@ namespace Fitness.Views
             source?.Dispose();
             source = new CancellationTokenSource();
 
+            if (oldState == WorkoutState.Playing)
+            {
+                screenActivityService.StopTimer();
+            }
+
             switch (newState)
             {
                 case WorkoutState.Loading:
@@ -145,6 +169,7 @@ namespace Fitness.Views
 
                 case WorkoutState.Playing:
                     _ = ExecutePlayingState(oldState);
+                    OnScreenActivated();
                     break;
 
                 case WorkoutState.OnHold:
@@ -275,6 +300,24 @@ namespace Fitness.Views
             }
         }
 
+        private void OnScreenActivated(object sender, System.EventArgs e)
+        {
+            OnScreenActivated();
+        }
+
+        private void OnScreenActivated()
+        {
+            UpperBar.Show();
+            PlayingView.BottomBar.Show();
+            screenActivityService.StartTimer();
+        }
+
+        private void OnScreenDeactivated(object sender, System.EventArgs e)
+        {
+            UpperBar.Hide();
+            PlayingView.BottomBar.Hide();
+        }
+
         private void OnPlayingViewRelayout(object sender, System.EventArgs e)
         {
             SetPositionAndSize();
index fdd427fd5ad6d2bc0886377109d2392d2be1510d..987d738fc408c0d6593482d2018b8734ed375dc8 100644 (file)
@@ -48,7 +48,8 @@
     
     <!--Layer-->
 
-    <views:BarView BindingContext="{Binding Source={x:Reference Root}, Path=BindingContext}"
+    <views:BarView x:Name="UpperBar"
+                   BindingContext="{Binding Source={x:Reference Root}, Path=BindingContext}"
                    PrevCommand="{Binding PreviousWorkout}"
                    NextCommand="{Binding NextWorkout}"
                    Title="{Binding CurrentWorkout.Title}"/>
index 15413262ef569854a6c4ee0022e8de755418efb2..bc67a7d50e36f60ec6f7d8fbd0bde0d8a766b06a 100644 (file)
           Weight="1"/>
     
     <!--Bar-->
-    <View Size="{views:SizeInUnits Height=30}"
+    <View x:Name="BottomBar"
+          Size="{views:SizeInUnits Height=30}"
           WidthSpecification="{Static LayoutParamPolicies.MatchParent}">
         <ctrl:NinePatchButton BindingContext="{Binding Source={x:Reference Root}, Path=BindingContext}"
                               PositionUsesPivotPoint="True"