Browse Source

Refactor code example to improve code reuse and reduce bloat. If no refname is passed, capture the displayed screen.

foxmicha 7 years ago
parent
commit
d92a431ed4
1 changed files with 3 additions and 40 deletions
  1. 3
    40
      example/App.js

+ 3
- 40
example/App.js View File

52
       snapshotContentContainer: false
52
       snapshotContentContainer: false
53
     }
53
     }
54
   };
54
   };
55
-
56
-  captureScreenshot = () =>
57
-    captureScreen(this.state.value)
58
-      .then(
59
-        res =>
60
-          this.state.value.result !== "tmpfile"
61
-          ? res
62
-          : new Promise((success, failure) =>
63
-          // just a test to ensure res can be used in Image.getSize
64
-          Image.getSize(
65
-            res,
66
-            (width, height) => (
67
-              console.log(res, width, height), success(res)
68
-            ),
69
-            failure
70
-          )
71
-        )
72
-      )
73
-      .then(res =>
74
-        this.setState({
75
-          error: null,
76
-          res,
77
-          previewSource: {
78
-            uri:
79
-              this.state.value.result === "base64"
80
-                ? "data:image/" + this.state.value.format + ";base64," + res
81
-                : res
82
-          }
83
-        })
84
-      )
85
-      .catch(
86
-        error => (
87
-          console.warn(error),
88
-          this.setState({ error, res: null, previewSource: null })
89
-        )
90
-      );
91
-    
92
-
55
+  
93
   snapshot = refname => () =>
56
   snapshot = refname => () =>
94
-    captureRef(this.refs[refname], this.state.value)
57
+    (refname ? captureRef(this.refs[refname], this.state.value) : captureScreen(this.state.value))
95
       .then(
58
       .then(
96
         res =>
59
         res =>
97
           this.state.value.result !== "tmpfile"
60
           this.state.value.result !== "tmpfile"
184
             <Btn label="📷 MapView" onPress={this.snapshot("mapview")} />
147
             <Btn label="📷 MapView" onPress={this.snapshot("mapview")} />
185
             <Btn label="📷 WebView" onPress={this.snapshot("webview")} />
148
             <Btn label="📷 WebView" onPress={this.snapshot("webview")} />
186
             <Btn label="📷 Video" onPress={this.snapshot("video")} />
149
             <Btn label="📷 Video" onPress={this.snapshot("video")} />
187
-            <Btn label="📷 Native Screenshot" onPress={this.captureScreenshot}/>
150
+            <Btn label="📷 Native Screenshot" onPress={this.snapshot()}/>
188
             <Btn
151
             <Btn
189
               label="📷 Empty View (should crash)"
152
               label="📷 Empty View (should crash)"
190
               onPress={this.snapshot("empty")}
153
               onPress={this.snapshot("empty")}