Skip to content

disposable-services #540

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

Merged
merged 1 commit into from
Jan 3, 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

using System;
using System.Threading.Tasks;
using Android.Content;
using AndroidX.Lifecycle;
Expand All @@ -15,7 +16,7 @@ namespace Softeq.XToolkit.PushNotifications.Droid.Services
/// <summary>
/// Default implementation of <see cref="IDroidPushNotificationsConsumer"/> interface for Android platform.
/// </summary>
public sealed class DroidPushNotificationsConsumer : IDroidPushNotificationsConsumer
public sealed class DroidPushNotificationsConsumer : IDroidPushNotificationsConsumer, IDisposable
{
private readonly IDroidPushNotificationsParser _pushNotificationsParser;
private readonly IPushNotificationsHandler _pushNotificationsHandler;
Expand Down Expand Up @@ -53,6 +54,11 @@ public DroidPushNotificationsConsumer(
});
}

~DroidPushNotificationsConsumer()
{
Dispose(false);
}

/// <inheritdoc />
public bool TryHandleNotification(RemoteMessage message)
{
Expand Down Expand Up @@ -93,6 +99,12 @@ public Task OnUnregisterFromPushNotifications()
return _pushTokenSynchronizer.UnregisterFromRemotePushNotificationsAsync();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void OnMessageReceivedInternal(PushNotificationModel parsedNotification, bool inForeground)
{
if (parsedNotification.IsSilent)
Expand All @@ -104,5 +116,16 @@ private void OnMessageReceivedInternal(PushNotificationModel parsedNotification,
_pushNotificationsHandler.HandlePushNotificationReceived(parsedNotification, inForeground);
}
}

private void Dispose(bool isDisposing)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private void Dispose(bool isDisposing)
protected virtual void Dispose(bool isDisposing)

{
if (isDisposing)
{
Execute.BeginOnUIThread(() =>
{
ProcessLifecycleOwner.Get().Lifecycle.RemoveObserver(_lifecycleObserver);
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Softeq.XToolkit.PushNotifications.Droid.Services
/// interfaces for Android platform. Handles all interactions with the platform, related to push notifications.
/// </summary>
public sealed class DroidPushNotificationsService :
IPushNotificationsService, IActivityLauncherDelegate, IDroidFirebaseMessagingHandler
IPushNotificationsService, IActivityLauncherDelegate, IDroidFirebaseMessagingHandler, IDisposable
{
private readonly IDroidPushNotificationsConsumer _pushNotificationsConsumer;
private readonly ILogger _logger;
Expand All @@ -40,6 +40,11 @@ public DroidPushNotificationsService(
_logger = logManager.GetLogger<DroidPushNotificationsService>();
}

~DroidPushNotificationsService()
{
Dispose(false);
}

private bool IsRegistered { get; set; }

/// <inheritdoc />
Expand Down Expand Up @@ -114,6 +119,20 @@ public bool TryHandleLaunchIntent(Intent? intent)
return intent != null && _pushNotificationsConsumer.TryHandlePushNotificationIntent(intent);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool isDisposing)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private void Dispose(bool isDisposing)
protected virtual void Dispose(bool isDisposing)

{
if (isDisposing)
{
_registrationSemaphore.Dispose();
}
}

/// <inheritdoc />
void IDroidFirebaseMessagingHandler.OnNotificationReceived(RemoteMessage message)
{
Expand Down