Skip to content

Commit 0bcf733

Browse files
committed
Revert "refactor(Toast): Deleting the toast module"
1 parent e018a12 commit 0bcf733

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
2+
using ReactNative.Bridge;
3+
using ReactNative.Modules.Toast;
4+
using System;
5+
6+
namespace ReactNative.Tests.Modules.Toast
7+
{
8+
[TestClass]
9+
public class ToastNotificationTests
10+
{
11+
const string TEST_CATEGORY = "Modules";
12+
13+
[TestMethod, TestCategory(TEST_CATEGORY)]
14+
public void ToastModule_Null_ArgumentsTest()
15+
{
16+
AssertEx.Throws<ArgumentNullException>(
17+
() => new ToastModule(null),
18+
ex => Assert.AreEqual("reactContext", ex.ParamName));
19+
20+
var context = new ReactContext();
21+
var module = new ToastModule(context);
22+
Assert.AreSame(context, module.Context);
23+
}
24+
25+
[TestMethod, TestCategory(TEST_CATEGORY)]
26+
public void Send_Toast_Invalid_Duration()
27+
{
28+
var context = new ReactContext();
29+
var module = new ToastModule(context);
30+
31+
AssertEx.Throws<ArgumentException>(
32+
() => module.show("Invalid Toast", -1),
33+
ex => Assert.AreEqual("duration", ex.ParamName));
34+
}
35+
36+
[TestMethod, TestCategory(TEST_CATEGORY)]
37+
[Ignore]
38+
public void Send_Basic_Toast()
39+
{
40+
var context = new ReactContext();
41+
var module = new ToastModule(context);
42+
43+
module.show("SHORT TOAST", 0);
44+
}
45+
46+
[TestMethod, TestCategory(TEST_CATEGORY)]
47+
[Ignore]
48+
public void Send_Long_Toast()
49+
{
50+
var context = new ReactContext();
51+
var module = new ToastModule(context);
52+
53+
module.show("LONG TOAST container", 1);
54+
}
55+
}
56+
}

ReactWindows/ReactNative.Tests/ReactNative.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<Compile Include="Modules\Network\TaskCancellationManagerTests.cs" />
128128
<Compile Include="Modules\StatusBar\StatusBarModuleTests.cs" />
129129
<Compile Include="Modules\Storage\AsyncStorageModuleTests.cs" />
130+
<Compile Include="Modules\Toast\ToastNotificationTests.cs" />
130131
<Compile Include="Modules\WebSocket\WebSocketModuleTests.cs" />
131132
<Compile Include="Properties\AssemblyInfo.cs" />
132133
<Compile Include="ReactInstanceManagerTests.cs" />
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using Windows.Data.Xml.Dom;
4+
using Windows.UI.Notifications;
5+
6+
namespace ReactNative.Modules.Toast
7+
{
8+
class ToastHelper
9+
{
10+
internal static void SendToast(string message, string duration = "short")
11+
{
12+
var doc = MakeDoc(message).Result;
13+
var notification = new ToastNotification(doc);
14+
15+
// Get the toast notification manager for the current app.
16+
ToastNotificationManager.CreateToastNotifier().Show(notification);
17+
}
18+
19+
static async Task<XmlDocument> MakeDoc(string content, Durations duration = Durations.@short)
20+
{
21+
// TODO: alt templates: https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx
22+
using (var stream = new MemoryStream())
23+
using (var reader = new StreamReader(stream))
24+
{
25+
await BuildNotificationXml(stream, content, duration);
26+
stream.Position = 0;
27+
var xml = await reader.ReadToEndAsync();
28+
var doc = new XmlDocument();
29+
doc.LoadXml(xml);
30+
return doc;
31+
}
32+
}
33+
34+
static async Task BuildNotificationXml(Stream stream, string content, Durations duration = Durations.@short)
35+
{
36+
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
37+
settings.OmitXmlDeclaration = true;
38+
settings.Async = true;
39+
40+
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream, settings))
41+
{
42+
await writer.WriteStartElementAsync(null, "toast", null);
43+
await writer.WriteAttributeStringAsync(null, "activationType", null, "foreground");
44+
await writer.WriteAttributeStringAsync(null, "duration", null, duration.ToString());
45+
46+
await writer.WriteStartElementAsync(null, "visual", null);
47+
await writer.WriteStartElementAsync(null, "binding", null);
48+
await writer.WriteAttributeStringAsync(null, "template", null, "ToastText01");
49+
50+
await writer.WriteStartElementAsync(null, "text", null);
51+
await writer.WriteAttributeStringAsync(null, "id", null, "1");
52+
await writer.WriteStringAsync(content);
53+
await writer.WriteEndElementAsync();
54+
55+
await writer.WriteEndElementAsync();
56+
await writer.FlushAsync();
57+
}
58+
}
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using ReactNative.Bridge;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace ReactNative.Modules.Toast
6+
{
7+
enum Durations : int { @short, @long }
8+
9+
sealed class ToastModule : ReactContextNativeModuleBase
10+
{
11+
const string DURATION_SHORT_KEY = "SHORT";
12+
const string DURATION_LONG_KEY = "LONG";
13+
14+
public ToastModule(ReactContext reactContext)
15+
: base(reactContext)
16+
{
17+
}
18+
19+
public override string Name
20+
{
21+
get
22+
{
23+
return "ToastModule";
24+
}
25+
}
26+
27+
public override IReadOnlyDictionary<string, object> Constants
28+
{
29+
get
30+
{
31+
return new Dictionary<string, object>
32+
{
33+
{ DURATION_SHORT_KEY, DURATION_SHORT_KEY },
34+
{ DURATION_LONG_KEY, DURATION_LONG_KEY },
35+
};
36+
}
37+
}
38+
39+
[ReactMethod]
40+
public void show(string message, int duration)
41+
{
42+
if (Enum.IsDefined(typeof(Durations), duration))
43+
{
44+
var durationToUse = (Durations)duration;
45+
ToastHelper.SendToast(message, durationToUse.ToString());
46+
}
47+
else
48+
{
49+
throw new ArgumentException("invalid duration for Toast", "duration");
50+
}
51+
}
52+
}
53+
}

ReactWindows/ReactNative/ReactNative.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@
358358
<Compile Include="UIManager\BorderedCanvasManager.cs" />
359359
<Compile Include="Views\View\ReactViewManager.cs" />
360360
<Compile Include="Views\TextInput\ReactTextInputManager.cs" />
361+
<Compile Include="Modules\Toast\ToastHelper.cs" />
362+
<Compile Include="Modules\Toast\ToastModule.cs" />
361363
<Compile Include="UIManager\BorderedViewParentManager.cs" />
362364
<Compile Include="Views\Web\Events\WebViewLoadingErrorEvent.cs" />
363365
<Compile Include="Views\Web\Events\WebViewLoadingEvent.cs" />

0 commit comments

Comments
 (0)