RAViewController.m 13 KB

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