react-native-macos/Libraries/ActionSheetIOS/RCTActionSheetManager.m

181 строка
6.3 KiB
Mathematica
Исходник Обычный вид История

/**
* 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.
*/
2015-03-13 20:45:27 +03:00
CocoaPods frameworks compatibility: Step 2 (#25619) Summary: This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349. It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`. The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` . There are still a few breaking changes which I hope will be acceptable: - `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path. - ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823. - ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823. - ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967. - ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823. - Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled. Still to do: - ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302. - I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? � ## Changelog [iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks. Pull Request resolved: https://github.com/facebook/react-native/pull/25619 Test Plan: ### FB ``` buck build catalyst ``` ### Sample Project Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed. You can see that it works with these steps: 1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git` 2. `git checkout fix-frameworks-subspecs` 3. `cd ios && pod install` 4. `cd .. && react-native run-ios` The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again. ### RNTesterPods `RNTesterPodsPods` can now work with or without `use_frameworks!`. 1. Go to the `RNTester` directory and run `pod install`. 2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine. 3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again. 4. Run the tests again and see that it still works with frameworks enabled. Reviewed By: PeteTheHeat Differential Revision: D16465247 Pulled By: PeteTheHeat fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
#import <React/RCTActionSheetManager.h>
2015-03-13 20:45:27 +03:00
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTLog.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
2015-03-13 20:45:27 +03:00
2015-04-08 18:52:48 +03:00
@interface RCTActionSheetManager () <UIActionSheetDelegate>
2015-03-13 20:45:27 +03:00
@end
@implementation RCTActionSheetManager
{
// Use NSMapTable, as UIAlertViews do not implement <NSCopying>
// which is required for NSDictionary keys
NSMapTable *_callbacks;
2015-03-13 20:45:27 +03:00
}
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
- (dispatch_queue_t)methodQueue
2015-03-13 20:45:27 +03:00
{
return dispatch_get_main_queue();
2015-03-13 20:45:27 +03:00
}
- (void)presentViewController:(UIViewController *)alertController
onParentViewController:(UIViewController *)parentViewController
anchorViewTag:(NSNumber *)anchorViewTag
{
alertController.modalPresentationStyle = UIModalPresentationPopover;
UIView *sourceView = parentViewController.view;
if (anchorViewTag) {
sourceView = [self.bridge.uiManager viewForReactTag:anchorViewTag];
} else {
alertController.popoverPresentationController.permittedArrowDirections = 0;
}
alertController.popoverPresentationController.sourceView = sourceView;
alertController.popoverPresentationController.sourceRect = sourceView.bounds;
[parentViewController presentViewController:alertController animated:YES completion:nil];
}
2015-04-08 18:52:48 +03:00
RCT_EXPORT_METHOD(showActionSheetWithOptions:(NSDictionary *)options
callback:(RCTResponseSenderBlock)callback)
2015-03-13 20:45:27 +03:00
{
if (RCTRunningInAppExtension()) {
RCTLogError(@"Unable to show action sheet from app extension");
return;
}
2015-03-13 20:45:27 +03:00
if (!_callbacks) {
_callbacks = [NSMapTable strongToStrongObjectsMapTable];
}
2015-03-13 20:45:27 +03:00
NSString *title = [RCTConvert NSString:options[@"title"]];
NSString *message = [RCTConvert NSString:options[@"message"]];
NSArray<NSString *> *buttons = [RCTConvert NSStringArray:options[@"options"]];
NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1;
Feature/action sheet destructive button indexes (#18254) Summary: This is a recreation of #13924, rebased on top of master, as the former PR wasn't re-reviewed and automatically closed by the bot. iOS [Action Sheets docs](https://developer.apple.com/ios/human-interface-guidelines/ui-views/action-sheets/) say > Make destructive choices prominent. Use red for buttons that perform destructive or dangerous actions, and display these buttons at the top of an action sheet. Currently ActionSheetIOS's showActionSheetWithOptions only supports a single destructive button via the prop `destructiveButtonIndex`. This PR maintains backwards compatibility with `destructiveButtonIndex` while simultaneously supporting `destructiveButtonIndexes` allowing developers to pass an array of destructive indexes ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 1], }, () => {}); ``` <img width="282" alt="actionsheet" src="https://cloud.githubusercontent.com/assets/3091143/25963211/1c211a16-3646-11e7-9b7c-c9a2dbea7832.png"> Some additional tests, all working as expected (item only red if it is a matching index). ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 19], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: undefined, }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 5, 0, 0], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 5, 0, 0, 'non numeric', 12.34], }, () => {}); ``` The following will crash the app but I believe this is expected ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: 'not an array', }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: null, }, () => {}); ``` - [x] Explain the **motivation** for making this change. - [x] Provide a **test plan** demonstrating that the code is solid. - [x] Match the **code formatting** of the rest of the codebase. - [x] Target the `master` branch, NOT a "stable" branch. Pull Request resolved: https://github.com/facebook/react-native/pull/18254 Differential Revision: D13680516 Pulled By: hramos fbshipit-source-id: ac183cdcf5e1daef8e3c584dcf6a921bbecad475
2019-01-24 23:03:00 +03:00
NSArray<NSNumber *> *destructiveButtonIndices;
if ([options[@"destructiveButtonIndex"] isKindOfClass:[NSArray class]]) {
destructiveButtonIndices = [RCTConvert NSArray:options[@"destructiveButtonIndex"]];
} else {
NSNumber *destructiveButtonIndex = options[@"destructiveButtonIndex"] ? [RCTConvert NSNumber:options[@"destructiveButtonIndex"]] : @-1;
destructiveButtonIndices = @[destructiveButtonIndex];
}
UIViewController *controller = RCTPresentedViewController();
if (controller == nil) {
RCTLogError(@"Tried to display action sheet but there is no application window. options: %@", options);
return;
}
2015-03-13 20:45:27 +03:00
/*
* The `anchor` option takes a view to set as the anchor for the share
* popup to point to, on iPads running iOS 8. If it is not passed, it
* defaults to centering the share popup on screen without any arrows.
*/
NSNumber *anchorViewTag = [RCTConvert NSNumber:options[@"anchor"]];
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleActionSheet];
NSInteger index = 0;
for (NSString *option in buttons) {
UIAlertActionStyle style = UIAlertActionStyleDefault;
Feature/action sheet destructive button indexes (#18254) Summary: This is a recreation of #13924, rebased on top of master, as the former PR wasn't re-reviewed and automatically closed by the bot. iOS [Action Sheets docs](https://developer.apple.com/ios/human-interface-guidelines/ui-views/action-sheets/) say > Make destructive choices prominent. Use red for buttons that perform destructive or dangerous actions, and display these buttons at the top of an action sheet. Currently ActionSheetIOS's showActionSheetWithOptions only supports a single destructive button via the prop `destructiveButtonIndex`. This PR maintains backwards compatibility with `destructiveButtonIndex` while simultaneously supporting `destructiveButtonIndexes` allowing developers to pass an array of destructive indexes ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 1], }, () => {}); ``` <img width="282" alt="actionsheet" src="https://cloud.githubusercontent.com/assets/3091143/25963211/1c211a16-3646-11e7-9b7c-c9a2dbea7832.png"> Some additional tests, all working as expected (item only red if it is a matching index). ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 19], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: undefined, }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 5, 0, 0], }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: [0, 5, 0, 0, 'non numeric', 12.34], }, () => {}); ``` The following will crash the app but I believe this is expected ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: 'not an array', }, () => {}); ``` ```js ActionSheetIOS.showActionSheetWithOptions({ options: ['one', 'two', 'three'], destructiveButtonIndexes: null, }, () => {}); ``` - [x] Explain the **motivation** for making this change. - [x] Provide a **test plan** demonstrating that the code is solid. - [x] Match the **code formatting** of the rest of the codebase. - [x] Target the `master` branch, NOT a "stable" branch. Pull Request resolved: https://github.com/facebook/react-native/pull/18254 Differential Revision: D13680516 Pulled By: hramos fbshipit-source-id: ac183cdcf5e1daef8e3c584dcf6a921bbecad475
2019-01-24 23:03:00 +03:00
if ([destructiveButtonIndices containsObject:@(index)]) {
style = UIAlertActionStyleDestructive;
} else if (index == cancelButtonIndex) {
style = UIAlertActionStyleCancel;
}
NSInteger localIndex = index;
[alertController addAction:[UIAlertAction actionWithTitle:option
style:style
handler:^(__unused UIAlertAction *action){
callback(@[@(localIndex)]);
}]];
index++;
}
alertController.view.tintColor = [RCTConvert UIColor:options[@"tintColor"]];
[self presentViewController:alertController onParentViewController:controller anchorViewTag:anchorViewTag];
2015-03-13 20:45:27 +03:00
}
2015-04-08 18:52:48 +03:00
RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
failureCallback:(RCTResponseErrorBlock)failureCallback
2015-04-08 18:52:48 +03:00
successCallback:(RCTResponseSenderBlock)successCallback)
2015-03-13 20:45:27 +03:00
{
if (RCTRunningInAppExtension()) {
RCTLogError(@"Unable to show action sheet from app extension");
return;
}
NSMutableArray<id> *items = [NSMutableArray array];
NSString *message = [RCTConvert NSString:options[@"message"]];
if (message) {
[items addObject:message];
}
NSURL *URL = [RCTConvert NSURL:options[@"url"]];
if (URL) {
if ([URL.scheme.lowercaseString isEqualToString:@"data"]) {
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:URL
options:(NSDataReadingOptions)0
error:&error];
if (!data) {
failureCallback(error);
return;
}
[items addObject:data];
} else {
[items addObject:URL];
}
}
if (items.count == 0) {
RCTLogError(@"No `url` or `message` to share");
return;
}
UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
NSString *subject = [RCTConvert NSString:options[@"subject"]];
if (subject) {
[shareController setValue:subject forKey:@"subject"];
}
NSArray *excludedActivityTypes = [RCTConvert NSStringArray:options[@"excludedActivityTypes"]];
if (excludedActivityTypes) {
shareController.excludedActivityTypes = excludedActivityTypes;
}
UIViewController *controller = RCTPresentedViewController();
shareController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
if (activityError) {
failureCallback(activityError);
} else {
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
}
};
NSNumber *anchorViewTag = [RCTConvert NSNumber:options[@"anchor"]];
shareController.view.tintColor = [RCTConvert UIColor:options[@"tintColor"]];
[self presentViewController:shareController onParentViewController:controller anchorViewTag:anchorViewTag];
2015-03-13 20:45:27 +03:00
}
@end