CommonEditorAutoCompleteView.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // CommonEditorAutoCompleteView.m
  3. // Storage Master
  4. //
  5. // Created by Ray on 12/12/2017.
  6. // Copyright © 2017 R&J. All rights reserved.
  7. //
  8. #import "CommonEditorAutoCompleteView.h"
  9. #import "RAUtils.h"
  10. @implementation CommonEditorAutoCompleteView
  11. /*
  12. // Only override drawRect: if you perform custom drawing.
  13. // An empty implementation adversely affects performance during animation.
  14. - (void)drawRect:(CGRect)rect {
  15. // Drawing code
  16. }
  17. */
  18. - (void)awakeFromNib {
  19. [super awakeFromNib];
  20. self.searchInput.delegate = nil;
  21. // Initialization code
  22. }
  23. - (IBAction)onCancelClick:(id)sender {
  24. // [self.active_field endEditing:true];
  25. // self.hidden = true;
  26. // self.searchInput.text = nil;
  27. [self.searchInput endEditing:true];
  28. self.arr_result = nil;
  29. self.hidden=true;
  30. [self.ResultTableView reloadData];
  31. }
  32. #pragma mark - TextField Delegate
  33. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  34. // autocompleteTableView.hidden = NO;
  35. NSString *substring = [NSString stringWithString:textField.text];
  36. substring = [substring stringByReplacingCharactersInRange:range withString:string];
  37. // [self searchAutocompleteEntriesWithSubstring:substring];
  38. // self.active_field textrangef
  39. bool canchange=[self.active_field.delegate textField:self.active_field shouldChangeCharactersInRange:range replacementString:string];
  40. // bool canchange=[self.active_field shouldChangeTextInRange:nil replacementText:string];
  41. if(canchange)
  42. {
  43. // [self searchAutocomplete:substring];
  44. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
  45. self.keyword = substring;
  46. [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8];
  47. }
  48. return canchange;
  49. }
  50. -(void) cancelSearch
  51. {
  52. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
  53. self.arr_result = nil;
  54. // self.hidden=true;
  55. [self.ResultTableView reloadData];
  56. }
  57. -(void) performSearch:(NSString*) keyword
  58. {
  59. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
  60. self.keyword = keyword;
  61. [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8];
  62. }
  63. - (void)textFieldDidEndEditing:(UITextField *)textField
  64. {
  65. if(self.returnValue)
  66. self.returnValue(textField.text);
  67. textField.text = nil;
  68. }
  69. //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
  70. //- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
  71. //- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
  72. //- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
  73. //- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
  74. //
  75. //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
  76. //
  77. //- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
  78. //- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
  79. -(void) searchAutocomplete:(NSString*)keyword
  80. {
  81. // if(keyword.length==0)
  82. // {
  83. // self.arr_result = nil;
  84. // [self.ResultTableView reloadData];
  85. // return;
  86. // }
  87. __weak typeof(self) weakself = self;
  88. if(self.dataSource)
  89. {
  90. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  91. NSString* searchid=[[NSUUID new] UUIDString];
  92. weakself.lastSearchID=searchid;
  93. // int s=[RAUtils getRandomNumber:1 to:3];
  94. // sleep(s);
  95. // NSLog(@"search id %@ sleep %d",searchid,s);
  96. NSMutableDictionary* params = [self.params mutableCopy];
  97. params[@"keyword"] = keyword;
  98. [weakself.dataSource sync_loadCadidate:params completionHandler:^(NSMutableDictionary *result) {
  99. NSArray* arr_ret = result[@"data"];
  100. if([searchid isEqualToString:weakself.lastSearchID])
  101. {
  102. self.arr_result = arr_ret;
  103. [self.ResultTableView reloadData];
  104. }
  105. else
  106. {
  107. DebugLog(@"expired search");
  108. }
  109. } ];
  110. // NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword search_id:weakself.lastSearchID];
  111. // self.arr_result = [self.dataSource sync_loadCadidate:keyword search_id:[[NSUUID new] UUIDString]];
  112. // dispatch_async(dispatch_get_main_queue(), ^{
  113. // });
  114. // }
  115. // });
  116. }
  117. }
  118. #pragma mark - TableView Delegate & DataSource
  119. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  120. return self.arr_result.count;
  121. // return self.modeArray.count;
  122. }
  123. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  124. // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mode_cell" forIndexPath:indexPath];
  125. // // RAModel *model = self.modelist[indexPath.row];//[self.modeArray objectAtIndex:indexPath.row];
  126. //
  127. //// [cell setModeinfo:[self.modelist[indexPath.row] mutableCopy]];
  128. // // [cell setModel:model];
  129. // return cell;
  130. static NSString *MyIdentifier = @"MyIdentifier";
  131. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  132. if (cell == nil)
  133. {
  134. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
  135. reuseIdentifier:MyIdentifier];
  136. }
  137. cell.textLabel.text = self.arr_result[indexPath.row][0];//[@"value"];
  138. cell.detailTextLabel.text = self.arr_result[indexPath.row][1];
  139. //cell.detailTextLabel.text = self.arr_result[indexPath.row];//[@"description"];
  140. return cell;
  141. }
  142. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. return 40.f;
  144. }
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  146. // if(self.returnValue)
  147. // self.returnValue(self.arr_result[indexPath.row][@"value"]);
  148. // self.searchInput.text = self.arr_result[indexPath.row][@"value"];
  149. // [self.searchInput endEditing:true];
  150. if(self.returnValue)
  151. self.returnValue(self.arr_result[indexPath.row][0]/*[@"value"]*/);
  152. // self.searchInput.text = nil;
  153. self.arr_result = nil;
  154. self.hidden=true;
  155. [self.ResultTableView reloadData];
  156. // RAModeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  157. // // RAModel *model = cell.model;
  158. // NSMutableDictionary* modeinfo= cell.modeinfo;
  159. //
  160. // if ([modeinfo[@"type"] isEqualToString:@"predef_query"]) {
  161. // [self processPredefQueryModel:modeinfo];
  162. //
  163. // } else if ([modeinfo[@"type"] isEqualToString:@"query"]) {
  164. // [self processQueryModel:modeinfo];
  165. //
  166. // } else if ([modeinfo[@"type"] isEqualToString:@"local_func"]) {
  167. // [self processLocalFunModel:modeinfo];
  168. //
  169. // } else if ([modeinfo[@"type"] isEqualToString:@"submode"]) {
  170. // [self processSubmodeModel:modeinfo];
  171. // }
  172. }
  173. @end