Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Essentials] Add DateTimeOffset overload in Preferences #22815

Merged
merged 10 commits into from
Jun 25, 2024
10 changes: 10 additions & 0 deletions src/Essentials/src/Preferences/Preferences.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public void Set<T>(string key, T value, string sharedName)
case DateTime dt:
editor.PutLong(key, dt.ToBinary());
break;
case DateTimeOffset dt:
editor.PutString(key, dt.ToString("O"));
break;
}
}
editor.Apply();
Expand Down Expand Up @@ -143,6 +146,13 @@ public T Get<T>(string key, T defaultValue, string sharedName)
var encodedValue = sharedPreferences.GetLong(key, dt.ToBinary());
value = DateTime.FromBinary(encodedValue);
break;
case DateTimeOffset dt:
var savedDateTimeOffset = sharedPreferences.GetString(key, dt.ToString("O"));
if (DateTimeOffset.TryParse(savedDateTimeOffset, out var dateTimeOffset))
{
value = dateTimeOffset;
}
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void Set<T>(string key, T value, string sharedName)
var encodedDateTime = Convert.ToString(dt.ToBinary(), CultureInfo.InvariantCulture);
userDefaults.SetString(encodedDateTime, key);
break;
case DateTimeOffset dt:
userDefaults.SetString(dt.ToString("O"), key);
break;
}
}
}
Expand Down Expand Up @@ -124,6 +127,13 @@ public T Get<T>(string key, T defaultValue, string sharedName)
var encodedDateTime = Convert.ToInt64(savedDateTime, CultureInfo.InvariantCulture);
value = DateTime.FromBinary(encodedDateTime);
break;
case DateTimeOffset dt:
var savedDateTimeOffset = userDefaults.StringForKey(key);
if (DateTimeOffset.TryParse(savedDateTimeOffset, out var dateTimeOffset))
{
value = dateTimeOffset;
}
break;
case string s:
// the case when the string is not null
value = userDefaults.StringForKey(key);
Expand Down
17 changes: 17 additions & 0 deletions src/Essentials/src/Preferences/Preferences.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ public static DateTime Get(string key, DateTime defaultValue) =>
/// <inheritdoc cref="Set(string, string?)"/>
public static void Set(string key, DateTime value) =>
Set(key, value, null);

/// <inheritdoc cref="Get(string, string?)"/>
public static DateTimeOffset Get(string key, DateTimeOffset defaultValue) =>
Get(key, defaultValue, null);

/// <inheritdoc cref="Set(string, string?)"/>
public static void Set(string key, DateTimeOffset value) =>
Set(key, value, null);

/// <inheritdoc cref="IPreferences.Get{T}(string, T, string?)"/>
public static DateTime Get(string key, DateTime defaultValue, string? sharedName) =>
Expand All @@ -238,6 +246,14 @@ public static DateTime Get(string key, DateTime defaultValue, string? sharedName
public static void Set(string key, DateTime value, string? sharedName) =>
Current.Set<DateTime>(key, value, sharedName);

/// <inheritdoc cref="IPreferences.Get{T}(string, T, string?)"/>
public static DateTimeOffset Get(string key, DateTimeOffset defaultValue, string? sharedName) =>
Current.Get<DateTimeOffset>(key, defaultValue, sharedName);

/// <inheritdoc cref="IPreferences.Set{T}(string, T, string?)"/>
public static void Set(string key, DateTimeOffset value, string? sharedName) =>
Current.Set<DateTimeOffset>(key, value, sharedName);

static IPreferences Current => Storage.Preferences.Default;

internal static string GetPrivatePreferencesSharedName(string feature) =>
Expand All @@ -263,6 +279,7 @@ internal static void SetDefault(IPreferences? implementation) =>
typeof(double),
typeof(float),
typeof(DateTime),
typeof(DateTimeOffset)
};

