Add NativeModule Spec for ActionSheetManager

Summary: This diff introduces NativeActionSheetManager.

Reviewed By: fkgozali

Differential Revision: D15803420

fbshipit-source-id: 736e7fb59be7d2bd1db064c33be660c7ccd346f4
This commit is contained in:
Ramanpreet Nara 2019-07-19 12:02:19 -07:00 коммит произвёл Facebook Github Bot
Родитель 6f09dc03bf
Коммит 929a7cbfbd
5 изменённых файлов: 292 добавлений и 15 удалений

Просмотреть файл

@ -9,8 +9,7 @@
*/ */
'use strict'; 'use strict';
const RCTActionSheetManager = require('../BatchedBridge/NativeModules') import RCTActionSheetManager from './NativeActionSheetManager';
.ActionSheetManager;
const invariant = require('invariant'); const invariant = require('invariant');
const processColor = require('../StyleSheet/processColor'); const processColor = require('../StyleSheet/processColor');
@ -54,9 +53,12 @@ const ActionSheetIOS = {
'Options must be a valid object', 'Options must be a valid object',
); );
invariant(typeof callback === 'function', 'Must provide a valid callback'); invariant(typeof callback === 'function', 'Must provide a valid callback');
invariant(RCTActionSheetManager, "ActionSheetManager does't exist");
const {tintColor, ...remainingOptions} = options;
RCTActionSheetManager.showActionSheetWithOptions( RCTActionSheetManager.showActionSheetWithOptions(
{...options, tintColor: processColor(options.tintColor)}, {...remainingOptions, tintColor: processColor(tintColor)},
callback, callback,
); );
}, },
@ -101,6 +103,7 @@ const ActionSheetIOS = {
typeof successCallback === 'function', typeof successCallback === 'function',
'Must provide a valid successCallback', 'Must provide a valid successCallback',
); );
invariant(RCTActionSheetManager, "ActionSheetManager does't exist");
RCTActionSheetManager.showShareActionSheetWithOptions( RCTActionSheetManager.showShareActionSheetWithOptions(
{...options, tintColor: processColor(options.tintColor)}, {...options, tintColor: processColor(options.tintColor)},
failureCallback, failureCallback,

Просмотреть файл

@ -0,0 +1,50 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from '../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
+getConstants: () => {||};
+showActionSheetWithOptions: (
options: {|
+title?: ?string,
+message?: ?string,
+options: ?Array<string>,
// Supports Array<number> as well.
+destructiveButtonIndex?: ?number,
+cancelButtonIndex?: ?number,
+anchor?: ?number,
+tintColor?: ?number,
|},
callback: (buttonIndex: number) => void,
) => void;
+showShareActionSheetWithOptions: (
options: {|
+message?: ?string,
+url?: ?string,
+subject?: ?string,
+anchor?: ?number,
+tintColor?: ?number,
+excludedActivityTypes?: ?Array<string>,
|},
failureCallback: (error: {|
+domain: string,
+code: string,
+userInfo?: ?Object,
+message: string,
|}) => void,
successCallback: (completed: boolean, activityType: ?string) => void,
) => void;
}
export default TurboModuleRegistry.get<Spec>('ActionSheetManager');

Просмотреть файл

@ -120,6 +120,53 @@ namespace facebook {
}
} // namespace react
} // namespace facebook
@implementation RCTCxxConvert (NativeActionSheetManager_SpecShowActionSheetWithOptionsOptions)
+ (RCTManagedPointer *)JS_NativeActionSheetManager_SpecShowActionSheetWithOptionsOptions:(id)json
{
return facebook::react::managedPointer<JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions>(json);
}
@end
@implementation RCTCxxConvert (NativeActionSheetManager_SpecShowShareActionSheetWithOptionsOptions)
+ (RCTManagedPointer *)JS_NativeActionSheetManager_SpecShowShareActionSheetWithOptionsOptions:(id)json
{
return facebook::react::managedPointer<JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions>(json);
}
@end
@implementation RCTCxxConvert (NativeActionSheetManager_SpecShowShareActionSheetWithOptionsFailureCallbackError)
+ (RCTManagedPointer *)JS_NativeActionSheetManager_SpecShowShareActionSheetWithOptionsFailureCallbackError:(id)json
{
return facebook::react::managedPointer<JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsFailureCallbackError>(json);
}
@end
namespace facebook {
namespace react {
static facebook::jsi::Value __hostFunction_NativeActionSheetManagerSpecJSI_showActionSheetWithOptions(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "showActionSheetWithOptions", @selector(showActionSheetWithOptions:callback:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeActionSheetManagerSpecJSI_showShareActionSheetWithOptions(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "showShareActionSheetWithOptions", @selector(showShareActionSheetWithOptions:failureCallback:successCallback:), args, count);
}
NativeActionSheetManagerSpecJSI::NativeActionSheetManagerSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker)
: ObjCTurboModule("ActionSheetManager", instance, jsInvoker) {
methodMap_["showActionSheetWithOptions"] = MethodMetadata {2, __hostFunction_NativeActionSheetManagerSpecJSI_showActionSheetWithOptions};
setMethodArgConversionSelector(@"showActionSheetWithOptions", 0, @"JS_NativeActionSheetManager_SpecShowActionSheetWithOptionsOptions:");
methodMap_["showShareActionSheetWithOptions"] = MethodMetadata {3, __hostFunction_NativeActionSheetManagerSpecJSI_showShareActionSheetWithOptions};
setMethodArgConversionSelector(@"showShareActionSheetWithOptions", 0, @"JS_NativeActionSheetManager_SpecShowShareActionSheetWithOptionsOptions:");
} }
} // namespace react } // namespace react

