ICRTaskResultContentView.m 7.2 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
//
//  ICRTaskResultContentView.m
//  XFFruit
//
//  Created by Xummer on 15/4/14.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRTaskResultContentView.h"
#import "ICRTaskDetailContentView.h"
#import "LBorderView.h"

#define TR_VERTICAL_MARGIN          (10.0f)
#define TR_HORIZON_MARGIN           (15.0f)
#define TR_INNER_GAP                (5.0f)

@interface ICRTaskResultContentView ()

@property (strong, nonatomic) IBTUIView *m_topResultView;
@property (strong, nonatomic) IBTUILabel *m_resultLabel;
@property (strong, nonatomic) IBTUILabel *m_resultValueLabel;
@property (strong, nonatomic) IBTUIView *m_resultValueBG;
@property (strong, nonatomic) IBTUILabel *m_attachmentImageLabel;
@property (strong, nonatomic) UIImageView *m_attachImageView;
@property (strong, nonatomic) LBorderView *m_referenceView;
@property (strong, nonatomic) ICRTaskDetailContentView *m_rDetailView;

@end

@implementation ICRTaskResultContentView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    
    [self initSubviews];
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGSize vSize = CGSizeZero;
    CGFloat fMaxW = self.width - 2 * TR_HORIZON_MARGIN;
    CGFloat fDy = 0;
    
    [_m_resultLabel sizeToFit];
    _m_resultLabel.origin = (CGPoint){
        .x = TR_HORIZON_MARGIN,
        .y = TR_VERTICAL_MARGIN
    };
    
    CGFloat fInsert = 2;
    CGFloat fW = fMaxW - 2 * fInsert;
    vSize = CGSizeMake(fMaxW - 2 * fInsert, CGFLOAT_MAX);
    vSize = [_m_resultValueLabel widthLimitedSizeThatFits:vSize];
    _m_resultValueLabel.frame = (CGRect){
        .origin.x = fInsert,
        .origin.y = fInsert,
        .size.width = fW,
        .size.height = MAX(18, vSize.height)
    };
    
    _m_resultValueBG.frame = (CGRect){
        .origin.x = _m_resultLabel.left,
        .origin.y = _m_resultLabel.bottom + 2,
        .size.width = fMaxW,
        .size.height = _m_resultValueLabel.bottom + fInsert
    };
    
    fDy = _m_resultValueBG.bottom + TR_INNER_GAP;
    
    if (!_m_attachImageView.hidden) {
        [_m_attachmentImageLabel sizeToFit];
        _m_attachmentImageLabel.origin = (CGPoint){
            .x = _m_resultLabel.left,
            .y = fDy
        };
        
        UIImage *image = _m_attachImageView.image;
        CGFloat hWRatio =  image ? (image.size.height / image.size.width) : 1;
        
        _m_attachImageView.frame = (CGRect){
            .origin.x = _m_attachmentImageLabel.left,
            .origin.y = _m_attachmentImageLabel.bottom + 2,
            .size.width = fMaxW,
            .size.height = fMaxW * hWRatio
        };
        
        fDy = _m_attachImageView.bottom + TR_INNER_GAP;
    }
    
    _m_topResultView.frame = (CGRect){
        .origin.x = 0,
        .origin.y = 0,
        .size.width = self.width,
        .size.height = fDy
    };
    
    fDy = _m_topResultView.bottom + TR_INNER_GAP;
    
    _m_rDetailView.width = fMaxW;
    [_m_rDetailView layoutSubviews];
    
    _m_referenceView.frame = (CGRect){
        .origin.x = TR_HORIZON_MARGIN,
        .origin.y = fDy,
        .size.width = _m_rDetailView.width,
        .size.height = _m_rDetailView.height
    };
    
    fDy = _m_referenceView.bottom + TR_INNER_GAP;
    
    self.height = fDy;
}

#pragma mark - Private Method

