-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[camera] Initial iOS Pigeon conversion (#6553)
Converts one platform channel method to Pigeon, setting up all the Pigeon plumbing and test scaffolding. The Camera API surface is relatively large, so this lays a foundation for incremental conversion, minimizing the mixing of Pigeon setup with the individual method conversions. Part of flutter/flutter#117905
- Loading branch information
1 parent
87ae14f
commit ba1e24b
Showing
15 changed files
with
557 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/camera/camera_avfoundation/ios/Classes/messages.g.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// Autogenerated from Pigeon (v18.0.0), do not edit directly. | ||
// See also: https://pub.dev/packages/pigeon | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@protocol FlutterBinaryMessenger; | ||
@protocol FlutterMessageCodec; | ||
@class FlutterError; | ||
@class FlutterStandardTypedData; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
typedef NS_ENUM(NSUInteger, FCPPlatformCameraLensDirection) { | ||
/// Front facing camera (a user looking at the screen is seen by the camera). | ||
FCPPlatformCameraLensDirectionFront = 0, | ||
/// Back facing camera (a user looking at the screen is not seen by the camera). | ||
FCPPlatformCameraLensDirectionBack = 1, | ||
/// External camera which may not be mounted to the device. | ||
FCPPlatformCameraLensDirectionExternal = 2, | ||
}; | ||
|
||
/// Wrapper for FCPPlatformCameraLensDirection to allow for nullability. | ||
@interface FCPPlatformCameraLensDirectionBox : NSObject | ||
@property(nonatomic, assign) FCPPlatformCameraLensDirection value; | ||
- (instancetype)initWithValue:(FCPPlatformCameraLensDirection)value; | ||
@end | ||
|
||
@class FCPPlatformCameraDescription; | ||
|
||
@interface FCPPlatformCameraDescription : NSObject | ||
/// `init` unavailable to enforce nonnull fields, see the `make` class method. | ||
- (instancetype)init NS_UNAVAILABLE; | ||
+ (instancetype)makeWithName:(NSString *)name | ||
lensDirection:(FCPPlatformCameraLensDirection)lensDirection; | ||
/// The name of the camera device. | ||
@property(nonatomic, copy) NSString *name; | ||
/// The direction the camera is facing. | ||
@property(nonatomic, assign) FCPPlatformCameraLensDirection lensDirection; | ||
@end | ||
|
||
/// The codec used by FCPCameraApi. | ||
NSObject<FlutterMessageCodec> *FCPCameraApiGetCodec(void); | ||
|
||
@protocol FCPCameraApi | ||
/// Returns the list of available cameras. | ||
- (void)availableCamerasWithCompletion:(void (^)(NSArray<FCPPlatformCameraDescription *> *_Nullable, | ||
FlutterError *_Nullable))completion; | ||
@end | ||
|
||
extern void SetUpFCPCameraApi(id<FlutterBinaryMessenger> binaryMessenger, | ||
NSObject<FCPCameraApi> *_Nullable api); | ||
|
||
extern void SetUpFCPCameraApiWithSuffix(id<FlutterBinaryMessenger> binaryMessenger, | ||
NSObject<FCPCameraApi> *_Nullable api, | ||
NSString *messageChannelSuffix); | ||
|
||
NS_ASSUME_NONNULL_END |
155 changes: 155 additions & 0 deletions
155
packages/camera/camera_avfoundation/ios/Classes/messages.g.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// Autogenerated from Pigeon (v18.0.0), do not edit directly. | ||
// See also: https://pub.dev/packages/pigeon | ||
|
||
#import "messages.g.h" | ||
|
||
#if TARGET_OS_OSX | ||
#import <FlutterMacOS/FlutterMacOS.h> | ||
#else | ||
#import <Flutter/Flutter.h> | ||
#endif | ||
|
||
#if !__has_feature(objc_arc) | ||
#error File requires ARC to be enabled. | ||
#endif | ||
|
||
static NSArray *wrapResult(id result, FlutterError *error) { | ||
if (error) { | ||
return @[ | ||
error.code ?: [NSNull null], error.message ?: [NSNull null], error.details ?: [NSNull null] | ||
]; | ||
} | ||
return @[ result ?: [NSNull null] ]; | ||
} | ||
|
||
static id GetNullableObjectAtIndex(NSArray *array, NSInteger key) { | ||
id result = array[key]; | ||
return (result == [NSNull null]) ? nil : result; | ||
} | ||
|
||
@implementation FCPPlatformCameraLensDirectionBox | ||
- (instancetype)initWithValue:(FCPPlatformCameraLensDirection)value { | ||
self = [super init]; | ||
if (self) { | ||
_value = value; | ||
} | ||
return self; | ||
} | ||
@end | ||
|
||
@interface FCPPlatformCameraDescription () | ||
+ (FCPPlatformCameraDescription *)fromList:(NSArray *)list; | ||
+ (nullable FCPPlatformCameraDescription *)nullableFromList:(NSArray *)list; | ||
- (NSArray *)toList; | ||
@end | ||
|
||
@implementation FCPPlatformCameraDescription | ||
+ (instancetype)makeWithName:(NSString *)name | ||
lensDirection:(FCPPlatformCameraLensDirection)lensDirection { | ||
FCPPlatformCameraDescription *pigeonResult = [[FCPPlatformCameraDescription alloc] init]; | ||
pigeonResult.name = name; | ||
pigeonResult.lensDirection = lensDirection; | ||
return pigeonResult; | ||
} | ||
+ (FCPPlatformCameraDescription *)fromList:(NSArray *)list { | ||
FCPPlatformCameraDescription *pigeonResult = [[FCPPlatformCameraDescription alloc] init]; | ||
pigeonResult.name = GetNullableObjectAtIndex(list, 0); | ||
pigeonResult.lensDirection = [GetNullableObjectAtIndex(list, 1) integerValue]; | ||
return pigeonResult; | ||
} | ||
+ (nullable FCPPlatformCameraDescription *)nullableFromList:(NSArray *)list { | ||
return (list) ? [FCPPlatformCameraDescription fromList:list] : nil; | ||
} | ||
- (NSArray *)toList { | ||
return @[ | ||
self.name ?: [NSNull null], | ||
@(self.lensDirection), | ||
]; | ||
} | ||
@end | ||
|
||
@interface FCPCameraApiCodecReader : FlutterStandardReader | ||
@end | ||
@implementation FCPCameraApiCodecReader | ||
- (nullable id)readValueOfType:(UInt8)type { | ||
switch (type) { | ||
case 128: | ||
return [FCPPlatformCameraDescription fromList:[self readValue]]; | ||
default: | ||
return [super readValueOfType:type]; | ||
} | ||
} | ||
@end | ||
|
||
@interface FCPCameraApiCodecWriter : FlutterStandardWriter | ||
@end | ||
@implementation FCPCameraApiCodecWriter | ||
- (void)writeValue:(id)value { | ||
if ([value isKindOfClass:[FCPPlatformCameraDescription class]]) { | ||
[self writeByte:128]; | ||
[self writeValue:[value toList]]; | ||
} else { | ||
[super writeValue:value]; | ||
} | ||
} | ||
@end | ||
|
||
@interface FCPCameraApiCodecReaderWriter : FlutterStandardReaderWriter | ||
@end | ||
@implementation FCPCameraApiCodecReaderWriter | ||
- (FlutterStandardWriter *)writerWithData:(NSMutableData *)data { | ||
return [[FCPCameraApiCodecWriter alloc] initWithData:data]; | ||
} | ||
- (FlutterStandardReader *)readerWithData:(NSData *)data { | ||
return [[FCPCameraApiCodecReader alloc] initWithData:data]; | ||
} | ||
@end | ||
|
||
NSObject<FlutterMessageCodec> *FCPCameraApiGetCodec(void) { | ||
static FlutterStandardMessageCodec *sSharedObject = nil; | ||
static dispatch_once_t sPred = 0; | ||
dispatch_once(&sPred, ^{ | ||
FCPCameraApiCodecReaderWriter *readerWriter = [[FCPCameraApiCodecReaderWriter alloc] init]; | ||
sSharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter]; | ||
}); | ||
return sSharedObject; | ||
} | ||
|
||
void SetUpFCPCameraApi(id<FlutterBinaryMessenger> binaryMessenger, NSObject<FCPCameraApi> *api) { | ||
SetUpFCPCameraApiWithSuffix(binaryMessenger, api, @""); | ||
} | ||
|
||
void SetUpFCPCameraApiWithSuffix(id<FlutterBinaryMessenger> binaryMessenger, | ||
NSObject<FCPCameraApi> *api, NSString *messageChannelSuffix) { | ||
messageChannelSuffix = messageChannelSuffix.length > 0 | ||
? [NSString stringWithFormat:@".%@", messageChannelSuffix] | ||
: @""; | ||
/// Returns the list of available cameras. | ||
{ | ||
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc] | ||
initWithName:[NSString stringWithFormat:@"%@%@", | ||
@"dev.flutter.pigeon.camera_avfoundation." | ||
@"CameraApi.getAvailableCameras", | ||
messageChannelSuffix] | ||
binaryMessenger:binaryMessenger | ||
codec:FCPCameraApiGetCodec()]; | ||
if (api) { | ||
NSCAssert( | ||
[api respondsToSelector:@selector(availableCamerasWithCompletion:)], | ||
@"FCPCameraApi api (%@) doesn't respond to @selector(availableCamerasWithCompletion:)", | ||
api); | ||
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) { | ||
[api availableCamerasWithCompletion:^( | ||
NSArray<FCPPlatformCameraDescription *> *_Nullable output, | ||
FlutterError *_Nullable error) { | ||
callback(wrapResult(output, error)); | ||
}]; | ||
}]; | ||
} else { | ||
[channel setMessageHandler:nil]; | ||
} | ||
} | ||
} |
Oops, something went wrong.