AnnoDetailViewController.m 12.2 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9 10 11 12
//
//  AnnoDetailViewController.m
//  redstar
//
//  Created by admin on 15/11/30.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "AnnoDetailViewController.h"
#import "AnnounceDetailHeadView.h"
#import "AnnoContentTableViewCell.h"
#import "AnnounceDetailFootView.h"
13
#import "AttachmentModel.h"
admin's avatar
admin committed
14 15 16 17 18

#import "HttpClient.h"

#import "AnnounceDetailModel.h"

admin's avatar
admin committed
19
#import "AttachmentTableViewCell.h"
admin's avatar
admin committed
20

21 22
#import <AFNetworking.h>

admin's avatar
admin committed
23 24
#define kAnnounceContentCell @"AnnoContentTableViewCell"
#define kAttachmentTableViewCell @"AttachmentTableViewCell"
25
@interface AnnoDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIDocumentInteractionControllerDelegate>
admin's avatar
admin committed
26 27 28 29 30

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AnnounceDetailHeadView *headerView;
@property (nonatomic, strong) AnnounceDetailFootView *footerView;
@property (nonatomic, strong) AnnounceDetailModel *annoDetail;
31 32

@property (nonatomic, strong) NSMutableArray *attachmentArray;
admin's avatar
admin committed
33 34 35 36 37 38 39 40
@end

@implementation AnnoDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
41 42
    self.attachmentArray = [NSMutableArray array];
    
admin's avatar
admin committed
43 44 45 46 47 48 49 50 51 52 53 54
    [self setNav];
    
    [self requestAnnoDetail];
    
    [self settingYetRead];
}

- (void)requestAnnoDetail
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@", kRedStarURL, kAnnounceDetailURL, self.affiche_uuid];
    HttpClient *httpCilent = [[HttpClient alloc] initWithUrl:url];
    [httpCilent getAnnounceDetailWithCompletion:^(id response, NSError *error) {
admin's avatar
admin committed
55
        NSLog(@"anno   公告详情 = %@", response);
admin's avatar
admin committed
56 57 58 59
        NSDictionary *dict = response[@"data"];
        AnnounceDetailModel *annoDetail = [AnnounceDetailModel announceDetailModelWithDict:dict];
        _annoDetail = annoDetail;
        
60 61 62 63 64 65
        for (NSDictionary *attachmentDict in _annoDetail.attachmentUrls) {
            AttachmentModel *atta = [[AttachmentModel alloc] init];
            [atta setValuesForKeysWithDictionary:attachmentDict];
            [_attachmentArray addObject:atta];
        }
        
admin's avatar
admin committed
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
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }];
}

- (void)settingYetRead
{
    NSString *user_uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_uuid"];
    
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    NSString *url = [NSString stringWithFormat:@"%@%@%@?user_uuid=%@&read_time=%@", kRedStarURL, kAnnounceYetReadURL, self.affiche_uuid, user_uuid, dateString];
    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    HttpClient *httpCilent = [[HttpClient alloc] initWithUrl:url];
    NSLog(@"uuurl = %@", url);
    [httpCilent settingAnnounceYetReadWithCompletion:^(id response, NSError *error) {
        NSLog(@"已读 response = %@", response);
    }];
}

// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setNav
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"公告"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
}

admin's avatar
admin committed
112 113
- (void)showFile:(UIButton *)sender
{
114 115
    NSInteger row = sender.tag - 998473;
    AttachmentModel *atta = _attachmentArray[row];
admin's avatar
admin committed
116
    
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = paths.lastObject;
    NSString *url = [NSString stringWithFormat:@"%@/%@", path, atta.fileName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:url]) {
        
        UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:url]];//为该对象初始化一个加载路径
        docController.delegate = self;//设置代理
        [docController presentPreviewAnimated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"文件不存在,请先下载在查看!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self;
admin's avatar
admin committed
136 137 138 139
}

- (void)downloadFile:(UIButton *)sender
{
140 141 142 143 144
    NSInteger row = sender.tag - 998473;
    AttachmentModel *atta = _attachmentArray[row];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = paths.lastObject;
admin's avatar
admin committed
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
    [self downloadFileURL:[NSString stringWithFormat:@"%@%@", kRedStarURL, atta.fileUrl] savePath:path fileName:atta.fileName];
}

- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //检查本地文件是否已存在
    NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName];
    
    //检查附件是否存在
    if ([fileManager fileExistsAtPath:fileName]) {
        NSLog(@"存在了");
    }else{
        NSLog(@"不存在");

        //创建附件存储目录
        if (![fileManager fileExistsAtPath:aSavePath]) {
            [fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        
        //下载附件
        NSURL *url = [[NSURL alloc] initWithString:aUrl];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        operation.inputStream   = [NSInputStream inputStreamWithURL:url];
        operation.outputStream  = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];
        
        //下载进度控制
        
        //已完成下载
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSLog(@"下载成功");
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"下载失败");
        }];
        
        [operation start];
    }
admin's avatar
admin committed
188
}
admin's avatar
admin committed
189 190 191 192 193 194 195

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableView Delegate/DataSource
196 197 198 199 200
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

admin's avatar
admin committed
201 202
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
203 204 205 206 207
    if (section == 0) {
        return 1;
    } else {
        return _attachmentArray.count;
    }
admin's avatar
admin committed
208 209 210 211 212
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
213
    if (indexPath.section == 0) {
admin's avatar
admin committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        AnnoContentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kAnnounceContentCell];
        if (!cell) {
            cell = [[AnnoContentTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kAnnounceContentCell];
        }
        cell.titleLabel.text = @"内容";
        
        NSString *str = [NSString stringWithFormat:@"<style> html{ font-size: 15px; color: #444444 } </style>%@", _annoDetail.content];
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        cell.contentLabel.attributedText = attrStr;
        
        cell.backgroundColor = kAnnounceHeaderColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        AttachmentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kAttachmentTableViewCell];
        if (!cell) {
            cell = [[AttachmentTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kAttachmentTableViewCell];
        }
232 233
        AttachmentModel *atta = _attachmentArray[indexPath.row];
        NSString *name = atta.fileName;
admin's avatar
admin committed
234
        [cell.nameBtn setTitle:name forState:UIControlStateNormal];
235
        
admin's avatar
admin committed
236 237 238
        
        [cell.nameBtn addTarget:self action:@selector(showFile:) forControlEvents:UIControlEventTouchUpInside];
        [cell.downloadBtn addTarget:self action:@selector(downloadFile:) forControlEvents:UIControlEventTouchUpInside];
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        cell.downloadBtn.tag = 998473 + indexPath.row;
        cell.nameBtn.tag = 998473 + indexPath.row;
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = paths.lastObject;
        NSString *url = [NSString stringWithFormat:@"%@/%@", path, atta.fileName];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:url]) {
            cell.downloadBtn.selected = YES;
            cell.downloadBtn.backgroundColor = [UIColor grayColor];
        } else {
            cell.downloadBtn.selected = NO;
            cell.downloadBtn.backgroundColor = [UIColor colorWithRed:47 / 255.0 green:209/ 255.0 blue:92 / 255.0 alpha:1.0];

        }
admin's avatar
admin committed
255 256

        return cell;
admin's avatar
admin committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100.0;
        _tableView.tableHeaderView = self.headerView;
        [_tableView registerClass:[AnnoContentTableViewCell class] forCellReuseIdentifier:kAnnounceContentCell];
admin's avatar
admin committed
272
        [_tableView registerClass:[AttachmentTableViewCell class] forCellReuseIdentifier:kAttachmentTableViewCell];
admin's avatar
admin committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        [self.view addSubview:_tableView];
        
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self.view addConstraint:tableTop];
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
        
        NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableBottom];
    }
    return _tableView;
}

- (AnnounceDetailHeadView *)headerView
{
    if (!_headerView) {
        _headerView = [[AnnounceDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 120)];
        _headerView.backgroundColor = kAnnounceHeaderColor;
        _headerView.annoDetail = _annoDetail;
        }
    return _headerView;
}

- (AnnounceDetailFootView *)footerView
{
    if (!_footerView) {
303
        _footerView = [[AnnounceDetailFootView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
admin's avatar
admin committed
304 305 306 307 308
    }
    return _footerView;
}

@end