Просмотреть файл

@ -79,6 +79,90 @@ namespace facebook {
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook
namespace JS {
namespace NativeActionSheetManager {
struct SpecShowActionSheetWithOptionsOptions {
NSString *title() const;
NSString *message() const;
folly::Optional<facebook::react::LazyVector<NSString *>> options() const;
folly::Optional<double> destructiveButtonIndex() const;
folly::Optional<double> cancelButtonIndex() const;
folly::Optional<double> anchor() const;
folly::Optional<double> tintColor() const;
SpecShowActionSheetWithOptionsOptions(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativeActionSheetManager_SpecShowActionSheetWithOptionsOptions)
+ (RCTManagedPointer *)JS_NativeActionSheetManager_SpecShowActionSheetWithOptionsOptions:(id)json;
@end
namespace JS {
namespace NativeActionSheetManager {
struct SpecShowShareActionSheetWithOptionsOptions {
NSString *message() const;
NSString *url() const;
NSString *subject() const;
folly::Optional<double> anchor() const;
folly::Optional<double> tintColor() const;
folly::Optional<facebook::react::LazyVector<NSString *>> excludedActivityTypes() const;
SpecShowShareActionSheetWithOptionsOptions(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativeActionSheetManager_SpecShowShareActionSheetWithOptionsOptions)
+ (RCTManagedPointer *)JS_NativeActionSheetManager_SpecShowShareActionSheetWithOptionsOptions:(id)json;
@end
namespace JS {
namespace NativeActionSheetManager {
struct SpecShowShareActionSheetWithOptionsFailureCallbackError {
NSString *domain() const;
NSString *code() const;
id<NSObject> _Nullable userInfo() const;
NSString *message() const;
SpecShowShareActionSheetWithOptionsFailureCallbackError(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativeActionSheetManager_SpecShowShareActionSheetWithOptionsFailureCallbackError)
+ (RCTManagedPointer *)JS_NativeActionSheetManager_SpecShowShareActionSheetWithOptionsFailureCallbackError:(id)json;
@end
@protocol NativeActionSheetManagerSpec <RCTBridgeModule, RCTTurboModule>
- (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions &)options
callback:(RCTResponseSenderBlock)callback;
- (void)showShareActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions &)options
failureCallback:(RCTResponseSenderBlock)failureCallback
successCallback:(RCTResponseSenderBlock)successCallback;
@end
namespace facebook {
namespace react {
/**
* ObjC++ class for module 'ActionSheetManager'
*/
class JSI_EXPORT NativeActionSheetManagerSpecJSI : public ObjCTurboModule {
public:
NativeActionSheetManagerSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker);
};
} // namespace react
} // namespace facebook
namespace JS { namespace JS {
namespace NativeAlertManager { namespace NativeAlertManager {
struct Args { struct Args {
@ -1796,6 +1880,91 @@ namespace facebook {
#import <RCTTypeSafety/RCTConvertHelpers.h> #import <RCTTypeSafety/RCTConvertHelpers.h>
inline NSString *JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::title() const
{
id const p = _v[@"title"];
return RCTBridgingToString(p);
}
inline NSString *JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::message() const
{
id const p = _v[@"message"];
return RCTBridgingToString(p);
}
inline folly::Optional<facebook::react::LazyVector<NSString *>> JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::options() const
{
id const p = _v[@"options"];
return RCTBridgingToOptionalVec(p, ^NSString *(id itemValue_0) { return RCTBridgingToString(itemValue_0); });
}
inline folly::Optional<double> JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::destructiveButtonIndex() const
{
id const p = _v[@"destructiveButtonIndex"];
return RCTBridgingToOptionalDouble(p);
}
inline folly::Optional<double> JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::cancelButtonIndex() const
{
id const p = _v[@"cancelButtonIndex"];
return RCTBridgingToOptionalDouble(p);
}
inline folly::Optional<double> JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::anchor() const
{
id const p = _v[@"anchor"];
return RCTBridgingToOptionalDouble(p);
}
inline folly::Optional<double> JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::tintColor() const
{
id const p = _v[@"tintColor"];
return RCTBridgingToOptionalDouble(p);
}
inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::message() const
{
id const p = _v[@"message"];
return RCTBridgingToString(p);
}
inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::url() const
{
id const p = _v[@"url"];
return RCTBridgingToString(p);
}
inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::subject() const
{
id const p = _v[@"subject"];
return RCTBridgingToString(p);
}
inline folly::Optional<double> JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::anchor() const
{
id const p = _v[@"anchor"];
return RCTBridgingToOptionalDouble(p);
}
inline folly::Optional<double> JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::tintColor() const
{
id const p = _v[@"tintColor"];
return RCTBridgingToOptionalDouble(p);
}
inline folly::Optional<facebook::react::LazyVector<NSString *>> JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::excludedActivityTypes() const
{
id const p = _v[@"excludedActivityTypes"];
return RCTBridgingToOptionalVec(p, ^NSString *(id itemValue_0) { return RCTBridgingToString(itemValue_0); });
}
inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsFailureCallbackError::domain() const
{
id const p = _v[@"domain"];
return RCTBridgingToString(p);
}
inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsFailureCallbackError::code() const
{
id const p = _v[@"code"];
return RCTBridgingToString(p);
}
inline id<NSObject> _Nullable JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsFailureCallbackError::userInfo() const
{
id const p = _v[@"userInfo"];
return p;
}
inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsFailureCallbackError::message() const
{
id const p = _v[@"message"];
return RCTBridgingToString(p);
}
inline NSString *JS::NativeAlertManager::Args::title() const inline NSString *JS::NativeAlertManager::Args::title() const
{ {
id const p = _v[@"title"]; id const p = _v[@"title"];

Просмотреть файл

@ -15,10 +15,8 @@ const Platform = require('../Utilities/Platform');
const invariant = require('invariant'); const invariant = require('invariant');
const processColor = require('../StyleSheet/processColor'); const processColor = require('../StyleSheet/processColor');
const { const {ShareModule} = require('../BatchedBridge/NativeModules');
ActionSheetManager, import NativeActionSheetManager from '../ActionSheetIOS/NativeActionSheetManager';
ShareModule,
} = require('../BatchedBridge/NativeModules');
type Content = type Content =
| {title?: string, message: string} | {title?: string, message: string}
@ -86,8 +84,22 @@ class Share {
return ShareModule.share(content, options.dialogTitle); return ShareModule.share(content, options.dialogTitle);
} else if (Platform.OS === 'ios') { } else if (Platform.OS === 'ios') {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ActionSheetManager.showShareActionSheetWithOptions( const tintColor = processColor(options.tintColor);
{...content, ...options, tintColor: processColor(options.tintColor)},
invariant(
NativeActionSheetManager,
'NativeActionSheetManager is not registered on iOS, but it should be.',
);
NativeActionSheetManager.showShareActionSheetWithOptions(
{
message:
typeof content.message === 'string' ? content.message : undefined,
url: typeof content.url === 'string' ? content.url : undefined,
subject: options.subject,
tintColor: tintColor != null ? tintColor : undefined,
excludedActivityTypes: options.excludedActivityTypes,
},
error => reject(error), error => reject(error),
(success, activityType) => { (success, activityType) => {
if (success) { if (success) {
@ -111,17 +123,13 @@ class Share {
/** /**
* The content was successfully shared. * The content was successfully shared.
*/ */
static get sharedAction(): string { static sharedAction: 'sharedAction' = 'sharedAction';
return 'sharedAction';
}
/** /**
* The dialog has been dismissed. * The dialog has been dismissed.
* @platform ios * @platform ios
*/ */
static get dismissedAction(): string { static dismissedAction: 'dismissedAction' = 'dismissedAction';
return 'dismissedAction';
}
} }
module.exports = Share; module.exports = Share;