GithubHelp home page GithubHelp logo

0xc3u / plugin.maui.health Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 9.95 MB

The Plugin.Maui.Health provides access to Apple Health

License: MIT License

C# 100.00%
apple-healthkit dotnet ios maui plugin cross-platform fitness dotnet-maui

plugin.maui.health's Introduction

Plugin.Maui.Health

Plugin.Maui.Health provides the ability to access personal health data in your .NET MAUI application.

Build Status

ci

Install Plugin

NuGet

Available on NuGet.

Install with the dotnet CLI: dotnet add package Plugin.Maui.Health, or through the NuGet Package Manager in Visual Studio.

API Usage

Plugin.Maui.Health provides the HealthDataProvider class that has a single property Property that you can get or set.

You can either use it as a static class, e.g.: Health.Default.Property = 1 or with dependency injection: builder.Services.AddSingleton<IHealth>(Health.Default);

Platform supported

Platform Minimum Version Supported
iOS 14.0+
Android currently not supported

Permissions

Before you can start using Health, you will need to request the proper permissions on each platform.

iOS

You need to add permissions in your Info.plist file to read/write to the HealthKit store.

<key>NSHealthShareUsageDescription</key>
<string>We need access to your health data to read your steps and other metrics.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>We need access to write your steps and other metrics.</string>

Add ´Entitlements.plist´ to you iOS platform project.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>com.apple.developer.healthkit</key>
		<true/>
	</dict>
</plist>

Dependency Injection

You will first need to register the Health with the MauiAppBuilder following the same pattern that the .NET MAUI Essentials libraries follow.

builder.Services.AddSingleton(Health.Default);

You can then enable your classes to depend on IHealth as per the following example.

public class HealthViewModel
{
    readonly IHealth Health;

    public HealthViewModel(IHealth Health)
    {
        this.Health = Health;
    }
}

Straight usage

Alternatively if you want to skip using the dependency injection approach you can use the Health.Default property.

public class HealthViewModel
{
    public async Task<double> ReadStepsCountAsync()
    {
        	var hasPermission = await health.CheckPermissionAsync(Health.Enums.HealthParameter.StepCount, Health.Enums.PermissionType.Read | Health.Enums.PermissionType.Write);
			if (hasPermission)
			{
				return await health.ReadCountAsync(Enums.HealthParameter.StepCount, DateTime.Now.AddDays(-1), DateTime.Now);
			}
    }
}

Health Plugin

Once you have registered the Health plugin you can interact with it in the following ways:

Properties

  • IsSupported Gets a value indicating whether reading the Health is supported on this device.

Models

  • Sample Represents a health-related sample, containing information such as time range, value, source, unit and description.

Methods

  • CheckPermissionAsync Checks and requests the specified permissions for a given health parameter.
  • ReadCountAsync Reads the cumulative count of a specified "HealthParameter" within a given date range.
  • ReadLatestAsync Reads the latest health data value for a specified "HealthParameter" within a given date range.
  • ReadLatestAvailableAsync Reads the latest health data available value for a specified "HealthParameter" within a given date range and returns a Sample object.
  • ReadAverageAsync Reads the average value of a specified "HealthParameter" within a given date range.
  • ReadMinAsync Reads the min value of a specified "HealthParameter" within a given date range.
  • ReadMaxAsync Reads the max value of a specified "HealthParameter" within a given date range.
  • ReadAllAsync Reads all health data for a specified "HealthParameter" within a given date range and returns them as a list of Sample objects.
  • WriteAsync Writes a value for a specified "HealthParameter" to the HealthKit store.

Examples

Checking Permissions
	// check if user granted permission for reading and writing the step count
	var hasPermission = await health.CheckPermissionAsync(Health.Enums.HealthParameter.StepCount, Health.Enums.PermissionType.Write);
Reading a value
	// reading the steps count from the health kit
	var hasPermission = await health.CheckPermissionAsync(Health.Enums.HealthParameter.StepCount, Health.Enums.PermissionType.Write);
	if (hasPermission)
	{
		var stepsCount = await health.ReadCountAsync(Enums.HealthParameter.StepCount, DateTime.Now.AddDays(-1), DateTime.Now);
	}
Writing a value
	// writing the body mass into the health kit

	var hasPermission = await Health.CheckPermissionAsync(HealthParameter.BodyMass, PermissionType.Read | PermissionType.Write);
	if (hasPermission)
	{
		await Health.WriteAsync(HealthParameter.BodyMass, DateTime.Now, NewBodyMass, Units.Mass.Kilograms);
	}

Sample App

Screenshot of the sample app - Dashboard Screenshot of the sample app - Vitamins Screenshot of the sample app - Body Measurements Screenshot of the permission granted to the sample app

Try the sample app to test the plugin by your own.

Remarks

When using the Plugin, make sure that you pass the correct HealthParameter with the corresponding unit to the methods. There is a utility class called Constants.Units that contains the supported units.

Acknowledgements

This project could not have came to be without these projects and people, thank you! <3

plugin.maui.health's People

Contributors

0xc3u avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

plugin.maui.health's Issues

ReadLatestAsync can be interpreted incorrectly

Currently, when we call ReadLatestAsync, we get the latest value within a given date range (with no further information).

However, ReadLatestAsync can also be understood as getting the latest available value.

Please add a new function to get the latest available value.
e.g. Task<Sample> ReadLatestAvailableAsync(HealthParameter healthParameter, string unit)
By returning a Sample here, we can get the information about when the data was recorded.

Forecast on compatibility with .NET 8 and Android

Hello!

First of all, hank you very much for the plugin!

I would like to know if there is an intent to upgrade it to .NET 8 in the near future and add Android compatibility as well. .NET 8 and MAUI are still in early stages, I understand, but in my company we are looking forward to using these technologies soon and this plugin would help a lot.

best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.