Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch subscription list #17

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could maybe do something like this here to avoid creating an empty list, but it's probably not a big deal either way...

subscriptionsPendingResult.AddResultCallback(new ResultCallback((result) 
{
    List<string> list = result switch
    {
        HashSet hashSet => CastHashSetToList(hashSet),
        _ => new List<string>(),
    }

    subscriptions(list);
} 

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
Loading