Skip to content

Commit

Permalink
added a new delegate method
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoo-msft committed Oct 27, 2020
1 parent 3ef109b commit d3303b4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ - (void)doIfValidationPassed:(ACOInputResults *)results button:(UIButton *)butto

- (void)doIfValidationFailed:(ACOInputResults *)results button:(UIButton *)button
{
[super doIfValidationFailed:results button:button];
if (results) {
if ([self.view.acrActionDelegate respondsToSelector:@selector(didChangeViewLayout:newFrame:properties:)]) {
UIView *viewToFocus = (UIView *)results.firstFailedInput;
NSDictionary *prop = @{@"actiontype" : @"submit", @"firstResponder" : results.firstFailedInput};
if (viewToFocus) {
[self.view.acrActionDelegate didChangeViewLayout:CGRectNull newFrame:viewToFocus.frame properties:prop];
} else {
[self.view.acrActionDelegate didChangeViewLayout:CGRectNull newFrame:CGRectNull properties:prop];
}
}
}
button.backgroundColor = UIColor.redColor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,16 @@ - (void)didChangeViewLayout:(CGRect)oldFrame newFrame:(CGRect)newFrame
[self.scrView scrollRectToVisible:newFrame animated:YES];
}

- (void)didChangeViewLayout:(CGRect)oldFrame newFrame:(CGRect)newFrame properties:(NSDictionary *)properties
{
NSString *actiontype = (NSString *)properties[@"actiontype"];
if ([actiontype isEqualToString:@"submit"]) {
[self.scrView setContentOffset:newFrame.origin animated:YES];
} else {
[self.scrView scrollRectToVisible:newFrame animated:YES];
}
}

- (void)didChangeVisibility:(UIButton *)button isVisible:(BOOL)isVisible
{
if (isVisible) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
- (void)didLoadElements;
- (void)didChangeVisibility:(UIButton *)button isVisible:(BOOL)isVisible;
- (void)didChangeViewLayout:(CGRect)oldFrame newFrame:(CGRect)newFrame;
- (void)didChangeViewLayout:(CGRect)oldFrame newFrame:(CGRect)newFrame properties:(NSDictionary *)properties;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@
#import "ACRView.h"
#import <UIKit/UIKit.h>

// keys used in retrieving values from properties that are dispatced by
// didChangeViewLayout: newFrame: properties:
// possible values contained in the properties
// a key that retreives an ActionType
extern NSString *const ACRAggregateTargetActionType;
// a possible value of ActionType key, SubmitAction
extern NSString *const ACRAggregateTargetSubmitAction;
// a key that retreives a view that is a FirstResponder in AdaptiveCards as a result of a submit action on the card
extern NSString *const ACRAggregateTargetFirstResponder;

// AggregateTraget is used to relay the signal back to host
// It's associated with Action.Submit
@interface ACRAggregateTarget : NSObject <ACRSelectActionDelegate>
@property ACOBaseActionElement *actionElement;
@property (weak) ACRView *view;
@property (weak) ACRColumnView *currentShowcard;

- (instancetype)initWithActionElement:(ACOBaseActionElement *)actionElement rootView:(ACRView *)rootView;

// It's the event handler when the button referenced by sender is activated
- (IBAction)send:(UIButton *)sender;

// it's the method called when input validation succeed
- (void)doIfValidationPassed:(ACOInputResults *)results button:(UIButton *)button;

// it's the method called when input validation failed
- (void)doIfValidationFailed:(ACOInputResults *)results button:(UIButton *)button;

// it's called after input gathering is done, and does UI updates for inputs UI
- (void)updateInputUI:(ACOInputResults *)result button:(UIButton *)button;

- (void)doSelectAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#import "ACRViewPrivate.h"
#import <UIKit/UIKit.h>

NSString *const ACRAggregateTargetActionType = @"actiontype";
NSString *const ACRAggregateTargetSubmitAction = @"submit";
NSString *const ACRAggregateTargetFirstResponder = @"firstResponder";

@implementation ACRAggregateTarget

- (instancetype)initWithActionElement:(ACOBaseActionElement *)actionElement rootView:(ACRView *)rootView;
Expand All @@ -27,10 +31,12 @@ - (instancetype)initWithActionElement:(ACOBaseActionElement *)actionElement root
return self;
}

// main entry point to the event handler, override each methods whithin it for custom behaviors
- (IBAction)send:(UIButton *)sender
{
// dispatch and validate inputs
ACOInputResults *result = [_view dispatchAndValidateInput:_currentShowcard];

// update UI with the inputs
[self updateInputUI:result button:sender];

if (result.hasValidationPassed) {
Expand All @@ -43,15 +49,31 @@ - (IBAction)send:(UIButton *)sender
- (void)doIfValidationPassed:(ACOInputResults *)results button:(UIButton *)button
{
if (results) {
// if a validation passes, gathered input is set in the adaptive card
[[_view card] setInputs:results.gatheredInputs];
// dispatch the card to the host app
[_view.acrActionDelegate didFetchUserResponses:[_view card] action:_actionElement];
}
}

- (void)doIfValidationFailed:(ACOInputResults *)result button:(UIButton *)button
{
if (result) {
if (result.hasViewChangedForAnyViews && [_view.acrActionDelegate respondsToSelector:@selector(didChangeViewLayout:newFrame:)]) {
// layout changed and notify subscribers

if ([self.view.acrActionDelegate respondsToSelector:@selector(didChangeViewLayout:newFrame:properties:)]) {
// focus is set to the first failed input,
UIView *viewToFocus = (UIView *)result.firstFailedInput;
// prepare params for notification
NSDictionary *prop = @{ACRAggregateTargetActionType : ACRAggregateTargetSubmitAction, ACRAggregateTargetFirstResponder : result.firstFailedInput};
// we want to pass this view to the host app, so the app can compute content offset for scrolling for the focus
if (viewToFocus) {
[self.view.acrActionDelegate didChangeViewLayout:CGRectNull newFrame:viewToFocus.frame properties:prop];
} else {
[self.view.acrActionDelegate didChangeViewLayout:CGRectNull newFrame:CGRectNull properties:prop];
}
} else if ([_view.acrActionDelegate respondsToSelector:@selector(didChangeViewLayout:newFrame:)]) {
// notify layout change
[_view.acrActionDelegate didChangeViewLayout:CGRectNull newFrame:CGRectNull];
}
}
Expand Down

0 comments on commit d3303b4

Please sign in to comment.