-
Notifications
You must be signed in to change notification settings - Fork 27.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios_platform_images] Convert to Pigeon (#4945)
Replaces the manual platform method code with Pigeon. Also adds some additional Dart unit tests. Since the needs are minimal, this uses a manual fake rather than introducing Mockito. Fixes #117911
- Loading branch information
1 parent
d08ebfd
commit 51e74b9
Showing
13 changed files
with
482 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.2.2+3 | ||
|
||
* Converts platform communication to Pigeon. | ||
|
||
## 0.2.2+2 | ||
|
||
* Adds pub topics to package metadata. | ||
|
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
2 changes: 1 addition & 1 deletion
2
...s/ios_platform_images/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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 (v11.0.1), 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 | ||
|
||
@class FPIPlatformImageData; | ||
|
||
/// A serialization of a platform image's data. | ||
@interface FPIPlatformImageData : NSObject | ||
/// `init` unavailable to enforce nonnull fields, see the `make` class method. | ||
- (instancetype)init NS_UNAVAILABLE; | ||
+ (instancetype)makeWithData:(FlutterStandardTypedData *)data scale:(NSNumber *)scale; | ||
/// The image data. | ||
@property(nonatomic, strong) FlutterStandardTypedData *data; | ||
/// The image's scale factor. | ||
@property(nonatomic, strong) NSNumber *scale; | ||
@end | ||
|
||
/// The codec used by FPIPlatformImagesApi. | ||
NSObject<FlutterMessageCodec> *FPIPlatformImagesApiGetCodec(void); | ||
|
||
@protocol FPIPlatformImagesApi | ||
/// Returns the URL for the given resource, or null if no such resource is | ||
/// found. | ||
- (nullable NSString *)resolveURLForResource:(NSString *)resourceName | ||
withExtension:(nullable NSString *)extension | ||
error:(FlutterError *_Nullable *_Nonnull)error; | ||
/// Returns the data for the image resource with the given name, or null if | ||
/// no such resource is found. | ||
- (nullable FPIPlatformImageData *)loadImageWithName:(NSString *)name | ||
error:(FlutterError *_Nullable *_Nonnull)error; | ||
@end | ||
|
||
extern void FPIPlatformImagesApiSetup(id<FlutterBinaryMessenger> binaryMessenger, | ||
NSObject<FPIPlatformImagesApi> *_Nullable api); | ||
|
||
NS_ASSUME_NONNULL_END |
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,163 @@ | ||
// 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 (v11.0.1), 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; | ||
} | ||
|
||
@interface FPIPlatformImageData () | ||
+ (FPIPlatformImageData *)fromList:(NSArray *)list; | ||
+ (nullable FPIPlatformImageData *)nullableFromList:(NSArray *)list; | ||
- (NSArray *)toList; | ||
@end | ||
|
||
@implementation FPIPlatformImageData | ||
+ (instancetype)makeWithData:(FlutterStandardTypedData *)data scale:(NSNumber *)scale { | ||
FPIPlatformImageData *pigeonResult = [[FPIPlatformImageData alloc] init]; | ||
pigeonResult.data = data; | ||
pigeonResult.scale = scale; | ||
return pigeonResult; | ||
} | ||
+ (FPIPlatformImageData *)fromList:(NSArray *)list { | ||
FPIPlatformImageData *pigeonResult = [[FPIPlatformImageData alloc] init]; | ||
pigeonResult.data = GetNullableObjectAtIndex(list, 0); | ||
NSAssert(pigeonResult.data != nil, @""); | ||
pigeonResult.scale = GetNullableObjectAtIndex(list, 1); | ||
NSAssert(pigeonResult.scale != nil, @""); | ||
return pigeonResult; | ||
} | ||
+ (nullable FPIPlatformImageData *)nullableFromList:(NSArray *)list { | ||
return (list) ? [FPIPlatformImageData fromList:list] : nil; | ||
} | ||
- (NSArray *)toList { | ||
return @[ | ||
(self.data ?: [NSNull null]), | ||
(self.scale ?: [NSNull null]), | ||
]; | ||
} | ||
@end | ||
|
||
@interface FPIPlatformImagesApiCodecReader : FlutterStandardReader | ||
@end | ||
@implementation FPIPlatformImagesApiCodecReader | ||
- (nullable id)readValueOfType:(UInt8)type { | ||
switch (type) { | ||
case 128: | ||
return [FPIPlatformImageData fromList:[self readValue]]; | ||
default: | ||
return [super readValueOfType:type]; | ||
} | ||
} | ||
@end | ||
|
||
@interface FPIPlatformImagesApiCodecWriter : FlutterStandardWriter | ||
@end | ||
@implementation FPIPlatformImagesApiCodecWriter | ||
- (void)writeValue:(id)value { | ||
if ([value isKindOfClass:[FPIPlatformImageData class]]) { | ||
[self writeByte:128]; | ||
[self writeValue:[value toList]]; | ||
} else { | ||
[super writeValue:value]; | ||
} | ||
} | ||
@end | ||
|
||
@interface FPIPlatformImagesApiCodecReaderWriter : FlutterStandardReaderWriter | ||
@end | ||
@implementation FPIPlatformImagesApiCodecReaderWriter | ||
- (FlutterStandardWriter *)writerWithData:(NSMutableData *)data { | ||
return [[FPIPlatformImagesApiCodecWriter alloc] initWithData:data]; | ||
} | ||
- (FlutterStandardReader *)readerWithData:(NSData *)data { | ||
return [[FPIPlatformImagesApiCodecReader alloc] initWithData:data]; | ||
} | ||
@end | ||
|
||
NSObject<FlutterMessageCodec> *FPIPlatformImagesApiGetCodec(void) { | ||
static FlutterStandardMessageCodec *sSharedObject = nil; | ||
static dispatch_once_t sPred = 0; | ||
dispatch_once(&sPred, ^{ | ||
FPIPlatformImagesApiCodecReaderWriter *readerWriter = | ||
[[FPIPlatformImagesApiCodecReaderWriter alloc] init]; | ||
sSharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter]; | ||
}); | ||
return sSharedObject; | ||
} | ||
|
||
void FPIPlatformImagesApiSetup(id<FlutterBinaryMessenger> binaryMessenger, | ||
NSObject<FPIPlatformImagesApi> *api) { | ||
/// Returns the URL for the given resource, or null if no such resource is | ||
/// found. | ||
{ | ||
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc] | ||
initWithName:@"dev.flutter.pigeon.ios_platform_images.PlatformImagesApi.resolveUrl" | ||
binaryMessenger:binaryMessenger | ||
codec:FPIPlatformImagesApiGetCodec()]; | ||
if (api) { | ||
NSCAssert([api respondsToSelector:@selector(resolveURLForResource:withExtension:error:)], | ||
@"FPIPlatformImagesApi api (%@) doesn't respond to " | ||
@"@selector(resolveURLForResource:withExtension:error:)", | ||
api); | ||
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) { | ||
NSArray *args = message; | ||
NSString *arg_resourceName = GetNullableObjectAtIndex(args, 0); | ||
NSString *arg_extension = GetNullableObjectAtIndex(args, 1); | ||
FlutterError *error; | ||
NSString *output = [api resolveURLForResource:arg_resourceName | ||
withExtension:arg_extension | ||
error:&error]; | ||
callback(wrapResult(output, error)); | ||
}]; | ||
} else { | ||
[channel setMessageHandler:nil]; | ||
} | ||
} | ||
/// Returns the data for the image resource with the given name, or null if | ||
/// no such resource is found. | ||
{ | ||
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc] | ||
initWithName:@"dev.flutter.pigeon.ios_platform_images.PlatformImagesApi.loadImage" | ||
binaryMessenger:binaryMessenger | ||
codec:FPIPlatformImagesApiGetCodec()]; | ||
if (api) { | ||
NSCAssert( | ||
[api respondsToSelector:@selector(loadImageWithName:error:)], | ||
@"FPIPlatformImagesApi api (%@) doesn't respond to @selector(loadImageWithName:error:)", | ||
api); | ||
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) { | ||
NSArray *args = message; | ||
NSString *arg_name = GetNullableObjectAtIndex(args, 0); | ||
FlutterError *error; | ||
FPIPlatformImageData *output = [api loadImageWithName:arg_name error:&error]; | ||
callback(wrapResult(output, error)); | ||
}]; | ||
} else { | ||
[channel setMessageHandler:nil]; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.