1//
2//  KBStatusView.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 "KBStatusView.h"
10
11#import <YOLayout/YOLayout+PrefabLayouts.h>
12
13@interface KBStatusView ()
14@property KBLabel *titleLabel;
15@property KBLabel *label;
16@property KBLabel *descriptionLabel;
17@property YOHBox *buttons;
18@end
19
20@implementation KBStatusView
21
22- (void)viewInit {
23  [super viewInit];
24  [self kb_setBackgroundColor:KBAppearance.currentAppearance.backgroundColor];
25
26  YOView *contentView = [YOView view];
27  {
28    _titleLabel = [KBLabel label];
29    [contentView addSubview:_titleLabel];
30
31    _label = [KBLabel label];
32    [contentView addSubview:_label];
33
34    _descriptionLabel = [KBLabel label];
35    [contentView addSubview:_descriptionLabel];
36
37    _buttons = [YOHBox box:@{@"horizontalAlignment": @"center", @"spacing": @(10)}];
38    [contentView addSubview:_buttons];
39
40    YOSelf yself = self;
41    contentView.viewLayout = [YOLayout layoutWithLayoutBlock:^CGSize(id<YOLayout> layout, CGSize size) {
42      CGFloat y = yself.insets.top;
43
44      y += [layout sizeToFitVerticalInFrame:CGRectMake(yself.insets.left, y, size.width - yself.insets.left - yself.insets.right, 0) view:yself.titleLabel].size.height + 40;
45
46      y += [layout sizeToFitVerticalInFrame:CGRectMake(yself.insets.left, y, size.width - yself.insets.left - yself.insets.right, 0) view:yself.label].size.height + 20;
47
48      if ([yself.descriptionLabel hasText]) {
49        y += -10;
50        y += [layout sizeToFitVerticalInFrame:CGRectMake(yself.insets.left, y, size.width - yself.insets.left - yself.insets.right, 0) view:yself.descriptionLabel].size.height + 20;
51      }
52
53      if ([yself.buttons.subviews count] > 0) {
54        y += 20;
55        y += [layout sizeToFitVerticalInFrame:CGRectMake(0, y, size.width, 0) view:yself.buttons].size.height;
56      }
57
58      y += yself.insets.bottom;
59      return CGSizeMake(size.width, y);
60    }];
61  }
62  [self addSubview:contentView];
63  self.viewLayout = [YOLayout center:contentView];
64}
65
66- (void)viewDidAppear:(BOOL)animated { }
67
68- (void)setError:(NSError *)error title:(NSString *)title retry:(dispatch_block_t)retry close:(dispatch_block_t)close {
69  [self setText:error.localizedDescription description:error.localizedRecoverySuggestion title:title retry:retry close:close];
70}
71
72- (void)setText:(NSString *)text description:(NSString *)description title:(NSString *)title retry:(dispatch_block_t)retry close:(dispatch_block_t)close {
73  [_titleLabel setText:title style:KBTextStyleHeaderLarge options:0 alignment:NSCenterTextAlignment lineBreakMode:NSLineBreakByWordWrapping];
74  [_label setText:text style:KBTextStyleDefault options:0 alignment:NSCenterTextAlignment lineBreakMode:NSLineBreakByWordWrapping];
75
76  [_descriptionLabel setText:description style:KBTextStyleDefault options:0 alignment:NSCenterTextAlignment lineBreakMode:NSLineBreakByWordWrapping];
77
78  [_buttons kb_removeAllSubviews];
79
80  if (retry) {
81    KBButton *retryButton = [KBButton buttonWithText:@"Retry" style:KBButtonStylePrimary];
82    retryButton.targetBlock = retry;
83    [_buttons addSubview:retryButton];
84  }
85
86  if (close) {
87    KBButton *closeButton = [KBButton buttonWithText:@"Close" style:KBButtonStyleDefault];
88    closeButton.targetBlock = close;
89    [_buttons addSubview:closeButton];
90  }
91
92  [self setNeedsLayout];
93}
94
95@end
96