Skip to content

Commit c228f21

Browse files
committed
- Manually edit the pigeon auto-generated create function on iOS to asynchronously return a FlutterReply when the video player is created for phAssets
1 parent f9caa95 commit c228f21

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎packages/video_player/video_player/ios/Classes/FLTVideoPlayerPlugin.m‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
#import <Photos/Photos.h>
1111
#import "VIMediaCache.h"
1212

13+
// TODO(recastrodiaz) remove duplicate interface. Taken from messages.m
14+
@interface FLTTextureMessage ()
15+
+ (FLTTextureMessage*)fromMap:(NSDictionary*)dict;
16+
- (NSDictionary*)toMap;
17+
@end
18+
1319
@interface VIMediaCacheSingleton : NSObject {
1420
VIResourceLoaderManager* resourceLoaderManager;
1521
}
@@ -848,7 +854,9 @@ - (void)initialize:(FlutterError* __autoreleasing*)error {
848854
[_players removeAllObjects];
849855
}
850856

851-
- (FLTTextureMessage*)create:(FLTCreateMessage*)input error:(FlutterError**)error {
857+
- (FLTTextureMessage*)create:(FLTCreateMessage*)input
858+
error:(FlutterError* __strong*)error
859+
callback:(FlutterReply)callback {
852860
FLTFrameUpdater* frameUpdater = [[FLTFrameUpdater alloc] initWithRegistry:_registry];
853861
FLTVideoPlayer* player;
854862
if (input.asset) {
@@ -859,7 +867,9 @@ - (FLTTextureMessage*)create:(FLTCreateMessage*)input error:(FlutterError**)erro
859867
assetPath = [_registrar lookupKeyForAsset:input.asset];
860868
}
861869
player = [[FLTVideoPlayer alloc] initWithAsset:assetPath frameUpdater:frameUpdater];
862-
return [self onPlayerSetup:player frameUpdater:frameUpdater];
870+
FLTTextureMessage* output = [self onPlayerSetup:player frameUpdater:frameUpdater];
871+
callback(wrapResult([output toMap], *error));
872+
return nil;
863873
} else if (input.uri) {
864874
NSString* phAssetPrefix = @"phasset://";
865875
if ([input.uri hasPrefix:phAssetPrefix]) {
@@ -869,16 +879,22 @@ - (FLTTextureMessage*)create:(FLTCreateMessage*)input error:(FlutterError**)erro
869879
initWithPHAssetLocalIdentifier:phAssetArg
870880
frameUpdater:frameUpdater
871881
onPlayerCreated:^(FLTVideoPlayer* player) {
872-
[self onPlayerSetup:player frameUpdater:frameUpdater];
882+
FLTTextureMessage* output =
883+
[self onPlayerSetup:player frameUpdater:frameUpdater];
884+
callback(wrapResult([output toMap], *error));
873885
}];
874886
return nil;
875887
} else {
876888
player = [[FLTVideoPlayer alloc] initWithURL:[NSURL URLWithString:input.uri]
877889
frameUpdater:frameUpdater];
878-
return [self onPlayerSetup:player frameUpdater:frameUpdater];
890+
FLTTextureMessage* output = [self onPlayerSetup:player frameUpdater:frameUpdater];
891+
callback(wrapResult([output toMap], *error));
892+
return nil;
879893
}
880894
} else {
881895
*error = [FlutterError errorWithCode:@"video_player" message:@"not implemented" details:nil];
896+
FLTTextureMessage* output = nil;
897+
callback(wrapResult([output toMap], *error));
882898
return nil;
883899
}
884900
}
@@ -950,4 +966,18 @@ - (void)pause:(FLTTextureMessage*)input error:(FlutterError**)error {
950966
[player pause];
951967
}
952968

969+
// TODO(recastrodiaz) remove duplicate function. Taken from messages.m
970+
static NSDictionary* wrapResult(NSDictionary* result, FlutterError* error) {
971+
NSDictionary* errorDict = (NSDictionary*)[NSNull null];
972+
if (error) {
973+
errorDict = [NSDictionary
974+
dictionaryWithObjectsAndKeys:(error.code ? error.code : [NSNull null]), @"code",
975+
(error.message ? error.message : [NSNull null]), @"message",
976+
(error.details ? error.details : [NSNull null]), @"details",
977+
nil];
978+
}
979+
return [NSDictionary dictionaryWithObjectsAndKeys:(result ? result : [NSNull null]), @"result",
980+
errorDict, @"error", nil];
981+
}
982+
953983
@end

‎packages/video_player/video_player/ios/Classes/messages.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Autogenerated from Pigeon (v0.1.0-experimental.11), do not edit directly.
22
// See also: https://pub.dev/packages/pigeon
3+
#import <Flutter/Flutter.h>
34
#import <Foundation/Foundation.h>
45
@protocol FlutterBinaryMessenger;
56
@class FlutterError;
@@ -55,7 +56,8 @@ NS_ASSUME_NONNULL_BEGIN
5556
@protocol FLTVideoPlayerApi
5657
- (void)initialize:(FlutterError *_Nullable *_Nonnull)error;
5758
- (FLTTextureMessage *)create:(FLTCreateMessage *)input
58-
error:(FlutterError *_Nullable *_Nonnull)error;
59+
error:(FlutterError *_Nullable *_Nonnull)error
60+
callback:(FlutterReply)callback;
5961
- (void)dispose:(FLTTextureMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
6062
- (void)setLooping:(FLTLoopingMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
6163
- (void)setVolume:(FLTVolumeMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;

‎packages/video_player/video_player/ios/Classes/messages.m‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ void FLTVideoPlayerApiSetup(id<FlutterBinaryMessenger> binaryMessenger, id<FLTVi
227227
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
228228
FlutterError *error;
229229
FLTCreateMessage *input = [FLTCreateMessage fromMap:message];
230-
FLTTextureMessage *output = [api create:input error:&error];
231-
callback(wrapResult([output toMap], error));
230+
[api create:input error:&error callback:callback];
232231
}];
233232
} else {
234233
[channel setMessageHandler:nil];

0 commit comments

Comments
 (0)