Skip to content

Commit 15ec76b

Browse files
authored
Merge pull request #10 from Vadko/10.12-bug-fixes
feat: onDismiss for android & even emitting fixed
2 parents 4684a74 + c3fc792 commit 15ec76b

13 files changed

Lines changed: 120 additions & 69 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ try {
192192
193193
### `open(filepath: string, options?: Object): Promise<void>`
194194
195-
| Parameter | Type | Description |
196-
| ---------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
197-
| **filepath** | string | The absolute path where the file is stored. The file needs to have a valid extension to be successfully detected. Use [react-native-fs constants](https://github.com/itinance/react-native-fs#constants) to determine the absolute path correctly. |
198-
| **options** (optional) | Object | Some options to customize the behaviour. See below. |
195+
| Parameter | Type | Description |
196+
| ---------------------- | ------ |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
197+
| **filepath** | string | The absolute path where the file is stored. The file needs to have a valid extension to be successfully detected. Use [expo-file-system constants](https://docs.expo.dev/versions/latest/sdk/filesystem/#constants) to determine the absolute path correctly. |
198+
| **options** (optional) | Object | Some options to customize the behaviour. See below. |
199199
200200
#### Options
201201

android/src/main/java/com/fileviewerturbo/FileViewerTurboModuleImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
11
package com.fileviewerturbo;
22

3+
import android.app.Activity;
34
import android.content.Intent;
45
import android.content.pm.PackageManager;
56
import android.net.Uri;
67
import android.webkit.MimeTypeMap;
78

9+
import com.facebook.react.bridge.ActivityEventListener;
10+
import com.facebook.react.bridge.BaseActivityEventListener;
811
import com.facebook.react.bridge.Promise;
912
import com.facebook.react.bridge.ReactApplicationContext;
13+
import com.facebook.react.bridge.ReactMethod;
1014
import com.facebook.react.bridge.ReadableMap;
15+
import com.facebook.react.modules.core.DeviceEventManagerModule;
16+
1117
import java.io.File;
12-
import java.math.BigInteger;
1318

1419
public class FileViewerTurboModuleImpl {
1520
final private ReactApplicationContext mContext;
1621
public static final String NAME = "FileViewerTurbo";
1722
private static final String SHOW_OPEN_WITH_DIALOG = "showOpenWithDialog" ;
1823
private static final String SHOW_STORE_SUGGESTIONS ="showAppsSuggestions";
24+
private static final String DISMISS_EVENT = "onViewerDidDismiss";
1925
private static final Integer RN_FILE_VIEWER_REQUEST = 33341;
2026

2127
public FileViewerTurboModuleImpl(ReactApplicationContext context) {
2228
mContext = context;
29+
ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
30+
@Override
31+
public void onActivityResult(final Activity activity, final int requestCode, final int resultCode, final Intent intent) {
32+
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
33+
.emit(DISMISS_EVENT, null);
34+
}
35+
36+
};
37+
mContext.addActivityEventListener(mActivityEventListener);
2338
}
2439

25-
public void open(String path, String currentId, ReadableMap options, Promise promise) {
40+
public void open(String path, ReadableMap options, Promise promise) {
2641
Uri contentUri = null;
27-
Boolean showOpenWithDialog = options.hasKey(SHOW_OPEN_WITH_DIALOG) ? options.getBoolean(SHOW_OPEN_WITH_DIALOG) : false;
28-
Boolean showStoreSuggestions = options.hasKey(SHOW_STORE_SUGGESTIONS) ? options.getBoolean(SHOW_STORE_SUGGESTIONS) : false;
42+
boolean showOpenWithDialog = options.hasKey(SHOW_OPEN_WITH_DIALOG) && options.getBoolean(SHOW_OPEN_WITH_DIALOG);
43+
boolean showStoreSuggestions = options.hasKey(SHOW_STORE_SUGGESTIONS) && options.getBoolean(SHOW_STORE_SUGGESTIONS);
2944

3045
if(path.startsWith("content://")) {
3146
contentUri = Uri.parse(path);
@@ -38,7 +53,7 @@ public void open(String path, String currentId, ReadableMap options, Promise pro
3853
}
3954
try {
4055
final String packageName = mContext.getCurrentActivity().getPackageName();
41-
final String authority = new StringBuilder(packageName).append(".provider").toString();
56+
final String authority = packageName + ".provider";
4257
contentUri = FileProvider.getUriForFile(mContext.getCurrentActivity(), authority, newFile);
4358
}
4459
catch(IllegalArgumentException e) {
@@ -72,7 +87,7 @@ public void open(String path, String currentId, ReadableMap options, Promise pro
7287

7388
if (shareIntent.resolveActivity(pm) != null) {
7489
try {
75-
mContext.getCurrentActivity().startActivityForResult(intentActivity, new BigInteger(currentId.replace("-", ""), 16).intValue() + RN_FILE_VIEWER_REQUEST);
90+
mContext.getCurrentActivity().startActivityForResult(intentActivity, RN_FILE_VIEWER_REQUEST);
7691
promise.resolve(null);
7792
}
7893
catch(Exception e) {

android/src/newarch/java/com/fileviewerturbo/FileViewerTurboModule.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ class FileViewerTurboModule(reactContext: ReactApplicationContext) : NativeFileV
99

1010
override fun getName(): String = FileViewerTurboModuleImpl.NAME
1111

12-
override fun open(path: String?, currentId: String?, options: ReadableMap?, promise: Promise?) {
13-
implementation.open(path, currentId, options, promise)
12+
override fun open(path: String?, options: ReadableMap?, promise: Promise?) {
13+
implementation.open(path, options, promise)
14+
}
15+
16+
override fun addListener(eventType: String?) {
17+
//
18+
}
19+
20+
override fun removeListeners(count: Double) {
21+
//
1422
}
1523
}

android/src/oldarch/java/com/fileviewerturbo/FileViewerTurboModule.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fileviewerturbo
22

3+
import com.facebook.react.bridge.BridgeReactContext
34
import com.facebook.react.bridge.Promise
45
import com.facebook.react.bridge.ReactApplicationContext
56
import com.facebook.react.bridge.ReactContextBaseJavaModule
@@ -12,7 +13,17 @@ class FileViewerTurboModule(context: ReactApplicationContext) : ReactContextBase
1213
override fun getName(): String = FileViewerTurboModuleImpl.NAME
1314

1415
@ReactMethod
15-
fun open(path: String, currentId: String, options: ReadableMap, promise: Promise) {
16-
implementation.open(path, currentId, options, promise)
16+
fun open(path: String, options: ReadableMap, promise: Promise) {
17+
implementation.open(path, options, promise)
18+
}
19+
20+
@ReactMethod
21+
fun addListener(type: String?) {
22+
//
23+
}
24+
25+
@ReactMethod
26+
fun removeListeners(type: Int?) {
27+
//
1728
}
1829
}

example/android/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
3232
# your application. You should enable this flag either if you want
3333
# to write custom TurboModules/Fabric components OR use libraries that
3434
# are providing them.
35-
newArchEnabled=false
35+
newArchEnabled=true
3636

3737
# Use this property to enable or disable the Hermes JS engine.
3838
# If set to false, you will be using JSC instead.

example/ios/FileViewerTurboExample.xcodeproj/project.pbxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,10 @@
590590
"-DFOLLY_CFG_NO_COROUTINES=1",
591591
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
592592
);
593-
OTHER_LDFLAGS = "$(inherited) ";
593+
OTHER_LDFLAGS = (
594+
"$(inherited)",
595+
" ",
596+
);
594597
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
595598
SDKROOT = iphoneos;
596599
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
@@ -659,7 +662,10 @@
659662
"-DFOLLY_CFG_NO_COROUTINES=1",
660663
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
661664
);
662-
OTHER_LDFLAGS = "$(inherited) ";
665+
OTHER_LDFLAGS = (
666+
"$(inherited)",
667+
" ",
668+
);
663669
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
664670
SDKROOT = iphoneos;
665671
USE_HERMES = true;

example/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ENV['RCT_NEW_ARCH_ENABLED'] = '0'
1+
ENV['RCT_NEW_ARCH_ENABLED'] = '1'
22

33
# Resolve react_native_pods.rb with node to allow for hoisting
44
require Pod::Executable.execute_command('node', ['-p',

example/ios/Podfile.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ PODS:
12631263
- ReactCommon/turbomodule/bridging
12641264
- ReactCommon/turbomodule/core
12651265
- Yoga
1266-
- react-native-file-viewer-turbo (0.1.1):
1266+
- react-native-file-viewer-turbo (0.1.6):
12671267
- DoubleConversion
12681268
- glog
12691269
- hermes-engine
@@ -1763,7 +1763,7 @@ EXTERNAL SOURCES:
17631763
SPEC CHECKSUMS:
17641764
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
17651765
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
1766-
dr-pogodin-react-native-fs: f085f7e8e673a018523664542c3e5651638142a5
1766+
dr-pogodin-react-native-fs: 4a8aae59ceae45d96f8f98159cffa3db8acb3ed2
17671767
FBLazyVector: be7314029d6ec6b90f0f75ce1195b8130ed9ac4f
17681768
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
17691769
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
@@ -1778,16 +1778,16 @@ SPEC CHECKSUMS:
17781778
React-CoreModules: 291be650024d9db086c95fd1d7e7d9607c6de62b
17791779
React-cxxreact: 5cf17d13ca0fc0734e1bb0ed9615d1d1fc45ef78
17801780
React-debug: 931ca94abd6b1bcab539e356e20df788afecae8f
1781-
React-defaultsnativemodule: fa1917ffdbb17e2a03f9a2b26e6f7342f59662c0
1782-
React-domnativemodule: c9dcbf0a872f3d02210f005c084d0fc1fdb83f2d
1781+
React-defaultsnativemodule: 6afc2dd3619bac12dc54c1ee939bf14f9aa96b42
1782+
React-domnativemodule: f140d46f6f3c3f1efc987c98b464fcbece0cc93a
17831783
React-Fabric: e1774fe4b579e34c2c5721e9351c8ce869e7b5f0
17841784
React-FabricComponents: 528ff9f96d150379ed404221d70cc7019ca76865
17851785
React-FabricImage: 31680b7ddc740e040277176fbd6541fcf0fd44af
17861786
React-featureflags: 7c7a74b65ee5a228f520b387ebfe0e8d9cecc622
1787-
React-featureflagsnativemodule: bc298d49634a5b2e0576b9bda6a0dc337ef158f8
1787+
React-featureflagsnativemodule: dd3450366b1c9557975e457ce6baa151ccee84da
17881788
React-graphics: 7f0d3e06d356e8476bd8ba95d90762fc01138ebc
17891789
React-hermes: f83fafe6a1c845dace7abad4a5d7366cbb42ab96
1790-
React-idlecallbacksnativemodule: 733deb5f645226868cf49a0eb798c62082a3fb34
1790+
React-idlecallbacksnativemodule: 14ce331438e2bca7d464a8a211b14543aff4dc91
17911791
React-ImageManager: 2b9274ea973f43597a554a182d7ef525836172c6
17921792
React-jserrorhandler: 3b521485275d295cfc6ec6bfa921a1d608693ecf
17931793
React-jsi: fd23c1d759feb709784fd4c835b510b90a94dd12
@@ -1796,17 +1796,17 @@ SPEC CHECKSUMS:
17961796
React-jsitracing: 11b6646d7b2ecdc7a475f65b2cb12d3805964195
17971797
React-logger: 26155dc23db5c9038794db915f80bd2044512c2e
17981798
React-Mapbuffer: ad1ba0205205a16dbff11b8ade6d1b3959451658
1799-
React-microtasksnativemodule: 36e3a415c6a3a5c4784b5c6bad38013b8ca35be4
1800-
react-native-file-viewer-turbo: 7d3babc3e28aa6c65ca2bfb223322e6b1540cfcb
1799+
React-microtasksnativemodule: e771eb9eb6ace5884ee40a293a0e14a9d7a4343c
1800+
react-native-file-viewer-turbo: f6d61e6b7ad0534afb91c71abb81685026f01c1b
18011801
React-nativeconfig: aeed6e2a8ac02b2df54476afcc7c663416c12bf7
18021802
React-NativeModulesApple: c5b7813da94136f50ef084fa1ac077332dcfc658
18031803
React-perflogger: 6afb7eebf7d9521cc70481688ccddf212970e9d3
18041804
React-performancetimeline: 81884d35896b22d51832e7c8748c8330ec73c491
18051805
React-RCTActionSheet: c940a35d71686941ac2b96dd07bde11ea0f0c34f
18061806
React-RCTAnimation: e1dbb4e530d6f58437ab2fae372de3788ecdffab
1807-
React-RCTAppDelegate: 3aa8a7b8bff5afcc00f947471c2fc93bd2093de5
1807+
React-RCTAppDelegate: f9825950ac2c52ae1cf46b648bb362b86b62fe41
18081808
React-RCTBlob: 9cdac4721a76e2d132fb1760eafd0a8f150d1c96
1809-
React-RCTFabric: b833ecb9bb715858731f658accae0804bd2f4a7b
1809+
React-RCTFabric: c0aa01a448bcebb1326d068ed7545eb11561e663
18101810
React-RCTImage: f09f5165807e1a69a2bbac6c7168a8ed57ed4e26
18111811
React-RCTLinking: 4ea06b79cba7e15d8af4d86b1dcede6bd29a47fd
18121812
React-RCTNetwork: 43a38148c7a4a2380e76b08f07f02ee8eaac8965
@@ -1828,6 +1828,6 @@ SPEC CHECKSUMS:
18281828
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
18291829
Yoga: 3deb2471faa9916c8a82dda2a22d3fba2620ad37
18301830

1831-
PODFILE CHECKSUM: 8ec1cff111ee4d951a14bcc1bd65d834dee5924a
1831+
PODFILE CHECKSUM: 65ea4c8568e49fafe33b3a076bc8830a73bc168d
18321832

18331833
COCOAPODS: 1.16.2

example/src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export default function App() {
2929
toFile: localFile,
3030
};
3131
downloadFile(options).promise.then(() =>
32-
open(localFile).then(console.log).catch(console.error)
32+
open(localFile, { onDismiss: () => console.log('dismissed!') })
33+
.then(console.log)
34+
.catch(console.error)
3335
);
3436
};
3537

ios/FileViewerTurbo.mm

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ - (id)initWithPath:(NSString *)file title:(NSString *)title {
3434
@interface CustomQLViewController: QLPreviewController<QLPreviewControllerDataSource>
3535

3636
@property(nonatomic, strong) File *file;
37-
@property(nonatomic, strong) NSString *invocation;
37+
@property(nonatomic, strong) NSNumber *invocation;
3838

3939
@end
4040

4141
@implementation CustomQLViewController
4242

43-
- (instancetype)initWithFile:(File *)file identifier:(NSString *)invocation {
43+
- (instancetype)initWithFile:(File *)file identifier:(NSNumber *)invocation {
4444
if(self = [super init]) {
4545
_file = file;
4646
_invocation = invocation;
@@ -67,7 +67,8 @@ @implementation FileViewerTurbo {
6767
bool hasListeners;
6868
}
6969

70-
static RCTEventEmitter* staticEventEmitter = nil;
70+
static RCTEventEmitter *staticEventEmitter = nil;
71+
static NSNumber *invocationId = @33341;
7172

7273
-(void)startObserving {
7374
hasListeners = YES;
@@ -130,7 +131,7 @@ + (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)
130131
- (void)_sendEventWithName:(NSString *)eventName body:(id)body {
131132
#ifdef RCT_NEW_ARCH_ENABLED
132133
if ([eventName isEqualToString:@"onViewerDidDismiss"]) {
133-
[self emitOnViewerDidDismiss:body];
134+
[self emitOnViewerDidDismiss];
134135
}
135136
#else
136137
if (hasListeners && staticEventEmitter != nil) {
@@ -147,22 +148,25 @@ - (void)_sendEventWithName:(NSString *)eventName body:(id)body {
147148

148149

149150
- (void)previewControllerDidDismiss:(CustomQLViewController *)controller {
150-
[self _sendEventWithName:@"onViewerDidDismiss" body:@{@"id": ((CustomQLViewController*)controller).invocation}];
151+
[self _sendEventWithName:@"onViewerDidDismiss" body:nil];
151152
}
152153

153154

154155
- (void)dismissView:(id)sender {
155156
UIViewController* controller = [FileViewerTurbo topViewController];
156-
[self _sendEventWithName:@"onViewerDidDismiss" body:@{@"id": ((CustomQLViewController*)controller).invocation}];
157+
[self _sendEventWithName:@"onViewerDidDismiss" body:nil];
157158
[[FileViewerTurbo topViewController] dismissViewControllerAnimated:YES completion:nil];
158159
}
159160

160161
RCT_EXPORT_MODULE(FileViewerTurbo)
161162

162-
RCT_EXPORT_METHOD(open:(NSString *)path currentId:(NSString *)currentId options:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
163+
RCT_EXPORT_METHOD(open:(NSString *)path
164+
options:(NSDictionary *)options
165+
resolve:(RCTPromiseResolveBlock)resolve
166+
reject:(RCTPromiseRejectBlock)reject) {
167+
163168
NSString *displayName = options[@"displayName"];
164169
File *file = [[File alloc] initWithPath:path title:displayName];
165-
NSString *invocationId = currentId;
166170

167171
QLPreviewController *controller = [[CustomQLViewController alloc] initWithFile:file identifier:invocationId];
168172
controller.delegate = self;

0 commit comments

Comments
 (0)