- (void)initSubviews {
    
    // Top Result
    self.m_topResultView = [[IBTUIView alloc] init];
    _m_topResultView.backgroundColor = [UIColor colorWithR:238 g:246 b:255 a:1];
    
    [self addSubview:_m_topResultView];
    
    self.m_resultLabel =
    [[self class] titleLabelWithText:[IBTCommon localizableString:@"Process Result"]];
    [_m_topResultView addSubview:_m_resultLabel];
    
    self.m_resultValueBG = [[IBTUIView alloc] init];
    _m_resultValueBG.layer.cornerRadius = IBT_DEFAULT_CORNER_RADIUS;
    _m_resultValueBG.layer.borderWidth = 1;
    _m_resultValueBG.layer.borderColor = ICR_TINTCOLOR.CGColor;
    [_m_topResultView addSubview:_m_resultValueBG];
    
    self.m_resultValueLabel =
    [[self class] titleLabelWithText:nil];
    _m_resultValueLabel.numberOfLines = 0;
    [_m_resultValueBG addSubview:_m_resultValueLabel];

    self.m_attachmentImageLabel =
    [[self class] titleLabelWithText:[IBTCommon localizableString:@"Attachment Image"]];
    [_m_topResultView addSubview:_m_attachmentImageLabel];
    
    self.m_attachImageView = [[UIImageView alloc] init];
    [_m_topResultView addSubview:_m_attachImageView];
    
    
    // Bottom Refference View
    self.m_referenceView = [[LBorderView alloc] init];
    _m_referenceView.cornerRadius = IBT_DEFAULT_CORNER_RADIUS;
    _m_referenceView.borderType = BorderTypeDashed;
    _m_referenceView.borderWidth = .5f;
    _m_referenceView.borderColor = [UIColor lightGrayColor];
    _m_referenceView.dashPattern = 4;
    _m_referenceView.spacePattern = 2;
    [self addSubview:_m_referenceView];
    
    self.m_rDetailView = [[ICRTaskDetailContentView alloc] initWithIsAttach:YES];
    [_m_referenceView addSubview:_m_rDetailView];
}

+ (IBTUILabel *)titleLabelWithText:(NSString *)text {
    IBTUILabel *label = [[IBTUILabel alloc] init];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = ICR_TINTCOLOR;
    label.font = [UIFont systemFontOfSize:15];
    label.text = text;
    
    return label;
}

@end

#import "ICRTask.h"
#import "ICRAttachment.h"

@implementation ICRTaskResultContentView (Configure)

- (void)updateWithTask:(ICRTask *)task {
    self.m_resultValueLabel.text = task.processResult;
    
    [_m_rDetailView updateWithTask:task];
    
    ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
        NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ? ORDER BY %@", [ICRAttachment TableName], @"objectId", @"seq"];
        return [db executeQuery:sql, task.uuid ];
    };
    
    __weak typeof(self)weakSelf = self;
    ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        ICRAttachment *attach = [fetchedObjects count] > 2 ? fetchedObjects[ 1 ] : nil;
        if ([attach.fileName extensionIsImageType]) {
            strongSelf.m_attachImageView.hidden = NO;
            NSString *imgeUrl = [ICRHTTPController AttachmentUrlWithID:@( attach.aID )];
            [strongSelf.m_attachImageView sd_setImageWithURL:[NSURL URLWithString:imgeUrl]
                                            placeholderImage:[UIImage imageWithColor:ICR_IMAGE_BG_COLOR]
                                                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                                       [strongSelf layoutSubviews];
                                                   }];
        }
        else {
            strongSelf.m_attachImageView.hidden = YES;
        }
        
        strongSelf.m_attachmentImageLabel.hidden = strongSelf.m_attachImageView.hidden;
        
        [strongSelf layoutSubviews];
    };
    
    ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
    [dbCtrl runFetchForClass:[ICRAttachment class]
                  fetchBlock:fetchBlk
           fetchResultsBlock:fetchResultsBlk];
    
}

@end