Skip to content

Commit d92a431

Browse files
committed
Refactor code example to improve code reuse and reduce bloat. If no refname is passed, capture the displayed screen.
1 parent 5ed8cf9 commit d92a431

File tree

1 file changed

+3
-40
lines changed

1 file changed

+3
-40
lines changed

example/App.js

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,46 +52,9 @@ export default class App extends Component {
5252
snapshotContentContainer: false
5353
}
5454
};
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+
9356
snapshot = refname => () =>
94-
captureRef(this.refs[refname], this.state.value)
57+
(refname ? captureRef(this.refs[refname], this.state.value) : captureScreen(this.state.value))
9558
.then(
9659
res =>
9760
this.state.value.result !== "tmpfile"
@@ -184,7 +147,7 @@ export default class App extends Component {
184147
<Btn label="📷 MapView" onPress={this.snapshot("mapview")} />
185148
<Btn label="📷 WebView" onPress={this.snapshot("webview")} />
186149
<Btn label="📷 Video" onPress={this.snapshot("video")} />
187-
<Btn label="📷 Native Screenshot" onPress={this.captureScreenshot}/>
150+
<Btn label="📷 Native Screenshot" onPress={this.snapshot()}/>
188151
<Btn
189152
label="📷 Empty View (should crash)"
190153
onPress={this.snapshot("empty")}

0 commit comments

Comments
 (0)