Skip to content

Commit

Permalink
Fixed #353 and #354, preprocessor directives issue
Browse files Browse the repository at this point in the history
  • Loading branch information
thudugala committed Jan 15, 2023
1 parent c71fefb commit 9e1a9ff
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Sample/Direct Maui/LocalNotification.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public partial class App : Application
{
public App()
public App(MainPage mainPage)
{
InitializeComponent();

//MainPage = new AppShell();

MainPage = new NavigationPage(new MainPage());
MainPage = new NavigationPage(mainPage);
}
}
30 changes: 18 additions & 12 deletions Sample/Direct Maui/LocalNotification.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ public partial class MainPage : ContentPage
{
private int _tapCount;
private string _cacheFilePath;
private readonly INotificationService _notificationService;

public MainPage()
public MainPage(INotificationService notificationService)
{
InitializeComponent();

LocalNotificationCenter.Current.NotificationReceiving = OnNotificationReceiving;
LocalNotificationCenter.Current.NotificationReceived += ShowCustomAlertFromNotification;
LocalNotificationCenter.Current.NotificationActionTapped += Current_NotificationActionTapped;
_notificationService = notificationService;

_notificationService.NotificationReceiving = OnNotificationReceiving;
_notificationService.NotificationReceived += ShowCustomAlertFromNotification;
_notificationService.NotificationActionTapped += Current_NotificationActionTapped;

NotifyDatePicker.MinimumDate = DateTime.Today;
NotifyTimePicker.Time = DateTime.Now.TimeOfDay.Add(TimeSpan.FromSeconds(10));
Expand Down Expand Up @@ -163,12 +166,12 @@ private async void Button_Clicked(object sender, EventArgs e)

try
{
if (await LocalNotificationCenter.Current.AreNotificationsEnabled() == false)
if (await _notificationService.AreNotificationsEnabled() == false)
{
await LocalNotificationCenter.Current.RequestNotificationPermission();
await _notificationService.RequestNotificationPermission();
}

var ff = await LocalNotificationCenter.Current.Show(request);
var ff = await _notificationService.Show(request);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -226,7 +229,10 @@ private async void Current_NotificationActionTapped(NotificationActionEventArgs
var message = list[2];
var tapCount = list[3];

await ((NavigationPage)App.Current.MainPage).Navigation.PushModalAsync(new NotificationPage(int.Parse(id), message,
await ((NavigationPage)App.Current.MainPage).Navigation.PushModalAsync(
new NotificationPage(_notificationService,
int.Parse(id),
message,
int.Parse(tapCount)));
return;
}
Expand All @@ -241,12 +247,12 @@ private async void Current_NotificationActionTapped(NotificationActionEventArgs
DisplayAlert(e.Request.Title, "Hello Button was Tapped", "OK");
});

LocalNotificationCenter.Current.Cancel(e.Request.NotificationId);
_notificationService.Cancel(e.Request.NotificationId);
break;

case 101:
await File.AppendAllTextAsync(_cacheFilePath, $"{Environment.NewLine}Cancel {DateTime.Now}");
LocalNotificationCenter.Current.Cancel(e.Request.NotificationId);
_notificationService.Cancel(e.Request.NotificationId);
break;
}
}
Expand Down Expand Up @@ -276,7 +282,7 @@ private void ScheduleNotification(string title, double seconds)
NotifyTime = DateTime.Now.AddSeconds(seconds),
}
};
LocalNotificationCenter.Current.Show(notification);
_notificationService.Show(notification);
}

private void ScheduleNotificationGroup()
Expand All @@ -293,7 +299,7 @@ private void ScheduleNotificationGroup()
IsGroupSummary = true
}
};
LocalNotificationCenter.Current.Show(notification);
_notificationService.Show(notification);
}

private void ShowCustomAlertFromNotification(NotificationEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions Sample/Direct Maui/LocalNotification.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public static MauiApp CreateMauiApp()
//logging.AddConsole();
});

