From 8d3ee8dd498a201fbaf43a8458e6bb81f7b40efd Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Wed, 2 Sep 2020 04:59:57 +0200 Subject: [PATCH 1/2] Tweak the AppAction param order Also fix the build error --- .../DeviceTests.Shared/AppActions_Tests.cs | 24 +++++++++------- Samples/Samples/App.xaml.cs | 4 +-- .../AppActions/AppActions.android.cs | 9 ++---- .../AppActions/AppActions.ios.cs | 28 ++++++++++++++++--- .../AppActions/AppActions.shared.cs | 7 +++-- .../AppActions/AppActions.uwp.cs | 2 +- 6 files changed, 48 insertions(+), 26 deletions(-) diff --git a/DeviceTests/DeviceTests.Shared/AppActions_Tests.cs b/DeviceTests/DeviceTests.Shared/AppActions_Tests.cs index 44f1e998b..04189303e 100644 --- a/DeviceTests/DeviceTests.Shared/AppActions_Tests.cs +++ b/DeviceTests/DeviceTests.Shared/AppActions_Tests.cs @@ -26,18 +26,22 @@ public void IsSupported() #if __ANDROID_25__ || __IOS__ [Fact] - public void GetSetItems() + public async Task GetSetItems() { - if (AppActions.IsSupported) + if (!AppActions.IsSupported) + return; + + var actions = new List { - AppActions.Actions = new List - { - new AppAction("TEST1", "Test 1", "This is a test", new System.Uri("myapp://test1")), - new AppAction("TEST2", "Test 2", "This is a test 2", new System.Uri("myapp://test2")), - }; - - Assert.Contains(AppActions.Actions, a => a.ActionType == "TEST1"); - } + new AppAction("TEST1", "Test 1", "This is a test", "myapp://test1"), + new AppAction("TEST2", "Test 2", "This is a test 2", "myapp://test2"), + }; + + await AppActions.SetAsync(actions); + + var get = await AppActions.GetAsync(); + + Assert.Contains(get, a => a.Id == "TEST1"); } #endif } diff --git a/Samples/Samples/App.xaml.cs b/Samples/Samples/App.xaml.cs index d06e49f27..c152f8573 100644 --- a/Samples/Samples/App.xaml.cs +++ b/Samples/Samples/App.xaml.cs @@ -49,8 +49,8 @@ protected override async void OnStart() } await AppActions.SetAsync( - new AppAction("App Info", id: "app_info", icon: "app_info_action_icon"), - new AppAction("Battery Info", id: "battery_info")); + new AppAction("app_info", "App Info", icon: "app_info_action_icon"), + new AppAction("battery_info", "Battery Info")); } void AppActions_OnAppAction(object sender, AppActionEventArgs e) diff --git a/Xamarin.Essentials/AppActions/AppActions.android.cs b/Xamarin.Essentials/AppActions/AppActions.android.cs index 48636d574..1c4c78361 100644 --- a/Xamarin.Essentials/AppActions/AppActions.android.cs +++ b/Xamarin.Essentials/AppActions/AppActions.android.cs @@ -1,12 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Android.Content; using Android.Content.PM; -using Android.Content.Res; using Android.Graphics.Drawables; -using AndroidUri = Android.Net.Uri; namespace Xamarin.Essentials { @@ -39,7 +36,7 @@ static Task PlatformSetAsync(IEnumerable actions) } static AppAction ToAppAction(this ShortcutInfo shortcutInfo) => - new AppAction(shortcutInfo.ShortLabel, shortcutInfo.LongLabel, shortcutInfo.Id); + new AppAction(shortcutInfo.Id, shortcutInfo.ShortLabel, shortcutInfo.LongLabel); const string extraAppActionId = "EXTRA_XE_APP_ACTION_ID"; const string extraAppActionTitle = "EXTRA_XE_APP_ACTION_TITLE"; @@ -48,8 +45,8 @@ static AppAction ToAppAction(this ShortcutInfo shortcutInfo) => internal static AppAction ToAppAction(this Intent intent) => new AppAction( - intent.GetStringExtra(extraAppActionTitle), intent.GetStringExtra(extraAppActionId), + intent.GetStringExtra(extraAppActionTitle), intent.GetStringExtra(extraAppActionSubtitle), intent.GetStringExtra(extraAppActionIcon)); diff --git a/Xamarin.Essentials/AppActions/AppActions.ios.cs b/Xamarin.Essentials/AppActions/AppActions.ios.cs index cd1fe88be..7f2aefb50 100644 --- a/Xamarin.Essentials/AppActions/AppActions.ios.cs +++ b/Xamarin.Essentials/AppActions/AppActions.ios.cs @@ -37,15 +37,35 @@ internal static AppAction ToAppAction(this UIApplicationShortcutItem shortcutIte if (shortcutItem.UserInfo.TryGetValue((NSString)"id", out var idObj)) id = idObj?.ToString(); - return new AppAction(shortcutItem.Type, id, shortcutItem.LocalizedTitle, shortcutItem.LocalizedSubtitle); + string icon = null; + if (shortcutItem.UserInfo.TryGetValue((NSString)"icon", out var iconObj)) + icon = iconObj?.ToString(); + + return new AppAction(id, shortcutItem.LocalizedTitle, shortcutItem.LocalizedSubtitle, icon); } - static UIApplicationShortcutItem ToShortcutItem(this AppAction action) => - new UIApplicationShortcutItem( + static UIApplicationShortcutItem ToShortcutItem(this AppAction action) + { + var keys = new List(); + var values = new List(); + + // id + keys.Add((NSString)"id"); + values.Add((NSString)action.Id); + + // icon + if (!string.IsNullOrEmpty(action.Icon)) + { + keys.Add((NSString)"icon"); + values.Add((NSString)action.Icon); + } + + return new UIApplicationShortcutItem( AppActions.Type, action.Title, action.Subtitle, action.Icon != null ? UIApplicationShortcutIcon.FromTemplateImageName(action.Icon) : null, - new NSDictionary((NSString)"id", (NSString)action.Id.ToString())); + new NSDictionary(keys.ToArray(), values.ToArray())); + } } } diff --git a/Xamarin.Essentials/AppActions/AppActions.shared.cs b/Xamarin.Essentials/AppActions/AppActions.shared.cs index e5a8dd08d..44aad7d90 100644 --- a/Xamarin.Essentials/AppActions/AppActions.shared.cs +++ b/Xamarin.Essentials/AppActions/AppActions.shared.cs @@ -34,12 +34,13 @@ public AppActionEventArgs(AppAction appAction) public class AppAction { - public AppAction(string title, string id, string subtitle = null, string icon = null) + public AppAction(string id, string title, string subtitle = null, string icon = null) { - Title = title; + Id = id ?? throw new ArgumentNullException(nameof(id)); + Title = title ?? throw new ArgumentNullException(nameof(title)); + Subtitle = subtitle; Icon = icon; - Id = id; } public string Title { get; set; } diff --git a/Xamarin.Essentials/AppActions/AppActions.uwp.cs b/Xamarin.Essentials/AppActions/AppActions.uwp.cs index 3cb2de664..832bd4c05 100644 --- a/Xamarin.Essentials/AppActions/AppActions.uwp.cs +++ b/Xamarin.Essentials/AppActions/AppActions.uwp.cs @@ -68,7 +68,7 @@ static async Task PlatformSetAsync(IEnumerable actions) } static AppAction ToAction(this JumpListItem item) - => new AppAction(item.DisplayName, ArgumentsToId(item.Arguments), item.Description); + => new AppAction(ArgumentsToId(item.Arguments), item.DisplayName, item.Description); static string ArgumentsToId(string arguments) { From 9abbd37dfc948f54654b3a7a6acc1695cea260e0 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Wed, 2 Sep 2020 05:05:23 +0200 Subject: [PATCH 2/2] Some fixes --- Tests/AppActions_Tests.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Tests/AppActions_Tests.cs b/Tests/AppActions_Tests.cs index 3be20d4e3..c5933193b 100644 --- a/Tests/AppActions_Tests.cs +++ b/Tests/AppActions_Tests.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using Xamarin.Essentials; using Xunit; @@ -10,11 +8,11 @@ public class AppActions_Tests { [Fact] public void AppActions_SetActions() => - Assert.Throws(() => AppActions.Actions = new List()); + Assert.ThrowsAsync(() => AppActions.SetAsync(new List())); [Fact] public void AppActions_GetActions() => - Assert.Throws(() => AppActions.Actions); + Assert.ThrowsAsync(() => AppActions.GetAsync()); [Fact] public void AppActions_IsSupported() =>