/
ModernSearchBarCell.swift
133 lines (110 loc) · 7.28 KB
/
ModernSearchBarCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// ModernSearchBarCell.swift
// SearchBarCompletion
//
// Created by Philippe on 06/03/2017.
// Copyright © 2017 CookMinute. All rights reserved.
//
import UIKit
public class ModernSearchBarCell: UITableViewCell {
public static let defaultMargin: CGFloat = 10
let imgModernSearchBar = UIImageView()
var labelModelSearchBar = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setup()
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func awakeFromNib() {
super.awakeFromNib()
self.setup()
}
private func setup(){
///Setup image
self.imgModernSearchBar.translatesAutoresizingMaskIntoConstraints = false
self.imgModernSearchBar.contentMode = .scaleAspectFill
///Setup label
self.labelModelSearchBar.translatesAutoresizingMaskIntoConstraints = false
self.labelModelSearchBar.numberOfLines = 0
self.labelModelSearchBar.lineBreakMode = NSLineBreakMode.byWordWrapping
self.contentView.addSubview(imgModernSearchBar)
self.contentView.addSubview(labelModelSearchBar)
}
///Configure constraint for each row of suggestionsView
public func configureConstraints(heightImage: CGFloat, widthImage: CGFloat){
///Image constraints
let verticalImageConstraint = NSLayoutConstraint(item: self.imgModernSearchBar, attribute:.centerYWithinMargins, relatedBy: .equal, toItem: self.contentView, attribute: .centerYWithinMargins, multiplier: 1.0, constant: 0)
let topImageConstraint = NSLayoutConstraint(item: self.imgModernSearchBar, attribute:.top, relatedBy: .greaterThanOrEqual, toItem: self.contentView, attribute: .top, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
topImageConstraint.priority = UILayoutPriorityDefaultLow
let bottomImageConstraint = NSLayoutConstraint(item: self.imgModernSearchBar, attribute:.bottom, relatedBy: .lessThanOrEqual, toItem: self.contentView, attribute: .bottom, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
bottomImageConstraint.priority = UILayoutPriorityDefaultLow
let leftImageConstraint = NSLayoutConstraint(item: self.imgModernSearchBar, attribute:.left, relatedBy: .equal, toItem: self.contentView, attribute: .left, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
leftImageConstraint.priority = UILayoutPriorityDefaultHigh
let heightImageConstraint = NSLayoutConstraint(item: self.imgModernSearchBar, attribute:.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: heightImage)
let widthImageConstraint = NSLayoutConstraint(item: self.imgModernSearchBar, attribute:.width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: widthImage)
///Label constraints
let topLabelConstraint = NSLayoutConstraint(item: self.labelModelSearchBar, attribute:.top, relatedBy: .greaterThanOrEqual, toItem: self.contentView, attribute: .top, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
topLabelConstraint.priority = UILayoutPriorityDefaultHigh
let bottomLabelConstraint = NSLayoutConstraint(item: self.labelModelSearchBar, attribute:.bottom, relatedBy: .lessThanOrEqual, toItem: self.contentView, attribute: .bottom, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
bottomLabelConstraint.priority = UILayoutPriorityDefaultHigh
let verticalLabelConstraint = NSLayoutConstraint(item: self.labelModelSearchBar, attribute:.centerYWithinMargins, relatedBy: .equal, toItem: self.contentView, attribute: .centerYWithinMargins, multiplier: 1.0, constant: 0)
let leftLabelConstraint = NSLayoutConstraint(item: self.labelModelSearchBar, attribute:.left, relatedBy: .equal, toItem: self.imgModernSearchBar, attribute: .right, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
leftLabelConstraint.priority = UILayoutPriorityDefaultHigh
let leftLabelConstraintIfImageNil = NSLayoutConstraint(item: self.labelModelSearchBar, attribute:.left, relatedBy: .equal, toItem: self.contentView, attribute: .left, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
leftLabelConstraintIfImageNil.priority = UILayoutPriorityDefaultLow
let rightLabelConstraint = NSLayoutConstraint(item: self.labelModelSearchBar, attribute:.right, relatedBy: .equal, toItem: self.contentView, attribute: .right, multiplier: 1.0, constant: ModernSearchBarCell.defaultMargin)
rightLabelConstraint.priority = UILayoutPriorityDefaultLow
NSLayoutConstraint.activate([verticalImageConstraint, topImageConstraint, bottomImageConstraint, leftImageConstraint,heightImageConstraint, widthImageConstraint,
verticalLabelConstraint, leftLabelConstraint, rightLabelConstraint, topLabelConstraint, bottomLabelConstraint, leftLabelConstraintIfImageNil])
}
///Configure image of suggestionsView
public func configureImage(choice: ModernSearchBar.Choice, searchImage: UIImage, suggestionsListWithUrl: Array<ModernSearchBarModel>, position: Int, isImageRound: Bool, heightImage: CGFloat){
switch choice {
///Show image from asset
case .normal:
self.imgModernSearchBar.image = searchImage
break
///Show image from URL
case .withUrl:
let model = suggestionsListWithUrl[position]
if (model.imgCache != nil){
self.imgModernSearchBar.image = model.imgCache
} else {
self.downloadImage(model: model)
}
break
}
if (isImageRound){
self.imgModernSearchBar.layer.cornerRadius = heightImage.divided(by: 2)
self.imgModernSearchBar.clipsToBounds = true
}
}
///Otherwise constaint on label doesn't works...
public func configureWidthMaxLabel(suggestionsViewWidth: CGFloat, searchImageWidth: CGFloat) {
var widthMax: CGFloat = suggestionsViewWidth
widthMax.subtract(ModernSearchBarCell.defaultMargin.multiplied(by: 3))
widthMax.subtract(searchImageWidth)
self.labelModelSearchBar.preferredMaxLayoutWidth = widthMax
}
//----------------------------
private func downloadImage(model: ModernSearchBarModel) {
DispatchQueue.global(qos: .background).async {
self.getDataFromUrl(url: model.url) { (data, response, error) in
guard let data = data, error == nil else { return }
let image = UIImage(data: data)
DispatchQueue.main.async() { () -> Void in
model.imgCache = image
self.imgModernSearchBar.image = image
}
}
}
}
private func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
URLSession.shared.dataTask(with: url) {
(data, response, error) in
completion(data, response, error)
}.resume()
}
}