OpenedTabsCollectionViewCell.swift 4.63 KB
import UIKit
import SnapKit

protocol OpenedTabsCellDelegate: AnyObject {
    func deleteTabDelegateFunc(cell: OpenedTabsCollectionViewCell)
}

final class OpenedTabsCollectionViewCell: UICollectionViewCell {
    var model: BrowserTabDataBase? {
        didSet {
            handleUI()
        }
    }

    weak var delegate: OpenedTabsCellDelegate?

    let siteNameLabel: UILabel = {
        let obj = UILabel()
        obj.font = FontConstants.regularFont_14
        return obj
    }()

    let sitePreviewImageContainer: UIView = {
        let obj = UIView()
        obj.clipsToBounds = true
        obj.layer.cornerRadius = 10
        return obj
    }()

    let sitePreviewImage: UIImageView = {
        let obj = UIImageView()
        obj.contentMode = .scaleAspectFill
        return obj
    }()

    let closeTabButton: UIButton = {
        let obj = UIButton()
        obj.setImage(UIImage(systemName: "xmark"), for: .normal)
        obj.contentMode = .scaleAspectFill
        obj.tintColor = .gray
        return obj
    }()
    
    var panGestureRecognizer: UIPanGestureRecognizer!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setup() {
        self.backgroundColor = .white
        layer.cornerRadius = 10
        shadowSetup()
        contentView.addSubview(siteNameLabel)
        contentView.addSubview(closeTabButton)
        contentView.addSubview(sitePreviewImageContainer)
        sitePreviewImageContainer.addSubview(sitePreviewImage)
        setupConstraints()
        addingTargets()
        setupPanGesture()
    }

    private func setupConstraints() {
        closeTabButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.trailing.equalToSuperview().offset(-8)
            make.height.equalTo(14)
            make.width.equalTo(14)
        }

        siteNameLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalTo(closeTabButton.snp.leading).offset(-2)
        }

        sitePreviewImageContainer.snp.makeConstraints { make in
            make.top.equalTo(closeTabButton.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-8)
            make.leading.trailing.equalToSuperview().inset(8)
        }

        sitePreviewImage.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        shadowSetup()
    }
}



//MARK: Helpers
extension OpenedTabsCollectionViewCell {
    func setupPanGesture() {
        panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
        addGestureRecognizer(panGestureRecognizer)
    }
    
    func gestureRecognizer() {
        let swipeRightGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
        swipeRightGestureRecognizer.direction = .right
        addGestureRecognizer(swipeRightGestureRecognizer)
    }
    
    private func shadowSetup() {
        layer.shadowColor = UIColor.black.withAlphaComponent(0.1).cgColor
        layer.shadowOpacity = 1
        layer.shadowRadius = 30
        layer.shadowOffset = CGSize(width: 0, height: 0)
    }
    
    func addingTargets() {
        closeTabButton.addTarget(self, action: #selector(deleteTab), for: .touchUpInside)
    }
    
}


//MARK: Handlers
extension OpenedTabsCollectionViewCell {
    private func handleUI() {
        siteNameLabel.text = model?.tabTitle
        sitePreviewImage.image = model?.snapshotImageData?.toImage()
    }
}


//MARK: Actions
extension OpenedTabsCollectionViewCell {
    @objc
    private func deleteTab() {
        delegate?.deleteTabDelegateFunc(cell: self)
        if let tabId = model?.tabId {
            TabManager.shared.deleteTab(tabId: tabId)
        }
    }

    @objc
    private func handleSwipe() {
        deleteTab()
    }

    @objc
    private func handlePan(_ gesture: UIPanGestureRecognizer) {
        let translation = gesture.translation(in: self)
        let width = self.frame.width
        let threshold = width / 2
        
        if gesture.state == .changed {
            transform = CGAffineTransform(translationX: translation.x, y: 0)
        } else if gesture.state == .ended {
            if abs(translation.x) >= threshold {
                deleteTab()
            } else {
                UIView.animate(withDuration: 0.3) {
                    self.transform = .identity
                }
            }
        }
    }
}