PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ReadBook 4/Classes/CharpterController.m

http://read-book.googlecode.com/
Objective C | 393 lines | 311 code | 49 blank | 33 comment | 44 complexity | 4f6e8f37ed1d0fbcb353613352b5fc60 MD5 | raw file
  1. //
  2. // CharpterController.m
  3. // ReadBook
  4. //
  5. // Created by xu jiapan on 11-1-11.
  6. // Copyright 2011 hangzhou. All rights reserved.
  7. //
  8. #import "CharpterController.h"
  9. #define CONNECT_ID_getChapterList @"getChapterList"
  10. @implementation CharpterController
  11. @synthesize charpaterimage;
  12. @synthesize charpaterimageView;
  13. @synthesize myDatas;
  14. @synthesize scrollView;
  15. @synthesize getstureStartPoint;
  16. @synthesize onlineReadData;
  17. // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil _contentID:(NSString*)cid{
  19. if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
  20. // Custom initialization
  21. contentId = cid;
  22. charpaterimageView = [[UIImageView alloc] init];
  23. charpaterimageView.userInteractionEnabled = YES;
  24. self.navigationController.title = @"??";
  25. charpaterIndex = 1;
  26. ConnectServerControl *csc = [[ConnectServerControl alloc] init];
  27. NSMutableDictionary *parameterDict1 = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
  28. [parameterDict1 setObject:cid forKey:@"contentId"];
  29. Request *req1 = [[Request alloc]initWithAction:CONNECT_ID_getChapterList
  30. parameterDict:parameterDict1
  31. requestMethod:@"GET"];
  32. [csc getResponseWithRequest:req1.request
  33. delegate:self
  34. connectId:CONNECT_ID_getChapterList];
  35. [req1 release];
  36. [csc release];
  37. }
  38. return self;
  39. }
  40. -(void)viewWillDisappear:(BOOL)animated{
  41. [super viewWillDisappear:animated];
  42. //[SCAppUtils removeCustomizeNavigationController:self.navigationController];
  43. }
  44. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. //add navBar backgroundImage
  48. [SCAppUtils customizeNavigationController:self.navigationController];
  49. [self.view addSubview:charpaterimageView];
  50. }
  51. - (void)loadContentView:(BOOL)indicator{
  52. for (UIView *view in [charpaterimageView subviews]) {
  53. [view removeFromSuperview]; //?????
  54. }
  55. NSUInteger i; //????
  56. NSUInteger total;
  57. float x, y;
  58. BOOL bType = NO; //??:YES ?? NO ??
  59. VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:0];
  60. NSUInteger count = [volumnInfo.ChapterInfoList count];
  61. if (count == 0) {
  62. count = [myDatas.VolumnInfoList count];
  63. bType = YES;
  64. }
  65. if (indicator) {
  66. x = 15.f;
  67. y = 40.f;
  68. if (count > 22) {
  69. if(charpaterIndex * 22 > count) {
  70. total = count;
  71. } else {
  72. total = 22;
  73. }
  74. } else {
  75. total = count;
  76. }
  77. i = (charpaterIndex - 1) * 22;
  78. for (i; i < total; i++) {
  79. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  80. button.frame = CGRectMake(x, y, 30.f, 400.f);
  81. //button.alpha = 0.1;
  82. UITextView *charpaterLabel = [[UITextView alloc] initWithFrame:CGRectMake(x, y, 30.f, 350.f)];
  83. charpaterLabel.textColor = [UIColor blackColor];
  84. charpaterLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:20.f];
  85. charpaterLabel.backgroundColor = [UIColor clearColor];
  86. //charpaterLabel.lineBreakMode = UILineBreakModeHeadTruncation;
  87. charpaterLabel.contentMode = UIViewContentModeTop;
  88. //charpaterLabel.numberOfLines = 0;
  89. if (bType) {
  90. VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:i];
  91. charpaterLabel.text = volumnInfo.volumnName;
  92. button.tag = [volumnInfo.volumnID intValue];
  93. }else {
  94. ChapterInfo *charpterInfo = [volumnInfo.ChapterInfoList objectAtIndex:i];
  95. charpaterLabel.text = charpterInfo.chapterName;
  96. button.tag = [charpterInfo.chapterID intValue];
  97. }
  98. [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
  99. [charpaterimageView addSubview:charpaterLabel];
  100. [charpaterimageView addSubview:button];
  101. [charpaterLabel release];
  102. if(x > 600.f) {
  103. x = 15.f;
  104. y = 550.f;
  105. }else {
  106. x += 60.f;
  107. }
  108. }
  109. }else {
  110. x = 15.f;
  111. y = 40.f;
  112. if (count > 15) {
  113. if(charpaterIndex * 15 > count) {
  114. total = count;
  115. } else {
  116. total = 15;
  117. }
  118. }else {
  119. total = count;
  120. }
  121. i = (charpaterIndex - 1) * 15;
  122. for (i; i < total; i++) {
  123. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  124. button.frame = CGRectMake(x, y, 30.f, 400.f);
  125. UITextView *charpaterLabel = [[UITextView alloc] initWithFrame:CGRectMake(x, y, 30.f, 600.f)];
  126. charpaterLabel.textColor = [UIColor blackColor];
  127. charpaterLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:20.f];
  128. charpaterLabel.backgroundColor = [UIColor clearColor];
  129. //charpaterLabel.lineBreakMode = UILineBreakModeHeadTruncation;
  130. //charpaterLabel.numberOfLines = 0;
  131. if (bType) {
  132. VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:i];
  133. charpaterLabel.text = volumnInfo.volumnName;
  134. button.tag = [volumnInfo.volumnID intValue];
  135. }else {
  136. ChapterInfo *charpterInfo = [volumnInfo.ChapterInfoList objectAtIndex:i];
  137. charpaterLabel.text = charpterInfo.chapterName;
  138. button.tag = [charpterInfo.chapterID intValue];
  139. }
  140. [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
  141. [charpaterimageView addSubview:charpaterLabel];
  142. [charpaterimageView addSubview:button];
  143. [charpaterLabel release];
  144. x += 60.f;
  145. }
  146. //pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(charpaterimageView.frame.size.width - 100.0f , 0.0f, 100.0f, 20.0f)];
  147. // pageControl.numberOfPages = 2;
  148. // [charpaterimageView addSubview:pageControl];
  149. // [self.view addSubview:charpaterimageView];
  150. }
  151. }
  152. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  153. // Overriden to allow any orientation.
  154. if (interfaceOrientation == UIInterfaceOrientationPortrait
  155. || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
  156. charpaterimage = [UIImage imageNamed:@"Vertical.png"];
  157. charpaterimageView.image = charpaterimage;
  158. charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);
  159. [self loadContentView:YES];
  160. }
  161. else {
  162. charpaterimage = [UIImage imageNamed:@"Horizontal.png"];
  163. charpaterimageView.image = charpaterimage;
  164. charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
  165. [self loadContentView:NO];
  166. }
  167. return YES;
  168. }
  169. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
  170. if (interfaceOrientation == UIInterfaceOrientationPortrait
  171. || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
  172. charpaterimage = [UIImage imageNamed:@"Vertical.png"];
  173. charpaterimageView.image = charpaterimage;
  174. charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);
  175. [self loadContentView:YES];
  176. }
  177. else {
  178. charpaterimage = [UIImage imageNamed:@"Horizontal.png"];
  179. charpaterimageView.image = charpaterimage;
  180. charpaterimageView.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
  181. [self loadContentView:NO];
  182. }
  183. }
  184. - (void)didReceiveMemoryWarning {
  185. // Releases the view if it doesn't have a superview.
  186. [super didReceiveMemoryWarning];
  187. // Release any cached data, images, etc that aren't in use.
  188. }
  189. - (void)viewDidUnload {
  190. self.charpaterimageView = nil;
  191. [super viewDidUnload];
  192. // Release any retained subviews of the main view.
  193. // e.g. self.myOutlet = nil;
  194. }
  195. #pragma mark -
  196. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  197. UITouch *touch = [touches anyObject];
  198. getstureStartPoint = [touch locationInView:self.view];
  199. }
  200. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  201. }
  202. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  203. UITouch *touch = [touches anyObject];
  204. CGPoint currentPosition = [touch locationInView:self.view];
  205. CGFloat deltaX = fabsf(getstureStartPoint.x - currentPosition.x);
  206. if (deltaX >= kMinimumGestureLength) {
  207. NSUInteger pageTotal = [self getTotalPageCount];
  208. if (getstureStartPoint.x > currentPosition.x) {
  209. charpaterIndex++;
  210. if (charpaterIndex > pageTotal) {
  211. charpaterIndex = pageTotal;
  212. } else {
  213. CATransition *animation = [CATransition animation];
  214. [animation setDuration:0.3f];
  215. [animation setType:kCATransitionPush];
  216. [animation setSubtype:kCATransitionFromRight];
  217. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  218. [[charpaterimageView layer] addAnimation:animation forKey:@"switchView"];
  219. }
  220. }else {
  221. charpaterIndex--;
  222. if (charpaterIndex < 1) {
  223. charpaterIndex = 1;
  224. }else {
  225. CATransition *animation = [CATransition animation];
  226. [animation setDuration:0.3f];
  227. [animation setType:kCATransitionPush];
  228. [animation setSubtype:kCATransitionFromLeft];
  229. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  230. [[charpaterimageView layer] addAnimation:animation forKey:@"switchView"];
  231. }
  232. }
  233. BOOL bTag;
  234. if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
  235. bTag = NO;
  236. } else {
  237. bTag = YES;
  238. }
  239. [self loadContentView:bTag];
  240. }
  241. }
  242. - (NSUInteger)getTotalPageCount {
  243. VolumnInfo *volumnInfo = [myDatas.VolumnInfoList objectAtIndex:0];
  244. NSUInteger count = [volumnInfo.ChapterInfoList count];
  245. if (count == 0) {
  246. count = [myDatas.VolumnInfoList count];
  247. }
  248. NSUInteger pageOneCount; //??????????
  249. if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
  250. pageOneCount = 15;
  251. } else {
  252. pageOneCount = 22;
  253. }
  254. return count % pageOneCount == 0 ? count / pageOneCount : count / pageOneCount + 1;
  255. }
  256. //????
  257. - (void)dismissSelf{
  258. [self dismissModalViewControllerAnimated:YES];
  259. }
  260. - (void)buttonPress:(id)sender {
  261. UIButton *button = (UIButton*)sender;
  262. [self getOnlineReadData:button.tag];
  263. }
  264. - (void)getOnlineReadData:(int)chapId{
  265. NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease] ;
  266. [dict setObject:contentId forKey:@"contentId"];
  267. [dict setObject:[NSNumber numberWithInt:chapId] forKey:@"chapterId"];
  268. ConnectServerControl *connectServerControl = [[ConnectServerControl alloc] init];
  269. Request *req = [[Request alloc]initWithAction:@"getChapterInfo" parameterDict:dict requestMethod:@"GET"];
  270. [connectServerControl getResponseWithRequest:req.request
  271. delegate:self
  272. connectId:[[NSString alloc]initWithFormat:@"%d",chapId]];
  273. [req release];
  274. [connectServerControl release];
  275. }
  276. - (void)requestFinished:(ASIHTTPRequest *)request cid:(NSString *)_connectId{
  277. if ([_connectId isEqualToString:CONNECT_ID_getChapterList]) {
  278. getChapterList *getChapterListxmlPaser = [[getChapterList alloc] init];
  279. [getChapterListxmlPaser parseXMLAtData:[request rawResponseData] parseError:nil];
  280. self.myDatas = getChapterListxmlPaser.datasource;
  281. [getChapterListxmlPaser release];
  282. }else {
  283. GetchapterInfo *xmlPaser = [[GetchapterInfo alloc] init];
  284. [xmlPaser parseXMLAtData:[request rawResponseData] parseError:nil];
  285. onlineReadData = xmlPaser.datasource;
  286. if (!onlineReadData) {
  287. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n"
  288. message:@"????????"
  289. delegate:self
  290. cancelButtonTitle:@"??"
  291. otherButtonTitles:nil, nil];
  292. [alert show];
  293. [alert release];
  294. }else {
  295. //TODO 1 ????????????
  296. OnlineContentParser *contentXmlParser = [[OnlineContentParser alloc] init];
  297. [contentXmlParser parseXMLAtString:((getchapter_chapterinfo *)onlineReadData).content parseError:nil];
  298. NSLog(@"contentXmlParser.title = %@",contentXmlParser.title);
  299. NSLog(@"contentXmlParser.content = %@",contentXmlParser.content);
  300. NSLog(@"contentXmlParser.imageUrl = %@",contentXmlParser.imageUrl);
  301. NSFileManager *fileManager = [NSFileManager defaultManager];
  302. //?????????????????????
  303. NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  304. NSString *tempPath = [NSString stringWithFormat:@"%@/readcache/%@",[dirArray objectAtIndex:0],contentId];
  305. if (![fileManager fileExistsAtPath:tempPath]) {
  306. [fileManager createDirectoryAtPath:tempPath withIntermediateDirectories:YES attributes:nil error:nil];
  307. }
  308. NSString *extFile = (contentXmlParser.imageUrl?@"jpg":@"txt");
  309. NSString *filepath = [NSString stringWithFormat:@"%@/readcache/%@/%@.%@",[dirArray objectAtIndex:0],contentId,_connectId,extFile];
  310. //???????????
  311. if (![fileManager fileExistsAtPath:filepath]) {
  312. //????????????
  313. if (contentXmlParser.title) {
  314. [[NSString stringWithString:contentXmlParser.content] writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  315. }else {
  316. //????????
  317. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:contentXmlParser.imageUrl]];
  318. [request setDownloadDestinationPath:filepath];
  319. [request startSynchronous];
  320. }
  321. }
  322. [contentXmlParser release];
  323. //????????????
  324. LeavesViewController *viewController = [[LeavesViewController alloc]initWithNibName:@"LeavesViewController"
  325. bundle:[NSBundle mainBundle]
  326. contentId:contentId
  327. contentName:@""
  328. chapterId:_connectId
  329. position:0
  330. readmode:ReadModeOnLine];
  331. viewController.hidesBottomBarWhenPushed = YES;
  332. [self.navigationController pushViewController:viewController animated:NO];
  333. [viewController release];
  334. }
  335. [xmlPaser release];
  336. }
  337. }
  338. - (void)dealloc {
  339. [onlineReadData release];
  340. [myDatas release];
  341. [charpaterimageView release];
  342. [super dealloc];
  343. }
  344. @end