Skip to content

Commit 0de80db

Browse files
biancacamargomachadobianca-camargo-machado1
and
bianca-camargo-machado1
authored
Download de xlsx - Android (#33)
Co-authored-by: bianca-camargo-machado1 <[email protected]>
1 parent 0157212 commit 0de80db

File tree

9 files changed

+89
-44
lines changed

9 files changed

+89
-44
lines changed

android/app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ android {
189189
}
190190

191191
dependencies {
192+
implementation project(':rn-fetch-blob')
192193
implementation project(':react-native-camera')
193194
implementation fileTree(dir: "libs", include: ["*.jar"])
194195
implementation "com.facebook.react:react-native:+" // From node_modules

android/app/src/main/AndroidManifest.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.dtool">
33

4-
<uses-permission android:name="android.permission.INTERNET" />
4+
<uses-permission android:name="android.permission.INTERNET"/>
55
<uses-permission android:name="android.permission.CAMERA"/>
66
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
7-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
8-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
8+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
99

1010
<application
1111
android:name=".MainApplication"
@@ -23,6 +23,7 @@
2323
<intent-filter>
2424
<action android:name="android.intent.action.MAIN" />
2525
<category android:name="android.intent.category.LAUNCHER" />
26+
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
2627
</intent-filter>
2728
</activity>
2829
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

android/app/src/main/java/com/dtool/MainApplication.java

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import androidx.multidex.MultiDexApplication;
55
import com.facebook.react.PackageList;
66
import com.facebook.react.ReactApplication;
7+
import com.RNFetchBlob.RNFetchBlobPackage;
78
import org.reactnative.camera.RNCameraPackage;
89
import com.facebook.react.ReactNativeHost;
910
import com.facebook.react.ReactPackage;

android/settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
rootProject.name = 'dTool'
2+
include ':rn-fetch-blob'
3+
project(':rn-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/rn-fetch-blob/android')
24
include ':react-native-camera'
35
project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android')
46
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

ios/Podfile

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ target 'dTool' do
3636

3737
pod 'react-native-camera', :path => '../node_modules/react-native-camera'
3838

39+
pod 'rn-fetch-blob', :path => '../node_modules/rn-fetch-blob'
40+
3941
target 'dToolTests' do
4042
inherit! :search_paths
4143
# Pods for testing

ios/dTool/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</dict>
4141
</dict>
4242
<key>NSLocationWhenInUseUsageDescription</key>
43-
<string></string>
43+
<string/>
4444
<key>UILaunchStoryboardName</key>
4545
<string>LaunchScreen</string>
4646
<key>UIRequiredDeviceCapabilities</key>

package-lock.json

+40-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@
2828
"react-native-device-info": "^5.6.0",
2929
"react-native-dotenv": "^0.2.0",
3030
"react-native-elements": "^2.0.0",
31-
"react-native-fs": "^2.16.6",
3231
"react-native-gesture-handler": "^1.6.0",
3332
"react-native-material-dropdown": "^0.11.1",
3433
"react-native-material-kit": "^0.6.0-beta.1",
3534
"react-native-reanimated": "^1.7.1",
3635
"react-native-safe-area-context": "^0.7.3",
3736
"react-native-screens": "^2.4.0",
38-
"react-native-share": "^3.3.3",
3937
"react-native-svg": "^12.0.3",
4038
"react-native-svg-transformer": "^0.14.3",
4139
"react-native-vector-icons": "^6.6.0",
4240
"react-redux": "^7.2.0",
4341
"redux": "^4.0.5",
42+
"rn-fetch-blob": "^0.12.0",
4443
"rne-modal-tooltip": "gist:b28c003d87c619674def0878473338a0"
4544
},
4645
"devDependencies": {
@@ -51,6 +50,8 @@
5150
"@types/jest": "^24.9.1",
5251
"@types/react": "^16.9.25",
5352
"@types/react-native": "^0.60.25",
53+
"@types/react-native-fetch-blob": "^0.10.5",
54+
"@types/react-native-share": "^3.3.0",
5455
"@types/react-native-vector-icons": "^6.4.5",
5556
"@types/react-redux": "^7.1.9",
5657
"@types/react-test-renderer": "16.9.1",

src/services/appService.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getUniqueId } from "react-native-device-info";
2+
import { PermissionsAndroid, YellowBox } from "react-native";
23
import axios from "axios";
3-
import RNFS from "react-native-fs";
4-
import Share from "react-native-share";
4+
import RNFetchBlob from "rn-fetch-blob";
55
import moment from "moment";
66
import {
77
clear,
@@ -16,6 +16,8 @@ import {
1616
import api from "./API";
1717
import { Permission, Reports } from "./types";
1818

19+
YellowBox.ignoreWarnings(["Require cycle"]);
20+
1921
/**
2022
* Coordena operações complexas da aplicação (principalmente aquelas relacionadas
2123
* ao fluxo online/offline), de forma a simplificar a implementação das telas do app.
@@ -206,29 +208,43 @@ const uploadExecutions = async (): Promise<void> => {
206208
* - `network`: erro de rede.
207209
*/
208210
export const downloadReport = async (): Promise<void> => {
211+
try {
212+
await PermissionsAndroid.requestMultiple([
213+
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
214+
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
215+
]);
216+
} catch (err) {
217+
console.warn(err);
218+
}
219+
209220
await authenticate();
210221

211222
try {
212223
const { code, token } = await getAuth();
213224
const url = `${api.defaults.baseURL}/reports/complete`;
214225
const filename = `Relatório dTool - ${code}.xlsx`;
215-
const localFile = `${RNFS.DocumentDirectoryPath}/${filename}`;
216-
217-
const { promise } = RNFS.downloadFile({
218-
fromUrl: url,
219-
toFile: localFile,
220-
headers: { Authorization: `Bearer ${token}` },
221-
});
222-
223-
await promise;
224-
225-
await Share.open({
226-
url: `file://${localFile}`,
227-
title: `Salvar relatório do hospital`,
228-
failOnCancel: false,
229-
});
230-
231-
await RNFS.unlink(localFile);
226+
const localFile = `${RNFetchBlob.fs.dirs.DownloadDir}/${filename}`;
227+
228+
RNFetchBlob.config({
229+
addAndroidDownloads: {
230+
useDownloadManager: true, // required
231+
notification: true,
232+
path: `file://${localFile}`,
233+
mime:
234+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
235+
description: "Relatório DTool.",
236+
},
237+
})
238+
.fetch("GET", url, {
239+
Authorization: `Bearer ${token}`,
240+
})
241+
.then((res) => {
242+
// when response status code is 200
243+
console.log("The file saved to ", res.path());
244+
})
245+
.catch((_errorMessage) => {
246+
console.log(_errorMessage);
247+
});
232248
} catch (error) {
233249
// invalid ID
234250
if (error.response?.status === 400) {

0 commit comments

Comments
 (0)