1//
2//  KBErrorView.m
3//  Keybase
4//
5//  Created by Gabriel on 3/6/15.
6//  Copyright (c) 2015 Gabriel Handford. All rights reserved.
7//
8
9#import "KBErrorView.h"
10
11@interface KBErrorView ()
12@property KBLabel *label;
13@property KBLabel *descriptionLabel;
14@property KBButton *closeButton;
15@end
16
17@implementation KBErrorView
18
19- (void)viewInit {
20  [super viewInit];
21  [self kb_setBackgroundColor:KBAppearance.currentAppearance.dangerBackgroundColor];
22
23  _label = [KBLabel label];
24  [self addSubview:_label];
25
26  _descriptionLabel = [KBLabel label];
27  [self addSubview:_descriptionLabel];
28
29  _closeButton = [KBButton buttonWithText:@"Close" style:KBButtonStyleDefault];
30  [self addSubview:_closeButton];
31
32  YOSelf yself = self;
33  self.viewLayout = [YOLayout layoutWithLayoutBlock:^CGSize(id<YOLayout> layout, CGSize size) {
34    CGFloat y = 20;
35    y += [layout sizeToFitVerticalInFrame:CGRectMake(20, y, size.width - 40, 0) view:yself.label].size.height + 20;
36
37    if ([yself.descriptionLabel hasText]) {
38      y += -10;
39      y += [layout sizeToFitVerticalInFrame:CGRectMake(20, y, size.width - 40, 0) view:yself.descriptionLabel].size.height + 20;
40    }
41
42    if (!yself.closeButton.hidden) {
43      y += [layout centerWithSize:CGSizeMake(120, 0) frame:CGRectMake(0, y, size.width, 0) view:yself.closeButton].size.height + 20;
44    }
45
46    return CGSizeMake(size.width, y);
47  }];
48}
49
50- (void)setError:(NSError *)error completion:(dispatch_block_t)completion {
51  [_label setText:error.localizedDescription style:KBTextStyleDefault options:KBTextOptionsDanger alignment:NSCenterTextAlignment lineBreakMode:NSLineBreakByWordWrapping];
52  [_descriptionLabel setText:error.localizedRecoverySuggestion style:KBTextStyleSecondaryText options:KBTextOptionsDanger alignment:NSCenterTextAlignment lineBreakMode:NSLineBreakByWordWrapping];
53
54  if (completion) {
55    _closeButton.targetBlock = completion;
56    _closeButton.hidden = NO;
57  } else {
58    _closeButton.hidden = YES;
59  }
60
61  [self setNeedsLayout];
62}
63
64@end
65