Skip to content

Commit 1956472

Browse files
committed
done
1 parent 0aa0963 commit 1956472

5 files changed

Lines changed: 153 additions & 33 deletions

File tree

Coding_iOS/AppDelegate.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "AppDelegate.h"
1616
#import "RootTabViewController.h"
1717
#import "LoginViewController.h"
18+
#import "AFNetworking.h"
1819
#import "AFNetworkActivityIndicatorManager.h"
1920
#import "Login.h"
2021
#import "UnReadManager.h"
@@ -52,8 +53,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5253
// Override point for customization after application launch.
5354
self.window.backgroundColor = [UIColor whiteColor];
5455

55-
//网络指示标志
56+
//网络
5657
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
58+
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
5759

5860
//设置导航条样式
5961
[self customizeInterface];

Coding_iOS/Util/Manager/Coding_FileManager.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ - (instancetype)init
9797

9898

9999
+ (NSString *)downloadPath{
100-
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
100+
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
101101
NSString *downloadPath = [documentPath stringByAppendingPathComponent:@"Coding_Download"];
102102
return downloadPath;
103103
}
104104

105105
+ (NSString *)uploadPath{
106-
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
106+
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
107107
NSString *uploadPath = [documentPath stringByAppendingPathComponent:@"Coding_Upload"];
108108
return uploadPath;
109109
}

Coding_iOS/Util/Manager/StartImagesManager.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,29 @@
99
#import <Foundation/Foundation.h>
1010

1111
@class StartImage;
12+
@class Group;
1213

1314
@interface StartImagesManager : NSObject
1415
+ (instancetype)shareManager;
1516

1617
- (StartImage *)randomImage;
1718
- (StartImage *)curImage;
1819

20+
- (void)refreshImagesPlist;
21+
- (void)startDownloadImages;
22+
1923
@end
2024

2125
@interface StartImage : NSObject
22-
@property (strong, nonatomic) NSString *fileName, *pathDisk, *descriptionStr;
23-
@property (assign, nonatomic) BOOL hasBeenDownload;
24-
- (UIImage *)image;
26+
@property (strong, nonatomic) NSString *url;
27+
@property (strong, nonatomic) Group *group;
28+
@property (strong, nonatomic) NSString *fileName, *descriptionStr, *pathDisk;
29+
2530
+ (StartImage *)defautImage;
26-
+ (StartImage *)stFromDict:(NSDictionary *)dict;
31+
- (UIImage *)image;
32+
- (void)startDownloadImage;
33+
@end
34+
35+
@interface Group : NSObject
36+
@property (strong, nonatomic) NSString *name, *author;
2737
@end

Coding_iOS/Util/Manager/StartImagesManager.m

Lines changed: 131 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
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

Coding_iOS/Views/EaseStartView.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,18 @@ - (void)startAnimationWithCompletionBlock:(void(^)(EaseStartView *easeStartView)
9494
_bgImageView.alpha = 0.0;
9595
_logoIconView.alpha = 1.0;
9696
_descriptionStrLabel.alpha = 0.0;
97+
self.alpha = 1.0;
9798

9899
@weakify(self);
100+
99101
[UIView animateWithDuration:2.0 animations:^{
100102
@strongify(self);
101103
self.bgImageView.alpha = 1.0;
102104
[self.bgImageView setFrame:CGRectMake(-kScreen_Width/20, -kScreen_Height/20, 1.1*kScreen_Width, 1.1*kScreen_Height)];
103105
self.descriptionStrLabel.alpha = 1.0;
104106
} completion:^(BOOL finished) {
105107
self.backgroundColor = [UIColor clearColor];
106-
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
108+
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
107109
@strongify(self);
108110
self.bgImageView.alpha = 0.0;
109111
self.logoIconView.alpha = 0.0;

0 commit comments

Comments
 (0)