forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogModule.cs
125 lines (110 loc) · 3.82 KB
/
DialogModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Newtonsoft.Json.Linq;
using ReactNative.Bridge;
using ReactNative.Collections;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Popups;
namespace ReactNative.Modules.Dialog
{
class DialogModule : ReactContextNativeModuleBase, ILifecycleEventListener
{
private MessageDialog _pendingDialog;
private bool _isInForeground;
public DialogModule(ReactContext reactContext)
: base(reactContext)
{
}
public override string Name
{
get
{
return "DialogManagerWindows";
}
}
public override IReadOnlyDictionary<string, object> Constants
{
get
{
return new Dictionary<string, object>
{
{ DialogModuleHelper.ActionButtonClicked, DialogModuleHelper.ActionButtonClicked },
{ DialogModuleHelper.ActionDismissed, DialogModuleHelper.ActionDismissed },
{ DialogModuleHelper.KeyButtonPositive, DialogModuleHelper.KeyButtonPositiveValue },
{ DialogModuleHelper.KeyButtonNegative, DialogModuleHelper.KeyButtonNegativeValue },
};
}
}
public override void Initialize()
{
Context.AddLifecycleEventListener(this);
}
public void OnSuspend()
{
_isInForeground = false;
}
public async void OnResume()
{
_isInForeground = true;
var pendingDialog = _pendingDialog;
_pendingDialog = null;
if (pendingDialog != null)
{
await pendingDialog.ShowAsync().AsTask().ConfigureAwait(false);
}
}
public void OnDestroy()
{
}
[ReactMethod]
public void showAlert(
JObject config,
ICallback errorCallback,
ICallback actionCallback)
{
var message = config.Value<string>("message") ?? "";
var messageDialog = new MessageDialog(message)
{
Title = config.Value<string>("title"),
};
if (config.ContainsKey(DialogModuleHelper.KeyButtonPositive))
{
messageDialog.Commands.Add(new UICommand
{
Label = config.Value<string>(DialogModuleHelper.KeyButtonPositive),
Id = DialogModuleHelper.KeyButtonPositiveValue,
Invoked = target => OnInvoked(target, actionCallback),
});
}
if (config.ContainsKey(DialogModuleHelper.KeyButtonNegative))
{
messageDialog.Commands.Add(new UICommand
{
Label = config.Value<string>(DialogModuleHelper.KeyButtonNegative),
Id = DialogModuleHelper.KeyButtonNegativeValue,
Invoked = target => OnInvoked(target, actionCallback),
});
}
RunOnDispatcher(async () =>
{
if (_isInForeground)
{
await messageDialog.ShowAsync().AsTask().ConfigureAwait(false);
}
else
{
_pendingDialog = messageDialog;
}
});
}
private void OnInvoked(IUICommand target, ICallback callback)
{
callback.Invoke(DialogModuleHelper.ActionButtonClicked, target.Id);
}
private static async void RunOnDispatcher(DispatchedHandler action)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action).AsTask().ConfigureAwait(false);
}
}
}