/ReadBook 4/Classes/CharpterController.m
Objective C | 393 lines | 311 code | 49 blank | 33 comment | 44 complexity | 4f6e8f37ed1d0fbcb353613352b5fc60 MD5 | raw file
- //
- // CharpterController.m
- // ReadBook
- //
- // Created by xu jiapan on 11-1-11.
- // Copyright 2011 hangzhou. All rights reserved.
- //
- #import "CharpterController.h"
- #define CONNECT_ID_getChapterList @"getChapterList"
- @implementation CharpterController
- @synthesize charpaterimage;
- @synthesize charpaterimageView;
- @synthesize myDatas;
- @synthesize scrollView;
- @synthesize getstureStartPoint;
- @synthesize onlineReadData;
- // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil _contentID:(NSString*)cid{
- if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
- // Custom initialization
- contentId = cid;
- charpaterimageView = [[UIImageView alloc] init];
- charpaterimageView.userInteractionEnabled = YES;
- self.navigationController.title = @"??";
- charpaterIndex = 1;
- ConnectServerControl *csc = [[ConnectServerControl alloc] init];
- NSMutableDictionary *parameterDict1 = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
- [parameterDict1 setObject:cid forKey:@"contentId"];
- Request *req1 = [[Request alloc]initWithAction:CONNECT_ID_getChapterList
- parameterDict:parameterDict1
- requestMethod:@"GET"];
- [csc getResponseWithRequest:req1.request
- delegate:self
- connectId:CONNECT_ID_getChapterList];
- [req1 release];
- [csc release];
- }
- return self;
- }
- -(void)viewWillDisappear:(BOOL)animated{
- [super viewWillDisappear:animated];
- //[SCAppUtils removeCustomizeNavigationController:self.navigationController];
- }
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- //add navBar backgroundImage
- [SCAppUtils customizeNavigationController:self.navigationController];
- [self.view addSubview:charpaterimageView];
- }
- - (void)loadContentView:(BOOL)indicator{
-
- for (UIView *view in [charpaterimageView subviews]) {
- [view removeFromSuperview]; //?????
- }
-
- NSUInteger i; //????
- NSUInteger total;
- float x, y;
- BOOL bType = NO; //??:YES ?? NO ??
- VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:0];
- NSUInteger count = [volumnInfo.ChapterInfoList count];
- if (count == 0) {
- count = [myDatas.VolumnInfoList count];
- bType = YES;
- }
- if (indicator) {
- x = 15.f;
- y = 40.f;
-
- if (count > 22) {
- if(charpaterIndex * 22 > count) {
- total = count;
- } else {
- total = 22;
- }
- } else {
- total = count;
- }
-
-
- i = (charpaterIndex - 1) * 22;
- for (i; i < total; i++) {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(x, y, 30.f, 400.f);
- //button.alpha = 0.1;
- UITextView *charpaterLabel = [[UITextView alloc] initWithFrame:CGRectMake(x, y, 30.f, 350.f)];
- charpaterLabel.textColor = [UIColor blackColor];
- charpaterLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:20.f];
- charpaterLabel.backgroundColor = [UIColor clearColor];
- //charpaterLabel.lineBreakMode = UILineBreakModeHeadTruncation;
- charpaterLabel.contentMode = UIViewContentModeTop;
- //charpaterLabel.numberOfLines = 0;
- if (bType) {
- VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:i];
- charpaterLabel.text = volumnInfo.volumnName;
- button.tag = [volumnInfo.volumnID intValue];
- }else {
- ChapterInfo *charpterInfo = [volumnInfo.ChapterInfoList objectAtIndex:i];
- charpaterLabel.text = charpterInfo.chapterName;
- button.tag = [charpterInfo.chapterID intValue];
- }
- [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
- [charpaterimageView addSubview:charpaterLabel];
- [charpaterimageView addSubview:button];
- [charpaterLabel release];
- if(x > 600.f) {
- x = 15.f;
- y = 550.f;
- }else {
- x += 60.f;
- }
- }
- }else {
- x = 15.f;
- y = 40.f;
- if (count > 15) {
- if(charpaterIndex * 15 > count) {
- total = count;
- } else {
- total = 15;
- }
- }else {
- total = count;
- }
-
- i = (charpaterIndex - 1) * 15;
- for (i; i < total; i++) {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(x, y, 30.f, 400.f);
- UITextView *charpaterLabel = [[UITextView alloc] initWithFrame:CGRectMake(x, y, 30.f, 600.f)];
- charpaterLabel.textColor = [UIColor blackColor];
- charpaterLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:20.f];
- charpaterLabel.backgroundColor = [UIColor clearColor];
- //charpaterLabel.lineBreakMode = UILineBreakModeHeadTruncation;
- //charpaterLabel.numberOfLines = 0;
- if (bType) {
- VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:i];
- charpaterLabel.text = volumnInfo.volumnName;
- button.tag = [volumnInfo.volumnID intValue];
- }else {
- ChapterInfo *charpterInfo = [volumnInfo.ChapterInfoList objectAtIndex:i];
- charpaterLabel.text = charpterInfo.chapterName;
- button.tag = [charpterInfo.chapterID intValue];
- }
- [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
- [charpaterimageView addSubview:charpaterLabel];
- [charpaterimageView addSubview:button];
- [charpaterLabel release];
- x += 60.f;
-
- }
- //pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(charpaterimageView.frame.size.width - 100.0f , 0.0f, 100.0f, 20.0f)];
- // pageControl.numberOfPages = 2;
- // [charpaterimageView addSubview:pageControl];
- // [self.view addSubview:charpaterimageView];
- }
-
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Overriden to allow any orientation.
- if (interfaceOrientation == UIInterfaceOrientationPortrait
- || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
- charpaterimage = [UIImage imageNamed:@"Vertical.png"];
- charpaterimageView.image = charpaterimage;
- charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);
- [self loadContentView:YES];
-
- }
- else {
- charpaterimage = [UIImage imageNamed:@"Horizontal.png"];
- charpaterimageView.image = charpaterimage;
- charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
- [self loadContentView:NO];
- }
-
- return YES;
- }
- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
- if (interfaceOrientation == UIInterfaceOrientationPortrait
- || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
- charpaterimage = [UIImage imageNamed:@"Vertical.png"];
- charpaterimageView.image = charpaterimage;
- charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);
- [self loadContentView:YES];
- }
- else {
- charpaterimage = [UIImage imageNamed:@"Horizontal.png"];
- charpaterimageView.image = charpaterimage;
- charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
- [self loadContentView:NO];
- }
- }
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
-
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- self.charpaterimageView = nil;
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- #pragma mark -
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch *touch = [touches anyObject];
- getstureStartPoint = [touch locationInView:self.view];
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- }
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch *touch = [touches anyObject];
- CGPoint currentPosition = [touch locationInView:self.view];
- CGFloat deltaX = fabsf(getstureStartPoint.x - currentPosition.x);
- if (deltaX >= kMinimumGestureLength) {
- NSUInteger pageTotal = [self getTotalPageCount];
- if (getstureStartPoint.x > currentPosition.x) {
- charpaterIndex++;
- if (charpaterIndex > pageTotal) {
- charpaterIndex = pageTotal;
- } else {
- CATransition *animation = [CATransition animation];
- [animation setDuration:0.3f];
- [animation setType:kCATransitionPush];
- [animation setSubtype:kCATransitionFromRight];
- [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- [[charpaterimageView layer] addAnimation:animation forKey:@"switchView"];
- }
- }else {
- charpaterIndex--;
- if (charpaterIndex < 1) {
- charpaterIndex = 1;
- }else {
- CATransition *animation = [CATransition animation];
- [animation setDuration:0.3f];
- [animation setType:kCATransitionPush];
- [animation setSubtype:kCATransitionFromLeft];
- [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- [[charpaterimageView layer] addAnimation:animation forKey:@"switchView"];
- }
- }
- BOOL bTag;
- if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
- bTag = NO;
- } else {
- bTag = YES;
- }
- [self loadContentView:bTag];
- }
- }
- - (NSUInteger)getTotalPageCount {
- VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:0];
- NSUInteger count = [volumnInfo.ChapterInfoList count];
- if (count == 0) {
- count = [myDatas.VolumnInfoList count];
- }
- NSUInteger pageOneCount; //??????????
- if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
- pageOneCount = 15;
- } else {
- pageOneCount = 22;
- }
-
- return count % pageOneCount == 0 ? count / pageOneCount : count / pageOneCount + 1;
- }
- //????
- - (void)dismissSelf{
- [self dismissModalViewControllerAnimated:YES];
- }
- - (void)buttonPress:(id)sender {
- UIButton *button = (UIButton*)sender;
- [self getOnlineReadData:button.tag];
- }
- - (void)getOnlineReadData:(int)chapId{
- NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease] ;
- [dict setObject:contentId forKey:@"contentId"];
- [dict setObject:[NSNumber numberWithInt:chapId] forKey:@"chapterId"];
- ConnectServerControl *connectServerControl = [[ConnectServerControl alloc] init];
- Request *req = [[Request alloc]initWithAction:@"getChapterInfo" parameterDict:dict requestMethod:@"GET"];
- [connectServerControl getResponseWithRequest:req.request
- delegate:self
- connectId:[[NSString alloc]initWithFormat:@"%d",chapId]];
- [req release];
- [connectServerControl release];
- }
- - (void)requestFinished:(ASIHTTPRequest *)request cid:(NSString *)_connectId{
- if ([_connectId isEqualToString:CONNECT_ID_getChapterList]) {
- getChapterList *getChapterListxmlPaser = [[getChapterList alloc] init];
- [getChapterListxmlPaser parseXMLAtData:[request rawResponseData] parseError:nil];
- self.myDatas = getChapterListxmlPaser.datasource;
- [getChapterListxmlPaser release];
- }else {
- GetchapterInfo *xmlPaser = [[GetchapterInfo alloc] init];
- [xmlPaser parseXMLAtData:[request rawResponseData] parseError:nil];
- onlineReadData = xmlPaser.datasource;
- if (!onlineReadData) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n"
- message:@"????????"
- delegate:self
- cancelButtonTitle:@"??"
- otherButtonTitles:nil, nil];
- [alert show];
- [alert release];
- }else {
- //TODO 1 ????????????
- OnlineContentParser *contentXmlParser = [[OnlineContentParser alloc] init];
- [contentXmlParser parseXMLAtString:((getchapter_chapterinfo *)onlineReadData).content parseError:nil];
-
- NSLog(@"contentXmlParser.title = %@",contentXmlParser.title);
- NSLog(@"contentXmlParser.content = %@",contentXmlParser.content);
- NSLog(@"contentXmlParser.imageUrl = %@",contentXmlParser.imageUrl);
-
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
-
- //?????????????????????
- NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
- NSString *tempPath = [NSString stringWithFormat:@"%@/readcache/%@",[dirArray objectAtIndex:0],contentId];
- if (![fileManager fileExistsAtPath:tempPath]) {
- [fileManager createDirectoryAtPath:tempPath withIntermediateDirectories:YES attributes:nil error:nil];
- }
- NSString *extFile = (contentXmlParser.imageUrl?@"jpg":@"txt");
- NSString *filepath = [NSString stringWithFormat:@"%@/readcache/%@/%@.%@",[dirArray objectAtIndex:0],contentId,_connectId,extFile];
-
- //???????????
- if (![fileManager fileExistsAtPath:filepath]) {
- //????????????
- if (contentXmlParser.title) {
- [[NSString stringWithString:contentXmlParser.content] writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- }else {
- //????????
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:contentXmlParser.imageUrl]];
- [request setDownloadDestinationPath:filepath];
- [request startSynchronous];
- }
- }
- [contentXmlParser release];
-
- //????????????
- LeavesViewController *viewController = [[LeavesViewController alloc]initWithNibName:@"LeavesViewController"
- bundle:[NSBundle mainBundle]
- contentId:contentId
- contentName:@""
- chapterId:_connectId
- position:0
- readmode:ReadModeOnLine];
- viewController.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:viewController animated:NO];
- [viewController release];
- }
- [xmlPaser release];
- }
- }
- - (void)dealloc {
- [onlineReadData release];
- [myDatas release];
- [charpaterimageView release];
- [super dealloc];
- }
- @end