Skip to content

Commit

Permalink
feat: Add Compass sensor Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
morning4coffe-dev committed Aug 15, 2023
1 parent e5b91a5 commit 77d5b7a
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_Devices\CompassTests.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_Devices\LightSensorTests.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -5515,6 +5519,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_Devices\LampTests.xaml.cs">
<DependentUpon>LampTests.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_Devices\CompassTests.xaml.cs">
<DependentUpon>CompassTests.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_Devices\LightSensorTests.xaml.cs">
<DependentUpon>LightSensorTests.xaml</DependentUpon>
</Compile>
Expand Down
39 changes: 39 additions & 0 deletions src/SamplesApp/UITests.Shared/Windows_Devices/CompassTests.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<UserControl x:Class="UITests.Shared.Windows_Devices.CompassTests"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Shared.Windows_Devices"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:valueconverters="using:UITests.ValueConverters"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<UserControl.Resources>
<valueconverters:BoolNegationValueConverter x:Key="BoolNegation" />
</UserControl.Resources>

<ContentControl IsEnabled="{Binding CompassAvailable}">
<StackPanel>
<TextBlock Text="{Binding SensorStatus}" />

<Button Command="{Binding AttachReadingChangedCommand}"
Content="Attach ReadingChanged"
IsEnabled="{Binding ReadingChangedAttached, Converter={StaticResource BoolNegation}}" />
<Button Command="{Binding DetachReadingChangedCommand}"
Content="Detach ReadingChanged"
IsEnabled="{Binding ReadingChangedAttached}" />

<TextBlock Text="Last reading"
Style="{ThemeResource SubtitleTextBlockStyle}" />
<TextBlock>
<Run FontWeight="Bold">HeadingMagneticNorth:</Run>
<Run Text="{Binding HeadingMagneticNorth}" />
</TextBlock>
<TextBlock>
<Run FontWeight="Bold">Timestamp: </Run>
<Run Text="{Binding Timestamp}" />
</TextBlock>
</StackPanel>
</ContentControl>
</UserControl>
132 changes: 132 additions & 0 deletions src/SamplesApp/UITests.Shared/Windows_Devices/CompassTests.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.Disposables;
using Uno.UI.Samples.Controls;
using Uno.UI.Samples.UITests.Helpers;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace UITests.Shared.Windows_Devices
{
[Sample(
"Windows.Devices",
Name = "Compass",
Description = "Demonstrates use of Windows.Devices.Sensors.Compass",
ViewModelType = typeof(CompassTestsViewModel),
IgnoreInSnapshotTests = true)]
public sealed partial class CompassTests : UserControl
{
public CompassTests()
{
this.InitializeComponent();

}

[Bindable]
internal class CompassTestsViewModel : ViewModelBase
{
private Compass _compass;
private bool _readingChangedAttached;
private string _sensorStatus;
private double _headingMagneticNorth;
private string _timestamp;

public CompassTestsViewModel(CoreDispatcher dispatcher) : base(dispatcher)
{

_compass = Compass.GetDefault();
if (_compass != null)
{
_compass.ReportInterval = 250;
SensorStatus = "Compass created";
}
else
{
SensorStatus = "Compass not available on this device";
}
Disposables.Add(Disposable.Create(() =>
{
if (_compass != null)
{
_compass.ReadingChanged -= Compass_ReadingChanged;
}
}));
}

public Command AttachReadingChangedCommand => new Command((p) =>
{
_compass.ReadingChanged += Compass_ReadingChanged;
ReadingChangedAttached = true;
});

public Command DetachReadingChangedCommand => new Command((p) =>
{
_compass.ReadingChanged -= Compass_ReadingChanged;
ReadingChangedAttached = false;
});

public bool CompassAvailable => _compass != null;

public string SensorStatus
{
get => _sensorStatus;
private set
{
_sensorStatus = value;
RaisePropertyChanged();
}
}

public bool ReadingChangedAttached
{
get => _readingChangedAttached;
private set
{
_readingChangedAttached = value;
RaisePropertyChanged();
}
}

public double HeadingMagneticNorth
{
get => _headingMagneticNorth;
private set
{
_headingMagneticNorth = value;
RaisePropertyChanged();
}
}

public string Timestamp
{
get => _timestamp;
private set
{
_timestamp = value;
RaisePropertyChanged();
}
}

private async void Compass_ReadingChanged(Compass sender, CompassReadingChangedEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
HeadingMagneticNorth = args.Reading.HeadingMagneticNorth;
Timestamp = args.Reading.Timestamp.ToString("R");
});
}
}
}
}

0 comments on commit 77d5b7a

Please sign in to comment.