<commit_before>//
//  QuerySuggestionView.swift
//  Client
//
//  Created by <NAME> on 1/2/17.
//  Copyright © 2017 Mozilla. All rights reserved.
//

import UIKit
protocol QuerySuggestionDelegate : class {
    func autoComplete(suggestion: String)
}

class QuerySuggestionView: UIView {
    //MARK:- Constants
    private let kViewHeight: CGFloat = 44
    private let scrollView = UIScrollView()
    private let boldFontAttributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(17), NSForegroundColorAttributeName: UIColor.whiteColor()]
    private let normalFontAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(16), NSForegroundColorAttributeName: UIColor.whiteColor()]
    private let bgColor = UIColor(rgb: 0xADB5BD)
    private let separatorBgColor = UIColor(rgb: 0xC7CBD3)
    private let margin: CGFloat = 10
    
    //MARK:- instance variables
    weak var delegate : QuerySuggestionDelegate? = nil
    private var currentText = ""
    
    
    init() {
        let applicationFrame = UIScreen.mainScreen().applicationFrame
        let frame = CGRectMake(0.0, 0.0, CGRectGetWidth(applicationFrame), kViewHeight);
        
        super.init(frame: frame)
        self.autoresizingMask = .FlexibleWidth
        self.backgroundColor = bgColor
        
        scrollView.frame = frame
        self.addSubview(self.scrollView)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector:  #selector(QuerySuggestionView.viewRotated), name: UIDeviceOrientationDidChangeNotification, object: nil)
        
        if !QuerySuggestions.isEnabled() {
            self.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func didEnterText(text: String) {
        currentText = text
        
        guard QuerySuggestions.isEnabled() && OrientationUtil.isPortrait() else {
            self.hidden = true
            return
        }
        
        guard !text.isEmpty else {
            clearSuggestions()
            return
        }

        QuerySuggestions.getSuggestions(text) { [weak self] responseData in
            self?.processSuggestionsResponse(text, responseData: responseData)<commit_msg>[IB-1206] Don't show QuerySuggestions before user start typing the query<commit_after>//
//  QuerySuggestionView.swift
//  Client
//
//  Created by <NAME> on 1/2/17.
//  Copyright © 2017 Mozilla. All rights reserved.
//

import UIKit
protocol QuerySuggestionDelegate : class {
    func autoComplete(suggestion: String)
}

class QuerySuggestionView: UIView {
    //MARK:- Constants
    private let kViewHeight: CGFloat = 44
    private let scrollView = UIScrollView()
    private let boldFontAttributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(17), NSForegroundColorAttributeName: UIColor.whiteColor()]
    private let normalFontAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(16), NSForegroundColorAttributeName: UIColor.whiteColor()]
    private let bgColor = UIColor(rgb: 0xADB5BD)
    private let separatorBgColor = UIColor(rgb: 0xC7CBD3)
    private let margin: CGFloat = 10
    
    //MARK:- instance variables
    weak var delegate : QuerySuggestionDelegate? = nil
    private var currentText = ""
    
    
    init() {
        let applicationFrame = UIScreen.mainScreen().applicationFrame
        let frame = CGRectMake(0.0, 0.0, CGRectGetWidth(applicationFrame), kViewHeight);
        
        super.init(frame: frame)
        self.autoresizingMask = .FlexibleWidth
        self.backgroundColor = bgColor
        
        scrollView.frame = frame
        self.addSubview(self.scrollView)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector:  #selector(QuerySuggestionView.viewRotated), name: UIDeviceOrientationDidChangeNotification, object: nil)
        
        //initially hide the view until the user type query
        self.hidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func didEnterText(text: String) {
        currentText = text
        
        guard QuerySuggestions.isEnabled() && OrientationUtil.isPortrait() else {
            self.hidden = true
            return
        }
        
        guard !text.isEmpty else {
            clearSuggestions()
            self.hidden = true
            return
        }
        self.hidden = false
        QuerySuggestions.getSuggestions(text) { [weak self] responseData in
            self?.processSuggestionsResponse(text, responseData: responseData)
{'fraction_non_alphanumeric': 0.08506944444444445, 'fraction_numerical': 0.01171875, 'mean_word_length': 2.759380097879282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2271442', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1271, 'n_words': 301}