Skip to content

Commit

Permalink
Merge pull request #1179 from unoplatform/fix/ios.simpleorientation.c…
Browse files Browse the repository at this point in the history
…rash

fix(iOS): SimpleOrientationSensor Sample Crash
  • Loading branch information
jeromelaban authored Nov 27, 2024
2 parents 493b9cd + 44069a4 commit 86ace90
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions Uno.Gallery/Views/SamplePages/SimpleOrientationSamplePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
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;

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()
Expand All @@ -17,15 +22,15 @@ 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)
if (viewModel.ObservingReadingChange)
{
viewModel.StartObserveReadingChange();
viewModel.StopObservingReadingChange();
}
else
{
viewModel.StopObservingReadingChange();
viewModel.StartObserveReadingChange();
}
}
}
Expand All @@ -35,50 +40,60 @@ 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<DateTimeOffset?>(); set => SetProperty(value); }

public string ButtonContent { get => GetProperty<string>(); set => SetProperty(value); }
public bool ObservingReadingChange { get => GetProperty<bool>(); set => SetProperty(value); }
public SimpleOrientation Orientation { get => GetProperty<SimpleOrientation>(); 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;
}

public void StopObservingReadingChange()
{
_simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged;
if (_simpleOrientationSensor is not null)
{
_simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged;
}

ButtonContent = _startObservingContent;
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;
});
}
}
}

0 comments on commit 86ace90

Please sign in to comment.