public static object ConvertToObject(NSObject obj)
        {
            object val = null;

            if (obj != null)
            {
                if (obj is NSString)
                {
                    string s = obj.ToString();

                    //allows us to persist datetimes with offsets in a roundtrippable format (NSDate doesn't do timezones)
                    DateTimeOffset dt;
                    if (DateTimeOffset.TryParse(s, out dt))
                    {
                        return(dt);
                    }

                    return(s);
                }
                else if (obj is NSDate)
                {
                    return(DateTimeOffsetHelper.FromNSDate((NSDate)obj));
                }
                else if (obj is NSUuid)
                {
                    return(new Guid(((NSUuid)obj).GetBytes()));
                }
                else if (obj is NSDecimalNumber)
                {
                    return(decimal.Parse(obj.ToString(), CultureInfo.InvariantCulture));
                }
                else if (obj is NSNumber)
                {
                    var x = (NSNumber)obj;
                    switch (x.ObjCType)
                    {
                    case "c":
                        return(x.BoolValue);

                    case "l":
                    case "i":
                        return(x.Int32Value);

                    case "s":
                        return(x.Int16Value);

                    case "q":
                        return(x.Int64Value);

                    case "Q":
                        return(x.UInt64Value);

                    case "C":
                        return(x.ByteValue);

                    case "L":
                    case "I":
                        return(x.UInt32Value);

                    case "S":
                        return(x.UInt16Value);

                    case "f":
                        return(x.FloatValue);

                    case "d":
                        return(x.DoubleValue);

                    case "B":
                        return(x.BoolValue);

                    default:
                        return(x.ToString());
                    }
                }
                else if (obj.GetType() == typeof(NSDate))
                {
                    val = DateTimeOffsetHelper.FromNSDate((NSDate)obj);
                }
                else if (obj is NSArray)
                {
                    NSArray  array  = obj as NSArray;
                    object[] oarray = new object[array.Count];
                    for (int i = 0; i < (int)array.Count; i++)
                    {
                        oarray[i] = ConvertToObject(array.GetItem <NSObject>((nuint)i));
                    }
                    val = oarray;
                }
                else if (obj is NSNull)
                {
                    val = null;
                }
                else
                {
                    val = obj;
                }
            }

            return(val);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the current step information from the pedometer sensor.
        /// </summary>
        /// <returns></returns>
        public IReadOnlyDictionary <PedometerStepKind, PedometerReading> GetCurrentReadings()
        {
#if WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP
            var rawReadings = _pedometer.GetCurrentReadings();
            Dictionary <PedometerStepKind, PedometerReading> readings = new Dictionary <Sensors.PedometerStepKind, Sensors.PedometerReading>();
            foreach (KeyValuePair <Windows.Devices.Sensors.PedometerStepKind, Windows.Devices.Sensors.PedometerReading> r in rawReadings)
            {
                readings.Add((PedometerStepKind)((int)r.Key), r.Value);
            }

            return(readings);
#elif __IOS__
            Dictionary <PedometerStepKind, PedometerReading> readings = new Dictionary <PedometerStepKind, PedometerReading>();
            _lastData = null;
            _pedometer.QueryPedometerData(NSDate.FromTimeIntervalSince1970(DateTimeOffset.Now.Subtract(DateTimeOffset.Now.TimeOfDay).ToUnixTimeSeconds()), NSDate.FromTimeIntervalSince1970(DateTimeOffset.Now.ToUnixTimeSeconds()), PedometerDataHandler);
            _handle.WaitOne();
            if (_lastData != null)
            {
                readings.Add(PedometerStepKind.Unknown, new PedometerReading(_lastData.NumberOfSteps.Int32Value, TimeSpan.FromSeconds(_lastData.EndDate.SecondsSinceReferenceDate - _lastData.StartDate.SecondsSinceReferenceDate), PedometerStepKind.Unknown, DateTimeOffsetHelper.FromNSDate(_lastData.EndDate)));
            }

            return(readings);
#elif TIZEN
            Dictionary <PedometerStepKind, PedometerReading> readings = new Dictionary <Sensors.PedometerStepKind, Sensors.PedometerReading>();
            readings.Add(PedometerStepKind.Running, new PedometerReading((int)_pedometer.RunStepCount, TimeSpan.Zero, PedometerStepKind.Running, DateTimeOffset.Now));
            readings.Add(PedometerStepKind.Walking, new PedometerReading((int)_pedometer.WalkStepCount, TimeSpan.Zero, PedometerStepKind.Walking, DateTimeOffset.Now));
            return(readings);
#else
            throw new PlatformNotSupportedException();
#endif
        }