| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- //
- // CommonEditorAutoCompleteView.m
- // Storage Master
- //
- // Created by Ray on 12/12/2017.
- // Copyright © 2017 R&J. All rights reserved.
- //
- #import "CommonEditorAutoCompleteView.h"
- #import "RAUtils.h"
- @implementation CommonEditorAutoCompleteView
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- - (void)awakeFromNib {
- [super awakeFromNib];
- self.searchInput.delegate = nil;
- // Initialization code
- }
- - (IBAction)onCancelClick:(id)sender {
- // [self.active_field endEditing:true];
- // self.hidden = true;
-
- // self.searchInput.text = nil;
- [self.searchInput endEditing:true];
-
- self.arr_result = nil;
- self.hidden=true;
- [self.ResultTableView reloadData];
- }
- #pragma mark - TextField Delegate
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- // autocompleteTableView.hidden = NO;
- NSString *substring = [NSString stringWithString:textField.text];
- substring = [substring stringByReplacingCharactersInRange:range withString:string];
- // [self searchAutocompleteEntriesWithSubstring:substring];
-
- // self.active_field textrangef
-
- bool canchange=[self.active_field.delegate textField:self.active_field shouldChangeCharactersInRange:range replacementString:string];
-
- // bool canchange=[self.active_field shouldChangeTextInRange:nil replacementText:string];
- if(canchange)
- {
- // [self searchAutocomplete:substring];
-
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
- self.keyword = substring;
- [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8];
- }
- return canchange;
- }
- -(void) cancelSearch
- {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
- self.arr_result = nil;
- // self.hidden=true;
- [self.ResultTableView reloadData];
- }
- -(void) performSearch:(NSString*) keyword
- {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
- self.keyword = keyword;
- [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8];
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField
- {
- if(self.returnValue)
- self.returnValue(textField.text);
- textField.text = nil;
- }
- //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- //- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
- //- (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
- //- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- //- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
- //
- //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- //
- //- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
- //- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
- -(void) searchAutocomplete:(NSString*)keyword
- {
-
- if(keyword.length==0)
- {
- self.arr_result = nil;
- [self.ResultTableView reloadData];
- return;
- }
- __weak typeof(self) weakself = self;
- if(self.dataSource)
- {
- // dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSString* searchid=[[NSUUID new] UUIDString];
- weakself.lastSearchID=searchid;
- // int s=[RAUtils getRandomNumber:1 to:3];
- // sleep(s);
- // NSLog(@"search id %@ sleep %d",searchid,s);
-
- NSMutableDictionary* params = [self.params mutableCopy];
- params[@"keyword"] = keyword;
- [weakself.dataSource sync_loadCadidate:params completionHandler:^(NSMutableDictionary *result) {
- NSArray* arr_ret = result[@"data"];
-
- if([searchid isEqualToString:weakself.lastSearchID])
- {
- self.arr_result = arr_ret;
- [self.ResultTableView reloadData];
- }
- else
- {
- NSLog(@"expired search");
- }
- } ];
-
- // NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword search_id:weakself.lastSearchID];
- // self.arr_result = [self.dataSource sync_loadCadidate:keyword search_id:[[NSUUID new] UUIDString]];
- // dispatch_async(dispatch_get_main_queue(), ^{
-
- // });
-
- // }
- // });
- }
-
-
- }
- #pragma mark - TableView Delegate & DataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.arr_result.count;
- // return self.modeArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mode_cell" forIndexPath:indexPath];
- // // RAModel *model = self.modelist[indexPath.row];//[self.modeArray objectAtIndex:indexPath.row];
- //
- //// [cell setModeinfo:[self.modelist[indexPath.row] mutableCopy]];
- // // [cell setModel:model];
- // return cell;
-
-
- static NSString *MyIdentifier = @"MyIdentifier";
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
-
- if (cell == nil)
- {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
- reuseIdentifier:MyIdentifier];
- }
- cell.textLabel.text = self.arr_result[indexPath.row][0];//[@"value"];
- cell.detailTextLabel.text = self.arr_result[indexPath.row][1];
- //cell.detailTextLabel.text = self.arr_result[indexPath.row];//[@"description"];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 40.f;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-
-
- // if(self.returnValue)
- // self.returnValue(self.arr_result[indexPath.row][@"value"]);
-
- // self.searchInput.text = self.arr_result[indexPath.row][@"value"];
- // [self.searchInput endEditing:true];
-
-
- if(self.returnValue)
- self.returnValue(self.arr_result[indexPath.row][0]/*[@"value"]*/);
- // self.searchInput.text = nil;
- self.arr_result = nil;
- self.hidden=true;
- [self.ResultTableView reloadData];
- // RAModeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
- // // RAModel *model = cell.model;
- // NSMutableDictionary* modeinfo= cell.modeinfo;
- //
- // if ([modeinfo[@"type"] isEqualToString:@"predef_query"]) {
- // [self processPredefQueryModel:modeinfo];
- //
- // } else if ([modeinfo[@"type"] isEqualToString:@"query"]) {
- // [self processQueryModel:modeinfo];
- //
- // } else if ([modeinfo[@"type"] isEqualToString:@"local_func"]) {
- // [self processLocalFunModel:modeinfo];
- //
- // } else if ([modeinfo[@"type"] isEqualToString:@"submode"]) {
- // [self processSubmodeModel:modeinfo];
- // }
-
- }
- @end
|