Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 92 additions & 14 deletions src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@
var scale = (float)targetWidth.Value / original.Width;
var newHeight = (int)(original.Height * scale);

using var resized = original.Resize(new SkiaSharp.SKImageInfo(targetWidth.Value, newHeight), SkiaSharp.SKFilterQuality.Medium);

Check warning on line 906 in src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-latest)

'SKBitmap.Resize(SKImageInfo, SKFilterQuality)' is obsolete: 'Use Resize(SKImageInfo info, SKSamplingOptions sampling) instead.'

Check warning on line 906 in src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-latest)

'SKFilterQuality' is obsolete: 'Use SKSamplingOptions instead.'

Check warning on line 906 in src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest)

'SKBitmap.Resize(SKImageInfo, SKFilterQuality)' is obsolete: 'Use Resize(SKImageInfo info, SKSamplingOptions sampling) instead.'

Check warning on line 906 in src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest)

'SKFilterQuality' is obsolete: 'Use SKSamplingOptions instead.'
if (resized == null) return pngData;

using var image = SkiaSharp.SKImage.FromBitmap(resized);
Expand Down Expand Up @@ -3609,10 +3609,8 @@
var entries = new List<object>();
foreach (var key in keys.OrderBy(k => k))
{
var value = sharedName != null
? Preferences.Get(key, (string?)null, sharedName)
: Preferences.Get(key, (string?)null);
entries.Add(new { key, value, sharedName });
var (value, type) = ReadPreferenceValue(key, sharedName);
entries.Add(new { key, value, type, sharedName });
}
return Task.FromResult(HttpResponse.Json(new { keys = entries }));
}
Expand All @@ -3622,6 +3620,76 @@
}
}

/// <summary>
/// Read a preference value trying all supported types.
/// Android SharedPreferences stores typed values and throws ClassCastException
/// if you read with the wrong type, so we try each in turn.
/// </summary>
private (object? Value, string Type) ReadPreferenceValue(string key, string? sharedName)
{
// Try string first (most common)
try
{
var s = sharedName != null
? Preferences.Get(key, (string?)null, sharedName)
: Preferences.Get(key, (string?)null);
return (s, "string");
}
catch { }

// Try int
try
{
var i = sharedName != null
? Preferences.Get(key, int.MinValue, sharedName)
: Preferences.Get(key, int.MinValue);
return (i, "int");
}
catch { }

// Try bool
try
{
var b = sharedName != null
? Preferences.Get(key, false, sharedName)
: Preferences.Get(key, false);
return (b, "bool");
}
catch { }

// Try double
try
{
var d = sharedName != null
? Preferences.Get(key, double.NaN, sharedName)
: Preferences.Get(key, double.NaN);
if (!double.IsNaN(d)) return (d, "double");
}
catch { }

// Try long
try
{
var l = sharedName != null
? Preferences.Get(key, long.MinValue, sharedName)
: Preferences.Get(key, long.MinValue);
return (l, "long");
}
catch { }

// Try float
try
{
var f = sharedName != null
? Preferences.Get(key, float.NaN, sharedName)
: Preferences.Get(key, float.NaN);
if (!float.IsNaN(f)) return (f, "float");
}
catch { }

return (null, "unknown");
}

private Task<HttpResponse> HandlePreferencesGet(HttpRequest request)
{
try
Expand All @@ -3630,18 +3698,28 @@
return Task.FromResult(HttpResponse.Error("key is required"));

request.QueryParams.TryGetValue("sharedName", out var sharedName);
var type = request.QueryParams.GetValueOrDefault("type", "string");
var requestedType = request.QueryParams.GetValueOrDefault("type", null);

Check warning on line 3701 in src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs

View workflow job for this annotation

GitHub Actions / build-and-test (macos-latest)

Argument of type 'Dictionary<string, string>' cannot be used for parameter 'dictionary' of type 'IReadOnlyDictionary<string, string?>' in 'string? CollectionExtensions.GetValueOrDefault<string, string?>(IReadOnlyDictionary<string, string?> dictionary, string key, string? defaultValue)' due to differences in the nullability of reference types.

Check warning on line 3701 in src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs

View workflow job for this annotation

GitHub Actions / build-and-test (windows-latest)

Argument of type 'Dictionary<string, string>' cannot be used for parameter 'dictionary' of type 'IReadOnlyDictionary<string, string?>' in 'string? CollectionExtensions.GetValueOrDefault<string, string?>(IReadOnlyDictionary<string, string?> dictionary, string key, string? defaultValue)' due to differences in the nullability of reference types.

object? value = type.ToLowerInvariant() switch
object? value;
string type;
if (requestedType != null)
{
"int" or "integer" => sharedName != null ? Preferences.Get(key, 0, sharedName) : Preferences.Get(key, 0),
"bool" or "boolean" => sharedName != null ? Preferences.Get(key, false, sharedName) : Preferences.Get(key, false),
"double" => sharedName != null ? Preferences.Get(key, 0.0, sharedName) : Preferences.Get(key, 0.0),
"float" => sharedName != null ? Preferences.Get(key, 0f, sharedName) : Preferences.Get(key, 0f),
"long" => sharedName != null ? Preferences.Get(key, 0L, sharedName) : Preferences.Get(key, 0L),
"datetime" => sharedName != null ? Preferences.Get(key, DateTime.MinValue, sharedName) : Preferences.Get(key, DateTime.MinValue),
_ => sharedName != null ? Preferences.Get(key, (string?)null, sharedName) : Preferences.Get(key, (string?)null),
};
type = requestedType;
value = type.ToLowerInvariant() switch
{
"int" or "integer" => sharedName != null ? Preferences.Get(key, 0, sharedName) : Preferences.Get(key, 0),
"bool" or "boolean" => sharedName != null ? Preferences.Get(key, false, sharedName) : Preferences.Get(key, false),
"double" => sharedName != null ? Preferences.Get(key, 0.0, sharedName) : Preferences.Get(key, 0.0),
"float" => sharedName != null ? Preferences.Get(key, 0f, sharedName) : Preferences.Get(key, 0f),
"long" => sharedName != null ? Preferences.Get(key, 0L, sharedName) : Preferences.Get(key, 0L),
"datetime" => sharedName != null ? Preferences.Get(key, DateTime.MinValue, sharedName) : Preferences.Get(key, DateTime.MinValue),
_ => sharedName != null ? Preferences.Get(key, (string?)null, sharedName) : Preferences.Get(key, (string?)null),
};
}
else
{
(value, type) = ReadPreferenceValue(key, sharedName);
}

var exists = sharedName != null ? Preferences.ContainsKey(key, sharedName) : Preferences.ContainsKey(key);
return Task.FromResult(HttpResponse.Json(new { key, value, type, exists, sharedName }));
Expand Down
Loading