// // UserIntrestVC.swift // WOKA // // Created by MacBook Pro on 30/04/24. // import UIKit struct Temp{ let text : String? var selected : Bool? } class UserIntrestVC: UIViewController { @IBOutlet weak var boyView: UIView! @IBOutlet weak var girlView: UIView! @IBOutlet weak var datePicker: UIDatePicker! @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var nextBtn: LocalisedElementsButton! @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var contentHeight: NSLayoutConstraint! var vm = UserIntrestVM() var test = [Temp(text: "ADVENTURES", selected: false), Temp(text: "CARTOONS", selected: false), Temp(text: "SUPER HEROES", selected: false), Temp(text: "SPORTS", selected: false), Temp(text: "SCIENCE", selected: false), Temp(text: "TRAVEL", selected: false), Temp(text: "ADVENTURES", selected: false), Temp(text: "CARTOONS", selected: false), Temp(text: "SUPER HEROES", selected: false), Temp(text: "SPORTS", selected: false), Temp(text: "SCIENCE", selected: false), Temp(text: "TRAVEL", selected: false)] override func viewDidLoad() { super.viewDidLoad() vm.vc = self vm.initView() boyView.roundCorner(radius: 25) girlView.roundCorner(radius: 25) datePicker.setValue(UIColor.appColor(.TextDarkBlue), forKeyPath: "textColor") } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(false, animated: animated) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.setNavigationBarHidden(true, animated: animated) } @IBAction func nextBtnTapped(_ sender: LocalisedElementsButton) { let sb = UIStoryboard(name: K.StoryBoard.authenticationSB, bundle: nil) let vc = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Authentication.selectAvatarVC) as! SelectAvatarVC self.navigationController?.pushViewController(vc, animated: true) } } // MARK: - CollectionView Delegate extension UserIntrestVC : UICollectionViewDelegate , UICollectionViewDataSource{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return test.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.Authentication.yourIntrestCell, for: indexPath) as! YourIntrestCell cell.setData(text: test[indexPath.row].text!,selected: test[indexPath.row].selected!) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { test[indexPath.row].selected?.toggle() UIView.performWithoutAnimation { self.collectionView.reloadItems(at: [IndexPath(row: indexPath.row, section: 0)]) } } } // MARK: - Collection Flow Layout extension UserIntrestVC : UICollectionViewDelegateFlowLayout{ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // Generate a random string for testing purposes let randomText = test[indexPath.row].text! // Define the font for the text let font = UIFont.systemFont(ofSize: 14) // You can adjust the font size as needed // Calculate the width of the text let textWidth = randomText.size(withAttributes: [.font: font]).width // Set the cell width based on the text width and any additional padding let cellWidth = textWidth + 25 // Add any additional spacing you want return CGSize(width: cellWidth, height: 45) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { section == 0 ? 15 : 5 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) } }