internal static void CheckIsSupportedType<T>()
Expand Down
11 changes: 11 additions & 0 deletions src/Essentials/src/Preferences/Preferences.tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public void Set<T>(string key, T value, string sharedName)
{
Preference.Set(fullKey, dt.ToBinary());
}
else if (value is DateTimeOffset dto)
{
Preference.Set(fullKey, dto.ToString("O"));
}
else
Preference.Set(fullKey, value);
}
Expand Down Expand Up @@ -85,6 +89,13 @@ public T Get<T>(string key, T defaultValue, string sharedName)
var encodedValue = Preference.Get<long>(fullKey);
value = (T)(object)DateTime.FromBinary(encodedValue);
break;
case DateTimeOffset dt:
var savedDateTimeOffset = Preference.Get<string>(fullKey);
if (DateTimeOffset.TryParse(savedDateTimeOffset, out var dateTimeOffset))
{
value = (T)(object)dateTimeOffset;
}
break;
default:
// the case when the string is null
if (typeof(T) == typeof(string))
Expand Down
28 changes: 28 additions & 0 deletions src/Essentials/src/Preferences/Preferences.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public void Set<T>(string key, T value, string sharedName)
{
appDataContainer.Values[key] = dt.ToBinary();
}
else if (value is DateTimeOffset dto)
{
appDataContainer.Values[key] = dto.ToString("O");
}
else
{
appDataContainer.Values[key] = value;
Expand All @@ -109,6 +113,13 @@ public T Get<T>(string key, T defaultValue, string sharedName)
{
return (T)(object)DateTime.FromBinary((long)tempValue);
}
else if (defaultValue is DateTimeOffset dto)
{
if (DateTimeOffset.TryParse((string)tempValue, out var dateTimeOffset))
{
return (T)(object)dateTimeOffset;
}
}
else
{
return (T)tempValue;
Expand Down Expand Up @@ -182,6 +193,10 @@ public void Set<T>(string key, T value, string sharedName = null)

if (value is null)
prefs.TryRemove(key, out _);
else if (value is DateTime dt)
prefs[key] = string.Format(CultureInfo.InvariantCulture, "{0}", dt.ToBinary());
else if (value is DateTimeOffset dto)
prefs[key] = dto.ToString("O");
else
prefs[key] = string.Format(CultureInfo.InvariantCulture, "{0}", value);

Expand All @@ -194,6 +209,19 @@ public T Get<T>(string key, T defaultValue, string sharedName = null)
{
if (inner.TryGetValue(key, out var value) && value is not null)
{
if (defaultValue is DateTime dt)
{
long tempValue = (long)Convert.ChangeType(value, typeof(long), CultureInfo.InvariantCulture);
return (T)(object)DateTime.FromBinary(tempValue);
}
else if (defaultValue is DateTimeOffset dto)
{
if (DateTimeOffset.TryParse((string)value, out var dateTimeOffset))
{
return (T)(object)dateTimeOffset;
}
}

try
{
return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
6 changes: 5 additions & 1 deletion src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ static Microsoft.Maui.Devices.Sensors.Geolocation.StopListeningForeground() -> v
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CapturePhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.CaptureVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickPhotoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
*REMOVED*Microsoft.Maui.Media.IMediaPicker.PickVideoAsync(Microsoft.Maui.Media.MediaPickerOptions? options = null) -> System.Threading.Tasks.Task<Microsoft.Maui.Storage.FileResult!>!
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue, string? sharedName) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Get(string! key, System.DateTimeOffset defaultValue) -> System.DateTimeOffset
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value, string? sharedName) -> void
static Microsoft.Maui.Storage.Preferences.Set(string! key, System.DateTimeOffset value) -> void
35 changes: 25 additions & 10 deletions src/Essentials/test/DeviceTests/Tests/Preferences_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.Maui.Storage;
using Xunit;

Expand Down Expand Up @@ -220,11 +221,7 @@ public void Not_ContainsKey(string sharedName)
Assert.False(Preferences.ContainsKey("NotContainsKey1", sharedName));
}

[Theory(
#if WINDOWS
Skip = "Fails on Windows unpackaged"
#endif
)]
[Theory]
[InlineData(null, DateTimeKind.Utc)]
[InlineData(sharedNameTestData, DateTimeKind.Utc)]
[InlineData(null, DateTimeKind.Local)]
Expand Down Expand Up @@ -455,11 +452,7 @@ public void Not_ContainsKey_NonStatic(string sharedName)
Assert.False(Preferences.Default.ContainsKey("NotContainsKey1", sharedName));
}

[Theory(
#if WINDOWS
Skip = "Fails on Windows unpackaged"
#endif
)]
[Theory]
[InlineData(null, DateTimeKind.Utc)]
[InlineData(sharedNameTestData, DateTimeKind.Utc)]
[InlineData(null, DateTimeKind.Local)]
Expand All @@ -475,6 +468,28 @@ public void DateTimePreservesKind_NonStatic(string sharedName, DateTimeKind kind
Assert.Equal(date, get);
Assert.Equal(kind, get.Kind);
}

public static IEnumerable<object[]> GetDateTimeOffsetData()
{
yield return [null, TimeSpan.Zero];
yield return [sharedNameTestData, TimeSpan.Zero];
yield return [null, TimeSpan.FromHours(1)];
yield return [sharedNameTestData, TimeSpan.FromHours(1)];
}

[Theory]
[MemberData(nameof(GetDateTimeOffsetData))]
public void DateTimeOffsetPreservesOffset_NonStatic(string sharedName, TimeSpan offset)
{
var date = new DateTime(2018, 05, 07, 8, 30, 0);
var dateTimeOffset = new DateTimeOffset(date, offset);

Preferences.Default.Set("datetimeoffset_offset", dateTimeOffset, sharedName);

var get = Preferences.Default.Get("datetimeoffset_offset", DateTimeOffset.MinValue, sharedName);

Assert.Equal(offset, get.Offset);
}
#endregion
}
}
Loading