|
//
// searchController.m
// ThePaperBase
//
// Created by Huixin on 15/7/27.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "searchController.h"
#import "searchTableViewController.h"
#import "searchHistoryTableViewController.h"
#import "searchStrDB.h"
@interface searchController () <UITextFieldDelegate, searchTableViewDelegate, searchHistoryTableViewDelegate>
@property(nonatomic, strong)UIView *cantainerView;
@property(nonatomic, strong)UIImageView *searchIcon;
@property(nonatomic, strong)UITextField *searchContent;
@property(nonatomic, strong)UIButton *searchBtn;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)searchHistoryTableViewController *historyTableviewVC;
@property(nonatomic, strong)searchTableViewController *tableviewVC;
@property(nonatomic, strong)UILabel *noDataView;
@property(nonatomic, strong)NSString *hotKey;
@end
@implementation searchController
- (void)viewDidLoad {
[super viewDidLoad];
[MobClick event:@"16"];
self.titleStr = @"搜索";
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode) name:REFRESHAFTERNIGHTMODE object:nil];
[self.view addSubview:self.cantainerView];
[self.cantainerView addSubview:self.searchIcon];
[self.cantainerView addSubview:self.searchContent];
[self.view addSubview:self.searchBtn];
[self.view addSubview:self.line];
[self.view addSubview:self.noDataView];
[self addChildViewController:self.tableviewVC];
[self.view addSubview:self.tableviewVC.view];
[self.tableviewVC didMoveToParentViewController:self];
[self addChildViewController:self.historyTableviewVC];
[self.view addSubview:self.historyTableviewVC.view];
[self.historyTableviewVC didMoveToParentViewController:self];
[self.cantainerView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.bottom).offset(15);
make.left.equalTo(self.view.left).offset(10);
make.height.equalTo(@30);
}];
[self.searchIcon makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.cantainerView.left).offset(6);
make.centerY.equalTo(self.cantainerView.centerY);
make.width.mas_equalTo(@11);
make.height.mas_equalTo(@12);
}];
[self.searchContent makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.searchIcon.right).offset(5);
make.right.equalTo(self.cantainerView.right);
make.centerY.equalTo(self.cantainerView.centerY);
make.height.equalTo(@17);
}];
[self.searchBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.cantainerView.top);
make.left.equalTo(self.cantainerView.right);
make.right.equalTo(self.view.right);
make.bottom.equalTo(self.cantainerView.bottom);
make.width.mas_equalTo(@50);
}];
[self.line makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.cantainerView.bottom).offset(15);
make.left.equalTo(self.view.left);
make.right.equalTo(self.view.right);
make.height.mas_equalTo(@0.5);
}];
[self.noDataView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(self.barHeight, 0, 0, 0));
}];
[self.tableviewVC.view makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.line.bottom);
make.left.equalTo(self.view.left);
make.bottom.equalTo(self.view.bottom);
make.right.equalTo(self.view.right);
}];
[self.historyTableviewVC.view makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.line.bottom);
make.left.equalTo(self.view.left);
make.bottom.equalTo(self.view.bottom);
make.right.equalTo(self.view.right);
}];
self.tableviewVC.delegate = self;
self.historyTableviewVC.delegate = self;
self.noDataView.hidden = YES;
self.tableviewVC.view.hidden = YES;
[self.historyTableviewVC loadDataList];
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:0 requestUrl:getHotKeyURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSArray *array = responseData[@"keys"];
Self.hotKey = array[0];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"大家都在搜:%@",array[0]] attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextGray]}];
Self.searchContent.attributedPlaceholder = atrString;
}else {
Self.searchContent.placeholder = @"";
ShowTextMessage(message);
}
}];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)needrefreshNightMode { //bug5058: 夜间模式实时切换
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_cantainerView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
_cantainerView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
_searchContent.textColor = [UIColor colorWithHexString:TextBlack];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"大家都在搜:%@", self.hotKey] attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextGray]}];
self.searchContent.attributedPlaceholder = atrString;
if ([[TPUserDefault instance].isNightMode intValue] > 0)
_searchContent.keyboardAppearance = UIKeyboardAppearanceDark;
else
_searchContent.keyboardAppearance = UIKeyboardAppearanceDefault;
[_searchBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
[_searchBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted];
_line.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
_noDataView.textColor = [UIColor colorWithHexString:TextLightGray];
}
#pragma mark - get and set method
- (UIView*)cantainerView {
if (!_cantainerView) {
_cantainerView = [[UIView alloc] init];
_cantainerView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; //bug6156: 夜间模式:进入搜索页,搜索框颜色和其他颜色要有个反差
_cantainerView.layer.borderWidth = 0.5;
_cantainerView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
_cantainerView.layer.cornerRadius = 4;
}
return _cantainerView;
}
- (UIImageView *)searchIcon {
if (!_searchIcon) {
_searchIcon = [[UIImageView alloc] init];
_searchIcon.image = Image(@"setting/search_icon.png");
_searchIcon.backgroundColor = [UIColor clearColor];
}
return _searchIcon;
}
- (UITextField *)searchContent {
if (!_searchContent) {
_searchContent = [[UITextField alloc] init];
_searchContent.textColor = [UIColor colorWithHexString:TextBlack];
_searchContent.textAlignment = NSTextAlignmentLeft;
if ([[TPUserDefault instance].isNightMode intValue] > 0)
_searchContent.keyboardAppearance = UIKeyboardAppearanceDark;
else
_searchContent.keyboardAppearance = UIKeyboardAppearanceDefault;
_searchContent.keyboardType = UIKeyboardTypeDefault;
_searchContent.backgroundColor = [UIColor clearColor];
_searchContent.clearButtonMode = UITextFieldViewModeWhileEditing;
_searchContent.font = appFont(TEXT_FIVE_LEVELSIZE, NO);
[_searchContent addTarget:self action:@selector(editChange:) forControlEvents:UIControlEventEditingChanged];
_searchContent.returnKeyType = UIReturnKeySearch; //bug:5036 键盘右下角改成“搜索”
_searchContent.delegate = self;
}
return _searchContent;
}
- (UIButton *)searchBtn {
if (!_searchBtn) {
_searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_searchBtn.titleLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
[_searchBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
[_searchBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted];
[_searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
//[_searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 10)];
[_searchBtn addTarget:self action:@selector(searchBtnSelector) forControlEvents:UIControlEventTouchUpInside];
_searchBtn.backgroundColor = [UIColor clearColor];
}
return _searchBtn;
}
- (UIView *)line {
if (!_line) {
_line = [[UIView alloc] init];
_line.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _line;
}
- (TPTableViewController*)tableviewVC {
if (!_tableviewVC) {
_tableviewVC = searchTableViewController.new;
}
return _tableviewVC;
}
- (searchHistoryTableViewController*)historyTableviewVC {
if (!_historyTableviewVC) {
_historyTableviewVC = searchHistoryTableViewController.new;
[_historyTableviewVC setHeaderHeight:(self.barHeight+60.5)];
}
return _historyTableviewVC;
}
- (UILabel *)noDataView {
if (!_noDataView) {
_noDataView = [[UILabel alloc]init];
_noDataView.textAlignment = NSTextAlignmentCenter;
_noDataView.font = appFont(TEXT_THREE_LEVELSIZE, NO);
_noDataView.textColor = [UIColor colorWithHexString:TextLightGray];
_noDataView.text = @"这个新闻貌似没在地球上发生过...";
_noDataView.backgroundColor = [UIColor clearColor];
}
return _noDataView;
}
#pragma mark - textfield event
- (void)editChange:(UITextField*)textField {
if (textField == self.searchContent) {
if ([textField.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) {
textField.text = disable_emoji(textField.text);
}
//bug6155: 搜索:搜索框中输入汉字时,当拼音输入达到上限时出现异常
int limit = 20;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textField markedTextRange];
//获取高亮部分
UITextPosition *position;
if (selectedRange) {
position = [textField positionFromPosition:selectedRange.start offset:0];
}else{
position = nil;
}
if (!position) {
if (textField.text.length > limit) {
textField.text = [textField.text substringToIndex:limit];
}
}
}else{// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
if(textField.text.length >limit){
textField.text = [textField.text substringToIndex:limit];
}
}
// if (isIOS8) { //bug5407: 【适配性】搜索:输入搜索内容达到字符数限制时,继续输入时闪退
// if (textField.text.length > 20) {
// textField.text = [textField.text substringToIndex:20];
// }
// }
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([self.searchContent isFirstResponder]) {
[self.searchContent resignFirstResponder];
}
[self searchBtnSelector];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isFirstResponder]) {
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
return NO;
}
// if (isNotIOS8) { //bug5407: 【适配性】搜索:输入搜索内容达到字符数限制时,继续输入时闪退
// NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// if (toBeString.length > 20 && range.length!=1){
// textField.text = [toBeString substringToIndex:20];
// return NO;
// }
// }
}
return YES;
}
#pragma mark - searchBtn event
- (void)searchBtnSelector {
[self.searchContent resignFirstResponder];
NSDictionary *dic = [NSDictionary dictionary];
__weak typeof(self) Self = self;
if (isBlankString(self.searchContent.text)) {
if (isBlankString(self.hotKey)) {
[Remote doJsonActionWithBlock:0 requestUrl:getHotKeyURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSArray *array = responseData[@"keys"];
Self.hotKey = array[0];
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"大家都在搜:%@",array[0]] attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:TextGray]}];
Self.searchContent.attributedPlaceholder = atrString;
}else {
Self.searchContent.placeholder = @"";
ShowTextMessage(message);
}
}];
}
if (!isBlankString(self.hotKey))
dic = @{@"k":self.hotKey};
}else{
NSString *content = self.searchContent.text;
NSArray *contentList = [content componentsSeparatedByRegex:@"\\s+"];
__block NSString* searchStr = @"";
[contentList enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
if (idx != contentList.count-1) {
searchStr = [searchStr stringByAppendingString:[NSString stringWithFormat:@"%@ ",obj]];
}else {
searchStr = [searchStr stringByAppendingString:obj];
}
}];
dic = @{@"k": searchStr};
}
if ([dic count]) {
self.tableviewVC.searchStr = [dic objectForKey:@"k"];
[searchStrDB addOrUpdateSearchStr:self.tableviewVC.searchStr];
[self remoteAskForSearchList:dic];
}
}
- (void)remoteAskForSearchList:(NSDictionary*)dict {
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:1 requestUrl:searchURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSMutableArray *list = responseData[@"searchList"];
NSMutableArray *searchList = [NSMutableArray array];
[list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
listContObjectVO *listBo = setJsonDicToDataModel(obj, [listContObjectVO class]);
[searchList addObject:listBo];
}];
if (searchList.count) {
Self.tableviewVC.dataList = searchList;
[Self.tableviewVC.tableView setContentOffset:CGPointZero];
NSString *url = responseData[@"nextUrl"];
Self.tableviewVC.nextUrl = url;
Self.tableviewVC.view.hidden = NO;
Self.noDataView.hidden = YES;
Self.historyTableviewVC.view.hidden = YES;
}
else {
Self.tableviewVC.view.hidden = YES;
Self.noDataView.hidden = NO;
Self.historyTableviewVC.view.hidden = YES;
}
}else {
Self.tableviewVC.view.hidden = YES;
Self.noDataView.hidden = YES;
Self.historyTableviewVC.view.hidden = NO;
ShowTextMessage(message);
}
}];
}
#pragma mark - searchTableView delegate
- (void)searchGoToContent:(listContObjectVO*)listBo {
pushContentWithListContentObject(self.navigationController, listBo);
}
#pragma mark - searchHistoryTableView delegate
- (void)searchFromHistoryStr:(NSString*)searchStr {
NSDictionary *dic = [NSDictionary dictionary];
NSArray *strList = [searchStr componentsSeparatedByRegex:@"\\s+"];
__block NSString* SearchStr = @"";
[strList enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
if (idx != strList.count-1) {
SearchStr = [SearchStr stringByAppendingString:[NSString stringWithFormat:@"%@ ",obj]];
}else {
SearchStr = [SearchStr stringByAppendingString:obj];
}
}];
dic = @{@"k": SearchStr};
self.searchContent.text = SearchStr;
if ([dic count]){
self.tableviewVC.searchStr = [dic objectForKey:@"k"];
[searchStrDB addOrUpdateSearchStr:self.tableviewVC.searchStr];
[self remoteAskForSearchList:dic];
}
}
#pragma mark - keyboard
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([self.searchContent isFirstResponder]) {
[self.searchContent resignFirstResponder];
}
}
- (void)tapToDissmissKeyboard:(id)sender {
[self.searchContent resignFirstResponder];
}
#pragma mark - naviBar
- (void)tapNaviBar:(id)sender {
if (![self.tableviewVC.view isHidden]) {
[self.tableviewVC.tableView setContentOffset:CGPointZero animated:YES];
return;
}
if (![self.historyTableviewVC.view isHidden]) {
[self.historyTableviewVC.tableView setContentOffset:CGPointZero animated:YES];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|