/
wenku8VC.m
executable file
·168 lines (164 loc) · 7.18 KB
/
wenku8VC.m
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
//
// ViewController.m
// wenku8Reader
//
// Created by wbuntu on 15/4/11.
// Copyright (c) 2015年 wbuntu. All rights reserved.
//
#import "wenku8VC.h"
#import "ShelfTVCCellCollectionViewCell.h"
#import "bookElement.h"
#import "articleViewController.h"
#import "ExrtaButton.h"
#import "MoreVC.h"
#import "CAWBook.h"
@interface wenku8VC()
@property(nonatomic,strong) NSMutableArray *array;
@property(nonatomic,strong) NSArray *titleArray;
@property(nonatomic,strong) UIActivityIndicatorView *indicator;
@property(nonatomic,strong) NSMutableDictionary *imageDic;
@end
@implementation wenku8VC
static NSString *identifier = @"cell";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// [self.view setBackgroundColor:[UIColor colorWithRed:245 green:245 blue:245 alpha:1]];
_indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame)-40, CGRectGetMidY(self.view.frame)-40, 80, 80)];
_indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:_indicator];
[_indicator startAnimating];
_titleArray = @[@"今日热榜",@"最近更新",@"新书榜单",@"每周推荐"];
_imageDic = [NSMutableDictionary dictionary];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self getWenkuIndex];
});
self.title = @"Wenku8";
}
-(void)getWenkuIndex
{
[[CAWNetwork defaultManager] dataTaskWithString:CAWFirstPageUrl completionHandler:^(NSData *data) {
NSData *received = data;
NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingAllowFragments error:nil];
NSDictionary *tempDic = [rootDic objectForKey:@"data"];
_array = [NSMutableArray arrayWithArray:[tempDic allValues]];
dispatch_async(dispatch_get_main_queue(), ^{
[_indicator stopAnimating];
[self.collectionView reloadData];
});
}];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSArray *arr = [_array objectAtIndex:section];
return arr.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return _array.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ShelfTVCCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSArray *arr = [_array objectAtIndex:indexPath.section];
CAWBook *book = [[CAWBook alloc] initWithDictionary:arr[indexPath.row]];
cell.title.text = book.title;
cell.cover.image = [_imageDic objectForKey:book.bookId];
if (cell.cover.image==nil) {
int bookId = [book.bookId intValue];
NSString *path =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"cover/%ds.jpg",bookId]];
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:path]) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *im = [UIImage imageWithContentsOfFile:path];
//opaque:NO 不透明
UIGraphicsBeginImageContextWithOptions(CGSizeMake(78, 117), NO, 0.0);
[im drawInRect:CGRectMake(0, 0, 78, 117)];
UIImage *other = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
cell.cover.image = other;
[_imageDic setObject:other forKey:book.bookId];
});
});
}
else
{
NSString *bookImage = [NSString stringWithFormat:CAWImageUrl,bookId];
[[CAWNetwork defaultManager] dataTaskWithString:bookImage completionHandler:^(NSData *data) {
[data writeToFile:path atomically:YES];
UIImage *im = [UIImage imageWithContentsOfFile:path];
//opaque:NO 不透明
UIGraphicsBeginImageContextWithOptions(CGSizeMake(78, 117), NO, 0.0);
[im drawInRect:CGRectMake(0, 0, 78, 117)];
UIImage *other = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
cell.cover.image = other;
[_imageDic setObject:other forKey:book.bookId];
});
}];
}
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view = nil;
if (kind == UICollectionElementKindSectionHeader) {
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
UILabel *lable = (UILabel*)[view viewWithTag:2];
[lable setText:[_titleArray objectAtIndex:indexPath.section]];
ExrtaButton *button =(ExrtaButton*)[view viewWithTag:3];
button.section = indexPath.section;
if (indexPath.section==3)
[button setHidden:YES];
else
{
[button setHidden:NO];
[button addTarget:self action:@selector(pushToSearchWithSection:) forControlEvents:UIControlEventTouchUpInside];
}
}
return view;
}
-(void)pushToSearchWithSection:(ExrtaButton*)sender
{
//NSLog(@"%d",sender.section);
NSString *urlString;
switch (sender.section)
{
case 0:
urlString = [CAWPageSectionUrl stringByAppendingString:@"sort=0&page=%d"];
break;
case 1:
urlString = [CAWPageSectionUrl stringByAppendingString:@"sort=1&page=%d"];
break;
case 2:
urlString = [CAWPageSectionUrl stringByAppendingString:@"sort=2&page=%d"];
break;
default:
break;
}
MoreVC *vc = [[MoreVC alloc] initWithStyle:UITableViewStylePlain UrlString:urlString AndTitle:[_titleArray objectAtIndex:sender.section]];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"articleInfo"]) {
articleViewController *vc = segue.destinationViewController;
ShelfTVCCellCollectionViewCell *cell = sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSArray *arr = [_array objectAtIndex:indexPath.section];
CAWBook *tempBook = [[CAWBook alloc] initWithDictionary:arr[indexPath.row]];
bookElement *book = [[bookElement alloc] init];
book.bookId = tempBook.bookId.intValue;
book.bookTitle = tempBook.title;
[vc setWithObject:book];
}
}
@end