session-ios/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift

235 lines
8 KiB
Swift
Raw Normal View History

2017-09-26 19:40:37 +02:00
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
import Foundation
2017-09-29 00:15:18 +02:00
class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate, GifPickerLayoutDelegate {
2017-09-26 19:40:37 +02:00
let TAG = "[GifPickerViewController]"
// MARK: Properties
var thread: TSThread?
var messageSender: MessageSender?
2017-09-26 19:40:37 +02:00
let searchBar: UISearchBar
let layout: GifPickerLayout
let collectionView: UICollectionView
2017-09-28 20:09:11 +02:00
var logoImageView: UIImageView?
2017-09-26 19:40:37 +02:00
2017-09-28 20:09:11 +02:00
var imageInfos = [GiphyImageInfo]()
private let kCellReuseIdentifier = "kCellReuseIdentifier"
2017-09-26 19:40:37 +02:00
// MARK: Initializers
@available(*, unavailable, message:"use other constructor instead.")
2017-09-26 19:40:37 +02:00
required init?(coder aDecoder: NSCoder) {
self.thread = nil
self.messageSender = nil
2017-09-26 19:40:37 +02:00
self.searchBar = UISearchBar()
self.layout = GifPickerLayout()
self.collectionView = UICollectionView(frame:CGRect.zero, collectionViewLayout:self.layout)
2017-09-26 19:40:37 +02:00
super.init(coder: aDecoder)
owsFail("\(self.TAG) invalid constructor")
}
required init(thread: TSThread, messageSender: MessageSender) {
self.thread = thread
self.messageSender = messageSender
2017-09-26 19:40:37 +02:00
self.searchBar = UISearchBar()
self.layout = GifPickerLayout()
self.collectionView = UICollectionView(frame:CGRect.zero, collectionViewLayout:self.layout)
2017-09-26 19:40:37 +02:00
super.init(nibName: nil, bundle: nil)
2017-09-29 00:15:18 +02:00
self.layout.delegate = self
2017-09-26 19:40:37 +02:00
}
// MARK: View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
2017-09-28 20:09:11 +02:00
view.backgroundColor = UIColor.black
2017-09-26 19:40:37 +02:00
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem:.stop,
target:self,
action:#selector(donePressed))
self.navigationItem.title = NSLocalizedString("GIF_PICKER_VIEW_TITLE",
comment: "Title for the 'gif picker' dialog.")
createViews()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
2017-09-28 20:09:11 +02:00
self.searchBar.becomeFirstResponder()
2017-09-28 20:09:11 +02:00
}
2017-09-26 19:40:37 +02:00
// MARK: Views
private func createViews() {
2017-09-28 20:09:11 +02:00
view.backgroundColor = UIColor.black
2017-09-26 19:40:37 +02:00
// Search
2017-09-28 20:09:11 +02:00
searchBar.searchBarStyle = .default
2017-09-26 19:40:37 +02:00
searchBar.delegate = self
searchBar.placeholder = NSLocalizedString("GIF_VIEW_SEARCH_PLACEHOLDER_TEXT",
comment:"Placeholder text for the search field in gif view")
2017-09-28 20:09:11 +02:00
searchBar.isTranslucent = false
searchBar.backgroundImage = UIImage(color:UIColor.clear)
searchBar.barTintColor = UIColor.black
searchBar.tintColor = UIColor.white
2017-09-26 19:40:37 +02:00
self.view.addSubview(searchBar)
searchBar.autoPinWidthToSuperview()
searchBar.autoPin(toTopLayoutGuideOf: self, withInset:0)
2017-09-28 20:09:11 +02:00
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.backgroundColor = UIColor.black
self.collectionView.register(GifPickerCell.self, forCellWithReuseIdentifier: kCellReuseIdentifier)
2017-09-26 19:40:37 +02:00
self.view.addSubview(self.collectionView)
self.collectionView.autoPinWidthToSuperview()
self.collectionView.autoPinEdge(.top, to:.bottom, of:searchBar)
self.collectionView.autoPin(toBottomLayoutGuideOf: self, withInset:0)
2017-10-01 02:26:29 +02:00
// The Giphy API requires us to "show their trademark prominently" in our GIF experience.
2017-09-28 20:09:11 +02:00
let logoImage = UIImage(named:"giphy_logo")
let logoImageView = UIImageView(image:logoImage)
self.logoImageView = logoImageView
self.view.addSubview(logoImageView)
logoImageView.autoCenterInSuperview()
2017-09-26 19:40:37 +02:00
self.updateContents()
}
2017-09-28 20:09:11 +02:00
private func setContentVisible(_ isVisible: Bool) {
self.collectionView.isHidden = !isVisible
if let logoImageView = self.logoImageView {
logoImageView.isHidden = isVisible
}
}
2017-09-26 19:40:37 +02:00
private func updateContents() {
2017-09-28 20:09:11 +02:00
if imageInfos.count < 1 {
setContentVisible(false)
} else {
setContentVisible(true)
}
2017-09-26 19:40:37 +02:00
2017-09-28 20:09:11 +02:00
self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.reloadData()
2017-09-26 19:40:37 +02:00
}
2017-09-28 20:09:11 +02:00
// MARK: - UICollectionViewDataSource
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageInfos.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let imageInfo = imageInfos[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:kCellReuseIdentifier, for: indexPath) as! GifPickerCell
cell.imageInfo = imageInfo
return cell
}
// MARK: - UICollectionViewDelegate
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at:indexPath) as? GifPickerCell else {
owsFail("\(TAG) unexpected cell.")
return
}
guard let asset = cell.fullAsset else {
Logger.info("\(TAG) unload cell selected.")
return
}
let filePath = asset.filePath
guard let dataSource = DataSourcePath.dataSource(withFilePath:filePath) else {
owsFail("\(TAG) couldn't load asset.")
return
}
let attachment = SignalAttachment(dataSource : dataSource, dataUTI: asset.rendition.utiType())
guard let thread = thread else {
owsFail("\(TAG) Missing thread.")
return
}
guard let messageSender = messageSender else {
owsFail("\(TAG) Missing messageSender.")
return
}
ThreadUtil.sendMessage(with: attachment, in: thread, messageSender: messageSender)
dismiss(animated: true, completion:nil)
2017-09-28 20:09:11 +02:00
}
public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
2017-09-29 05:30:33 +02:00
guard let cell = cell as? GifPickerCell else {
owsFail("\(TAG) unexpected cell.")
return
}
// We only want to load the cells which are on-screen.
cell.isCellVisible = true
2017-09-28 20:09:11 +02:00
}
public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
2017-09-29 05:30:33 +02:00
guard let cell = cell as? GifPickerCell else {
owsFail("\(TAG) unexpected cell.")
return
}
cell.isCellVisible = false
2017-09-28 20:09:11 +02:00
}
2017-09-26 19:40:37 +02:00
// MARK: - Event Handlers
func donePressed(sender: UIButton) {
dismiss(animated: true, completion:nil)
}
2017-09-28 20:09:11 +02:00
// MARK: - UISearchBarDelegate
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// TODO: We could do progressive search as the user types.
}
public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let text = searchBar.text else {
// TODO: Alert?
return
}
search(query:text)
}
private func search(query: String) {
self.searchBar.resignFirstResponder()
imageInfos = []
updateContents()
self.collectionView.contentOffset = CGPoint.zero
2017-09-28 20:09:11 +02:00
GifManager.sharedInstance.search(query: query, success: { [weak self] imageInfos in
guard let strongSelf = self else { return }
Logger.info("\(strongSelf.TAG) search complete")
strongSelf.imageInfos = imageInfos
strongSelf.updateContents()
},
failure: { [weak self] in
guard let strongSelf = self else { return }
Logger.info("\(strongSelf.TAG) search failed.")
})
}
2017-09-29 00:15:18 +02:00
// MARK: - GifPickerLayoutDelegate
func imageInfosForLayout() -> [GiphyImageInfo] {
return imageInfos
}
2017-09-26 19:40:37 +02:00
}