Skip to content

Commit

Permalink
feat: App/Window activation and visibility on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 22, 2021
1 parent fb18f1f commit 5dbf801
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI/BaseActivity.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public override void AddContentView(View view, ViewGroup.LayoutParams @params)
{
ResignCurrent();

Windows.UI.Xaml.Application.Current?.OnEnteredBackground();
Windows.UI.Xaml.Window.Current?.OnVisibilityChanged(false);
Windows.UI.Xaml.Application.Current?.OnEnteredBackground();
}

partial void InnerDestroy() => ResignCurrent();
Expand Down
48 changes: 45 additions & 3 deletions src/Uno.UI/UI/Xaml/Application.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uno.UI.Services;
using Uno.Extensions;
using Microsoft.Extensions.Logging;
using Windows.UI.Core;

#if HAS_UNO_WINUI
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
Expand All @@ -31,6 +32,8 @@ public Application()
{
Current = this;
ResourceHelper.ResourcesService = new ResourcesService(new[] { NSBundle.MainBundle });

SubscribeBackgroundNotifications();
}

public Application(IntPtr handle) : base(handle)
Expand Down Expand Up @@ -125,9 +128,6 @@ public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
_preventSecondaryActivationHandling = false;
}

public override void DidEnterBackground(UIApplication application)
=> OnSuspending();

partial void OnSuspendingPartial()
{
var operation = new SuspendingOperation(DateTime.Now.AddSeconds(10));
Expand Down Expand Up @@ -210,6 +210,48 @@ private bool TryGetUserActivityFromLaunchOptions(NSDictionary launchOptions, out

return userActivity != null;
}

private void SubscribeBackgroundNotifications()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
NSNotificationCenter.DefaultCenter.AddObserver(UIScene.DidEnterBackgroundNotification, OnEnteredBackground);
NSNotificationCenter.DefaultCenter.AddObserver(UIScene.WillEnterForegroundNotification, OnLeavingBackground);
NSNotificationCenter.DefaultCenter.AddObserver(UIScene.DidActivateNotification, OnActivated);
NSNotificationCenter.DefaultCenter.AddObserver(UIScene.WillDeactivateNotification, OnDeactivated);
}
else
{
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidEnterBackgroundNotification, OnEnteredBackground);
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, OnLeavingBackground);
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidBecomeActiveNotification, OnActivated);
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillResignActiveNotification, OnDeactivated);
}
}

private void OnEnteredBackground(NSNotification notification)
{
Windows.UI.Xaml.Window.Current?.OnVisibilityChanged(false);
EnteredBackground?.Invoke(this, new EnteredBackgroundEventArgs());

OnSuspending();
}

private void OnLeavingBackground(NSNotification notification)
{
LeavingBackground?.Invoke(this, new LeavingBackgroundEventArgs());
Windows.UI.Xaml.Window.Current?.OnVisibilityChanged(true);
}

private void OnActivated(NSNotification notification)
{
Windows.UI.Xaml.Window.Current?.OnActivated(CoreWindowActivationState.CodeActivated);
}

private void OnDeactivated(NSNotification notification)
{
Windows.UI.Xaml.Window.Current?.OnActivated(CoreWindowActivationState.Deactivated);
}
}
}
#endif

0 comments on commit 5dbf801

Please sign in to comment.