This is my code:
// UI View Controller Class
class EmotionExplorerViewController: UIViewController, UINavigationControllerDelegate {
func configureDataSource() {
// Cell registration for main section
let mainCellRegistration = UICollectionView.CellRegistration<EmotionExplorerCell, Item> { cell, indexPath,
item in
switch item {
case .main(emotion: let emotion, icon: let iconName, red: let red, green: let green, blue: let blue):
cell.delegate = self
cell.index = indexPath.row
print("collection view cell,", cell)
cell.configure(iconName: iconName, title: emotion, red: red, green: green, blue: blue)
case .title(title: _, buttonTitle: _):
break
case .filter(name: _):
break
case .skeleton: break
}
}
}
// Delegate method implementation
extension EmotionExplorerViewController: EmotionExplorerCellDelegate {
func repromptLLMForIconName(_ iconName: String, index: Int, emotion: String, red: CGFloat, green: CGFloat, blue: CGFloat) {
guard let numberOfTries = geminiResponse?.numberOfTries, numberOfTries < 3 else { return }
geminiResponse?.rempromptForIconName(iconName) { [self] emotions, error in
if var item = dataSource.itemIdentifier(for: IndexPath(row: index, section: 3)) {
var snapshot = dataSource.snapshot(for: .main)
if let newEmotion = emotions?.first {
var insertItem = Item.main(emotion: emotion, icon: newEmotion.sfSymbolName, red: red, green: green, blue: blue)
snapshot.insert([insertItem], after: item)
snapshot.delete([item])
dataSource.apply(snapshot, to: .main, animatingDifferences: true)
}
}
}
}
}
// UI Collection View Cell Class
protocol EmotionExplorerCellDelegate {
func repromptLLMForIconName(_ iconName: String, index: Int, emotion: String, red: CGFloat, green: CGFloat, blue: CGFloat)
}
class EmotionExplorerCell: UICollectionViewCell {
var delegate: EmotionExplorerCellDelegate?
var iconImageView: UIImageView!
var titleLabel: UILabel!
var index: Int!
var emotion: String!
var red: CGFloat!
var green: CGFloat!
var blue: CGFloat!
override init(frame: CGRect) {
super.init(frame: frame)
configureCell()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
iconImageView.image = nil
titleLabel.text = nil
contentView.backgroundColor = nil
}
private func configureCell() {
iconImageView = UIImageView()
iconImageView.contentMode = .scaleAspectFit
iconImageView.tintColor = .black
iconImageView.translatesAutoresizingMaskIntoConstraints = false
titleLabel = UILabel()
titleLabel.font = .systemFont(ofSize: 18, weight: .medium)
titleLabel.textColor = .black
titleLabel.translatesAutoresizingMaskIntoConstraints = false
contentView.layer.cornerRadius = 7
contentView.layer.cornerCurve = .continuous
contentView.clipsToBounds = true
contentView.addSubview(iconImageView)
contentView.addSubview(titleLabel)
NSLayoutConstraint.activate([
iconImageView.centerXAnchor
.constraint(equalTo: contentView.leadingAnchor, constant: 20),
iconImageView.centerYAnchor
.constraint(equalTo: contentView.centerYAnchor),
titleLabel.leadingAnchor
.constraint(equalTo: iconImageView.centerXAnchor, constant: 20),
titleLabel.centerYAnchor
.constraint(equalTo: contentView.centerYAnchor),
titleLabel.trailingAnchor
.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -16)
])
}
func configure(iconName: String, title: String, red: CGFloat, green: CGFloat, blue: CGFloat) {
if let image = UIImage(systemName: iconName) {
iconImageView.image = image
} else {
print("delegate", delegate)
delegate?.repromptLLMForIconName(iconName, index: index, emotion: emotion, red: red, green: green, blue: blue)
}
iconImageView.image = UIImage(systemName: iconName)
titleLabel.text = title.capitalized
let transformer = UIConfigurationColorTransformer.init { color in
color.withProminence(.quaternary).withAlphaComponent(0.6)
}
let color = UIColor(
red: red/255,
green: green/255,
blue: blue/255,
alpha: 1
)
contentView.backgroundColor = transformer.callAsFunction(color)
}
}