Merge branch 'charlesmchen/refineGifSearch' into release/2.18.0

pull/1/head
Matthew Chen 8 years ago
commit a7195e4040

@ -27,6 +27,8 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
} }
} }
var lastQuery: String = ""
public weak var delegate: GifPickerViewControllerDelegate? public weak var delegate: GifPickerViewControllerDelegate?
var thread: TSThread? var thread: TSThread?
@ -56,7 +58,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
self.searchBar = UISearchBar() self.searchBar = UISearchBar()
self.layout = GifPickerLayout() self.layout = GifPickerLayout()
self.collectionView = UICollectionView(frame:CGRect.zero, collectionViewLayout:self.layout) self.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.layout)
super.init(coder: aDecoder) super.init(coder: aDecoder)
owsFail("\(self.TAG) invalid constructor") owsFail("\(self.TAG) invalid constructor")
@ -68,7 +70,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
self.searchBar = UISearchBar() self.searchBar = UISearchBar()
self.layout = GifPickerLayout() self.layout = GifPickerLayout()
self.collectionView = UICollectionView(frame:CGRect.zero, collectionViewLayout:self.layout) self.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.layout)
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
@ -114,9 +116,9 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem:.cancel, self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel,
target:self, target: self,
action:#selector(donePressed)) action: #selector(donePressed))
self.navigationItem.title = NSLocalizedString("GIF_PICKER_VIEW_TITLE", self.navigationItem.title = NSLocalizedString("GIF_PICKER_VIEW_TITLE",
comment: "Title for the 'gif picker' dialog.") comment: "Title for the 'gif picker' dialog.")
@ -124,13 +126,13 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
reachability = Reachability.forInternetConnection() reachability = Reachability.forInternetConnection()
NotificationCenter.default.addObserver(self, NotificationCenter.default.addObserver(self,
selector:#selector(reachabilityChanged), selector: #selector(reachabilityChanged),
name:NSNotification.Name.reachabilityChanged, name: NSNotification.Name.reachabilityChanged,
object:nil) object: nil)
NotificationCenter.default.addObserver(self, NotificationCenter.default.addObserver(self,
selector:#selector(didBecomeActive), selector: #selector(didBecomeActive),
name:NSNotification.Name.UIApplicationDidBecomeActive, name: NSNotification.Name.UIApplicationDidBecomeActive,
object:nil) object: nil)
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
@ -156,12 +158,12 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
searchBar.searchBarStyle = .minimal searchBar.searchBarStyle = .minimal
searchBar.delegate = self searchBar.delegate = self
searchBar.placeholder = NSLocalizedString("GIF_VIEW_SEARCH_PLACEHOLDER_TEXT", searchBar.placeholder = NSLocalizedString("GIF_VIEW_SEARCH_PLACEHOLDER_TEXT",
comment:"Placeholder text for the search field in gif view") comment: "Placeholder text for the search field in gif view")
searchBar.backgroundColor = UIColor.white searchBar.backgroundColor = UIColor.white
self.view.addSubview(searchBar) self.view.addSubview(searchBar)
searchBar.autoPinWidthToSuperview() searchBar.autoPinWidthToSuperview()
searchBar.autoPin(toTopLayoutGuideOf: self, withInset:0) searchBar.autoPin(toTopLayoutGuideOf: self, withInset: 0)
self.collectionView.delegate = self self.collectionView.delegate = self
self.collectionView.dataSource = self self.collectionView.dataSource = self
@ -169,40 +171,40 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
self.collectionView.register(GifPickerCell.self, forCellWithReuseIdentifier: kCellReuseIdentifier) self.collectionView.register(GifPickerCell.self, forCellWithReuseIdentifier: kCellReuseIdentifier)
self.view.addSubview(self.collectionView) self.view.addSubview(self.collectionView)
self.collectionView.autoPinWidthToSuperview() self.collectionView.autoPinWidthToSuperview()
self.collectionView.autoPinEdge(.top, to:.bottom, of:searchBar) self.collectionView.autoPinEdge(.top, to: .bottom, of: searchBar)
let bottomBanner = UIView() let bottomBanner = UIView()
bottomBanner.backgroundColor = UIColor.black bottomBanner.backgroundColor = UIColor.black
self.view.addSubview(bottomBanner) self.view.addSubview(bottomBanner)
bottomBanner.autoPinWidthToSuperview() bottomBanner.autoPinWidthToSuperview()
bottomBanner.autoPinEdge(.top, to:.bottom, of:self.collectionView) bottomBanner.autoPinEdge(.top, to: .bottom, of: self.collectionView)
bottomBanner.autoPin(toBottomLayoutGuideOf: self, withInset:0) bottomBanner.autoPin(toBottomLayoutGuideOf: self, withInset: 0)
// The Giphy API requires us to "show their trademark prominently" in our GIF experience. // The Giphy API requires us to "show their trademark prominently" in our GIF experience.
let logoImage = UIImage(named:"giphy_logo") let logoImage = UIImage(named: "giphy_logo")
let logoImageView = UIImageView(image:logoImage) let logoImageView = UIImageView(image: logoImage)
bottomBanner.addSubview(logoImageView) bottomBanner.addSubview(logoImageView)
logoImageView.autoPinHeightToSuperview(withMargin:3) logoImageView.autoPinHeightToSuperview(withMargin: 3)
logoImageView.autoHCenterInSuperview() logoImageView.autoHCenterInSuperview()
let noResultsView = createErrorLabel(text:NSLocalizedString("GIF_VIEW_SEARCH_NO_RESULTS", let noResultsView = createErrorLabel(text: NSLocalizedString("GIF_VIEW_SEARCH_NO_RESULTS",
comment:"Indicates that the user's search had no results.")) comment: "Indicates that the user's search had no results."))
self.noResultsView = noResultsView self.noResultsView = noResultsView
self.view.addSubview(noResultsView) self.view.addSubview(noResultsView)
noResultsView.autoPinWidthToSuperview(withMargin:20) noResultsView.autoPinWidthToSuperview(withMargin: 20)
noResultsView.autoVCenterInSuperview() noResultsView.autoVCenterInSuperview()
let searchErrorView = createErrorLabel(text:NSLocalizedString("GIF_VIEW_SEARCH_ERROR", let searchErrorView = createErrorLabel(text: NSLocalizedString("GIF_VIEW_SEARCH_ERROR",
comment:"Indicates that an error occured while searching.")) comment: "Indicates that an error occured while searching."))
self.searchErrorView = searchErrorView self.searchErrorView = searchErrorView
self.view.addSubview(searchErrorView) self.view.addSubview(searchErrorView)
searchErrorView.autoPinWidthToSuperview(withMargin:20) searchErrorView.autoPinWidthToSuperview(withMargin: 20)
searchErrorView.autoVCenterInSuperview() searchErrorView.autoVCenterInSuperview()
searchErrorView.isUserInteractionEnabled = true searchErrorView.isUserInteractionEnabled = true
searchErrorView.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(retryTapped))) searchErrorView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(retryTapped)))
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle:.gray) let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
self.activityIndicator = activityIndicator self.activityIndicator = activityIndicator
self.view.addSubview(activityIndicator) self.view.addSubview(activityIndicator)
activityIndicator.autoCenterInSuperview() activityIndicator.autoCenterInSuperview()
@ -214,7 +216,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
let label = UILabel() let label = UILabel()
label.text = text label.text = text
label.textColor = UIColor.black label.textColor = UIColor.black
label.font = UIFont.ows_mediumFont(withSize:20) label.font = UIFont.ows_mediumFont(withSize: 20)
label.textAlignment = .center label.textAlignment = .center
label.numberOfLines = 0 label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping label.lineBreakMode = .byWordWrapping
@ -281,7 +283,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let imageInfo = imageInfos[indexPath.row] let imageInfo = imageInfos[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:kCellReuseIdentifier, for: indexPath) let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellReuseIdentifier, for: indexPath)
guard let gifCell = cell as? GifPickerCell else { guard let gifCell = cell as? GifPickerCell else {
owsFail("\(TAG) Unexpected cell type.") owsFail("\(TAG) Unexpected cell type.")
return cell return cell
@ -293,7 +295,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
// MARK: - UICollectionViewDelegate // MARK: - UICollectionViewDelegate
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at:indexPath) as? GifPickerCell else { guard let cell = collectionView.cellForItem(at: indexPath) as? GifPickerCell else {
owsFail("\(TAG) unexpected cell.") owsFail("\(TAG) unexpected cell.")
return return
} }
@ -302,11 +304,11 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
return return
} }
let filePath = asset.filePath let filePath = asset.filePath
guard let dataSource = DataSourcePath.dataSource(withFilePath:filePath) else { guard let dataSource = DataSourcePath.dataSource(withFilePath: filePath) else {
owsFail("\(TAG) couldn't load asset.") owsFail("\(TAG) couldn't load asset.")
return return
} }
let attachment = SignalAttachment(dataSource : dataSource, dataUTI: asset.rendition.utiType) let attachment = SignalAttachment(dataSource: dataSource, dataUTI: asset.rendition.utiType)
guard let thread = thread else { guard let thread = thread else {
owsFail("\(TAG) Missing thread.") owsFail("\(TAG) Missing thread.")
return return
@ -322,7 +324,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
self.delegate?.gifPickerDidSend(outgoingMessage: outgoingMessage) self.delegate?.gifPickerDidSend(outgoingMessage: outgoingMessage)
dismiss(animated: true, completion:nil) dismiss(animated: true, completion: nil)
} }
public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
@ -345,7 +347,7 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
// MARK: - Event Handlers // MARK: - Event Handlers
func donePressed(sender: UIButton) { func donePressed(sender: UIButton) {
dismiss(animated: true, completion:nil) dismiss(animated: true, completion: nil)
} }
// MARK: - UISearchBarDelegate // MARK: - UISearchBarDelegate
@ -370,6 +372,8 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
} }
public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.resignFirstResponder()
tryToSearch() tryToSearch()
} }
@ -384,7 +388,15 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
comment: "Alert message shown when user tries to search for GIFs without entering any search terms.")) comment: "Alert message shown when user tries to search for GIFs without entering any search terms."))
return return
} }
search(query:text)
let query = (text as String).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if (viewMode == .searching || viewMode == .results) && lastQuery == query {
Logger.info("\(TAG) ignoring duplicate search: \(query)")
return
}
search(query: query)
} }
private func search(query: String) { private func search(query: String) {
@ -392,9 +404,9 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
progressiveSearchTimer?.invalidate() progressiveSearchTimer?.invalidate()
progressiveSearchTimer = nil progressiveSearchTimer = nil
self.searchBar.resignFirstResponder()
imageInfos = [] imageInfos = []
viewMode = .searching viewMode = .searching
lastQuery = query
self.collectionView.contentOffset = CGPoint.zero self.collectionView.contentOffset = CGPoint.zero
GiphyAPI.sharedInstance.search(query: query, success: { [weak self] imageInfos in GiphyAPI.sharedInstance.search(query: query, success: { [weak self] imageInfos in

Loading…
Cancel
Save