builder.Services.AddSingleton<MainPage>();

return builder.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ namespace LocalNotification.Sample;

public partial class NotificationPage : ContentPage
{
public NotificationPage(int id, string message, int tabCount)
private readonly INotificationService _notificationService;

public NotificationPage(INotificationService notificationService, int id, string message, int tabCount)
{
InitializeComponent();
_notificationService = notificationService;

IdLabel.Text = $"Id {id}";
MessageLabel.Text = $"Message {message}";
Expand All @@ -15,7 +18,7 @@ public NotificationPage(int id, string message, int tabCount)

private async void Button_Clicked(object sender, EventArgs e)
{
var deliveredNotificationList = await LocalNotificationCenter.Current.GetDeliveredNotificationList();
var deliveredNotificationList = await _notificationService.GetDeliveredNotificationList();

if (deliveredNotificationList != null)
{
Expand Down
21 changes: 8 additions & 13 deletions Source/Plugin.LocalNotification/LocalNotificationCenter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Plugin.LocalNotification.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.Logging;
#if !NETSTANDARD2_0 && !NET6_0_OR_GREATER
#if ANDROID || IOS || MONOANDROID || XAMARINIOS
using Plugin.LocalNotification.Platforms;
#endif

Expand All @@ -21,10 +20,10 @@ public partial class LocalNotificationCenter

private static INotificationService CreateNotificationService()
{
#if NETSTANDARD2_0 || NET6_0_OR_GREATER
return null;
#else
#if ANDROID || IOS || MONOANDROID || XAMARINIOS
return new NotificationServiceImpl();
#else
return null;
#endif
}

Expand All @@ -41,11 +40,7 @@ public static INotificationService Current
get
{
var ret = implementation.Value;
if (ret == null)
{
throw new NotImplementedException(Properties.Resources.PluginNotFound);
}
return ret;
return ret ?? throw new NotImplementedException(Properties.Resources.PluginNotFound);
}
}

Expand All @@ -67,8 +62,8 @@ public static INotificationService Current
/// <summary>
///
/// </summary>
public static INotificationSerializer Serializer
{
public static INotificationSerializer Serializer
{
get
{
_serializer ??= new NotificationSerializer();
Expand Down Expand Up @@ -126,4 +121,4 @@ internal static string GetRequestSerialize(NotificationRequest request)
return serializedRequest;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER || ANDROID || IOS
#if NET6_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Maui.ApplicationModel;
Expand All @@ -15,7 +15,7 @@ namespace Plugin.LocalNotification
/// </summary>
public static class LocalNotificationExtensions
{
#if NET6_0_OR_GREATER || ANDROID || IOS
#if NET6_0_OR_GREATER
/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER || ANDROID || IOS
#if NET6_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Hosting;
Expand All @@ -8,7 +8,7 @@

namespace Plugin.LocalNotification
{
#if NET6_0_OR_GREATER || ANDROID || IOS
#if NET6_0_OR_GREATER
/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Description>The local notification plugin provides a way to show local notifications from Xamarin and MAUI apps.</Description>
<PackageIcon>icon.png</PackageIcon>
<Copyright>Copyright © Elvin (Tharindu) Thudugala</Copyright>
<Version>10.1.2</Version>
<Version>10.1.3</Version>
<PackageReleaseNotes>Check: https://github.com/thudugala/Plugin.LocalNotification/releases </PackageReleaseNotes>

<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -43,7 +43,7 @@
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-android'))">21.0</SupportedOSPlatformVersion>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.Contains('-android')) Or $(TargetFramework.Contains('-ios')) Or $(TargetFramework.StartsWith('net6.0')) Or $(TargetFramework.StartsWith('net7.0'))">
<PropertyGroup Condition="$(TargetFramework.StartsWith('net6')) Or $(TargetFramework.StartsWith('net7'))">
<UseMaui>true</UseMaui>
</PropertyGroup>

Expand Down

0 comments on commit 9e1a9ff

Please sign in to comment.