-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
125 lines (113 loc) · 3.56 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, {useCallback, useEffect, useState} from 'react';
import {Button, SafeAreaView, StatusBar, Text, View} from 'react-native';
import PdfRendererView from 'react-native-pdf-renderer';
import * as FileSystem from 'expo-file-system';
import ReactNativeBlobUtil from 'react-native-blob-util';
const PDF_URL = 'https://www.hq.nasa.gov/alsj/a17/A17_FlightPlan.pdf'; // 618 pages
// const PDF_URL = 'https://www.africau.edu/images/default/sample.pdf'; // 2 pages
function App(): JSX.Element {
const [totalPages, setTotalPages] = useState(0);
const [currentPage, setCurrentPage] = useState(0);
const [downloading, setDownloading] = useState(false);
const [singlePage, setSinglePage] = useState(false);
const [toggle, setToggle] = useState(true);
const [source, setSource] = useState<string>();
const downloadWithExpoFileSystem = useCallback(async () => {
try {
setDownloading(true);
/**
* Download the PDF file with any other library, like "expo-file-system", "rn-fetch-blob" or "react-native-blob-util"
*/
const response = await FileSystem.downloadAsync(
PDF_URL,
FileSystem.documentDirectory + 'file.pdf',
);
/*
* Then, set the local file URI to state and pass to the PdfRendererView source prop.
*/
setSource(response.uri);
} catch (err) {
console.warn(err);
} finally {
setDownloading(false);
}
}, []);
const downloadWithBlobUtil = useCallback(async () => {
try {
setDownloading(true);
/**
* Download the PDF file with any other library, like "expo-file-system", "rn-fetch-blob" or "react-native-blob-util"
*/
const dirs = ReactNativeBlobUtil.fs.dirs;
const response = await ReactNativeBlobUtil.config({
path: dirs.DocumentDir + '/file.pdf',
}).fetch('GET', PDF_URL);
/*
* Then, set the local file URI to state and pass to the PdfRendererView source prop.
*/
setSource(response.path());
} catch (err) {
console.warn(err);
} finally {
setDownloading(false);
}
}, []);
useEffect(() => {
downloadWithExpoFileSystem();
// downloadWithBlobUtil();
}, [downloadWithExpoFileSystem]);
const renderPdfView = () => {
if (downloading) {
return <Text>Downloading...</Text>;
}
if (!toggle) {
return <Text>Unmounted</Text>;
}
return (
<>
<Button
title="Single Page"
onPress={() => setSinglePage(prev => !prev)}
/>
<PdfRendererView
style={{backgroundColor: 'red'}}
source={source}
distanceBetweenPages={16}
maxZoom={2}
singlePage={singlePage}
onPageChange={(current, total) => {
console.log('onPageChange', {current, total});
setCurrentPage(current);
setTotalPages(total);
}}
/>
<View
style={{
position: 'absolute',
bottom: 16,
left: 0,
right: 0,
alignItems: 'center',
}}>
<Text
style={{
backgroundColor: 'rgba(255,255,255,0.5)',
color: 'black',
padding: 4,
borderRadius: 4,
}}>
{currentPage + 1}/{totalPages}
</Text>
</View>
</>
);
};
return (
<SafeAreaView style={{flex: 1}}>
<StatusBar translucent={false} />
<Button title="Mount/Unmount" onPress={() => setToggle(prev => !prev)} />
{renderPdfView()}
</SafeAreaView>
);
}
export default App;