Skip to content

Commit

Permalink
Merge pull request #698 from morning4coffe-dev/dev/doti/barometer-sample
Browse files Browse the repository at this point in the history
feat: Add Barometer sample
  • Loading branch information
agneszitte authored Jul 2, 2024
2 parents f9eacf8 + 79a6ed1 commit 283b680
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<Page x:Class="Uno.Gallery.Views.Samples.BarometerSamplePage"
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="BarometerSamplePage_Sample"
smtx:XamlDisplayExtensions.IgnorePath="XX"
smtx:XamlDisplayExtensions.Header="Barometer">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<!-- C# code
var barometer = Barometer.GetDefault();
barometer.ReadingChanged += Barometer_ReadingChanged;
private async void Barometer_ReadingChanged(Barometer sender, BarometerReadingChangedEventArgs args)
{
// If you want to update the UI in some way, ensure the Dispatcher is used,
// as the ReadingChanged event handler does not run on the UI thread.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OutputTextBlock.Text = $"Sensor reading in hPa = {args.Reading.StationPressureInHectopascals}, " +
$"timestamp = {args.Reading.Timestamp}";
});
}
-->

<TextBlock Visibility="{Binding Data.BarometerAvailable}"
Margin="0,0,0,20">
<LineBreak />
<Span FontWeight="Bold">Pressure (hPA): </Span>
<Run Text="{Binding Data.Pressure}" />
<LineBreak />
<Span FontWeight="Bold">Last read timestamp: </Span>
<Run Text="{Binding Data.LastReadTimestamp}" />
</TextBlock>

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

</local:SamplePageLayout>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using Uno.Gallery.ViewModels;
using Windows.Devices.Sensors;

namespace Uno.Gallery.Views.Samples
{
[SamplePage(SampleCategory.NonUIFeatures, "Barometer",
Description = "Represents a Barometer sensor. This sensor returns pressure in hectopascals (hPa).",
DocumentationLink = "https://platform.uno/docs/articles/features/barometer.html",
DataType = typeof(BarometerSamplePageViewModel))]
public sealed partial class BarometerSamplePage : Page
{
public BarometerSamplePage()
{
this.InitializeComponent();
}

private void ObserveReadingChangeButton_Click(object sender, RoutedEventArgs e)
{
if (!(((Sample)DataContext).Data is BarometerSamplePageViewModel viewModel))
return;


if (!viewModel.ObservingReadingChange)
{
viewModel.StartObserveReadingChange();
}
else
{
viewModel.StopObservingReadingChange();
}
}
}
}

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

private readonly Barometer _barometer;

public double Pressure { get => GetProperty<double>(); set => SetProperty(value); }
public string ButtonContent { get => GetProperty<string>(); set => SetProperty(value); }
public DateTimeOffset? LastReadTimestamp { get => GetProperty<DateTimeOffset?>(); set => SetProperty(value); }
public bool ObservingReadingChange { get => GetProperty<bool>(); set => SetProperty(value); }

public bool BarometerAvailable => _barometer != null;

public BarometerSamplePageViewModel()
{
_barometer = Barometer.GetDefault();

if (BarometerAvailable)
{
ButtonContent = _startObservingContent;
return;
}

ButtonContent = _notAvailable;
}

public void StartObserveReadingChange()
{
_barometer.ReadingChanged += Barometer_ReadingChanged;

Check warning on line 68 in Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Windows.Devices.Sensors.Barometer.ReadingChanged is not implemented in Uno (https://aka.platform.uno/notimplemented?m=Windows.Devices.Sensors.Barometer.ReadingChanged) (https://aka.platform.uno/notimplemented)
ButtonContent = _stopObservingContent;
ObservingReadingChange = true;
}

public void StopObservingReadingChange()
{
_barometer.ReadingChanged -= Barometer_ReadingChanged;

Check warning on line 75 in Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/BarometerSamplePage.xaml.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Windows.Devices.Sensors.Barometer.ReadingChanged is not implemented in Uno (https://aka.platform.uno/notimplemented?m=Windows.Devices.Sensors.Barometer.ReadingChanged) (https://aka.platform.uno/notimplemented)
ButtonContent = _startObservingContent;
ObservingReadingChange = false;
}

private void Barometer_ReadingChanged(Barometer sender, BarometerReadingChangedEventArgs args)
{
LastReadTimestamp = args.Reading.Timestamp;
Pressure = args.Reading.StationPressureInHectopascals;
}
}

0 comments on commit 283b680

Please sign in to comment.