@@ -281,7 +281,6 @@ - (void)updateListRowItemImageWithURL:(CPListImageRowItem *)item imgUrl:(NSStrin
281281 listTemplate.emptyViewSubtitleVariants = [RCTConvert NSArray: config[@" emptyViewSubtitleVariants" ]];
282282 }
283283 }
284- listTemplate.delegate = self;
285284 carPlayTemplate = listTemplate;
286285 }
287286 else if ([type isEqualToString: @" map" ]) {
@@ -1142,6 +1141,17 @@ - (void) applyConfigForMapTemplate:(CPMapTemplate*)mapTemplate templateId:(NSStr
11421141
11431142 NSArray *_imageItems = [item objectForKey: @" images" ];
11441143 NSArray *_imageUrls = [item objectForKey: @" imgUrls" ];
1144+ NSArray *_imageIds = [item objectForKey: @" imageIds" ];
1145+
1146+ NSString *_itemId = [item objectForKey: @" id" ];
1147+
1148+ if (_itemId == nil ) {
1149+ _itemId = (id )[NSNull null ];
1150+ }
1151+
1152+ if (_imageIds == nil ) {
1153+ _imageIds = (id )[NSNull null ];
1154+ }
11451155
11461156 if (_imageItems == nil && _imageUrls == nil ) {
11471157 UIImage *_image = [RCTConvert UIImage: _imageObj];
@@ -1163,8 +1173,37 @@ - (void) applyConfigForMapTemplate:(CPMapTemplate*)mapTemplate templateId:(NSStr
11631173 NSString *imgUrlString = [RCTConvert NSString: item[@" imgUrl" ]];
11641174 [self updateItemImageWithURL: _item imgUrl: imgUrlString];
11651175 }
1166- [_item setUserInfo: @{ @" index" : @(listIndex) }];
1167- [_items addObject: _item];
1176+ [_item setUserInfo: @{
1177+ @" index" : @(listIndex),
1178+ @" itemId" : _itemId
1179+ }];
1180+
1181+ _item.handler = ^(CPListItem *selectedItem, void (^completionHandler)(void )) {
1182+ NSDictionary *payload = @{
1183+ @" templateId" : templateId,
1184+ @" index" : selectedItem.userInfo [@" index" ],
1185+ @" itemId" : selectedItem.userInfo [@" itemId" ]
1186+ };
1187+ if (self->hasListeners ) {
1188+ [self sendEventWithName: @" didSelectListItem" body: payload];
1189+ }
1190+ self.selectedResultBlock = completionHandler;
1191+ };
1192+ if (_itemId) {
1193+ RNCPStore *store = [RNCPStore sharedManager ];
1194+ NSMutableArray *existing = store.itemsStore [_itemId];
1195+ if (existing == nil ) {
1196+ existing = [NSMutableArray array ];
1197+ store.itemsStore [_itemId] = existing;
1198+ }
1199+ [existing addObject: _item];
1200+ }
1201+
1202+ if (_item) {
1203+ [_items addObject: _item];
1204+ } else {
1205+ NSLog (@" The item was nil can't be created %@ " , item);
1206+ }
11681207 } else {
11691208 // parse images
11701209 NSMutableArray * _images = [NSMutableArray array ];
@@ -1204,17 +1243,68 @@ - (void) applyConfigForMapTemplate:(CPMapTemplate*)mapTemplate templateId:(NSStr
12041243 }
12051244 }
12061245
1246+ [_item setUserInfo: @{
1247+ @" index" : @(listIndex),
1248+ @" itemId" : _itemId,
1249+ @" imageIds" : _imageIds
1250+ }];
1251+
12071252 [_item setListImageRowHandler: ^(CPListImageRowItem * _Nonnull item, NSInteger index, dispatch_block_t _Nonnull completionBlock) {
12081253 // Find the current template
12091254 RNCPStore *store = [RNCPStore sharedManager ];
12101255 CPTemplate *template = [store findTemplateById: templateId];
1256+ NSString *imageId = nil ;
1257+
1258+ id imageIdsObj = item.userInfo [@" imageIds" ];
1259+ if ([imageIdsObj isKindOfClass: [NSArray class ]]) {
1260+ NSArray *imageIds = (NSArray *)imageIdsObj;
1261+ if (index < imageIds.count ) {
1262+ id candidate = imageIds[index];
1263+ if ([candidate isKindOfClass: [NSString class ]]) {
1264+ imageId = candidate;
1265+ }
1266+ }
1267+ }
1268+
1269+ if (!imageId) {
1270+ imageId = (NSString *)[NSNull null ];
1271+ }
1272+
12111273 if (template) {
1212- [self sendTemplateEventWithName: template name: @" didSelectListItemRowImage" json: @{ @" index" : @(listIndex), @" imageIndex" : @(index)}];
1274+ NSMutableDictionary *payload = [@{
1275+ @" index" : @(listIndex),
1276+ @" imageIndex" : @(index),
1277+ @" itemId" : item.userInfo [@" itemId" ],
1278+ } mutableCopy];
1279+
1280+ if (![imageId isKindOfClass: [NSNull class ]]) {
1281+ payload[@" imageId" ] = imageId;
1282+ }
1283+
1284+ [self sendTemplateEventWithName: template
1285+ name: @" didSelectListItemRowImage"
1286+ json: payload];
12131287 }
12141288 }];
12151289
1216- [_item setUserInfo: @{ @" index" : @(listIndex) }];
1217- [_items addObject: _item];
1290+ _item.handler = ^(CPListItem *selectedItem, void (^completionHandler)(void )) {
1291+ NSDictionary *payload = @{
1292+ @" templateId" : templateId,
1293+ @" index" : selectedItem.userInfo [@" index" ],
1294+ @" itemId" : selectedItem.userInfo [@" itemId" ]
1295+ };
1296+
1297+ if (self->hasListeners ) {
1298+ [self sendEventWithName: @" didSelectListItem" body: payload];
1299+ }
1300+
1301+ self.selectedResultBlock = completionHandler;
1302+ };
1303+ if (_item) {
1304+ [_items addObject: _item];
1305+ } else {
1306+ NSLog (@" The image row item could not be created: %@ " , item);
1307+ }
12181308 }
12191309
12201310 }
@@ -1433,10 +1523,28 @@ - (void)sendTemplateEventWithName:(CPTemplate *)template name:(NSString*)name {
14331523 [self sendTemplateEventWithName: template name: name json: @{}];
14341524}
14351525
1436- - (void )sendTemplateEventWithName : (CPTemplate *)template name : (NSString *)name json : (NSDictionary *)json {
1437- NSMutableDictionary *body = [[NSMutableDictionary alloc ] initWithDictionary: json];
1438- NSDictionary *userInfo = [template userInfo ];
1439- [body setObject: [userInfo objectForKey: @" templateId" ] forKey: @" templateId" ];
1526+ - (void )sendTemplateEventWithName : (CPTemplate *)template name : (NSString *)name json : (NSDictionary *)json {
1527+ NSMutableDictionary *body = [[NSMutableDictionary alloc ] initWithDictionary: json ?: @{}];
1528+
1529+ NSString *templateId = nil ;
1530+ NSDictionary *userInfo = nil ;
1531+
1532+ if ([template respondsToSelector: @selector (userInfo )]) {
1533+ userInfo = [template userInfo ];
1534+ }
1535+
1536+ if ([userInfo isKindOfClass: [NSDictionary class ]]) {
1537+ id value = userInfo[@" templateId" ];
1538+ if ([value isKindOfClass: [NSString class ]]) {
1539+ templateId = (NSString *)value;
1540+ }
1541+ }
1542+
1543+ if (templateId.length > 0 ) {
1544+ body[@" templateId" ] = templateId;
1545+ }
1546+ body[@" templateId" ] = templateId ?: @" " ;
1547+
14401548 if (hasListeners) {
14411549 [self sendEventWithName: name body: body];
14421550 }
@@ -1538,14 +1646,6 @@ - (void)searchTemplate:(CPSearchTemplate *)searchTemplate updatedSearchText:(NSS
15381646 self.searchResultBlock = completionHandler;
15391647}
15401648
1541- # pragma ListTemplate
1542-
1543- - (void )listTemplate : (CPListTemplate *)listTemplate didSelectListItem : (CPListItem *)item completionHandler : (void (^)(void ))completionHandler {
1544- NSNumber * index = [item.userInfo objectForKey: @" index" ];
1545- [self sendTemplateEventWithName: listTemplate name: @" didSelectListItem" json: @{ @" index" : index }];
1546- self.selectedResultBlock = completionHandler;
1547- }
1548-
15491649# pragma TabBarTemplate
15501650- (void )tabBarTemplate : (CPTabBarTemplate *)tabBarTemplate didSelectTemplate : (__kindof CPTemplate *)selectedTemplate {
15511651 NSString * selectedTemplateId = [[selectedTemplate userInfo ] objectForKey: @" templateId" ];
0 commit comments