SearchFilterViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //
  2. // SearchFilterViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-7-25.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SearchFilterViewController.h"
  9. #import "FilterCellCadedate.h"
  10. #import "FilterCellValue.h"
  11. @interface SearchFilterViewController ()
  12. @end
  13. @implementation SearchFilterViewController
  14. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  15. {
  16. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  17. if (self) {
  18. // Custom initialization
  19. }
  20. return self;
  21. }
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  26. self.navi_item.rightBarButtonItem = closeButton;
  27. // Do any additional setup after loading the view.
  28. }
  29. - (void)onCloseClick:(UIButton *)sender {
  30. [self dismissViewControllerAnimated:YES
  31. completion:^{
  32. }];
  33. }
  34. - (void)didReceiveMemoryWarning
  35. {
  36. [super didReceiveMemoryWarning];
  37. // Dispose of any resources that can be recreated.
  38. }
  39. - (void)onSwitchTap:(UISwitch *)sender {
  40. // UIView* sectionheader = sender.superview;
  41. int section = sender.superview.tag;
  42. if(section == 0 )
  43. return;
  44. for(int i=0;i<[[self.cadedate valueForKey:@"groups"]intValue];i++)
  45. {
  46. NSMutableDictionary* group_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"group_%d",i ]] mutableCopy];
  47. if(i==section-1)
  48. {
  49. if(sender.on)
  50. {
  51. [group_json setValue:@1 forKey:@"multi_select"];
  52. }
  53. else
  54. {
  55. [group_json setValue:@0 forKey:@"multi_select"];
  56. int count = [[group_json valueForKey:@"count"]intValue];
  57. for(int j=0;j<count;j++)
  58. {
  59. NSMutableDictionary* item_json = [[group_json objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  60. [item_json setValue:@0 forKey:@"check"];
  61. [group_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",j ]];
  62. }
  63. }
  64. }
  65. else
  66. {
  67. [group_json setValue:@0 forKey:@"multi_select"];
  68. int count = [[group_json valueForKey:@"count"]intValue];
  69. for(int j=0;j<count;j++)
  70. {
  71. NSMutableDictionary* item_json = [[group_json objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  72. [item_json setValue:@0 forKey:@"check"];
  73. [group_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",j ]];
  74. }
  75. }
  76. [self.cadedate setObject:group_json forKey:[NSString stringWithFormat:@"group_%d",i]];
  77. }
  78. [self.tableView reloadData];
  79. }
  80. - (void)OnDone:(UIButton *)sender {
  81. int section = sender.superview.tag;
  82. NSDictionary* group_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"group_%d",section-1 ]];
  83. // int multi_select = [[group_json valueForKey:@"multi_select"] intValue];
  84. NSMutableDictionary * value_json = self.value;
  85. if(value_json==nil)
  86. value_json=[[NSMutableDictionary alloc] init];
  87. int groups = [[value_json valueForKey:@"groups"] intValue];
  88. [value_json setValue:@(groups+1) forKey:@"groups"];
  89. NSMutableDictionary * value_group_json = [[NSMutableDictionary alloc] init];
  90. int count = [[group_json valueForKey:@"count"]intValue];
  91. int check_count=0;
  92. NSString * title = [[group_json valueForKey:@"title"] stringByAppendingString:@":"] ;
  93. for(int i=0;i<count;i++)
  94. {
  95. NSDictionary* item_json = [group_json objectForKey:[NSString stringWithFormat:@"item_%d",i]] ;
  96. int check = [[item_json valueForKey:@"check"] intValue];
  97. if(check==0)
  98. continue;
  99. check_count++;
  100. title = [title stringByAppendingString:[item_json valueForKey:@"title"]];
  101. title = [title stringByAppendingString:@","];
  102. [value_group_json setValue:[item_json valueForKey:@"val"] forKey:[NSString stringWithFormat:@"val_%d",check_count]];
  103. }
  104. if(check_count<1)
  105. return;
  106. NSString* key = [group_json valueForKey:@"key"];
  107. if(title.length>=1)
  108. title=[title substringToIndex:title.length-1];
  109. [value_group_json setValue:title forKey:@"title"];
  110. [value_group_json setValue:@(check_count) forKey:@"count" ];
  111. [value_group_json setValue:key forKey:@"key" ];
  112. [value_json setObject:value_group_json forKey:[NSString stringWithFormat:@"group_%d",groups]];
  113. self.value = value_json;
  114. // NSError *error = nil;
  115. // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value_json
  116. // options:NSJSONWritingPrettyPrinted
  117. // error:&error];
  118. //
  119. //
  120. // // NSData * d = [NSData alloc] initwithd
  121. // NSString *result = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  122. // NSLog(result);
  123. [self closewindow];
  124. }
  125. - (IBAction)OnDeleteValue:(UIButton *)sender {
  126. int groups = [[self.value valueForKey:@"groups"]intValue];
  127. if(groups==1)
  128. {
  129. self.value = nil;
  130. [self closewindow];
  131. }
  132. else
  133. {
  134. FilterCellValue *cell = (FilterCellValue *) sender.superview.superview.superview;
  135. // NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
  136. NSMutableDictionary * value_json=[[NSMutableDictionary alloc] init];
  137. int count = 0;
  138. for(int i=0;i<groups;i++)
  139. {
  140. NSDictionary* group_json = [self.value objectForKey:[NSString stringWithFormat:@"group_%d",i]];
  141. NSString* title = [group_json valueForKey:@"title"];
  142. if(![title isEqualToString:cell.labelTitle.text])
  143. {
  144. [value_json setObject:group_json forKey:[NSString stringWithFormat:@"group_%d",count]];
  145. count++;
  146. }
  147. }
  148. [value_json setValue:@(count) forKey:@"groups"];
  149. self.value = value_json;
  150. [self closewindow];
  151. }
  152. }
  153. #pragma mark - Table view data source
  154. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  155. {
  156. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
  157. // myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  158. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  159. // titleLabel.textColor=[UIColor whiteColor];
  160. titleLabel.backgroundColor = [UIColor clearColor];
  161. if(section==0)
  162. titleLabel.text= @"in use filters";
  163. else
  164. {
  165. NSDictionary* group_json =[self.cadedate objectForKey:[NSString stringWithFormat:@"group_%ld",(long)section-1]];
  166. titleLabel.text= [group_json valueForKey:@"title"];
  167. int multi_select = [[group_json valueForKey:@"multi_select"] intValue];
  168. UILabel *switch_Label = [[UILabel alloc] initWithFrame:CGRectMake(tableView.bounds.size.width-51-10-110, 0, 110, 22)];
  169. // titleLabel.textColor=[UIColor whiteColor];
  170. switch_Label.backgroundColor = [UIColor clearColor];
  171. switch_Label.text=@"Multi Select";
  172. // switch_Label.text
  173. UISwitch* btn_switch = [[UISwitch alloc] initWithFrame:CGRectMake(tableView.bounds.size.width-51-10, 0, 51, 31)];
  174. myView.tag = section;
  175. [btn_switch addTarget:self action:@selector(onSwitchTap:) forControlEvents:UIControlEventValueChanged];
  176. if(multi_select==1)
  177. {
  178. btn_switch.on=true;
  179. // CGRect rt = btn_switch.frame;
  180. btn_switch.frame=CGRectOffset(btn_switch.frame, -160, 0);
  181. switch_Label.frame=CGRectOffset(switch_Label.frame, -160, 0);
  182. UIButton* btn_done = [UIButton buttonWithType:UIButtonTypeSystem];
  183. btn_done.frame =CGRectMake(tableView.bounds.size.width-50-10, 0, 50, 30);
  184. // btn_done.titleLabel.text = @"Done";
  185. [btn_done setTitle:@"Done" forState:UIControlStateNormal];
  186. // btn_done.buttonType = UIButtonTypeSystem;
  187. // btn_done.backgroundColor =[UIColor redColor];
  188. [btn_done addTarget:self action:@selector(OnDone:) forControlEvents:UIControlEventTouchUpInside];
  189. [myView addSubview:btn_done];
  190. }
  191. [myView addSubview:switch_Label];
  192. [myView addSubview:btn_switch];
  193. }
  194. [titleLabel sizeToFit];
  195. // if(section==0)
  196. // titleLabel.text=NSLocalizedString(@"multi select", nil);
  197. // else
  198. // titleLabel.text=NSLocalizedString(@"hide_items", nil);
  199. // [titleLabel sizeToFit];
  200. [myView addSubview:titleLabel];
  201. //
  202. return myView;
  203. }
  204. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  205. {
  206. return [[self.cadedate valueForKey:@"groups"] intValue]+1;
  207. }
  208. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  209. // if(section==0)
  210. // return @"in use filters";
  211. // else
  212. // {
  213. // NSDictionary* group_json =[self.cadedate objectForKey:[NSString stringWithFormat:@"group_%d",section-1]];
  214. // return [group_json valueForKey:@"title"];
  215. // }
  216. //}
  217. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  218. {
  219. if(section==0 && self.value==nil)
  220. return 0;
  221. return 44;
  222. }
  223. //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  224. //{
  225. // return 88;
  226. //}
  227. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  228. {
  229. if(section==0)
  230. if(self.value==nil)
  231. return 0;
  232. else
  233. return [[self.value valueForKey:@"groups"] intValue];
  234. else
  235. {
  236. NSDictionary* group_json =[self.cadedate objectForKey:[NSString stringWithFormat:@"group_%d",section-1]];
  237. return [[group_json valueForKey:@"count"] intValue];
  238. }
  239. }
  240. -(void) closewindow
  241. {
  242. if (self.delegate && [self.delegate respondsToSelector:@selector(SearchFilterReturn:)]) {
  243. [self.delegate SearchFilterReturn:self.value];
  244. }
  245. [self dismissViewControllerAnimated:YES
  246. completion:^{
  247. }];}
  248. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  249. {
  250. if(indexPath.section==0)
  251. {
  252. NSString* CellIdentifier = @"FilterCellValue";
  253. FilterCellValue * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  254. NSDictionary* group_json = [self.value objectForKey:[NSString stringWithFormat:@"group_%ld",(long)indexPath.row ]];
  255. cell.labelTitle.text=[group_json valueForKey:@"title"];
  256. //lacell.textLabel.text =[group_json valueForKey:@"title"];
  257. return cell;
  258. }
  259. else
  260. {
  261. NSString* CellIdentifier = @"FilterCellCadedate";
  262. FilterCellCadedate * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  263. NSDictionary* group_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"group_%d",indexPath.section-1 ]];
  264. NSDictionary* item_json = [group_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  265. NSString* title = [item_json valueForKey:@"title"];
  266. cell.textLabel.text = title;
  267. int check = [[item_json valueForKey:@"check" ] intValue];
  268. if(check==1)
  269. cell.img_checkmark.hidden=false;
  270. else
  271. cell.img_checkmark.hidden=true;
  272. return cell;
  273. }
  274. }
  275. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  276. {
  277. UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
  278. cell.selected = false;
  279. if(indexPath.section==0)
  280. return;
  281. else
  282. {
  283. NSMutableDictionary* group_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"group_%d",indexPath.section-1 ]] mutableCopy];
  284. int multi_select = [[group_json valueForKey:@"multi_select"] intValue];
  285. NSMutableDictionary* item_json = [[group_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]] mutableCopy];
  286. if(multi_select==1)
  287. {
  288. int check = [[item_json valueForKey:@"check" ] intValue];
  289. if(check==1)
  290. {
  291. cell.accessoryType = UITableViewCellAccessoryNone;
  292. [item_json setValue:@0 forKey:@"check"];
  293. }
  294. else
  295. {
  296. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  297. [item_json setValue:@1 forKey:@"check"];
  298. }
  299. [group_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  300. [self.cadedate setObject:group_json forKey:[NSString stringWithFormat:@"group_%ld",(long)indexPath.section-1 ]];
  301. [self.tableView reloadData];
  302. }
  303. else
  304. {
  305. NSMutableDictionary * value_json = self.value;
  306. if(value_json==nil)
  307. value_json=[[NSMutableDictionary alloc] init];
  308. int groups = [[value_json valueForKey:@"groups"] intValue];
  309. [value_json setValue:@(groups+1) forKey:@"groups"];
  310. NSMutableDictionary * value_group_json = [[NSMutableDictionary alloc] init];
  311. // NSString* ttt =[group_json valueForKey:@"title"];
  312. NSString * title = [[[group_json valueForKey:@"title"] stringByAppendingString:@":"] stringByAppendingString:[item_json valueForKey:@"title"]];
  313. NSString* key = [group_json valueForKey:@"key"];
  314. [value_group_json setValue:title forKey:@"title"];
  315. [value_group_json setValue:@1 forKey:@"count" ];
  316. [value_group_json setValue:key forKey:@"key" ];
  317. [value_group_json setValue:[item_json valueForKey:@"val"] forKey:@"val_0"];
  318. [value_json setObject:value_group_json forKey:[NSString stringWithFormat:@"group_%d",groups]];
  319. self.value = value_json;
  320. NSError *error = nil;
  321. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value_json
  322. options:NSJSONWritingPrettyPrinted
  323. error:&error];
  324. // NSData * d = [NSData alloc] initwithd
  325. NSString *result = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  326. NSLog(result);
  327. [self closewindow];
  328. }
  329. }
  330. }
  331. /*
  332. #pragma mark - Navigation
  333. // In a storyboard-based application, you will often want to do a little preparation before navigation
  334. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  335. {
  336. // Get the new view controller using [segue destinationViewController].
  337. // Pass the selected object to the new view controller.
  338. }
  339. */
  340. @end