Skip to content

Commit 985bb0d

Browse files
committed
feat: load ListItem images asynchronously
Synchronous image loading is generally a bad idea for performance reasons. This changes image loading to asynchronous via a NSURLSessionDataTask. Also: Fix inline documentation for ListItem images
1 parent b9c3b8d commit 985bb0d

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

packages/react-native-carplay/ios/RNCarPlay.m

+22-4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ - (UIImage *)imageWithSize:(UIImage *)image convertToSize:(CGSize)size {
160160
return destImage;
161161
}
162162

163+
- (void)updateItemImageWithURL:(CPListItem *)item imgUrl:(NSString *)imgUrlString {
164+
NSURL *imgUrl = [NSURL URLWithString:imgUrlString];
165+
166+
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:imgUrl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
167+
if (data) {
168+
UIImage *image = [UIImage imageWithData:data];
169+
dispatch_async(dispatch_get_main_queue(), ^{
170+
[item setImage:image];
171+
});
172+
} else {
173+
NSLog(@"Failed to load image from URL: %@", imgUrl);
174+
}
175+
}];
176+
[task resume];
177+
}
178+
163179
RCT_EXPORT_METHOD(checkForConnection) {
164180
RNCPStore *store = [RNCPStore sharedManager];
165181
if ([store isConnected] && hasListeners) {
@@ -599,7 +615,8 @@ - (UIImage *)imageWithSize:(UIImage *)image convertToSize:(CGSize)size {
599615
}
600616
CPListItem *item = (CPListItem *)section.items[index];
601617
if (config[@"imgUrl"]) {
602-
[item setImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[RCTConvert NSString:config[@"imgUrl"]]]]]];
618+
NSString *imgUrlString = [RCTConvert NSString:config[@"imgUrl"]];
619+
[self updateItemImageWithURL:item imgUrl:imgUrlString];
603620
}
604621
if (config[@"image"]) {
605622
[item setImage:[RCTConvert UIImage:config[@"image"]]];
@@ -956,9 +973,6 @@ - (void) applyConfigForMapTemplate:(CPMapTemplate*)mapTemplate templateId:(NSStr
956973
NSString *_detailText = [item objectForKey:@"detailText"];
957974
NSString *_text = [item objectForKey:@"text"];
958975
UIImage *_image = [RCTConvert UIImage:[item objectForKey:@"image"]];
959-
if (item[@"imgUrl"]) {
960-
_image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[RCTConvert NSString:item[@"imgUrl"]]]]];
961-
}
962976
CPListItem *_item;
963977
if (@available(iOS 14.0, *)) {
964978
CPListItemAccessoryType accessoryType = _showsDisclosureIndicator ? CPListItemAccessoryTypeDisclosureIndicator : CPListItemAccessoryTypeNone;
@@ -969,6 +983,10 @@ - (void) applyConfigForMapTemplate:(CPMapTemplate*)mapTemplate templateId:(NSStr
969983
if ([item objectForKey:@"isPlaying"]) {
970984
[_item setPlaying:[RCTConvert BOOL:[item objectForKey:@"isPlaying"]]];
971985
}
986+
if (item[@"imgUrl"]) {
987+
NSString *imgUrlString = [RCTConvert NSString:item[@"imgUrl"]];
988+
[self updateItemImageWithURL:_item imgUrl:imgUrlString];
989+
}
972990
[_item setUserInfo:@{ @"index": @(index) }];
973991
[_items addObject:_item];
974992
index = index + 1;

packages/react-native-carplay/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-carplay",
3-
"version": "2.3.2",
3+
"version": "2.3.3",
44
"description": "CarPlay for React Native",
55
"main": "lib/index.js",
66
"react-native": "src/index.ts",

packages/react-native-carplay/src/interfaces/ListItem.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export interface ListItem {
1313
*/
1414
detailText?: string;
1515
/**
16-
* The image displayed on the leading edge of the list item cell.
16+
* Image from file system displayed on the leading edge of the list item cell.
1717
*/
1818
image?: ImageSourcePropType;
1919
/**
20-
* The image from file system displayed on the leading edge of the list item cell.
20+
* Url for image displayed on the leading edge of the list item cell.
2121
*/
2222
imgUrl?: null;
2323
/**

0 commit comments

Comments
 (0)