Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iOS): SimpleOrientationSensor Sample Crash #1179

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});
}
}
}
Loading