Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Vastly simplify api
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Dec 14, 2023
1 parent 556eb09 commit 3535749
Show file tree
Hide file tree
Showing 20 changed files with 1,083 additions and 930 deletions.
16 changes: 12 additions & 4 deletions Sample/HealthTestPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,24 @@
<TableRoot>
<TableSection>
<TextCell Text="Calories (total - kilocalories)"
Detail="{Binding Calories}" />
Detail="{Binding Calories}"
Command="{Binding NavToList}"
CommandParameter="Calories" />

<TextCell Text="Distance (total - meters)"
Detail="{Binding Distance}" />
Detail="{Binding Distance}"
Command="{Binding NavToList}"
CommandParameter="Distance" />

<TextCell Text="Heart Rate (avg)"
Detail="{Binding HeartRate}" />
Detail="{Binding HeartRate}"
Command="{Binding NavToList}"
CommandParameter="HeartRate" />

<TextCell Text="Steps (total)"
Detail="{Binding Steps}" />
Detail="{Binding Steps}"
Command="{Binding NavToList}"
CommandParameter="StepCount" />
</TableSection>
</TableRoot>
</TableView>
Expand Down
46 changes: 30 additions & 16 deletions Sample/HealthTestViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Shiny.Health;
using System.Linq;
using Shiny.Health;

namespace Sample;

Expand All @@ -15,17 +16,17 @@ IHealthService health

this.Load = ReactiveCommand.CreateFromTask(async () =>
{
var result = await health.RequestPermission(
new Permission(DistanceHealthMetric.Default, PermissionType.Read),
new Permission(CaloriesHealthMetric.Default, PermissionType.Read),
new Permission(StepCountHealthMetric.Default, PermissionType.Read),
new Permission(HeartRateHealthMetric.Default, PermissionType.Read)
var result = await health.RequestPermissions(
DataType.Calories,
DataType.Distance,
DataType.HeartRate,
DataType.StepCount
);
if (!result)
{
await this.Dialogs.Alert("Failed permission check");
return;
}
//if (!result)
//{
// await this.Dialogs.Alert("Failed permission check");
// return;
//}

if (this.Start < this.End)
{
Expand All @@ -41,21 +42,34 @@ IHealthService health
return;
}

this.Distance = (await health.Query(DistanceHealthMetric.Default, this.Start, this.End, Interval.Days)).Sum(x => x.Value);
this.Calories = (await health.Query(CaloriesHealthMetric.Default, this.Start, this.End, Interval.Days)).Sum(x => x.Value);
this.Steps = (await health.Query(StepCountHealthMetric.Default, this.Start, this.End, Interval.Days)).Sum(x => x.Value);
this.HeartRate = (await health.Query(HeartRateHealthMetric.Default, this.Start, this.End, Interval.Days)).Average(x => x.Value);
this.Distance = (await health.GetDistances(this.Start, this.End, Interval.Days)).Sum(x => x.Value);
this.Calories = (await health.GetCalories(this.Start, this.End, Interval.Days)).Sum(x => x.Value);
this.Steps = (await health.GetStepCounts(this.Start, this.End, Interval.Days)).Sum(x => x.Value);
this.HeartRate = (await health.GetAverageHeartRate(this.Start, this.End, Interval.Days)).Average(x => x.Value);
});
this.BindBusyCommand(this.Load);

this.NavToList = ReactiveCommand.CreateFromTask(async (string arg) =>
{
var type = Enum.Parse<DataType>(arg);
await this.Navigation.Navigate("ListPage", ("Type", type));
});
}


public override void OnAppearing()
{
base.OnAppearing();
this.Load.Execute(null);
}

public ICommand Load { get; }
public ICommand NavToList { get; }
[Reactive] public DateTimeOffset Start { get; set; }
[Reactive] public DateTimeOffset End { get; set; }

[Reactive] public string? ErrorText { get; private set; }
[Reactive] public int Steps { get; private set; }
[Reactive] public double Steps { get; private set; }
[Reactive] public double Calories { get; private set; }
[Reactive] public double Distance { get; private set; }
[Reactive] public double HeartRate { get; private set; }
Expand Down
65 changes: 65 additions & 0 deletions Sample/ListPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sample="clr-namespace:Sample"
xmlns:health="clr-namespace:Shiny.Health;assembly=Shiny.Health"
x:DataType="sample:ListViewModel"
x:Class="Sample.ListPage"
Title="{Binding Title}">

<StackLayout HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">

<Grid RowDefinitions="Auto, Auto, Auto"
ColumnDefinitions="1*, 2*, 2*">

<Label Text="Start Date"
Grid.Column="0"
Grid.Row="0" />

<DatePicker Date="{Binding DateStart}"
Grid.Column="1"
Grid.Row="0" />

