Skip to content

Commit

Permalink
converting to .net 8
Browse files Browse the repository at this point in the history
  • Loading branch information
thudugala committed Nov 18, 2023
1 parent d64c852 commit 73e4909
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 336 deletions.
4 changes: 2 additions & 2 deletions Sample/Direct Maui/LocalNotification.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static MauiApp CreateMauiApp()
{
ActionList = new HashSet<NotificationAction>(new List<NotificationAction>()
{
new NotificationAction(100)
new(100)
{
Title = "Hello",
Android =
Expand All @@ -43,7 +43,7 @@ public static MauiApp CreateMauiApp()
LaunchAppWhenTapped = true
}
},
new NotificationAction(101)
new(101)
{
Title = "Close",
Android =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AndroidAction
/// <summary>
///
/// </summary>
public AndroidIcon IconName { get; set; } = new AndroidIcon();
public AndroidIcon IconName { get; set; } = new ();

/// <summary>
/// Default is false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AndroidIcon(string resourceName)
/// </summary>
/// <param name="resourceName"></param>
/// <param name="type"></param>
public AndroidIcon(string resourceName, string type)
public AndroidIcon(string resourceName, string? type)
{
ResourceName = resourceName;
Type = type ?? DefaultType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,11 @@
///
/// </summary>
public class AndroidLocalNotificationBuilder : IAndroidLocalNotificationBuilder
{
/// <summary>
///
/// </summary>
public IList<NotificationChannelRequest> ChannelRequestList { get; }

/// <summary>
///
/// </summary>
public IList<NotificationChannelGroupRequest> GroupChannelRequestList { get; }

/// <summary>
///
/// </summary>
public AndroidLocalNotificationBuilder()
{
ChannelRequestList = new List<NotificationChannelRequest>();
GroupChannelRequestList = new List<NotificationChannelGroupRequest>();
}

{
internal IList<NotificationChannelRequest> ChannelRequestList { get; } = new List<NotificationChannelRequest>();

internal IList<NotificationChannelGroupRequest> GroupChannelRequestList { get; } = new List<NotificationChannelGroupRequest>();

/// <inheritdoc/>
public IAndroidLocalNotificationBuilder AddChannel(NotificationChannelRequest channelRequest)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public class AndroidScheduleOptions
}

var repeatInterval = GetNotifyRepeatInterval(repeatType, notifyRepeatInterval);
if (repeatInterval is null)
if (repeatInterval == TimeSpan.Zero)
{
return null;
}

var newNotifyTime = notifyTime.Value.Add(repeatInterval.Value);
var newNotifyTime = notifyTime.Value.Add(repeatInterval);
var nowTime = DateTime.Now.AddSeconds(10);
while (newNotifyTime <= nowTime)
{
newNotifyTime = newNotifyTime.Add(repeatInterval.Value);
newNotifyTime = newNotifyTime.Add(repeatInterval);
}
return newNotifyTime;
}
Expand All @@ -47,9 +47,9 @@ public class AndroidScheduleOptions
/// internal use, only for Android
/// </summary>
/// <returns></returns>
internal TimeSpan? GetNotifyRepeatInterval(NotificationRepeat repeatType, TimeSpan? notifyRepeatInterval)
internal TimeSpan GetNotifyRepeatInterval(NotificationRepeat repeatType, TimeSpan? notifyRepeatInterval)
{
TimeSpan? repeatInterval = null;
var repeatInterval = TimeSpan.Zero;
switch (repeatType)
{
case NotificationRepeat.Daily:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
public interface IAndroidLocalNotificationBuilder
{
/// <summary>
/// A representation of settings that apply to a collection of similarly themed notifications
/// A representation of settings that apply to a collection of similarly themed notifications.
/// Create Notification Channel when API >= 26.
/// </summary>
/// <param name="channelRequest"></param>
/// <returns></returns>
IAndroidLocalNotificationBuilder AddChannel(NotificationChannelRequest channelRequest);

/// <summary>
/// A grouping of related notification channels. e.g., channels that all belong to a single account.
/// Create Notification Channel Group when API >= 26.
/// If you'd like to further organize the appearance of your channels in the settings UI, you can create channel groups.
/// This is a good idea when your app supports multiple user accounts (such as for work profiles),
/// so you can create a notification channel group for each account.
/// This way, users can easily identify and control multiple notification channels that have identical names.
/// </summary>
/// <param name="groupChannelRequest"></param>
/// <returns></returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
/// </summary>
public class NotificationChannelGroupRequest
{
private string group = AndroidOptions.DefaultGroupId;
private string name = AndroidOptions.DefaultGroupName;

/// <summary>
/// The id of the group. Must be unique per package. the value may be truncated if it is too long
/// </summary>
public string Group { get; set; } = AndroidOptions.DefaultGroupId;

public string Group
{
get => string.IsNullOrWhiteSpace(group) ? AndroidOptions.DefaultGroupId : group;
set => group = string.IsNullOrWhiteSpace(value) ? AndroidOptions.DefaultGroupId : value;
}
/// <summary>
/// The user visible name of the group, The recommended maximum length is 40 characters; the value may be truncated if it is too long.
/// </summary>
public string Name { get; set; } = AndroidOptions.DefaultGroupName;
public string Name
{
get => string.IsNullOrWhiteSpace(name) ? AndroidOptions.DefaultGroupName : name;
set => name = string.IsNullOrWhiteSpace(value) ? AndroidOptions.DefaultGroupName : value;
}

/// <summary>
/// Constructor to pass values directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/// </summary>
public class NotificationChannelRequest
{
private string id = AndroidOptions.DefaultChannelId;
private string name = AndroidOptions.DefaultChannelName;

/// <summary>
/// Sets or gets, the level of interruption of this notification channel.
/// </summary>
Expand All @@ -14,12 +17,20 @@ public class NotificationChannelRequest
/// Sets or gets, The id of the channel. Must be unique per package. The value may be truncated if it is too lon
/// Also, NotificationRequest.Android.ChannelId must be set to the same Id to target this channel.
/// </summary>
public string Id { get; set; } = AndroidOptions.DefaultChannelId;
public string Id
{
get => string.IsNullOrWhiteSpace(id) ? AndroidOptions.DefaultChannelId : id;
set => id = string.IsNullOrWhiteSpace(value) ? AndroidOptions.DefaultChannelId : value;
}

/// <summary>
/// Sets or gets, the user visible name of this channel, default is General.
/// </summary>
public string Name { get; set; } = AndroidOptions.DefaultChannelName;
/// </summary>
public string Name
{
get => string.IsNullOrWhiteSpace(name) ? AndroidOptions.DefaultChannelName : name;
set => name = string.IsNullOrWhiteSpace(value) ? AndroidOptions.DefaultChannelName : value;
}

/// <summary>
/// Sets or gets, the user visible description of this channel.
Expand Down
2 changes: 2 additions & 0 deletions Source/Plugin.LocalNotification/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public interface INotificationService

/// <summary>
/// Request Notification Permission
/// Ask the user for permission to show notifications on iOS 10.0+ and Android 33+.
/// Returns true if Allowed.
/// </summary>
/// <returns></returns>
Task<bool> RequestNotificationPermission(NotificationPermission? permission = null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Plugin.LocalNotification.Json
{
/// <inheritdoc />
public class NotificationSerializer : INotificationSerializer
internal class NotificationSerializer : INotificationSerializer
{
/// <inheritdoc />
public virtual TValue? Deserialize<TValue>(string json)
Expand Down
21 changes: 5 additions & 16 deletions Source/Plugin.LocalNotification/LocalNotificationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,23 @@ public class LocalNotificationBuilder : ILocalNotificationBuilder
/// <summary>
/// Register notification categories and their corresponding actions
/// </summary>
public HashSet<NotificationCategory> CategorySet { get; private set; }
internal HashSet<NotificationCategory> CategorySet { get; } = new ();

/// <summary>
///
/// </summary>
public INotificationSerializer Serializer { get; private set; }
internal INotificationSerializer Serializer { get; private set; } = new NotificationSerializer();

/// <summary>
/// Android specific Builder.
/// </summary>
public AndroidLocalNotificationBuilder AndroidBuilder { get; private set; }
internal AndroidLocalNotificationBuilder AndroidBuilder { get; } = new();

/// <summary>
/// Android specific Builder.
/// </summary>
public iOSLocalNotificationBuilder IOSBuilder { get; private set; }

/// <summary>
///
/// </summary>
public LocalNotificationBuilder()
{
AndroidBuilder = new AndroidLocalNotificationBuilder();
IOSBuilder = new iOSLocalNotificationBuilder();
CategorySet = new HashSet<NotificationCategory>();
Serializer = new NotificationSerializer();
}

internal iOSLocalNotificationBuilder IOSBuilder { get; } = new();

/// <inheritdoc/>
public ILocalNotificationBuilder AddAndroid(Action<IAndroidLocalNotificationBuilder> android)
{
Expand Down
12 changes: 6 additions & 6 deletions Source/Plugin.LocalNotification/LocalNotificationCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static INotificationService CreateNotificationService()
/// <summary>
/// Internal Logger
/// </summary>
public static ILogger? Logger { get; set; }
internal static ILogger? Logger { get; set; }

/// <summary>
/// Internal Logger LogLevel
Expand Down Expand Up @@ -65,7 +65,7 @@ public static INotificationService Current
/// <summary>
///
/// </summary>
public static INotificationSerializer Serializer
internal static INotificationSerializer Serializer
{
get
{
Expand All @@ -75,7 +75,7 @@ public static INotificationSerializer Serializer
set => _serializer = value;
}

internal static NotificationRequest? GetRequest(string? serializedRequest)
internal static NotificationRequest GetRequest(string? serializedRequest)
{
Logger?.LogTrace($"Serialized Request [{serializedRequest}]");
if (string.IsNullOrWhiteSpace(serializedRequest))
Expand All @@ -84,18 +84,18 @@ public static INotificationSerializer Serializer
}

var request = Serializer.Deserialize<NotificationRequest>(serializedRequest);
return request;
return request ?? new NotificationRequest();
}

internal static List<NotificationRequest>? GetRequestList(string? serializedRequestList)
internal static List<NotificationRequest> GetRequestList(string? serializedRequestList)
{
if (string.IsNullOrWhiteSpace(serializedRequestList))
{
return [];
}

var requestList = Serializer.Deserialize<List<NotificationRequest>>(serializedRequestList);
return requestList;
return requestList ?? [];
}

internal static string GetRequestListSerialize(List<NotificationRequest> requestList)
Expand Down
24 changes: 7 additions & 17 deletions Source/Plugin.LocalNotification/LocalNotificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,15 @@ public static MauiAppBuilder UseLocalNotification(this MauiAppBuilder builder, A
#if ANDROID
life.AddAndroid(android =>
{
android.OnCreate((activity, savedInstanceState) =>
{
if (localNotificationBuilder.AndroidBuilder.ChannelRequestList.Any())
{
foreach (var channelRequest in localNotificationBuilder.AndroidBuilder.ChannelRequestList)
{
LocalNotificationCenter.CreateNotificationChannel(channelRequest);
}
}
if (localNotificationBuilder.AndroidBuilder.GroupChannelRequestList.Any())
{
foreach (var groupChannelReques in localNotificationBuilder.AndroidBuilder.GroupChannelRequestList)
{
LocalNotificationCenter.CreateNotificationChannelGroup(groupChannelReques);
}
}
android.OnCreate((activity, _) =>
{
LocalNotificationCenter.CreateNotificationChannels(localNotificationBuilder.AndroidBuilder.ChannelRequestList);
LocalNotificationCenter.CreateNotificationChannelGroups(localNotificationBuilder.AndroidBuilder.GroupChannelRequestList);
LocalNotificationCenter.NotifyNotificationTapped(activity.Intent);
})
.OnNewIntent((activity, intent) =>
.OnNewIntent((_, intent) =>
{
LocalNotificationCenter.NotifyNotificationTapped(intent);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ public void Initialize(IServiceProvider services)
if(builder is not null)
{
LocalNotificationCenter.Serializer = builder.Serializer;
}

if (builder?.CategorySet != null && builder.CategorySet.Any())
{
LocalNotificationCenter.Current.RegisterCategoryList(builder.CategorySet);
}
LocalNotificationCenter.Current.RegisterCategoryList(builder.CategorySet);
}
}
}
}
20 changes: 9 additions & 11 deletions Source/Plugin.LocalNotification/NotificationAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ namespace Plugin.LocalNotification
/// <summary>
///
/// </summary>
public class NotificationAction : IEquatable<NotificationAction>
/// <remarks>
/// ActionId is the unique identifier for the Category
/// </remarks>
/// <param name="actionId">A unique identifier for the Action</param>
public class NotificationAction(int actionId) : IEquatable<NotificationAction>

Check failure on line 14 in Source/Plugin.LocalNotification/NotificationAction.cs

View workflow job for this annotation

GitHub Actions / nuget

The feature 'primary constructors' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 14 in Source/Plugin.LocalNotification/NotificationAction.cs

View workflow job for this annotation

GitHub Actions / nuget

The feature 'primary constructors' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 14 in Source/Plugin.LocalNotification/NotificationAction.cs

View workflow job for this annotation

GitHub Actions / nuget

The feature 'primary constructors' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 14 in Source/Plugin.LocalNotification/NotificationAction.cs

View workflow job for this annotation

GitHub Actions / nuget

The feature 'primary constructors' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
{
/// <summary>
/// ActionId is the unique identifier for the Category
/// </summary>
/// <param name="actionId">A unique identifier for the Action</param>
public NotificationAction(int actionId)
{
ActionId = actionId;
}

/// <summary>
/// A unique identifier for the Action
/// </summary>
public int ActionId { get; }
public int ActionId { get; } = actionId;

/// <summary>
/// iOS specific properties.
Expand Down Expand Up @@ -54,13 +50,15 @@ public bool Equals(NotificationAction? other)
ActionId == other.ActionId;
}

public override bool Equals(object? obj) => Equals(obj as NotificationAction);

/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ActionId.GetHashCode();
}
}
}
}
2 changes: 2 additions & 0 deletions Source/Plugin.LocalNotification/NotificationCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public bool Equals(NotificationCategory? other)
CategoryType == other.CategoryType;
}

public override bool Equals(object? obj) => Equals(obj as NotificationCategory);

/// <summary>
///
/// </summary>
Expand Down
Loading

0 comments on commit 73e4909

Please sign in to comment.