Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes url loading sync issue for actions #1641

Merged
merged 2 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
6B268FE520CEF89100D99C1B /* (null) in Resources */ = {isa = PBXBuildFile; };
6B7B1A9B20C21CA900260731 /* SportingEvent.json in Resources */ = {isa = PBXBuildFile; fileRef = 6B7B1A9920C21CA800260731 /* SportingEvent.json */; };
6B9AB30620D9857B005C8E15 /* Image.Explicit.Size.json in Resources */ = {isa = PBXBuildFile; fileRef = 6B9AB30520D9857A005C8E15 /* Image.Explicit.Size.json */; };
6B9AB30C20DC4FB3005C8E15 /* IconsInSomeActions.json in Resources */ = {isa = PBXBuildFile; fileRef = 6B9AB30B20DC4FB3005C8E15 /* IconsInSomeActions.json */; };
6BF339D620A665E600DA5973 /* CustomTextBlockRenderer.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6BF339D420A665E600DA5973 /* CustomTextBlockRenderer.mm */; };
6BF339E320A66A3F00DA5973 /* AdaptiveCards.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6BF339E220A66A3F00DA5973 /* AdaptiveCards.framework */; };
6BF339E420A66A4D00DA5973 /* AdaptiveCards.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 6BF339E220A66A3F00DA5973 /* AdaptiveCards.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -140,6 +141,7 @@
30A3885C20D315AA00AAEE59 /* NotificationCard.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = NotificationCard.json; path = ../../../../samples/Tests/NotificationCard.json; sourceTree = "<group>"; };
6B7B1A9920C21CA800260731 /* SportingEvent.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SportingEvent.json; path = ../../../../samples/v1.0/Scenarios/SportingEvent.json; sourceTree = "<group>"; };
6B9AB30520D9857A005C8E15 /* Image.Explicit.Size.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = Image.Explicit.Size.json; path = ../../../../samples/Tests/Image.Explicit.Size.json; sourceTree = "<group>"; };
6B9AB30B20DC4FB3005C8E15 /* IconsInSomeActions.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = IconsInSomeActions.json; path = ../../../../samples/Tests/IconsInSomeActions.json; sourceTree = "<group>"; };
6BF339D420A665E600DA5973 /* CustomTextBlockRenderer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CustomTextBlockRenderer.mm; sourceTree = "<group>"; };
6BF339D520A665E600DA5973 /* CustomTextBlockRenderer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomTextBlockRenderer.h; sourceTree = "<group>"; };
6BF339E220A66A3F00DA5973 /* AdaptiveCards.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = AdaptiveCards.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -346,6 +348,7 @@
F4D33E341F045B6E00941E44 /* Jsons */ = {
isa = PBXGroup;
children = (
6B9AB30B20DC4FB3005C8E15 /* IconsInSomeActions.json */,
6B9AB30520D9857A005C8E15 /* Image.Explicit.Size.json */,
30A3885C20D315AA00AAEE59 /* NotificationCard.json */,
30860BC020C9B5C9009F9D99 /* ColumnSet_Container.VerticalStretch.json */,
Expand Down Expand Up @@ -528,6 +531,7 @@
F4933CD41F79852C00F6EBFD /* Image.HorizontalAlignment.json in Resources */,
F4933CC51F79852C00F6EBFD /* Action.Submit.json in Resources */,
6B268FE520CEF89100D99C1B /* (null) in Resources */,
6B9AB30C20DC4FB3005C8E15 /* IconsInSomeActions.json in Resources */,
F4933CED1F79852C00F6EBFD /* TextBlock.Weight.json in Resources */,
F4933D061F79853B00F6EBFD /* StockUpdate.json in Resources */,
30860BC620C9B5C9009F9D99 /* ColumnSet.Input.Number.VerticalStretch.json in Resources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@
F401A8761F0DB69B006D7AF2 /* ACRImageSetUICollectionView.mm */,
F401A8791F0DCBC8006D7AF2 /* ACRImageSetRenderer.h */,
F401A87A1F0DCBC8006D7AF2 /* ACRImageSetRenderer.mm */,
F42741101EF873A600399FBB /* ACRImageRenderer.h */,
F42741111EF873A600399FBB /* ACRImageRenderer.mm */,
F427411E1EF9DB8000399FBB /* ACRContainerRenderer.h */,
F427411F1EF9DB8000399FBB /* ACRContainerRenderer.mm */,
F42741221EFB274C00399FBB /* ACRColumnRenderer.h */,
Expand All @@ -766,8 +768,6 @@
F42741061EF8624F00399FBB /* ACRIBaseCardElementRenderer.h */,
F42741081EF864A900399FBB /* ACRBaseCardElementRenderer.h */,
F42741091EF864A900399FBB /* ACRBaseCardElementRenderer.mm */,
F42741101EF873A600399FBB /* ACRImageRenderer.h */,
F42741111EF873A600399FBB /* ACRImageRenderer.mm */,
F42741141EF895AB00399FBB /* ACRTextBlockRenderer.h */,
F42741151EF895AB00399FBB /* ACRTextBlockRenderer.mm */,
F43A940E1F1D60E30001920B /* ACRFactSetRenderer.h */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
@interface ACOHostConfig:NSObject

@property NSArray<NSString *> *fontFamilyNames;

@property BOOL allActionsHaveIcons;
@property CGFloat buttonPadding;
- (instancetype)init;

+ (ACOHostConfigParseResult *)fromJson:(NSString *)payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ - (instancetype)initWithConfig:(std::shared_ptr<HostConfig> const &)config
if([UIFont.familyNames containsObject:requestedFontFamilyName]){
_fontFamilyNames = @[requestedFontFamilyName];
}
_allActionsHaveIcons = YES;
_buttonPadding = 5;
}
return self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace AdaptiveCards;
@interface ACOHostConfig()

- (instancetype)initWithConfig:(std::shared_ptr<HostConfig> const &)config;
- (std::shared_ptr<HostConfig>) getHostConfig;
- (std::shared_ptr<HostConfig>)getHostConfig;
- (void)setHostConfig:(std::shared_ptr<HostConfig> const &)config;

+ (NSNumber *)getTextStrokeWidthForWeight:(TextWeight)weight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ - (UIButton* )renderButton:(ACRView *)rootView
[superview addTarget:target];

[button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

[button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

return button;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ - (UIButton* )renderButton:(ACRView *)rootView
[target createShowCard:inputs];

[button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

[button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

return button;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ - (UIButton* )renderButton:(ACRView *)view
[superview addTarget:target];

[button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

[button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

return button;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//

#import <UIKit/UIKit.h>
#import "SharedAdaptiveCard.h"
#import "HostConfig.h"
#import "ACRView.h"

@interface UIButton(ACRButton)
Expand Down
106 changes: 74 additions & 32 deletions source/ios/AdaptiveCards/AdaptiveCards/AdaptiveCards/ACRButton.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,70 @@

#import "ACOBaseActionElementPrivate.h"
#import "ACRButton.h"
#import "ACRView.h"
#import "ACRViewPrivate.h"
#import "ACRUIImageView.h"
#import "SharedAdaptiveCard.h"
#import "ACOHostConfigPrivate.h"

@implementation UIButton(ACRButton)

+ (void)setImageView:(UIImage*)image inButton:(UIButton*)button withConfig:(ACOHostConfig *)config
{
// Format the image so it fits in the button and is placed where it must be placed
CGSize contentSize = [button.titleLabel intrinsicContentSize];
float imageHeight = contentSize.height;
IconPlacement iconPlacement = [config getHostConfig]->actions.iconPlacement;

CGFloat widthToHeightRatio = 0.0f;
if(image){
if(image.size.height > 0) {
widthToHeightRatio = image.size.width / image.size.height;
}
}
CGSize imageSize = CGSizeMake(imageHeight * widthToHeightRatio, imageHeight);
ACRUIImageView *imageView = [[ACRUIImageView alloc] initWithFrame:CGRectMake(0, 0, imageSize.width, imageSize.height)];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.image = image;
[button addSubview:imageView];
// scale the image using UIImageView
[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:imageSize.width].active = YES;

[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:imageSize.height].active = YES;

if(iconPlacement == AdaptiveCards::IconPlacement::AboveTitle && config.allActionsHaveIcons) {
// fix image view to top and center x of the button
[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:button
attribute:NSLayoutAttributeTop multiplier:1.0 constant:config.buttonPadding].active = YES;
[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:button
attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0].active = YES;
// image can't be postion at the top of the title, so adjust title inset edges
[button setTitleEdgeInsets:UIEdgeInsetsMake(imageHeight, 0, -imageHeight, 0)];
// readjust content edge, so intrinsic content size can be accurately determined by system library, and give enough room for title and image icon
[button setContentEdgeInsets:UIEdgeInsetsMake(config.buttonPadding, config.buttonPadding + button.layer.cornerRadius, config.buttonPadding + imageHeight, config.buttonPadding + button.layer.cornerRadius)];
// configure button frame to correct size; in case translatesAutoresizingMaskIntoConstraints is used
button.frame = CGRectMake(0, 0, MAX(imageSize.width, contentSize.width), imageSize.height + config.buttonPadding);
} else {
int iconPadding = [config getHostConfig]->spacing.defaultSpacing;
[button setTitleEdgeInsets:UIEdgeInsetsMake(config.buttonPadding, (imageSize.width) + iconPadding, config.buttonPadding, -(iconPadding + imageSize.width))];
[button setContentEdgeInsets:UIEdgeInsetsMake(config.buttonPadding, config.buttonPadding, config.buttonPadding, imageSize.width + iconPadding + button.layer.cornerRadius)];
[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeLeft multiplier:1.0 constant:config.buttonPadding].active = YES;
[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0].active = YES;
button.frame = CGRectMake(0, 0, imageSize.width + config.buttonPadding + contentSize.width, MAX(imageSize.height, contentSize.height));
}
}

+ (UIButton* )rootView:(ACRView *)rootView
baseActionElement:(ACOBaseActionElement *)acoAction
title:(NSString *)title
Expand All @@ -20,41 +80,23 @@ + (UIButton* )rootView:(ACRView *)rootView
NSBundle* bundle = [NSBundle bundleWithIdentifier:@"MSFT.AdaptiveCards"];
UIButton *button = [bundle loadNibNamed:@"ACRButton" owner:rootView options:nil][0];
[button setTitle:title forState:UIControlStateNormal];

CGSize contentSize = [button.titleLabel intrinsicContentSize];
[button setFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)];
button.titleLabel.adjustsFontSizeToFitWidth = YES;

std::shared_ptr<AdaptiveCards::BaseActionElement> action = [acoAction element];
if([iconUrl length] != 0)
{
NSMutableDictionary *actionsViewMap = [rootView getActionsMap];
__block UIImageView *imgView = nil;
// Generate key for ImageViewMap
NSString *key = [NSString stringWithCString:action->GetId().c_str() encoding:[NSString defaultCStringEncoding]];
// Syncronize access to imageViewMap
dispatch_sync([rootView getSerialQueue], ^{
// if imageView is available, get it, otherwise cache UIButton, so it can be used once images are ready
if(actionsViewMap[key] && [actionsViewMap[key] isKindOfClass:[UIImageView class]])
{
imgView = actionsViewMap[key];
}
else
{
actionsViewMap[key] = button;
}
});

if(imgView)
{
[ACRView setImageView:imgView inButton:button withConfig:config];

// remove postfix added for imageMap access
std::string id = action->GetId();
std::size_t idx = id.find_last_of('_');
action->SetId(id.substr(0, idx));
}
NSDictionary *imageViewMap = [rootView getImageMap];
NSString *key = [ACRView generateKeyForActionElement:action];
UIImage *img = imageViewMap[key];

if(img){
[UIButton setImageView:img inButton:button withConfig:config];
} else {
// button's intrinsic content size is determined by title size and content edge
// add corner radius to content size by adding it to content edge inset
[button setContentEdgeInsets:UIEdgeInsetsMake(config.buttonPadding, config.buttonPadding + button.layer.cornerRadius, config.buttonPadding, config.buttonPadding + button.layer.cornerRadius)];
}

return button;
}


@end
33 changes: 17 additions & 16 deletions source/ios/AdaptiveCards/AdaptiveCards/AdaptiveCards/ACRRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,22 @@ + (UIView *)renderWithAdaptiveCards:(std::shared_ptr<AdaptiveCard> const &)adapt
[verticalView setStyle:style];

[rootView addTasksToConcurrentQueue:body];


std::vector<std::shared_ptr<BaseActionElement>> actions = adaptiveCard->GetActions();

if(!actions.empty()) {
[rootView loadImagesForActionsAndCheckIfAllActionsHaveIconImages:actions hostconfig:config];
}

[rootView waitForAsyncTasksToFinish];

[ACRRenderer render:verticalView rootView:rootView inputs:inputs withCardElems:body andHostConfig:config];

[[rootView card] setInputs:inputs];
[[rootView card] setInputs:inputs];

std::vector<std::shared_ptr<BaseActionElement>> actions = adaptiveCard->GetActions();
if(!actions.empty()) {
[ACRSeparator renderActionsSeparator:verticalView hostConfig:[config getHostConfig]];

[rootView addActionsToConcurrentQueue:actions];

[rootView waitForAsyncTasksToFinish];


// renders buttons and their associated actions
[ACRRenderer renderButton:rootView inputs:inputs superview:verticalView actionElems:actions hostConfig:config];
}
Expand All @@ -104,7 +105,8 @@ + (UIView *)renderWithAdaptiveCards:(std::shared_ptr<AdaptiveCard> const &)adapt
ACRRegistration *reg = [ACRRegistration getInstance];
UIView<ACRIContentHoldingView> *childview = nil;
NSDictionary<NSString *, NSNumber*> *attributes =
@{@"spacing":[NSNumber numberWithInt:[config getHostConfig]->actions.buttonSpacing]};
@{@"spacing":[NSNumber numberWithInt:[config getHostConfig]->actions.buttonSpacing],
@"distribution":[NSNumber numberWithInt:UIStackViewDistributionFillProportionally] };

if(ActionsOrientation::Horizontal == [config getHostConfig]->actions.actionsOrientation){
childview = [[ACRColumnSetView alloc] initWithFrame:CGRectMake(0, 0, superview.frame.size.width, superview.frame.size.height) attributes:attributes];
Expand Down Expand Up @@ -147,7 +149,6 @@ + (UIView *)renderWithAdaptiveCards:(std::shared_ptr<AdaptiveCard> const &)adapt
contentWidth = maxWidth;
}
childview.frame = CGRectMake(0, 0, contentWidth, contentHeight);

containingView.frame = CGRectMake(0, 0, superview.frame.size.width, contentHeight);
containingView.translatesAutoresizingMaskIntoConstraints = NO;
[containingView addSubview:childview];
Expand Down Expand Up @@ -187,9 +188,9 @@ + (UIView *)render:(UIView<ACRIContentHoldingView> *)view
{
ACRRegistration *reg = [ACRRegistration getInstance];
ACOBaseCardElement *acoElem = [[ACOBaseCardElement alloc] init];

UIView *prevStretchableElem = nil, *curStretchableElem = nil;

auto firstelem = elems.begin();
for(const auto &elem:elems)
{
Expand All @@ -207,9 +208,9 @@ + (UIView *)render:(UIView<ACRIContentHoldingView> *)view
}

[acoElem setElem:elem];

curStretchableElem = [renderer render:view rootView:rootView inputs:inputs baseCardElement:acoElem hostConfig:config];

if(elem->GetHeight() == HeightType::Stretch){
if(prevStretchableElem){
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:curStretchableElem
Expand All @@ -222,12 +223,12 @@ + (UIView *)render:(UIView<ACRIContentHoldingView> *)view
heightConstraint.priority = UILayoutPriorityDefaultLow;
heightConstraint.active = YES;
}

if([view isKindOfClass:[ACRColumnView class]]){
ACRColumnView *columnView = (ACRColumnView*)view;
columnView.hasStretchableView = YES;
}

prevStretchableElem = curStretchableElem;
}
}
Expand Down