/* * Pedometer * Copyright (c) 2017 Yusuf Olokoba */ namespace PedometerU { using Platforms; using System; using System.Linq; public sealed class Pedometer : IDisposable { #region --Properties-- /// /// How many updates has this pedometer received? Useful for calculating pedometer precision /// public int updateCount {get; private set;} /// /// Pedometer implementation for the current device. Do not use unless you know what you are doing /// public static IPedometer Implementation { get { return _Implementation = _Implementation ?? new IPedometer[] { new PedometerAndroid(), new PedometeriOS(), new PedometerGPS() // Always supported, uses GPS (so highly inaccurate) }.First(impl => impl.IsSupported).Initialize(); } } private static IPedometer _Implementation; #endregion #region --Op vars-- private int initialSteps; // Some step counters count from device boot, so subtract the initial count we get private double initialDistance; private readonly StepCallback callback; #endregion #region --Ctor-- /// /// Create a new pedometer and start listening for updates /// public Pedometer (StepCallback callback) { // Save the callback this.callback = callback; // Register callback Implementation.OnStep += OnStep; } #endregion #region --Operations-- /// /// Stop listening for pedometer updates and dispose the object /// public void Dispose () { // Unregister callback Implementation.OnStep -= OnStep; } /// /// Release Pedometer and all of its resources /// public static void Release () { if (_Implementation == null) return; // Release and dereference _Implementation.Release(); _Implementation = null; } private void OnStep (int steps, double distance) { // DEPLOY // UpdateCount post increment // Set initials and increment update count initialSteps = updateCount++ == 0 ? steps : initialSteps; initialDistance = steps == initialSteps ? distance : initialDistance; // If this is not the first step, then invoke the callback if (steps != initialSteps) if (callback != null) callback(steps - initialSteps, distance - initialDistance); } #endregion } /// /// A delegate used to pass pedometer information /// /// Number of steps taken /// Distance walked in meters public delegate void StepCallback (int steps, double distance); }