From c64d11e1988057d22da28cd4004fe88f322669ed Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 17 Aug 2026 23:00:04 +0100 Subject: [PATCH] WIP app invite: scaffold MAUI Essentials dependency in shared PostIt Adds Microsoft.Maui.Essentials package and true to src/PostIt/PostIt/PostIt.csproj so the shared project can compile code that calls MAUI Essentials APIs (Microsoft.Maui.ApplicationModel.*). Also adds a draft ContactService that wraps Contacts.Default.GetAllAsync() behind a runtime platform check and permission request. WIP caveats: - The portable MAUI Essentials facade compiles on net10.0 but throws NotImplementedInReferenceAssemblyException at runtime when no platform-specific MAUI Essentials binary is loaded. A PostIt.Android project (or equivalent) must reference the Android MAUI Essentials implementation for Contacts.Default.GetAllAsync() to actually work. - On desktop (Linux/macOS/Windows) the API is unsupported by design; ContactService currently throws PlatformNotSupportedException. A desktop stub returning Array.Empty() is the likely next step. - No tests yet. The scaffold is unverified at runtime; build passes. --- src/PostIt/Directory.Packages.props | 1 + src/PostIt/PostIt/PostIt.csproj | 5 ++- src/PostIt/PostIt/Services/ContactService.cs | 39 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/PostIt/PostIt/Services/ContactService.cs diff --git a/src/PostIt/Directory.Packages.props b/src/PostIt/Directory.Packages.props index 900f1428..62b3a343 100644 --- a/src/PostIt/Directory.Packages.props +++ b/src/PostIt/Directory.Packages.props @@ -14,6 +14,7 @@ + diff --git a/src/PostIt/PostIt/PostIt.csproj b/src/PostIt/PostIt/PostIt.csproj index d9cf96d3..1b14653a 100644 --- a/src/PostIt/PostIt/PostIt.csproj +++ b/src/PostIt/PostIt/PostIt.csproj @@ -3,11 +3,13 @@ net10.0 enable latest + true true 1.0.1.0 1.0.1.0 1.0.1-5+Branch.main.Sha.0617fc6bda7151c70559d87177e2dcfb1b60995f 1.0.1-5 + @@ -24,6 +26,7 @@ + @@ -39,4 +42,4 @@ - \ No newline at end of file + diff --git a/src/PostIt/PostIt/Services/ContactService.cs b/src/PostIt/PostIt/Services/ContactService.cs new file mode 100644 index 00000000..bc71d11b --- /dev/null +++ b/src/PostIt/PostIt/Services/ContactService.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Maui.ApplicationModel.Communication; +using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Devices; + +public class ContactService +{ + public async Task> GetDeviceContactsAsync() + { + // 1. Ensure the platform supports MAUI Essentials APIs + if (DeviceInfo.Current.Platform == DevicePlatform.Unknown) + { + throw new PlatformNotSupportedException("MAUI Essentials is not available on this platform."); + } + + try + { + // 2. Request runtime permission (Required for Android & iOS) + var status = await Permissions.RequestAsync(); + if (status != PermissionStatus.Granted) + { + // Permission denied by user + return Array.Empty(); + } + + // 3. Fetch all contacts + var contactsEnumerable = await Contacts.Default.GetAllAsync(); + return contactsEnumerable ?? Array.Empty(); + } + catch (Exception ex) + { + // Handle cross-platform exceptions or logs here + System.Diagnostics.Debug.WriteLine($"Error fetching contacts: {ex.Message}"); + return Array.Empty(); + } + } +}