diff --git a/.vscode/settings.json b/.vscode/settings.json index 64eac74..8453fbb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,7 @@ { - "dotnet-test-explorer.testProjectPath": "**/*Tests.@(csproj|vbproj|fsproj)" -} \ No newline at end of file + "dotnet-test-explorer.testProjectPath": "**/*Tests.@(csproj|vbproj|fsproj)", + "sonarlint.connectedMode.project": { + "connectionId": "dgmjr-io", + "projectKey": "dgmjr-io_Dgmjr.System.Extensions" + } +} diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..89e5659 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,21 @@ + + + + + + netstandard2.0;netstandard2.1;net6.0;net8.0 + $(MSBuildThisFileDirectory)Sdk.props + + + + diff --git a/Directory.Build.targets b/Directory.Build.targets new file mode 100644 index 0000000..e07fdea --- /dev/null +++ b/Directory.Build.targets @@ -0,0 +1,20 @@ + + + + + + $(MSBuildThisFileDirectory)Sdk.targets + + + + diff --git a/maui/Dgmjr.Microsoft.Maui.Extensions.csproj b/maui/Dgmjr.Microsoft.Maui.Extensions.csproj new file mode 100644 index 0000000..7f05382 --- /dev/null +++ b/maui/Dgmjr.Microsoft.Maui.Extensions.csproj @@ -0,0 +1,8 @@ + + + Extension methods for Microsoft MAUI framework + + + + + diff --git a/maui/Dgmjr.Microsoft.Maui.Extensions.sln b/maui/Dgmjr.Microsoft.Maui.Extensions.sln new file mode 100644 index 0000000..06a1a82 --- /dev/null +++ b/maui/Dgmjr.Microsoft.Maui.Extensions.sln @@ -0,0 +1,42 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B283EBC2-E01F-412D-9339-FD56EF114549}" + ProjectSection(SolutionItems) = preProject + ..\Directory.Build.props = ..\Directory.Build.props + ..\Directory.Build.targets = ..\Directory.Build.targets + ..\..\..\..\global.json = ..\..\..\..\global.json + ..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\Packages\Versions.Local.props + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Microsoft.Maui.Extensions", "Dgmjr.Microsoft.Maui.Extensions.csproj", "{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Local|Any CPU = Local|Any CPU + Debug|Any CPU = Debug|Any CPU + Testing|Any CPU = Testing|Any CPU + Staging|Any CPU = Staging|Any CPU + Production|Any CPU = Production|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Local|Any CPU.ActiveCfg = Local|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Local|Any CPU.Build.0 = Local|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Testing|Any CPU.ActiveCfg = Testing|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Testing|Any CPU.Build.0 = Testing|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Staging|Any CPU.ActiveCfg = Staging|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Staging|Any CPU.Build.0 = Staging|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Production|Any CPU.ActiveCfg = Local|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Production|Any CPU.Build.0 = Local|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3C15FE54-9A1C-43C9-8C8D-7AB7FA71BD87} + EndGlobalSection +EndGlobal diff --git a/maui/IImageExtensions.cs b/maui/IImageExtensions.cs new file mode 100644 index 0000000..c12c9bf --- /dev/null +++ b/maui/IImageExtensions.cs @@ -0,0 +1,36 @@ +namespace Microsoft.Maui.Graphics; + +using Microsoft.Maui.Graphics.Platform; +using System.Reflection; + +public static class IImageExtensions +{ + public static Size GetDimensions(this IImage image, int maxDimension = int.MaxValue) + { + var width = image.Width; + var height = image.Height; + if (width > maxDimension || height > maxDimension) + { + var ratio = width / (double)height; + if (ratio > 1) + { + width = maxDimension; + height = (int)(width / ratio); + } + else + { + height = maxDimension; + width = (int)(height * ratio); + } + } + return new Size(width, height); + } + + public static IImage ToImage(this Stream stream) => PlatformImage.FromStream(stream); + + public static Uri ToDataUri(this IImage image, string mimeType = "image/png") + { + var bytes = image.AsBytes(); + return new Uri($"data:{mimeType};base64,{Convert.ToBase64String(bytes)}"); + } +} diff --git a/maui/LICENSE.md b/maui/LICENSE.md new file mode 100755 index 0000000..4f60874 --- /dev/null +++ b/maui/LICENSE.md @@ -0,0 +1,35 @@ +--- +date: 2023-07-13T05:44:46:00.048Z +description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda... +keywords: + - IP + - copyright + - license + - mit +permissions: + - commercial-use + - modifications + - distribution + - private-use +conditions: + - include-copyright +limitations: + - liability + - warranty +lastmod: 2023-08-29T17:13:51:00.216Z +license: MIT +slug: mit-license +title: MIT License +type: license +--- + +# MIT License + +## Copyright © 2022-2023 [David G. Moore, Jr.](mailto:david@dgmjr.io "Send David an email") ([@dgmjr](https://github.com/dgmjr "Contact david on GitHub")), All Rights Reserved + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/maui/README.md b/maui/README.md new file mode 100644 index 0000000..2864c3a --- /dev/null +++ b/maui/README.md @@ -0,0 +1,21 @@ +--- +title: Microsoft MAUI Extensions +lastmod: 2023-23-22T18:23:41.1955-05:00Z +date: 2023-23-22T18:23:41.1956-05:00Z +license: MIT +slug: Dgmjr.Microsoft.Maui.Extensions-readme +version: +authors: + - dgmjr; +description: Extension methods for Microsoft MAUI framework +keywords: +- Dgmjr.Microsoft.Maui.Extensions +- dgmjr +- dgmjr-io +type: readme +--- + +# Microsoft MAUI Extensions + +This package contains extension methods for the Microsoft MAUI framework. + diff --git a/src/Dgmjr.System.Extensions.csproj b/src/Dgmjr.System.Extensions.csproj index 5cb312e..6c3d3d3 100644 --- a/src/Dgmjr.System.Extensions.csproj +++ b/src/Dgmjr.System.Extensions.csproj @@ -37,5 +37,7 @@ + + diff --git a/src/Dgmjr.System.Extensions.props b/src/Dgmjr.System.Extensions.props index e0a2b9f..aef3c20 100644 --- a/src/Dgmjr.System.Extensions.props +++ b/src/Dgmjr.System.Extensions.props @@ -43,6 +43,13 @@ + + + + + + + diff --git a/src/Dgmjr.System.Extensions.sln b/src/Dgmjr.System.Extensions.sln index c871d28..87735e6 100644 --- a/src/Dgmjr.System.Extensions.sln +++ b/src/Dgmjr.System.Extensions.sln @@ -2,13 +2,13 @@ # Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B283EBC2-E01F-412D-9339-FD56EF114549}" ProjectSection(SolutionItems) = preProject - ..\..\..\..\Directory.Build.props = ..\..\..\..\Directory.Build.props - ..\..\..\..\Directory.Build.targets = ..\..\..\..\Directory.Build.targets + ..\Directory.Build.props = ..\Directory.Build.props + ..\Directory.Build.targets = ..\Directory.Build.targets ..\..\..\..\global.json = ..\..\..\..\global.json ..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\Packages\Versions.Local.props EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.System.Extensions", "Dgmjr.System.Extensions.csproj", "{270D98D8-CC4A-4CE1-813E-9DDD04A79430}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.System.Extensions", "Dgmjr.System.Extensions.csproj", "{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -20,18 +20,18 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Local|Any CPU.ActiveCfg = Local|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Local|Any CPU.Build.0 = Local|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Debug|Any CPU.Build.0 = Debug|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Testing|Any CPU.ActiveCfg = Testing|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Testing|Any CPU.Build.0 = Testing|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Staging|Any CPU.ActiveCfg = Staging|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Staging|Any CPU.Build.0 = Staging|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Production|Any CPU.ActiveCfg = Local|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Production|Any CPU.Build.0 = Local|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Release|Any CPU.ActiveCfg = Release|Any CPU - {270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Release|Any CPU.Build.0 = Release|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Local|Any CPU.ActiveCfg = Local|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Local|Any CPU.Build.0 = Local|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Testing|Any CPU.ActiveCfg = Testing|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Testing|Any CPU.Build.0 = Testing|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Staging|Any CPU.ActiveCfg = Staging|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Staging|Any CPU.Build.0 = Staging|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Production|Any CPU.ActiveCfg = Local|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Production|Any CPU.Build.0 = Local|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/LICENSE.md b/src/LICENSE.md index 2b5eaac..4f60874 100755 --- a/src/LICENSE.md +++ b/src/LICENSE.md @@ -1,5 +1,5 @@ --- -date: 2023-07-13T05:44:46.048Z +date: 2023-07-13T05:44:46:00.048Z description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda... keywords: - IP @@ -13,10 +13,10 @@ permissions: - private-use conditions: - include-copyright -liimitations: +limitations: - liability - warranty -lastmod: 2023-08-29T17:13:51.216Z +lastmod: 2023-08-29T17:13:51:00.216Z license: MIT slug: mit-license title: MIT License diff --git a/src/System.Globalization/JsonLocaleConverter.cs b/src/System.Globalization/JsonLocaleConverter.cs new file mode 100644 index 0000000..cc52484 --- /dev/null +++ b/src/System.Globalization/JsonLocaleConverter.cs @@ -0,0 +1,41 @@ +namespace System.Globalization; + +using System.Text.Json.Serialization; +using System.Text.Json; +using Jso = System.Text.Json.JsonSerializerOptions; + +public class JsonLocaleConverter + : System.Text.Json.Serialization.JsonConverter +{ + public override System.Globalization.CultureInfo Read( + ref Utf8JsonReader reader, + type typeToConvert, + Jso options + ) + { + if (reader.TokenType == JsonTokenType.String) + { + try + { + return System.Globalization.CultureInfo.CreateSpecificCulture(reader.GetString()); + } + catch + { + return System.Globalization.CultureInfo.InvariantCulture; + } + } + else + { + return System.Globalization.CultureInfo.InvariantCulture; + } + } + + public override void Write( + Utf8JsonWriter writer, + System.Globalization.CultureInfo value, + Jso options + ) + { + writer.WriteStringValue(value.Name); + } +} diff --git a/src/System.IO/StreamExtensions.cs b/src/System.IO/StreamExtensions.cs index 04752aa..3a6b3ba 100644 --- a/src/System.IO/StreamExtensions.cs +++ b/src/System.IO/StreamExtensions.cs @@ -10,10 +10,10 @@ * License: MIT (https://opensource.org/licenses/MIT) */ -using System.Threading.Tasks; - namespace System.IO; +using System.Threading.Tasks; + public static class StreamExtensions { /// @@ -30,4 +30,24 @@ public static class StreamExtensions /// the contents of the as a to the end public static Task ReadToEndAsync(this Stream s) => new StreamReader(s).ReadToEndAsync(); + + /// + /// Reads + /// + /// The stream to read from + /// all s from the + public static byte[] ReadAllBytes(this Stream stream) + { + using var memoryStream = new MemoryStream(); + stream.CopyTo(memoryStream); + return memoryStream.ToArray(); + } + + /// + /// Reads + /// + /// The stream to read from + /// all s from the + public static async Task ReadAllBytesAsync(this Stream stream) => + await Task.Run(() => stream.ReadAllBytes()); } diff --git a/src/System/JsonIntegerToTimeSpanConverter.cs b/src/System/JsonIntegerToTimeSpanConverter.cs new file mode 100644 index 0000000..a4b58d2 --- /dev/null +++ b/src/System/JsonIntegerToTimeSpanConverter.cs @@ -0,0 +1,22 @@ +namespace System; + +using System.Text.Json.Serialization; +using System.Text.Json; +using Jso = System.Text.Json.JsonSerializerOptions; + +public class JsonIntegerToTimeSpanConverter : JsonConverter +{ + public override duration Read(ref Utf8JsonReader reader, type typeToConvert, Jso options) + { + if (reader.TokenType != JTokenType.Number) + { + throw new JsonException(); + } + return duration.FromSeconds(reader.GetInt64()); + } + + public override void Write(Utf8JsonWriter writer, duration value, Jso options) + { + writer.WriteNumberValue(value.TotalSeconds); + } +} diff --git a/src/System/JsonUriStringConverter.cs b/src/System/JsonUriStringConverter.cs new file mode 100644 index 0000000..3f719f1 --- /dev/null +++ b/src/System/JsonUriStringConverter.cs @@ -0,0 +1,54 @@ +namespace System; + +public class JsonUriStringConverter : System.Text.Json.Serialization.JsonConverter +{ + public override bool CanConvert(type typeToConvert) => typeToConvert == typeof(Uri); + + public override Uri? Read(ref Utf8JsonReader reader, type typeToConvert, Jso options) + { + try + { + return new(reader.GetString()); + } + catch + { + return default; + } + } + + public override void Write(Utf8JsonWriter writer, Uri value, Jso options) + { + writer.WriteStringValue(value.ToString()); + } +} + +public class JsonUriOrStringConverter : System.Text.Json.Serialization.JsonConverter +{ + public override bool CanConvert(type typeToConvert) => typeToConvert == typeof(UriOrString); + + public override UriOrString Read(ref Utf8JsonReader reader, type typeToConvert, Jso options) + { + return new(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, UriOrString value, Jso options) + { + writer.WriteStringValue(value.ToString()); + } +} + +public class JsonUriStringAttribute : JConverterAttribute +{ + public override JConverter CreateConverter(type typeToConvert) + { + return new JsonUriStringConverter(); + } +} + +public class JsonUriOrStringStringAttribute : JConverterAttribute +{ + public override JConverter CreateConverter(type typeToConvert) + { + return new JsonUriStringConverter(); + } +} diff --git a/src/System/UriExtensions.cs b/src/System/UriExtensions.cs new file mode 100644 index 0000000..0dd331f --- /dev/null +++ b/src/System/UriExtensions.cs @@ -0,0 +1,8 @@ +namespace System; + +using System.Net.Http; + +public static class UriExtensions +{ + public static Stream OpenRead(this Uri uri) => new HttpClient().GetStreamAsync(uri).Result; +} diff --git a/src/System/UriOrString.cs b/src/System/UriOrString.cs new file mode 100644 index 0000000..c2070f3 --- /dev/null +++ b/src/System/UriOrString.cs @@ -0,0 +1,27 @@ +namespace System; + +using OneOf; + +public class UriOrString : OneOfBase +{ + public UriOrString(Uri uri) + : base(uri) { } + + public UriOrString(string str) + : base(str) { } + + public static implicit operator UriOrString(Uri uri) => new(uri); + + public static implicit operator UriOrString(string str) => new(str); + + public static implicit operator Uri(UriOrString uriOrString) => + uriOrString.IsT0 ? uriOrString.AsT0 : new(uriOrString.AsT1); + + public override string ToString() + { + return ((Uri)this).ToString(); + } + + public static explicit operator string(UriOrString uriOrString) => + uriOrString.IsT1 ? uriOrString.AsT1 : uriOrString.AsT0.ToString(); +}