From 054a39d4871bddb458135f4f2c790c6931918b0b Mon Sep 17 00:00:00 2001 From: Khemiri mouna <40355084+khmMouna@users.noreply.github.com> Date: Thu, 12 Dec 2024 00:38:53 +0100 Subject: [PATCH] Add fetch subscription list (#17) --- src/Airship.Net/IAirship.cs | 13 ++++ src/Airship.Net/Platforms/Android/Airship.cs | 80 +++++++++++++++++++- src/Airship.Net/Platforms/iOS/Airship.cs | 53 +++++++++++++ 3 files changed, 145 insertions(+), 1 deletion(-) diff --git a/src/Airship.Net/IAirship.cs b/src/Airship.Net/IAirship.cs index ec58508..26eec3c 100644 --- a/src/Airship.Net/IAirship.cs +++ b/src/Airship.Net/IAirship.cs @@ -1,5 +1,6 @@ /* Copyright Airship and Contributors */ +using System.Collections.Generic; using AirshipDotNet.Attributes; using AirshipDotNet.Channel; using AirshipDotNet.Contact; @@ -173,6 +174,18 @@ public interface IAirship /// The tags. IEnumerable Tags { get; } + /// + /// Gets the channel subscription lists. + /// + /// The channel subscription lists. + void FetchChannelSubscriptionLists(Action> subscriptions); + + /// + /// Gets the contact subscription lists. + /// + /// The contact subscription lists. + void FetchContactSubscriptionLists(Action>> subscriptions); + /// /// Get the channel ID for the device. /// diff --git a/src/Airship.Net/Platforms/Android/Airship.cs b/src/Airship.Net/Platforms/Android/Airship.cs index 161b344..b452c01 100644 --- a/src/Airship.Net/Platforms/Android/Airship.cs +++ b/src/Airship.Net/Platforms/Android/Airship.cs @@ -192,9 +192,87 @@ private static Features FeaturesFromUAFeatures(int uAFeatures) return features; } - public IEnumerable Tags => UAirship.Shared().Channel.Tags; + private class ResultCallback : Java.Lang.Object, IResultCallback + { + Action action; + + internal ResultCallback(Action action) + { + this.action = action; + } + + public void OnResult(Java.Lang.Object? result) + { + action.Invoke(result); + } + } + + private List CastHashSetToList(HashSet set) + { + var list = new List(); + + var value = set.Iterator(); + if (value is not null) + { + while (value.HasNext) + { + var nextValue = (string?)value.Next(); + if (nextValue is not null) + { + list.Add(nextValue); + } + } + } + return list; + } + + public void FetchChannelSubscriptionLists(Action> subscriptions) + { + PendingResult subscriptionsPendingResult = UAirship.Shared().Channel.FetchSubscriptionListsPendingResult(); + + subscriptionsPendingResult.AddResultCallback(new ResultCallback((result) => + { + var list = new List(); + if (result is HashSet) + { + list = CastHashSetToList((HashSet)result); + } + + subscriptions(list); + })); + } + + public void FetchContactSubscriptionLists(Action>> subscriptions) + { + PendingResult subscriptionsPendingResult = UAirship.Shared().Contact.FetchSubscriptionListsPendingResult(); + + subscriptionsPendingResult.AddResultCallback(new ResultCallback((result) => + { + Dictionary> dictionary = new Dictionary>(); + if (result is not null) + { + var typedResult = (HashMap)result; + foreach (string? key in typedResult.KeySet()) + { + if (key is not null) + { + var typedValue = typedResult.Get(key); + + if (typedValue is not null && typedValue is HashSet) + { + var list = CastHashSetToList((HashSet)typedValue); + dictionary.Add(key, list); + } + + } + } + } + subscriptions(dictionary); + })); + } + public string? ChannelId => UAirship.Shared().Channel.Id; public void GetNamedUser(Action namedUser) => namedUser(UAirship.Shared().Contact.NamedUserId); diff --git a/src/Airship.Net/Platforms/iOS/Airship.cs b/src/Airship.Net/Platforms/iOS/Airship.cs index d4e3281..19a3d7f 100644 --- a/src/Airship.Net/Platforms/iOS/Airship.cs +++ b/src/Airship.Net/Platforms/iOS/Airship.cs @@ -202,6 +202,59 @@ private static Features FeaturesFromUAFeatures(UAFeatures uAFeatures) public IEnumerable Tags => UAirship.Channel.Tags; + public void FetchChannelSubscriptionLists(Action> subscriptions) + { + UAirship.Channel.FetchSubscriptionLists((lists) => + { + var list = new List(); + if (lists is not null) + { + foreach (string subscription in lists) + { + list.Add(subscription.ToString()); + } + } + subscriptions(list); + }); + } + + public void FetchContactSubscriptionLists(Action>> subscriptions) + { + UAirship.Contact.FetchSubscriptionLists((lists) => + { + var dictionary = new Dictionary>(); + if (lists is not null) + { + foreach (KeyValuePair kvp in lists) + { + string key = kvp.Key.ToString(); + var scopes = ((UAChannelScopes)kvp.Value).Values; + + if (key is not null && scopes is not null) + { + var list = new List(); + foreach (var scope in scopes) + { + list.Add(ScopeOrdinalToString(scope)); + } + dictionary.Add(key, list); + } + } + } + subscriptions(dictionary); + }); + } + + private string ScopeOrdinalToString(NSNumber ordinal) + => ordinal.LongValue switch + { + 0 => "app", + 1 => "web", + 2 => "email", + 3 => "sms", + _ => "unknown", + }; + public string? ChannelId => UAirship.Channel.Identifier; public void GetNamedUser(Action namedUser)