|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import "RCTActivityIndicatorViewComponentView.h" |
| 9 | + |
| 10 | +#import <fabric/activityindicator/ActivityIndicatorViewProps.h> |
| 11 | + |
| 12 | +using namespace facebook::react; |
| 13 | + |
| 14 | +static UIActivityIndicatorViewStyle convertActivityIndicatorViewStyle(const ActivityIndicatorViewSize &size) { |
| 15 | + switch (size) { |
| 16 | + case ActivityIndicatorViewSize::Small: |
| 17 | + return UIActivityIndicatorViewStyleWhite; |
| 18 | + case ActivityIndicatorViewSize::Large: |
| 19 | + return UIActivityIndicatorViewStyleWhiteLarge; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +@implementation RCTActivityIndicatorViewComponentView { |
| 24 | + UIActivityIndicatorView *_activityIndicatorView; |
| 25 | +} |
| 26 | + |
| 27 | +- (instancetype)initWithFrame:(CGRect)frame |
| 28 | +{ |
| 29 | + if (self = [super initWithFrame:frame]) { |
| 30 | + _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:self.bounds]; |
| 31 | + _activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 32 | + |
| 33 | + auto &&defaultProps = ActivityIndicatorViewProps(); |
| 34 | + |
| 35 | + if (defaultProps.animating) { |
| 36 | + [_activityIndicatorView startAnimating]; |
| 37 | + } else { |
| 38 | + [_activityIndicatorView stopAnimating]; |
| 39 | + } |
| 40 | + _activityIndicatorView.color = [UIColor colorWithCGColor:defaultProps.color.get()]; |
| 41 | + _activityIndicatorView.hidesWhenStopped = defaultProps.hidesWhenStopped; |
| 42 | + _activityIndicatorView.activityIndicatorViewStyle = convertActivityIndicatorViewStyle(defaultProps.size); |
| 43 | + |
| 44 | + [self addSubview:_activityIndicatorView]; |
| 45 | + } |
| 46 | + |
| 47 | + return self; |
| 48 | +} |
| 49 | + |
| 50 | +- (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps |
| 51 | +{ |
| 52 | + if (!oldProps) { |
| 53 | + oldProps = _props ?: std::make_shared<ActivityIndicatorViewProps>(); |
| 54 | + } |
| 55 | + _props = props; |
| 56 | + |
| 57 | + [super updateProps:props oldProps:oldProps]; |
| 58 | + |
| 59 | + auto oldViewProps = *std::dynamic_pointer_cast<const ActivityIndicatorViewProps>(oldProps); |
| 60 | + auto newViewProps = *std::dynamic_pointer_cast<const ActivityIndicatorViewProps>(props); |
| 61 | + |
| 62 | + if (oldViewProps.animating != newViewProps.animating) { |
| 63 | + if (newViewProps.animating) { |
| 64 | + [_activityIndicatorView startAnimating]; |
| 65 | + } else { |
| 66 | + [_activityIndicatorView stopAnimating]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (oldViewProps.color.get() != newViewProps.color.get()) { |
| 71 | + _activityIndicatorView.color = [UIColor colorWithCGColor:newViewProps.color.get()]; |
| 72 | + } |
| 73 | + |
| 74 | + // TODO: This prop should be deprecated. |
| 75 | + if (oldViewProps.hidesWhenStopped != newViewProps.hidesWhenStopped) { |
| 76 | + _activityIndicatorView.hidesWhenStopped = newViewProps.hidesWhenStopped; |
| 77 | + } |
| 78 | + |
| 79 | + if (oldViewProps.size != newViewProps.size) { |
| 80 | + _activityIndicatorView.activityIndicatorViewStyle = convertActivityIndicatorViewStyle(newViewProps.size); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@end |
0 commit comments