From f5a15974e1d0031f136b6c736c049670da9397cb Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Mon, 25 Nov 2024 23:22:12 -0500 Subject: [PATCH 1/4] chore: organize code to validate for null --- .../SimpleOrientationSamplePage.xaml.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs index 2c3907a36..75e8f7883 100644 --- a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs @@ -7,7 +7,9 @@ namespace Uno.Gallery.Views.Samples { - [SamplePage(SampleCategory.NonUIFeatures, "Simple Orientation", Description = "This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.", DocumentationLink = "https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.simpleorientationsensor")] + [SamplePage( + SampleCategory.NonUIFeatures, + "Simple Orientation", Description = "This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.", DocumentationLink = "https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.simpleorientationsensor")] public sealed partial class SimpleOrientationSamplePage : Page { public SimpleOrientationSamplePage() @@ -17,7 +19,7 @@ public SimpleOrientationSamplePage() private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e) { - if ((sender as Button)?.DataContext is SimpleOrientationSamplePageViewModel viewModel) + if (sender is Button { DataContext: SimpleOrientationSamplePageViewModel viewModel }) { if (!viewModel.ObservingReadingChange) { @@ -35,34 +37,37 @@ public class SimpleOrientationSamplePageViewModel : ViewModelBase { private const string _startObservingContent = "Start observing orientation changes"; private const string _stopObservingContent = "Stop observing orientation changes"; - private const string _noSensorAvailable = "SimpleOrientationSensor is not available on this device/platform"; - - private readonly SimpleOrientationSensor _simpleOrientationSensor; + private const string _noSensorAvailableContent = "SimpleOrientationSensor is not available on this device/platform"; + private readonly SimpleOrientationSensor? _simpleOrientationSensor; public DateTimeOffset? LastReadTimestamp { get => GetProperty(); set => SetProperty(value); } - public string ButtonContent { get => GetProperty(); set => SetProperty(value); } public bool ObservingReadingChange { get => GetProperty(); set => SetProperty(value); } public SimpleOrientation Orientation { get => GetProperty(); set => SetProperty(value); } - public bool SimpleOrientationAvailable => _simpleOrientationSensor != null; + public bool SimpleOrientationAvailable => _simpleOrientationSensor is not null; public SimpleOrientationSamplePageViewModel() { _simpleOrientationSensor = SimpleOrientationSensor.GetDefault(); - if (_simpleOrientationSensor != null) + if (SimpleOrientationAvailable) { ButtonContent = _startObservingContent; } else { - ButtonContent = _noSensorAvailable; + ButtonContent = _noSensorAvailableContent; } } public void StartObserveReadingChange() { + if (_simpleOrientationSensor is null) + { + return; + } + _simpleOrientationSensor.OrientationChanged += SimpleOrientationSensor_OrientationChanged; ButtonContent = _stopObservingContent; ObservingReadingChange = true; @@ -70,7 +75,11 @@ public void StartObserveReadingChange() public void StopObservingReadingChange() { - _simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged; + if (_simpleOrientationSensor is not null) + { + _simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged; + } + ButtonContent = _startObservingContent; ObservingReadingChange = false; } From 08c72a397c7e0d152a219a43d99b36e33446bc01 Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Mon, 25 Nov 2024 23:43:03 -0500 Subject: [PATCH 2/4] fix(iOS): SimpleOrientationSensor sample --- .../SamplePages/SimpleOrientationSamplePage.xaml.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs index 75e8f7883..735735a67 100644 --- a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs @@ -2,6 +2,8 @@ using System.ComponentModel; using Uno.Gallery.ViewModels; using Windows.Devices.Sensors; +using Windows.ApplicationModel.Core; +using Windows.UI.Core; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -84,10 +86,13 @@ public void StopObservingReadingChange() ObservingReadingChange = false; } - private void SimpleOrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args) + private async void SimpleOrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args) { - LastReadTimestamp = args.Timestamp; - Orientation = args.Orientation; + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + LastReadTimestamp = args.Timestamp; + Orientation = args.Orientation; + }); } } } From 9c0db7e06a4031faf798010f82cddb4ac1c13857 Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Mon, 25 Nov 2024 23:43:59 -0500 Subject: [PATCH 3/4] chore: invert if logic --- .../Views/SamplePages/SimpleOrientationSamplePage.xaml.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs index 735735a67..5e929a3f4 100644 --- a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs @@ -23,13 +23,13 @@ private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e) { if (sender is Button { DataContext: SimpleOrientationSamplePageViewModel viewModel }) { - if (!viewModel.ObservingReadingChange) + if (viewModel.ObservingReadingChange) { - viewModel.StartObserveReadingChange(); + viewModel.StopObservingReadingChange(); } else { - viewModel.StopObservingReadingChange(); + viewModel.StartObserveReadingChange(); } } } From 44069a449abade97305ff1f0dd5cfeffe55aabce Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Mon, 25 Nov 2024 23:48:31 -0500 Subject: [PATCH 4/4] chore: accept review suggestion Co-authored-by: Xiaotian Gu --- .../Views/SamplePages/SimpleOrientationSamplePage.xaml.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs index 5e929a3f4..d8b424729 100644 --- a/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs +++ b/Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs @@ -11,7 +11,8 @@ namespace Uno.Gallery.Views.Samples { [SamplePage( SampleCategory.NonUIFeatures, - "Simple Orientation", Description = "This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.", DocumentationLink = "https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.simpleorientationsensor")] + "Simple Orientation", Description = "This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.", + DocumentationLink = "https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.simpleorientationsensor")] public sealed partial class SimpleOrientationSamplePage : Page { public SimpleOrientationSamplePage()