// // RQDownloadModel.m // TEST // // Created by 张嵘 on 2018/10/22. // Copyright © 2018 张嵘. All rights reserved. // #import "RQDownloadModel.h" #import "RQDownloadHeader.h" @implementation RQDownloadModel - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.imageUrl forKey:@"imageUrl"]; [aCoder encodeObject:self.title forKey:@"title"]; [aCoder encodeObject:self.content forKey:@"content"]; [aCoder encodeObject:self.urlString forKey:@"urlString"]; [aCoder encodeObject:self.downloadDesc forKey:@"downloadDesc"]; [aCoder encodeObject:self.fileName forKey:@"fileName"]; [aCoder encodeObject:self.fileFormat forKey:@"fileFormat"]; [aCoder encodeObject:self.destinationPath forKey:@"destinationPath"]; [aCoder encodeObject:[NSNumber numberWithFloat:self.progress] forKey:@"progress"]; [aCoder encodeObject:[NSNumber numberWithInteger:self.status] forKey:@"status"]; [aCoder encodeObject:self.statusText forKey:@"statusText"]; [aCoder encodeObject:self.completeTime forKey:@"completeTime"]; [aCoder encodeObject:[NSNumber numberWithBool:self.isLast] forKey:@"isLast"]; [aCoder encodeObject:[NSNumber numberWithInteger:self.fileTotalSize] forKey:@"fileTotalSize"]; [aCoder encodeObject:[NSNumber numberWithInteger:self.fileDownloadSize] forKey:@"fileDownloadSize"]; [aCoder encodeObject:[NSNumber numberWithBool:self.isFinished] forKey:@"isFinished"]; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.imageUrl = [aDecoder decodeObjectForKey:@"imageUrl"]; self.title = [aDecoder decodeObjectForKey:@"title"]; self.content = [aDecoder decodeObjectForKey:@"content"]; self.urlString = [aDecoder decodeObjectForKey:@"urlString"]; self.downloadDesc = [aDecoder decodeObjectForKey:@"downloadDesc"]; self.fileName = [aDecoder decodeObjectForKey:@"fileName"]; self.fileFormat = [aDecoder decodeObjectForKey:@"fileFormat"]; self.destinationPath = [aDecoder decodeObjectForKey:@"destinationPath"]; self.progress = [[aDecoder decodeObjectForKey:@"progress"] floatValue]; self.status = [[aDecoder decodeObjectForKey:@"status"] integerValue]; self.statusText = [aDecoder decodeObjectForKey:@"statusText"]; self.completeTime = [aDecoder decodeObjectForKey:@"completeTime"]; self.isLast = [[aDecoder decodeObjectForKey:@"isLast"] boolValue]; self.fileTotalSize = [[aDecoder decodeObjectForKey:@"fileTotalSize"] integerValue]; self.fileDownloadSize = [[aDecoder decodeObjectForKey:@"fileDownloadSize"] integerValue]; self.isFinished = [[aDecoder decodeObjectForKey:@"isFinished"] boolValue]; } return self; } - (NSString *)destinationPath{ _destinationPath = [[RQCachesDirectory stringByAppendingString:self.fileName] stringByAppendingString:self.fileFormat]; return _destinationPath; } - (NSString *)fileName{ if (!_fileName) { NSTimeInterval timeInterval = [[NSDate date]timeIntervalSince1970]; //解决多个任务同时开始时 文件重名问题 NSString *timeStr = [NSString stringWithFormat:@"%.6f",timeInterval]; timeStr = [timeStr stringByReplacingOccurrencesOfString:@"." withString:@"_"]; _fileName = [NSString stringWithFormat:@"%@",timeStr]; } return _fileName; } - (NSString *)fileFormat{ if (!_fileFormat && _urlString) { NSArray *urlArr = [_urlString componentsSeparatedByString:@"."]; if (urlArr && urlArr.count>1) { self.fileFormat = [@"." stringByAppendingString:[urlArr lastObject]]; } } return _fileFormat; } - (void)setProgress:(CGFloat)progress{ if (_progress != progress) { _progress = progress; } if ([kRQDownloadManager enableProgressLog]) { NSLog(@"%@%@==%@==%.1f%%",self.fileName,self.fileFormat,self.statusText,progress*100*1.0); } if (self.progressChanged) { self.progressChanged(self); } } - (void)setStatus:(RQDownloadStatus)status{ if (_status != status) { _status = status; [self setStatusTextWith:_status]; if (self.statusChanged) { self.statusChanged(self); } } } - (void)setUrlString:(NSString *)urlString{ _urlString = urlString; NSArray *urlArr = [_urlString componentsSeparatedByString:@"."]; if (urlArr && urlArr.count>1) { self.fileFormat = [@"." stringByAppendingString:[urlArr lastObject]]; } } - (void)setCompleteTime:(NSString *)completeTime{ NSDateFormatter *fomatter = [[NSDateFormatter alloc]init]; _completeTime = [fomatter stringFromDate:[NSDate date]]; } - (void)setStatusTextWith:(RQDownloadStatus)status{ _status = status; switch (status) { case RQDownloadStatus_Running: { self.statusText = @"正在下载"; break; } case RQDownloadStatus_Suspended: { self.statusText = @"暂停下载"; break; } case RQDownloadStatus_Failed: { self.statusText = @"下载失败"; break; } case RQDownloadStatus_Cancel: { self.statusText = @"取消下载"; break; } case RQDownloadStatus_Waiting: { self.statusText = @"等待下载"; break; } case RQDownloadStatus_Completed: { self.statusText = @"下载完成"; break; } default: { break; } } NSLog(@"%@%@==%@",self.fileName,self.fileFormat,self.statusText); } + (NSArray *)mj_ignoredCodingPropertyNames{ return @[@"statusChanged",@"progressChanged",@"stream",@"operation"]; } - (NSInteger)fileDownloadSize{ // 获取文件下载长度 NSInteger fileDownloadSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.destinationPath error:nil][NSFileSize] integerValue]; _fileDownloadSize = fileDownloadSize; return _fileDownloadSize; } - (NSOutputStream *)stream{ if (!_stream) { _stream = [NSOutputStream outputStreamToFileAtPath:self.destinationPath append:YES]; } return _stream; } - (BOOL)isFinished{ return (self.fileTotalSize == self.fileDownloadSize) && (self.fileTotalSize != 0); } @end