CommonEditorAutoCompleteView.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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:(NSString*) keyword
  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. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  90. NSString* searchid=[[NSUUID new] UUIDString];
  91. weakself.lastSearchID=searchid;
  92. // int s=[RAUtils getRandomNumber:1 to:3];
  93. // sleep(s);
  94. // NSLog(@"search id %@ sleep %d",searchid,s);
  95. NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword ];
  96. // NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword search_id:weakself.lastSearchID];
  97. // self.arr_result = [self.dataSource sync_loadCadidate:keyword search_id:[[NSUUID new] UUIDString]];
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. if([searchid isEqualToString:weakself.lastSearchID])
  100. {
  101. self.arr_result = arr_ret;
  102. [self.ResultTableView reloadData];
  103. }
  104. else
  105. {
  106. NSLog(@"expired search");
  107. }
  108. });
  109. // }
  110. });
  111. }
  112. #pragma mark - TableView Delegate & DataSource
  113. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  114. return self.arr_result.count;
  115. // return self.modeArray.count;
  116. }
  117. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  118. // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mode_cell" forIndexPath:indexPath];
  119. // // RAModel *model = self.modelist[indexPath.row];//[self.modeArray objectAtIndex:indexPath.row];
  120. //
  121. //// [cell setModeinfo:[self.modelist[indexPath.row] mutableCopy]];
  122. // // [cell setModel:model];
  123. // return cell;
  124. static NSString *MyIdentifier = @"MyIdentifier";
  125. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  126. if (cell == nil)
  127. {
  128. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
  129. reuseIdentifier:MyIdentifier];
  130. }
  131. cell.textLabel.text = self.arr_result[indexPath.row][@"value"];
  132. cell.detailTextLabel.text = self.arr_result[indexPath.row][@"description"];
  133. return cell;
  134. }
  135. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  136. return 40.f;
  137. }
  138. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  139. // if(self.returnValue)
  140. // self.returnValue(self.arr_result[indexPath.row][@"value"]);
  141. // self.searchInput.text = self.arr_result[indexPath.row][@"value"];
  142. // [self.searchInput endEditing:true];
  143. if(self.returnValue)
  144. self.returnValue(self.arr_result[indexPath.row][@"value"]);
  145. // self.searchInput.text = nil;
  146. self.arr_result = nil;
  147. self.hidden=true;
  148. [self.ResultTableView reloadData];
  149. // RAModeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  150. // // RAModel *model = cell.model;
  151. // NSMutableDictionary* modeinfo= cell.modeinfo;
  152. //
  153. // if ([modeinfo[@"type"] isEqualToString:@"predef_query"]) {
  154. // [self processPredefQueryModel:modeinfo];
  155. //
  156. // } else if ([modeinfo[@"type"] isEqualToString:@"query"]) {
  157. // [self processQueryModel:modeinfo];
  158. //
  159. // } else if ([modeinfo[@"type"] isEqualToString:@"local_func"]) {
  160. // [self processLocalFunModel:modeinfo];
  161. //
  162. // } else if ([modeinfo[@"type"] isEqualToString:@"submode"]) {
  163. // [self processSubmodeModel:modeinfo];
  164. // }
  165. }
  166. @end