Sfoglia il codice sorgente

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

foxmicha 6 anni fa
parent
commit
d92a431ed4
1 ha cambiato i file con 3 aggiunte e 40 eliminazioni
  1. 3
    40
      example/App.js

+ 3
- 40
example/App.js Vedi File

@@ -52,46 +52,9 @@ export default class App extends Component {
52 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 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 58
       .then(
96 59
         res =>
97 60
           this.state.value.result !== "tmpfile"
@@ -184,7 +147,7 @@ export default class App extends Component {
184 147
             <Btn label="📷 MapView" onPress={this.snapshot("mapview")} />
185 148
             <Btn label="📷 WebView" onPress={this.snapshot("webview")} />
186 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 151
             <Btn
189 152
               label="📷 Empty View (should crash)"
190 153
               onPress={this.snapshot("empty")}