|
1 | 1 | using ReactNative.Bridge;
|
2 | 2 | using ReactNative.Modules.Core;
|
3 | 3 | using System.Collections.Generic;
|
| 4 | +using Windows.ApplicationModel.Core; |
4 | 5 | using Windows.UI.ViewManagement;
|
5 | 6 |
|
6 | 7 | namespace ReactNative.Modules.Accessibilityinfo
|
7 | 8 | {
|
8 |
| - class AccessibilityInfoModule : ReactContextNativeModuleBase |
| 9 | + class AccessibilityInfoModule : ReactContextNativeModuleBase, ILifecycleEventListener, IBackgroundEventListener |
9 | 10 | {
|
10 | 11 | private readonly AccessibilitySettings _accessibility = new AccessibilitySettings();
|
11 | 12 | private readonly UISettings _settings = new UISettings();
|
12 | 13 |
|
| 14 | + private bool _isSubscribed; |
| 15 | + |
13 | 16 | private string GetRgbaString(UIElementType type)
|
14 | 17 | {
|
15 | 18 | var color = _settings.UIElementColor(type);
|
@@ -53,20 +56,72 @@ public override IReadOnlyDictionary<string, object> Constants
|
53 | 56 | }
|
54 | 57 | }
|
55 | 58 |
|
| 59 | + private static bool HasCoreWindow => CoreApplication.MainView.CoreWindow != null; |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Called after the creation of a <see cref="IReactInstance"/>, |
| 63 | + /// </summary> |
56 | 64 | public override void Initialize()
|
57 | 65 | {
|
58 |
| - DispatcherHelpers.RunOnDispatcher(() => |
| 66 | + Context.AddLifecycleEventListener(this); |
| 67 | + Context.AddBackgroundEventListener(this); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Called when the application is suspended. |
| 72 | + /// </summary> |
| 73 | + public void OnSuspend() |
| 74 | + { |
| 75 | + Unsubscribe(); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Called when the application is resumed. |
| 80 | + /// </summary> |
| 81 | + public void OnResume() |
| 82 | + { |
| 83 | + Subscribe(); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Called when the host entered background mode. |
| 88 | + /// </summary> |
| 89 | + public void OnEnteredBackground() |
| 90 | + { |
| 91 | + Unsubscribe(); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Called when the host is leaving background mode. |
| 96 | + /// </summary> |
| 97 | + public void OnLeavingBackground() |
| 98 | + { |
| 99 | + Subscribe(); |
| 100 | + } |
| 101 | + |
| 102 | + private void Subscribe() |
| 103 | + { |
| 104 | + if (!_isSubscribed && HasCoreWindow) |
59 | 105 | {
|
| 106 | + _isSubscribed = true; |
60 | 107 | _accessibility.HighContrastChanged += OnHighContrastChanged;
|
61 |
| - }); |
| 108 | + } |
62 | 109 | }
|
63 | 110 |
|
64 |
| - public override void OnReactInstanceDispose() |
| 111 | + private void Unsubscribe() |
65 | 112 | {
|
66 |
| - DispatcherHelpers.RunOnDispatcher(() => |
| 113 | + if (_isSubscribed && HasCoreWindow) |
67 | 114 | {
|
68 | 115 | _accessibility.HighContrastChanged -= OnHighContrastChanged;
|
69 |
| - }); |
| 116 | + _isSubscribed = false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Called when the application is terminated. |
| 122 | + /// </summary> |
| 123 | + public void OnDestroy() |
| 124 | + { |
70 | 125 | }
|
71 | 126 |
|
72 | 127 | private void OnHighContrastChanged(AccessibilitySettings sender, object args)
|
|
0 commit comments