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

feat: Add Proximity sample #1028

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<Page x:Class="Uno.Gallery.Views.Samples.ProximitySamplePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:smtx="using:ShowMeTheXAML"
xmlns:local="using:Uno.Gallery"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<local:SamplePageLayout IsDesignAgnostic="True">
<local:SamplePageLayout.DesignAgnosticTemplate>
<DataTemplate>
<StackPanel>
<smtx:XamlDisplay UniqueKey="ProximitySamplePage_Sample"
smtx:XamlDisplayExtensions.IgnorePath="XX"
smtx:XamlDisplayExtensions.Header="Proximity">
<StackPanel Spacing="20">

<TextBlock>
<LineBreak />
<Span FontWeight="Bold">Distance: </Span>
<Run Text="{Binding Data.Distance}" />
<LineBreak />
</TextBlock>

<Button IsEnabled="{Binding Data.ProximityAvailable}" Click="ObserveReadingChangeButton_Click">
<TextBlock Text="{Binding Data.ButtonContent}" VerticalAlignment="Stretch" TextAlignment="Center" />
</Button>
</StackPanel>
</smtx:XamlDisplay>
</StackPanel>
</DataTemplate>
</local:SamplePageLayout.DesignAgnosticTemplate>
</local:SamplePageLayout>
</Grid>

</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Linq;
using Uno.Gallery.ViewModels;
using Windows.ApplicationModel.Core;
using Windows.Devices.Enumeration;
using Windows.Devices.Sensors;
using Windows.UI.Core;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Uno.Gallery.Views.Samples
{
[SamplePage(SampleCategory.NonUIFeatures, "Proximity",
Description = "Represents a Proximit sensor. This sensor returns distance.",
DocumentationLink = "https://platform.uno/docs/articles/features/proximity-sensor.html",
DataType = typeof(ProximitySamplePageViewModel))]
public sealed partial class ProximitySamplePage : Page
{
public ProximitySamplePage ()
{
this.InitializeComponent();
}

private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e)
{
if ((sender as Button)?.DataContext is ProximitySamplePageViewModel viewModel)
{
if (!viewModel.ObservingReadingChange)
{
viewModel.StartObserveReadingChange();
}
else
{
viewModel.StopObservingReadingChange();
}
}
}
}

public class ProximitySamplePageViewModel : ViewModelBase
{
private const string _startObservingContent = "Start observing proximity reading changes";
private const string _stopObservingContent = "Stop observing proximity reading changes";
private string _notAvailable = "Proximity is not available on this device/platform";

private ProximitySensor _proximity;

public string ButtonContent
{
get => GetProperty<string>();
set => SetProperty<string>(value);
}

public bool ObservingReadingChange
{
get => GetProperty<bool>();
set => SetProperty<bool>(value);
}

public uint? Distance
{
get => GetProperty<uint?>();
set => SetProperty<uint?>(value);
}

public bool IsDetected
{
get => GetProperty<bool>();
set => SetProperty<bool>(value);
}

public bool ProximityAvailable => _proximity != null;



public ProximitySamplePageViewModel()
{
var selector = ProximitySensor.GetDeviceSelector();
var devices = DeviceInformation.FindAllAsync(selector).GetAwaiter().GetResult();
var device = devices.FirstOrDefault();
if (device is not null)
{
var proximitySensor = ProximitySensor.FromId(device.Id);
if (proximitySensor is not null)
{
_proximity = proximitySensor;
}
else
{
ButtonContent = _notAvailable;
}
}
else
{
ButtonContent = _notAvailable;
}

}

public void StartObserveReadingChange()
{
_proximity.ReadingChanged += Proximity_ReadingChanged;
ButtonContent = _stopObservingContent;
ObservingReadingChange = true;
}

public void StopObservingReadingChange()
{
_proximity.ReadingChanged -= Proximity_ReadingChanged;
ButtonContent = _startObservingContent;
ObservingReadingChange = false;
}

private async void Proximity_ReadingChanged(ProximitySensor sender, ProximitySensorReadingChangedEventArgs args)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Distance = args.Reading.DistanceInMillimeters;
IsDetected = args.Reading.IsDetected;
});
}
}
}