|
@@ -1,6 +1,8 @@
|
1
|
1
|
using Newtonsoft.Json.Linq;
|
2
|
2
|
using ReactNative.Bridge;
|
|
3
|
+using ReactNative.UIManager;
|
3
|
4
|
using System;
|
|
5
|
+using System.IO;
|
4
|
6
|
using System.Collections.Generic;
|
5
|
7
|
using Windows.ApplicationModel.Core;
|
6
|
8
|
using Windows.UI.Core;
|
|
@@ -12,14 +14,15 @@ namespace RNViewShot
|
12
|
14
|
/// </summary>
|
13
|
15
|
class RNViewShotModule : ReactContextNativeModuleBase
|
14
|
16
|
{
|
15
|
|
- private ReactContext reactContext;
|
|
17
|
+ private const string ErrorUnableToSnapshot = "E_UNABLE_TO_SNAPSHOT";
|
|
18
|
+ private readonly ReactContext _reactContext;
|
16
|
19
|
|
17
|
20
|
/// <summary>
|
18
|
21
|
/// Instantiates the <see cref="RNViewShotModule"/>.
|
19
|
22
|
/// </summary>
|
20
|
23
|
public RNViewShotModule(ReactContext reactContext) : base(reactContext)
|
21
|
24
|
{
|
22
|
|
- this.reactContext = reactContext;
|
|
25
|
+ this._reactContext = reactContext;
|
23
|
26
|
}
|
24
|
27
|
|
25
|
28
|
/// <summary>
|
|
@@ -34,10 +37,9 @@ namespace RNViewShot
|
34
|
37
|
}
|
35
|
38
|
|
36
|
39
|
[ReactMethod]
|
37
|
|
- public void takeSnapshot(int tag, JObject options, IPromise promise) {
|
38
|
|
- // Android Equivalent Code
|
39
|
|
- //ReactApplicationContext context = getReactApplicationContext();
|
40
|
|
- //String format = options.hasKey("format") ? options.getString("format") : "png";
|
|
40
|
+ public void takeSnapshot(int tag, JObject options, IPromise promise)
|
|
41
|
+ {
|
|
42
|
+ string format = options["format"] != null ? options.Value<string>("format") : "png";
|
41
|
43
|
//Bitmap.CompressFormat compressFormat =
|
42
|
44
|
// format.equals("png")
|
43
|
45
|
// ? Bitmap.CompressFormat.PNG
|
|
@@ -48,31 +50,35 @@ namespace RNViewShot
|
48
|
50
|
// : null;
|
49
|
51
|
//if (compressFormat == null)
|
50
|
52
|
//{
|
51
|
|
- // promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Unsupported image format: " + format + ". Try one of: png | jpg | jpeg");
|
|
53
|
+ // promise.reject(ErrorUnableToSnapshot, "Unsupported image format: " + format + ". Try one of: png | jpg | jpeg");
|
52
|
54
|
// return;
|
53
|
55
|
//}
|
54
|
|
- //double quality = options.hasKey("quality") ? options.getDouble("quality") : 1.0;
|
55
|
|
- //DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
|
56
|
|
- //Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
|
57
|
|
- //Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
|
58
|
|
- //String result = options.hasKey("result") ? options.getString("result") : "file";
|
59
|
|
- //Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer") : false;
|
|
56
|
+ double quality = options["quality"] != null ? options.Value<double>("quality") : 1.0;
|
|
57
|
+ int? width = options["width"] != null ? options.Value<int?>("width") : null;
|
|
58
|
+ int? height = options["height"] != null ? options.Value<int?>("height") : null;
|
|
59
|
+ string result = options["result"] != null ? options.Value<string>("result") : "file";
|
|
60
|
+ bool snapshotContentContainer = options["snapshotContentContainer"] != null ? options.Value<bool>("snapshotContentContainer") : false;
|
|
61
|
+
|
|
62
|
+ UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
|
|
63
|
+ uiManager.AddUIBlock(new ViewShot(tag));
|
|
64
|
+
|
60
|
65
|
//try
|
61
|
66
|
//{
|
62
|
|
- // String name = options.hasKey("filename") ? options.getString("filename") : null;
|
63
|
|
- // File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
|
64
|
|
- // UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
|
65
|
|
- // uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, snapshotContentContainer, promise));
|
|
67
|
+ // string name = options["filename"] != null ? options.Value<string>("filename") : null;
|
|
68
|
+ // //File tmpFile = "file" == result ? createTempFile(this._reactContext, format, name) : null;
|
|
69
|
+ // UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
|
|
70
|
+ // //uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, snapshotContentContainer, promise));
|
66
|
71
|
//}
|
67
|
|
- //catch (Exception e) {
|
68
|
|
- // promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
|
|
72
|
+ //catch (Exception e)
|
|
73
|
+ //{
|
|
74
|
+ // promise.reject(ErrorUnableToSnapshot, "Failed to snapshot view tag " + tag);
|
69
|
75
|
//}
|
70
|
76
|
|
71
|
77
|
//Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
|
72
|
78
|
//Graphics graphics = Graphics.FromImage(bitmap as Image);
|
73
|
79
|
//graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
|
74
|
80
|
//bitmap.Save("c:\\screenshot.jpeg", ImageFormat.Jpeg);
|
75
|
|
- promise.Resolve("This is working!");
|
|
81
|
+ promise.Resolve("Format: " + format + " Quality: " + quality);
|
76
|
82
|
}
|
77
|
83
|
}
|
78
|
84
|
}
|