66// Copyright (c) 2014年 Coding. All rights reserved.
77//
88
9+
10+ #define kStartImageName @" start_image_name"
11+
912#import " StartImagesManager.h"
13+ #import " CodingNetAPIClient.h"
14+
1015
1116@interface StartImagesManager ()
12- @property (strong , nonatomic ) NSDictionary *imageDict;
1317@property (strong , nonatomic ) NSMutableArray *imageLoadedArray;
1418@property (strong , nonatomic ) StartImage *startImage;
1519@end
@@ -48,7 +52,7 @@ - (BOOL)createFolder:(NSString *)path{
4852}
4953
5054- (NSString *)downloadPath {
51- NSString *documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) lastObject ];
55+ NSString *documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) firstObject ];
5256 NSString *downloadPath = [documentPath stringByAppendingPathComponent: @" Coding_StartImages" ];
5357 return downloadPath;
5458}
@@ -61,6 +65,9 @@ - (StartImage *)randomImage{
6165 }else {
6266 _startImage = [StartImage defautImage ];
6367 }
68+
69+ [self saveDisplayImageName: _startImage.fileName];
70+ [self refreshImagesPlist ];
6471 return _startImage;
6572}
6673- (StartImage *)curImage {
@@ -75,46 +82,145 @@ - (NSString *)pathOfSTPlist{
7582}
7683
7784- (void )loadStartImages {
78- self.imageDict = [[NSDictionary alloc ] initWithContentsOfFile: [self pathOfSTPlist ]];
79- self.imageLoadedArray = [[NSMutableArray alloc ] init ];
80- NSString *path = [self downloadPath ];
81- NSArray *fileContents = [[NSFileManager defaultManager ] contentsOfDirectoryAtPath: path error: NULL ];
82- for (NSString *curFileName in [fileContents objectEnumerator ]) {
83- NSString *filePath = [path stringByAppendingPathComponent: curFileName];
84- BOOL isDirectory;
85- [[NSFileManager defaultManager ] fileExistsAtPath: filePath isDirectory: &isDirectory];
86- StartImage *st = [StartImage stFromDict: [self .imageDict objectForKey: curFileName]];
87- if (st) {
88- st.pathDisk = filePath;
89- [self .imageLoadedArray addObject: st];
85+ NSArray *plistArray = [NSArray arrayWithContentsOfFile: [self pathOfSTPlist ]];
86+ plistArray = [NSObject arrayFromJSON: plistArray ofObjects: @" StartImage" ];
87+
88+ NSMutableArray *imageLoadedArray = [[NSMutableArray alloc ] init ];
89+ NSFileManager *fm = [NSFileManager defaultManager ];
90+
91+ for (StartImage *curST in plistArray) {
92+ if ([fm fileExistsAtPath: curST.pathDisk]) {
93+ [imageLoadedArray addObject: curST];
94+ }
95+ }
96+
97+ NSString *preDisplayImageName = [self getDisplayImageName ];
98+ if (preDisplayImageName && preDisplayImageName.length > 0 ) {
99+ NSUInteger index = [imageLoadedArray indexOfObjectPassingTest: ^BOOL (id obj, NSUInteger idx, BOOL *stop) {
100+ if ([[(StartImage *)obj fileName ] isEqualToString: preDisplayImageName]) {
101+ *stop = YES ;
102+ return YES ;
103+ }
104+ return NO ;
105+ }];
106+ if (index != NSNotFound ) {
107+ [imageLoadedArray removeObjectAtIndex: index];
108+ }
109+ }
110+ self.imageLoadedArray = imageLoadedArray;
111+ }
112+
113+ - (void )refreshImagesPlist {
114+ NSString *aPath = @" api/wallpaper/wallpapers" ;
115+ NSDictionary *params = @{@" type" : @" 5" };
116+ [[CodingNetAPIClient sharedJsonClient ] GET: aPath parameters: params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
117+ DebugLog (@" \n ===========response===========\n %@ :\n %@ " , aPath, responseObject);
118+ id error = [self handleResponse: responseObject];
119+ if (!error) {
120+ NSArray *resultA = [responseObject valueForKey: @" data" ];
121+ if ([self createFolder: [self downloadPath ]]) {
122+ if ([resultA writeToFile: [self pathOfSTPlist ] atomically: YES ]) {
123+ [[StartImagesManager shareManager ] startDownloadImages ];
124+ }
125+ }
126+ }
127+ } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
128+ DebugLog (@" \n ===========response===========\n %@ :\n %@ " , aPath, error);
129+ }];
130+ }
131+
132+ - (void )startDownloadImages {
133+
134+ if ([[AFNetworkReachabilityManager sharedManager ] networkReachabilityStatus ] != AFNetworkReachabilityStatusReachableViaWiFi) {
135+ return ;
136+ }
137+
138+ NSArray *plistArray = [NSArray arrayWithContentsOfFile: [self pathOfSTPlist ]];
139+ plistArray = [NSObject arrayFromJSON: plistArray ofObjects: @" StartImage" ];
140+
141+ NSMutableArray *needToDownloadArray = [NSMutableArray array ];
142+ NSFileManager *fm = [NSFileManager defaultManager ];
143+ for (StartImage *curST in plistArray) {
144+ if (![fm fileExistsAtPath: curST.pathDisk]) {
145+ [needToDownloadArray addObject: curST];
90146 }
91147 }
148+
149+ for (StartImage *curST in needToDownloadArray) {
150+ [curST startDownloadImage ];
151+ }
152+ }
153+
154+
155+ - (void )saveDisplayImageName : (NSString *)name {
156+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults ];
157+ [defaults setObject: name forKey: kStartImageName ];
158+ [defaults synchronize ];
159+ }
160+
161+ - (NSString *)getDisplayImageName {
162+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults ];
163+ return [defaults objectForKey: kStartImageName ];
92164}
93165
94166@end
95167
168+
96169@implementation StartImage
170+ - (NSString *)fileName {
171+ if (!_fileName && _url.length > 0 ) {
172+ _fileName = [[_url componentsSeparatedByString: @" /" ] lastObject ];
173+ }
174+ return _fileName;
175+ }
97176
98- - (UIImage *)image {
99- return [UIImage imageWithContentsOfFile: self .pathDisk];
177+ - (NSString *)descriptionStr {
178+ if (!_descriptionStr && _group) {
179+ _descriptionStr = [NSString stringWithFormat: @" \" %@ \" @%@ " , _group.name.length > 0 ? _group.name : @" 今天天气不错" , _group.author.length > 0 ? _group.author : @" 作者不要打我" ];
180+ }
181+ return _descriptionStr;
182+ }
183+
184+ - (NSString *)pathDisk {
185+ if (!_pathDisk && _url) {
186+ NSString *documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) firstObject ];
187+ _pathDisk = [[documentPath
188+ stringByAppendingPathComponent: @" Coding_StartImages" ]
189+ stringByAppendingPathComponent: [[_url componentsSeparatedByString: @" /" ] lastObject ]];
190+ }
191+ return _pathDisk;
100192}
101193
102194+ (StartImage *)defautImage {
103195 StartImage *st = [[StartImage alloc ] init ];
104- st.hasBeenDownload = YES ;
105- st.descriptionStr = @" “最春光乍泄” @堂堂超栗子" ;
196+ st.descriptionStr = @" \" 最春光乍泄\" @堂堂超栗子" ;
106197 st.fileName = @" STARTIMAGE.jpg" ;
107-
108198 st.pathDisk = [[NSBundle mainBundle ] pathForResource: @" STARTIMAGE" ofType: @" jpg" ];
109199 return st;
110200}
111- + (StartImage *)stFromDict : (NSDictionary *)dict {
112- StartImage *st = [[StartImage alloc ] init ];
113- st.hasBeenDownload = YES ;
114201
115- st.descriptionStr = [dict objectForKey: @" descriptionStr" ];
116- st.fileName = [dict objectForKey: @" fileName" ];
117- return st;
202+ - (UIImage *)image {
203+ return [UIImage imageWithContentsOfFile: self .pathDisk];
118204}
119205
206+ - (void )startDownloadImage {
207+ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration ];
208+ AFURLSessionManager *manager = [[AFURLSessionManager alloc ] initWithSessionConfiguration: configuration];
209+ NSURL *URL = [NSURL URLWithString: self .url];
210+ NSURLRequest *request = [NSURLRequest requestWithURL: URL];
211+ NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest: request progress: nil destination: ^NSURL *(NSURL *targetPath, NSURLResponse *response) {
212+ NSString *documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) firstObject ];
213+ NSString *pathDisk = [[documentPath stringByAppendingPathComponent: @" Coding_StartImages" ] stringByAppendingPathComponent: [response suggestedFilename ]];
214+ return [NSURL fileURLWithPath: pathDisk];
215+ } completionHandler: ^(NSURLResponse *response, NSURL *filePath, NSError *error) {
216+ NSLog (@" downloaded file_path is to: %@ " , filePath);
217+ }];
218+ [downloadTask resume ];
219+ }
220+ @end
221+
222+ @implementation Group
223+
224+
225+
120226@end
0 commit comments