浏览代码

Rewrote Viewshoot to test #190 no problem noticed with refresh

closes #190
Gaëtan Renaudeau 5 年前
父节点
当前提交
075831b822
没有帐户链接到提交者的电子邮件
共有 1 个文件被更改,包括 80 次插入110 次删除
  1. 80
    110
      example/src/Viewshoot.js

+ 80
- 110
example/src/Viewshoot.js 查看文件

@@ -1,132 +1,102 @@
1
-/**
2
- * Sample How To Screenshot Screen inside of ScrollView
3
- * The original github from
4
- * https://github.com/gre/react-native-view-shot
5
- */
6
-import React, { Component } from 'react';
7
-import { ScrollView, StyleSheet, Text, View, Button, Image } from 'react-native';
8
-
1
+import React, { useCallback, useState, useRef } from 'react';
2
+import {
3
+  ScrollView,
4
+  StyleSheet,
5
+  Text,
6
+  View,
7
+  Image,
8
+  SafeAreaView,
9
+  RefreshControl,
10
+} from 'react-native';
9 11
 import ViewShot from 'react-native-view-shot';
12
+import Btn from './Btn';
10 13
 
11
-export default class Viewshoot extends Component {
12
-  static navigationProps = {
13
-    title: 'Viewshoot',
14
-  };
15
-
16
-  state = {
17
-    error: null,
18
-    res: null,
19
-    options: {
20
-      format: 'jpg',
21
-      quality: 0.9,
22
-    },
23
-  };
14
+const Viewshoot = () => {
15
+  const full = useRef();
16
+  const [preview, setPreview] = useState(null);
17
+  const [itemsCount, setItemsCount] = useState(10);
18
+  const [refreshing, setRefreshing] = useState(false);
24 19
 
25
-  renderContent() {
26
-    const data = [
27
-      1,
28
-      2,
29
-      3,
30
-      4,
31
-      5,
32
-      6,
33
-      7,
34
-      8,
35
-      9,
36
-      10,
37
-      11,
38
-      12,
39
-      13,
40
-      14,
41
-      15,
42
-      16,
43
-      17,
44
-      18,
45
-      19,
46
-      20,
47
-      21,
48
-      22,
49
-      23,
50
-    ];
51
-    return data.map((item, index) => {
52
-      return (
53
-        <View style={styles.item} key={index}>
54
-          <Text>{item}</Text>
55
-        </View>
56
-      );
57
-    });
58
-  }
20
+  const onCapture = useCallback(() => {
21
+    full.current.capture().then(uri => setPreview({ uri }));
22
+  }, []);
59 23
 
60
-  renderResultSnapshot() {
61
-    if (this.state.res !== null) {
62
-      console.log('Result on return snapshot: ', this.state.res);
63
-      return (
64
-        <Image
65
-          fadeDuration={0}
66
-          resizeMode="contain"
67
-          style={styles.previewImage}
68
-          source={this.state.res}
24
+  return (
25
+    <ScrollView
26
+      style={styles.container}
27
+      contentContainerStyle={styles.root}
28
+      refreshControl={
29
+        <RefreshControl
30
+          refreshing={refreshing}
31
+          onRefresh={() => {
32
+            setRefreshing(true);
33
+            setTimeout(() => {
34
+              setItemsCount(itemsCount + 10);
35
+              setRefreshing(false);
36
+            }, 5000);
37
+          }}
69 38
         />
70
-      );
71
-    }
72
-
73
-    return;
74
-  }
75
-
76
-  renderShootButton() {
77
-    return (
78
-      <Button
79
-        onPress={async () => await this.captureViewShoot()}
80
-        title="Shoot Me"
81
-        color="#841584"
82
-      />
83
-    );
84
-  }
85
-
86
-  full = React.createRef();
39
+      }
40
+    >
41
+      <SafeAreaView>
42
+        <ViewShot ref={full} style={styles.container}>
43
+          <Btn onPress={onCapture} label="Shoot Me" />
87 44
 
88
-  captureViewShoot() {
89
-    this.full.current.capture().then(uri => {
90
-      console.log('do something with ', uri);
91
-      this.setState({ res: { uri: uri } });
92
-    });
93
-  }
45
+          <Image
46
+            fadeDuration={0}
47
+            resizeMode="contain"
48
+            style={styles.previewImage}
49
+            source={preview}
50
+          />
94 51
 
95
-  renderViewShot() {
96
-    return (
97
-      <ScrollView style={styles.container}>
98
-        <ViewShot
99
-          ref={this.full}
100
-          options={{ format: this.state.options.format, quality: this.state.options.quality }}
101
-          style={styles.container}
102
-        >
103
-          {this.renderResultSnapshot()}
104
-          {this.renderContent()}
105
-          {this.renderShootButton()}
52
+          {Array(itemsCount)
53
+            .fill(null)
54
+            .map((_, index) => ({
55
+              key: index,
56
+              text: `${index + 1}`,
57
+              color: `hsl(${(index * 13) % 360}, 50%, 80%)`,
58
+            }))
59
+            .map(({ key, text, color }) => {
60
+              return (
61
+                <View style={[styles.item, { backgroundColor: color }]} key={key}>
62
+                  <Text style={styles.itemText}>{text}</Text>
63
+                </View>
64
+              );
65
+            })}
106 66
         </ViewShot>
107
-      </ScrollView>
108
-    );
109
-  }
110
-
111
-  render() {
112
-    return this.renderViewShot();
113
-  }
114
-}
67
+      </SafeAreaView>
68
+    </ScrollView>
69
+  );
70
+};
115 71
 
116 72
 const styles = StyleSheet.create({
117 73
   container: {
118 74
     flex: 1,
119 75
     backgroundColor: '#fff',
120 76
   },
77
+  root: {
78
+    paddingVertical: 20,
79
+  },
121 80
   content: {
122
-    padding: 10,
123 81
     backgroundColor: '#fff',
124 82
   },
125 83
   item: {
126
-    height: 50,
84
+    padding: 20,
85
+    alignItems: 'center',
86
+    justifyContent: 'center',
87
+  },
88
+  itemText: {
89
+    fontSize: 22,
90
+    color: '#666',
127 91
   },
128 92
   previewImage: {
129
-    width: 375,
130
-    height: 300,
93
+    height: 200,
94
+    backgroundColor: 'black',
131 95
   },
132 96
 });
97
+
98
+Viewshoot.navigationProps = {
99
+  title: 'Viewshoot',
100
+};
101
+
102
+export default Viewshoot;