Handled no camera case.
authorAndrzej Krawczyk <a.krawczyk@samsung.com>
Tue, 24 Aug 2021 15:28:35 +0000 (17:28 +0200)
committerPiotr Czaja <p.czaja@samsung.com>
Tue, 14 Sep 2021 11:01:34 +0000 (13:01 +0200)
Fitness/Services/NavigationService.cs

index 03d3cbf2cb6ddc858a3ad6d720fc244021697261..1ed12aaab4454442f6038dcc8f56e5c935b66143 100644 (file)
@@ -5,6 +5,7 @@ using Fitness.Models;
 using Fitness.ViewModels;
 using Fitness.Views;
 using Tizen.NUI;
+using Tizen.NUI.Components;
 
 namespace Fitness.Services
 {
@@ -47,12 +48,14 @@ namespace Fitness.Services
         public async Task NavigateToExercisingView(WorkoutViewModel workoutViewModel)
         {
             var result = await PrivilegeService.Instance.CheckCameraPrivilege();
-            if (result == Tizen.Security.RequestResult.AllowForever)
+            if (result == Tizen.Security.RequestResult.AllowForever
+                && TryCreateCameraDependentView(
+                    () => new ExercisingView()
+                    {
+                        BindingContext = new ExercisingViewModel(workoutViewModel),
+                    },
+                    out Controls.Page view))
             {
-                var view = new ExercisingView()
-                {
-                    BindingContext = new ExercisingViewModel(workoutViewModel),
-                };
                 navigation.Push(view);
             }
         }
@@ -65,9 +68,11 @@ namespace Fitness.Services
         public async Task NavigateToScanningView()
         {
             var result = await PrivilegeService.Instance.CheckCameraPrivilege();
-            if (result == Tizen.Security.RequestResult.AllowForever)
+
+            if (result == Tizen.Security.RequestResult.AllowForever
+                && TryCreateCameraDependentView(() => new ScanningView(), out Controls.Page view))
             {
-                navigation.Push(new ScanningView());
+                navigation.Push(view);
             }
         }
 
@@ -80,5 +85,27 @@ namespace Fitness.Services
         {
             navigation.PopToRoot();
         }
+
+        private bool TryCreateCameraDependentView(Func<Controls.Page> constructor, out Controls.Page view)
+        {
+            const string Message = "Cannot find the camera.";
+
+            try
+            {
+                view = constructor();
+
+                return true;
+            }
+            catch (System.Reflection.TargetInvocationException e) when (e.InnerException is Tizen.Multimedia.CameraDeviceNotFoundException)
+            {
+                Notification
+                    .MakeToast(Message, Notification.ToastBottom)
+                    .Post(Notification.ToastLong);
+
+                view = null;
+
+                return false;
+            }
+        }
     }
 }