Skip to content

Commit

Permalink
Add fetch subscription list (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
khmMouna authored Dec 11, 2024
1 parent 58d167b commit 054a39d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Airship.Net/IAirship.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Copyright Airship and Contributors */

using System.Collections.Generic;
using AirshipDotNet.Attributes;
using AirshipDotNet.Channel;
using AirshipDotNet.Contact;
Expand Down Expand Up @@ -173,6 +174,18 @@ public interface IAirship
/// <value>The tags.</value>
IEnumerable<string> Tags { get; }

/// <summary>
/// Gets the channel subscription lists.
/// </summary>
/// <value>The channel subscription lists.</value>
void FetchChannelSubscriptionLists(Action<List<string>> subscriptions);

/// <summary>
/// Gets the contact subscription lists.
/// </summary>
/// <value>The contact subscription lists.</value>
void FetchContactSubscriptionLists(Action<Dictionary<string, List<string>>> subscriptions);

/// <summary>
/// Get the channel ID for the device.
/// </summary>
Expand Down
80 changes: 79 additions & 1 deletion src/Airship.Net/Platforms/Android/Airship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,87 @@ private static Features FeaturesFromUAFeatures(int uAFeatures)
return features;
}


public IEnumerable<string> Tags => UAirship.Shared().Channel.Tags;

private class ResultCallback : Java.Lang.Object, IResultCallback
{
Action<Java.Lang.Object?> action;

internal ResultCallback(Action<Java.Lang.Object?> action)
{
this.action = action;
}

public void OnResult(Java.Lang.Object? result)
{
action.Invoke(result);
}
}

private List<string> CastHashSetToList(HashSet set)
{
var list = new List<string>();

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<List<string>> subscriptions)
{
PendingResult subscriptionsPendingResult = UAirship.Shared().Channel.FetchSubscriptionListsPendingResult();

subscriptionsPendingResult.AddResultCallback(new ResultCallback((result) =>
{
var list = new List<string>();
if (result is HashSet)
{
list = CastHashSetToList((HashSet)result);
}

subscriptions(list);
}));
}

public void FetchContactSubscriptionLists(Action<Dictionary<string, List<string>>> subscriptions)
{
PendingResult subscriptionsPendingResult = UAirship.Shared().Contact.FetchSubscriptionListsPendingResult();

subscriptionsPendingResult.AddResultCallback(new ResultCallback((result) =>
{
Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
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<string> namedUser) => namedUser(UAirship.Shared().Contact.NamedUserId);
Expand Down
53 changes: 53 additions & 0 deletions src/Airship.Net/Platforms/iOS/Airship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,59 @@ private static Features FeaturesFromUAFeatures(UAFeatures uAFeatures)

public IEnumerable<string> Tags => UAirship.Channel.Tags;

public void FetchChannelSubscriptionLists(Action<List<string>> subscriptions)
{
UAirship.Channel.FetchSubscriptionLists((lists) =>
{
var list = new List<string>();
if (lists is not null)
{
foreach (string subscription in lists)
{
list.Add(subscription.ToString());
}
}
subscriptions(list);
});
}

public void FetchContactSubscriptionLists(Action<Dictionary<string, List<String>>> subscriptions)
{
UAirship.Contact.FetchSubscriptionLists((lists) =>
{
var dictionary = new Dictionary<string, List<string>>();
if (lists is not null)
{
foreach (KeyValuePair<NSObject, NSObject> 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<string>();
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<string> namedUser)
Expand Down

0 comments on commit 054a39d

Please sign in to comment.