RAViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //The MIT License (MIT)
  2. //
  3. //Copyright (c) 2013 Rafał Augustyniak
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //this software and associated documentation files (the "Software"), to deal in
  7. //the Software without restriction, including without limitation the rights to
  8. //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. //the Software, and to permit persons to whom the Software is furnished to do so,
  10. //subject to the following conditions:
  11. //
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. //
  19. #import "RAViewController.h"
  20. #import "RATreeView.h"
  21. //#import "RADataObject.h"
  22. #import "AppDelegate.h"
  23. #import "MainViewController.h"
  24. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  25. @interface RAViewController () <RATreeViewDelegate, RATreeViewDataSource,UIGestureRecognizerDelegate>
  26. //@property (strong, nonatomic) NSArray *data;
  27. @property (strong,nonatomic)NSDictionary* categoryMenu;
  28. @property (strong, nonatomic) id expanded;
  29. @property (weak, nonatomic) RATreeView *treeView;
  30. @end
  31. @implementation RAViewController
  32. #ifndef RA_NOTIFICATION
  33. -(void) reload_container_getdata:(bool) update_data
  34. {
  35. [super reload_container_getdata:update_data];
  36. // if(update_data)
  37. // [self reload_data];
  38. // else
  39. // {
  40. // [self.itemListTable reloadData];
  41. // [self refresh_total];
  42. // }
  43. if(update_data==false)
  44. return;
  45. UIApplication * app = [UIApplication sharedApplication];
  46. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  47. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  48. NSDictionary* category=[RANetwork request_Cagegory];
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. if([[category valueForKey:@"result"] intValue]==2)
  51. {
  52. appDelegate.categoryMenu = category;
  53. [self SetMenu:appDelegate.categoryMenu ];
  54. }
  55. else
  56. if([[category valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  57. {
  58. [self SetMenu:nil ];
  59. }
  60. else
  61. {
  62. [RAUtils message_alert:[category valueForKey:@"err_msg"] title:@"Load Category Menu" controller:self] ;
  63. }
  64. });
  65. });
  66. }
  67. #endif
  68. -(void) SetMenu:(NSDictionary*) menu
  69. {
  70. if(menu==nil)
  71. {
  72. self.label_net_err.hidden=false;
  73. self.treeView.hidden = true;
  74. }
  75. else
  76. {
  77. self.label_net_err.hidden=true;
  78. self.treeView.hidden = false;
  79. }
  80. self.categoryMenu = menu;
  81. [self.treeView reloadData];
  82. }
  83. - (IBAction)CloseClick:(id)sender {
  84. [self dismissViewControllerAnimated:true completion:nil];
  85. }
  86. - (void)viewDidLoad
  87. {
  88. [super viewDidLoad];
  89. if(self.categoryMenu==nil)
  90. self.refresh_type = REFRESH_DATA;
  91. else
  92. self.refresh_type = REFRESH_VIEW;
  93. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  94. self.label_net_err.layer.borderWidth = 2.0;
  95. self.label_net_err.layer.cornerRadius=15;
  96. self.label_net_err.layer.masksToBounds=true;
  97. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  98. // tap.minimumPressDuration = 0.8; //定义按的时间
  99. [self.label_net_err addGestureRecognizer:tap];
  100. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  101. style:UIBarButtonItemStylePlain
  102. target:self
  103. action:@selector( CloseClick:)];
  104. self.navigationItem.rightBarButtonItem = closeButton;
  105. // CGRect frame = self.view.frame;
  106. // frame.origin.y = 200;//self.headerview.frame.size.height;
  107. // frame.size.height = frame.size.height - self.headerview.frame.size.height;
  108. RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.treeContainer.frame];
  109. treeView.delegate = self;
  110. treeView.dataSource = self;
  111. treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
  112. [treeView reloadData];
  113. // [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row
  114. [treeView setBackgroundColor: [UIColor whiteColor]];
  115. self.treeView = treeView;
  116. [self.treeContainer addSubview:treeView];
  117. // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
  118. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  119. // [app.window addGestureRecognizer:tapGesture];
  120. // tapGesture.delegate = self;
  121. // self.tapGesture = tapGesture;
  122. }
  123. //-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  124. //{
  125. // if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//如果当前是tableView
  126. // //做自己想做的事
  127. // return NO;
  128. // }
  129. // DebugLog(NSStringFromClass([touch.view class]));
  130. // return YES;
  131. //}
  132. //
  133. //-(void)handletapPressGesture:(UITapGestureRecognizer*)sender{
  134. // CGPoint point = [sender locationInView:self.view];
  135. // if (point.x<self.view.frame.origin.x || point.x >self.view.frame.origin.x+self.view.frame.size.width||point.y<self.view.frame.origin.y||point.y>self.view.frame.origin.y+self.view.frame.size.height) {
  136. // [self dismissViewControllerAnimated:YES
  137. // completion:^{
  138. // //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  139. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  140. // [app.window removeGestureRecognizer:sender];
  141. // }];
  142. // }
  143. //
  144. //}
  145. - (void)viewWillAppear:(BOOL)animated
  146. {
  147. [super viewWillAppear:animated];
  148. // if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
  149. // CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
  150. // float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;
  151. // self.treeView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
  152. // self.treeView.contentOffset = CGPointMake(0.0, -heightPadding);
  153. // }
  154. self.treeView.frame = self.treeContainer.bounds;
  155. }
  156. #pragma mark TreeView Delegate methods
  157. - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  158. {
  159. return 47;
  160. }
  161. - (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  162. {
  163. return 3 * treeNodeInfo.treeDepthLevel;
  164. }
  165. - (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  166. {
  167. return YES;
  168. }
  169. - (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
  170. {
  171. if ([item isEqual:self.expanded]) {
  172. return YES;
  173. }
  174. return NO;
  175. }
  176. - (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  177. {
  178. NSDictionary *data = item;
  179. int count = [[data valueForKey:@"count"] intValue];
  180. if(count==0)
  181. {
  182. NSString* category = [data valueForKey:@"id"];
  183. if (self.CateMenu_delegate && [self.CateMenu_delegate respondsToSelector:@selector(SelectCategory:)]) {
  184. [self.CateMenu_delegate SelectCategory:category];
  185. }
  186. /* MainViewController* pvc = (MainViewController*)self.rootViewController;
  187. [pvc switchToCagegory:category];*/
  188. [self dismissViewControllerAnimated:YES
  189. completion:^{
  190. //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  191. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  192. // [app.window removeGestureRecognizer:self.tapGesture];
  193. }];
  194. }
  195. // if (item == nil) {
  196. // NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%d",index] ];
  197. //
  198. // }
  199. // else
  200. // data=[data objectForKey:[NSString stringWithFormat:@"category_%d",index]];
  201. DebugLog(@"menu select %@",data);
  202. }
  203. - (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  204. {
  205. if (treeNodeInfo.treeDepthLevel == 0) {
  206. cell.backgroundColor = [UIColor whiteColor];
  207. } else if (treeNodeInfo.treeDepthLevel == 1) {
  208. cell.backgroundColor = UIColorFromRGB(0xD1EEFC);
  209. } else if (treeNodeInfo.treeDepthLevel == 2) {
  210. cell.backgroundColor = UIColorFromRGB(0xE0F8D8);
  211. }
  212. }
  213. #pragma mark TreeView Data Source
  214. - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  215. {
  216. // NSInteger numberOfChildren = [treeNodeInfo.children count];
  217. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  218. // if(numberOfChildren>0)
  219. // cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %@", [@(numberOfChildren) stringValue]];
  220. cell.textLabel.text = [((NSDictionary *)item) valueForKey:@"title"];
  221. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  222. if (treeNodeInfo.treeDepthLevel == 0) {
  223. cell.detailTextLabel.textColor = [UIColor blackColor];
  224. }
  225. return cell;
  226. }
  227. - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
  228. {
  229. if (item == nil) {
  230. return [[self.categoryMenu valueForKey:@"count" ] intValue];
  231. // return [self.data count];
  232. }
  233. NSDictionary* data = item;
  234. return [[data valueForKey:@"count"] intValue];
  235. // RADataObject *data = item;
  236. // return [data.children count];
  237. }
  238. - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
  239. {
  240. NSDictionary *data = item;
  241. if (item == nil) {
  242. NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index] ];
  243. return ret;
  244. }
  245. return[data objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index]];
  246. /*
  247. RADataObject *data = item;
  248. if (item == nil) {
  249. return [self.data objectAtIndex:index];
  250. }
  251. return [data.children objectAtIndex:index];
  252. */
  253. }
  254. - (void)reRefreshView {
  255. [self.treeView reloadData];
  256. }
  257. #pragma mark - RA_NOTIFICAITON
  258. -(void) refresh_ui
  259. {
  260. // [self.table reloadData];
  261. }
  262. -(void) reload_data
  263. {
  264. UIApplication * app = [UIApplication sharedApplication];
  265. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  266. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  267. NSDictionary* category=[RANetwork request_Cagegory];
  268. dispatch_async(dispatch_get_main_queue(), ^{
  269. if([[category valueForKey:@"result"] intValue]==2)
  270. {
  271. appDelegate.categoryMenu = category;
  272. [self SetMenu:appDelegate.categoryMenu ];
  273. }
  274. else
  275. if([[category valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  276. {
  277. [self SetMenu:nil ];
  278. }
  279. else
  280. {
  281. [RAUtils message_alert:[category valueForKey:@"err_msg"] title:@"Load Category Menu" controller:self] ;
  282. }
  283. });
  284. });
  285. }
  286. @end