RAViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. {
  91. self.notification_refreshLevel=NotificationRefreshLevelView;
  92. self.refresh_type = REFRESH_DATA;
  93. }
  94. else
  95. {
  96. self.notification_refreshLevel=NotificationRefreshLevelView;
  97. self.refresh_type = REFRESH_VIEW;
  98. }
  99. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  100. self.label_net_err.layer.borderWidth = 2.0;
  101. self.label_net_err.layer.cornerRadius=15;
  102. self.label_net_err.layer.masksToBounds=true;
  103. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  104. // tap.minimumPressDuration = 0.8; //定义按的时间
  105. [self.label_net_err addGestureRecognizer:tap];
  106. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  107. style:UIBarButtonItemStylePlain
  108. target:self
  109. action:@selector( CloseClick:)];
  110. self.navigationItem.rightBarButtonItem = closeButton;
  111. // CGRect frame = self.view.frame;
  112. // frame.origin.y = 200;//self.headerview.frame.size.height;
  113. // frame.size.height = frame.size.height - self.headerview.frame.size.height;
  114. RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.treeContainer.frame];
  115. treeView.delegate = self;
  116. treeView.dataSource = self;
  117. treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
  118. [treeView reloadData];
  119. // [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row
  120. [treeView setBackgroundColor: [UIColor whiteColor]];
  121. self.treeView = treeView;
  122. [self.treeContainer addSubview:treeView];
  123. // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
  124. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  125. // [app.window addGestureRecognizer:tapGesture];
  126. // tapGesture.delegate = self;
  127. // self.tapGesture = tapGesture;
  128. }
  129. //-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  130. //{
  131. // if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//如果当前是tableView
  132. // //做自己想做的事
  133. // return NO;
  134. // }
  135. // DebugLog(NSStringFromClass([touch.view class]));
  136. // return YES;
  137. //}
  138. //
  139. //-(void)handletapPressGesture:(UITapGestureRecognizer*)sender{
  140. // CGPoint point = [sender locationInView:self.view];
  141. // 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) {
  142. // [self dismissViewControllerAnimated:YES
  143. // completion:^{
  144. // //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  145. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  146. // [app.window removeGestureRecognizer:sender];
  147. // }];
  148. // }
  149. //
  150. //}
  151. - (void)viewWillAppear:(BOOL)animated
  152. {
  153. [super viewWillAppear:animated];
  154. // if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
  155. // CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
  156. // float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;
  157. // self.treeView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
  158. // self.treeView.contentOffset = CGPointMake(0.0, -heightPadding);
  159. // }
  160. self.treeView.frame = self.treeContainer.bounds;
  161. }
  162. #pragma mark TreeView Delegate methods
  163. - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  164. {
  165. return 47;
  166. }
  167. - (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  168. {
  169. return 3 * treeNodeInfo.treeDepthLevel;
  170. }
  171. - (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  172. {
  173. return YES;
  174. }
  175. - (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
  176. {
  177. if ([item isEqual:self.expanded]) {
  178. return YES;
  179. }
  180. return NO;
  181. }
  182. - (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  183. {
  184. NSDictionary *data = item;
  185. int count = [[data valueForKey:@"count"] intValue];
  186. if(count==0)
  187. {
  188. NSString* category = [data valueForKey:@"id"];
  189. if (self.CateMenu_delegate && [self.CateMenu_delegate respondsToSelector:@selector(SelectCategory:)]) {
  190. [self.CateMenu_delegate SelectCategory:category];
  191. }
  192. /* MainViewController* pvc = (MainViewController*)self.rootViewController;
  193. [pvc switchToCagegory:category];*/
  194. [self dismissViewControllerAnimated:YES
  195. completion:^{
  196. //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  197. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  198. // [app.window removeGestureRecognizer:self.tapGesture];
  199. }];
  200. }
  201. // if (item == nil) {
  202. // NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%d",index] ];
  203. //
  204. // }
  205. // else
  206. // data=[data objectForKey:[NSString stringWithFormat:@"category_%d",index]];
  207. DebugLog(@"menu select %@",data);
  208. }
  209. - (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  210. {
  211. if (treeNodeInfo.treeDepthLevel == 0) {
  212. cell.backgroundColor = [UIColor whiteColor];
  213. } else if (treeNodeInfo.treeDepthLevel == 1) {
  214. cell.backgroundColor = UIColorFromRGB(0xD1EEFC);
  215. } else if (treeNodeInfo.treeDepthLevel == 2) {
  216. cell.backgroundColor = UIColorFromRGB(0xE0F8D8);
  217. }
  218. }
  219. #pragma mark TreeView Data Source
  220. - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  221. {
  222. // NSInteger numberOfChildren = [treeNodeInfo.children count];
  223. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  224. // if(numberOfChildren>0)
  225. // cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %@", [@(numberOfChildren) stringValue]];
  226. cell.textLabel.text = [((NSDictionary *)item) valueForKey:@"title"];
  227. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  228. if (treeNodeInfo.treeDepthLevel == 0) {
  229. cell.detailTextLabel.textColor = [UIColor blackColor];
  230. }
  231. return cell;
  232. }
  233. - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
  234. {
  235. if (item == nil) {
  236. return [[self.categoryMenu valueForKey:@"count" ] intValue];
  237. // return [self.data count];
  238. }
  239. NSDictionary* data = item;
  240. return [[data valueForKey:@"count"] intValue];
  241. // RADataObject *data = item;
  242. // return [data.children count];
  243. }
  244. - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
  245. {
  246. NSDictionary *data = item;
  247. if (item == nil) {
  248. NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index] ];
  249. return ret;
  250. }
  251. return[data objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index]];
  252. /*
  253. RADataObject *data = item;
  254. if (item == nil) {
  255. return [self.data objectAtIndex:index];
  256. }
  257. return [data.children objectAtIndex:index];
  258. */
  259. }
  260. - (void)reRefreshView {
  261. [self.treeView reloadData];
  262. }
  263. #pragma mark - RA_NOTIFICAITON
  264. -(void) refresh_ui
  265. {
  266. // [self.table reloadData];
  267. }
  268. -(void) reload_data
  269. {
  270. UIApplication * app = [UIApplication sharedApplication];
  271. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  272. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  273. NSDictionary* category=[RANetwork request_Cagegory];
  274. dispatch_async(dispatch_get_main_queue(), ^{
  275. if([[category valueForKey:@"result"] intValue]==2)
  276. {
  277. appDelegate.categoryMenu = category;
  278. [self SetMenu:appDelegate.categoryMenu ];
  279. }
  280. else
  281. if([[category valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  282. {
  283. [self SetMenu:nil ];
  284. }
  285. else
  286. {
  287. [RAUtils message_alert:[category valueForKey:@"err_msg"] title:@"Load Category Menu" controller:self] ;
  288. }
  289. });
  290. });
  291. }
  292. @end