RAViewController.m 14 KB

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