dotnet / dotnet/maui

[Enhancement] More Platform Specific Lifecycle Events

Open
#999 4 comments 3 reactions 0 assignees View on GitHub
area-core-lifecycle proposal/open t/enhancement ☀️
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 10h
Merged PRs (30d)
297

Description

```csharp
public class AppleLifecycle
{
readonly ILogger logger;
public AppleLifecycle(ILogger logger) => this.logger = logger;

readonly List> handleEvents = new List>();
public IDisposable RegisterHandleEventsForBackgroundUrl(Func task)
{
this.handleEvents.Add(task);
return Disposable.Create(() => this.handleEvents.Remove(task));
}

internal async void HandleEventsForBackgroundUrl(string sessionIdentifier, Action completionHandler)
{
foreach (var handler in this.handleEvents)
{
try
{
await handler(sessionIdentifier);
}
catch (Exception ex)
{
this.logger.LogError(ex, "HandleEventsForBackgroundUrl");
}
}
completionHandler();
}

readonly List<(Action OnSuccess, Action OnError)> remoteReg = new List<(Action Success, Action Error)>();
public IDisposable RegisterForRemoteNotificationToken(Action onSuccess, Action onError)
{
var tuple = (onSuccess, onError);
this.remoteReg.Add(tuple);
return Disposable.Create(() => this.remoteReg.Remove(tuple));
}

internal void RegisteredForRemoteNotifications(NSData deviceToken)
{
foreach (var reg in this.remoteReg)
{
try
{
reg.OnSuccess(deviceToken);
}
catch (Exception ex)
{
this.logger.LogError(ex, "RegisteredForRemoteNotifications");
}
}
}

internal void FailedToRegisterForRemoteNotifications(NSError error)
{
foreach (var reg in this.remoteReg)
{
try
{
reg.OnError(error);
}
catch (Exception ex)
{
this.logger.LogError(ex, "FailedToRegisterForRemoteNotifications");
}
}
}

readonly List> receiveReg = new List>();
public IDisposable RegisterToReceiveRemoteNotifications(Func task)
{
this.receiveReg.Add(task);
return Disposable.Create(() => this.receiveReg.Add(task));
}

internal async void DidReceiveRemoteNotification(NSDictionary dictionary, Action completionHandler)
{
foreach (var reg in this.receiveReg)
{
try
{
await reg.Invoke(dictionary);
}
catch (Exception ex)
{
this.logger.LogError(ex, "DidReceiveRemoteNotification");
}
}
completionHandler(UIBackgroundFetchResult.NewData);
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.