<TimePicker Time="{Binding TimeStart}"
Grid.Column="2"
Grid.Row="0" />

<Label Text="End Date"
Grid.Column="0"
Grid.Row="1" />

<DatePicker Date="{Binding DateEnd}"
Grid.Column="1"
Grid.Row="1" />

<TimePicker Time="{Binding TimeEnd}"
Grid.Column="2"
Grid.Row="1" />
</Grid>

<Button Text="Run Query"
Command="{Binding Load}" />

<CollectionView ItemsSource="{Binding Data}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<CollectionView.EmptyView>
<Label Text="No Items" />
</CollectionView.EmptyView>

<CollectionView.ItemTemplate>
<DataTemplate x:DataType="health:NumericHealthResult">
<StackLayout Padding="0,5">
<StackLayout Orientation="Horizontal" Spacing="5">
<Label FontAttributes="Bold" Text="{Binding Start, StringFormat='{0:MM/dd/yyyy a\\t h:mm tt}'}" />
<Label FontAttributes="Bold" Text="to" />
<Label FontAttributes="Bold" Text="{Binding End, StringFormat='{0:MM/dd/yyyy a\\t h:mm tt}'}" />
</StackLayout>
<Label Text="{Binding Value, StringFormat='Value: {0}'}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage>
9 changes: 9 additions & 0 deletions Sample/ListPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Sample;

public partial class ListPage : ContentPage
{
public ListPage()
{
InitializeComponent();
}
}
83 changes: 83 additions & 0 deletions Sample/ListViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Shiny.Health;

namespace Sample;


public class ListViewModel : ViewModel
{
DataType type;

public ListViewModel(BaseServices services, IHealthService health) : base(services)
{
this.DateStart = DateTime.Now.AddDays(-1);
this.DateEnd = DateTime.Now;

this.Load = ReactiveCommand.CreateFromTask(
async () =>
{
var start = this.DateStart.Date.Add(this.TimeStart);
var end = this.DateEnd.Date.Add(this.TimeEnd);

switch (this.type)
{
case DataType.StepCount:
this.Data = (await health.GetStepCounts(start, end, Interval.Hours))
.Cast<object>()
.ToList();
break;

case DataType.HeartRate:
this.Data = (await health.GetAverageHeartRate(start, end, Interval.Hours))
.Cast<object>()
.ToList();
break;

case DataType.Calories:
this.Data = (await health.GetCalories(start, end, Interval.Hours))
.Cast<object>()
.ToList();
break;

case DataType.Distance:
this.Data = (await health.GetDistances(start, end, Interval.Hours))
.Cast<object>()
.ToList();
break;
}
}
);
}


public ICommand Load { get; }

[Reactive] public DateTime DateStart { get; set; }
[Reactive] public TimeSpan TimeStart { get; set; }
[Reactive] public DateTime DateEnd { get; set; }
[Reactive] public TimeSpan TimeEnd { get; set; }
[Reactive] public IList<object> Data { get; private set; }

public override void OnNavigatedTo(INavigationParameters parameters)
{
this.type = parameters.GetValue<DataType>("Type");
switch (this.type)
{
case DataType.Calories:
this.Title = "Total Calories";
break;

case DataType.Distance:
this.Title = "Total Distance";
break;

case DataType.HeartRate:
this.Title = "Average Heart Rate";
break;

case DataType.StepCount:
this.Title = "Total Steps";
break;
}
this.Load.Execute(null);
}
}
1 change: 1 addition & 0 deletions Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static MauiAppBuilder RegisterInfrastructure(this MauiAppBuilder builder)
static MauiAppBuilder RegisterViews(this MauiAppBuilder builder)
{
builder.Services.RegisterForNavigation<HealthTestPage, HealthTestViewModel>();
builder.Services.RegisterForNavigation<ListPage, ListViewModel>();
return builder;
}
}
4 changes: 2 additions & 2 deletions Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!--<TargetFrameworks>$(TargetFrameworks);net8.0-ios</TargetFrameworks>-->
<TargetFrameworks>$(TargetFrameworks);net8.0-ios</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);net8.0-android</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>Sample</RootNamespace>
Expand Down Expand Up @@ -44,7 +44,7 @@
<PackageReference Include="CommunityToolkit.Maui" Version="7.0.0" />
<PackageReference Include="Prism.DryIoc.Maui" Version="9.0.271-pre" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.1" />
<PackageReference Include="Shiny.Framework" Version="4.0.0-beta-0013" />
<PackageReference Include="Shiny.Framework" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.3" />
</ItemGroup>
Expand Down
9 changes: 0 additions & 9 deletions Shiny.Health/Extensions.cs

This file was deleted.

34 changes: 0 additions & 34 deletions Shiny.Health/HealthMetric.cs

This file was deleted.

Loading

0 comments on commit 3535749

Please sign in to comment.