HistoryTableViewCell.swift 3.07 KB
//
//  HistoryTableViewCell.swift
//  browser
//
//  Created by Artem Talko on 29.09.2023.
//
//MARK: Checked

import UIKit
import SnapKit

final class HistoryTableViewCell: UITableViewCell {
    static let tabID = String(describing: HistoryTableViewCell.self)
    
    var model: HistoryDataBase? {
        didSet {
            handleUI()
        }
    }
    
    private let searchImageLogo: UIImageView = {
        let obj = UIImageView()
        obj.image = UIImage(systemName: "magnifyingglass")
        obj.tintColor = .gray
        obj.contentMode = .scaleAspectFit
        return obj
    }()
    
    private let lastVisitSiteLabel: UILabel = {
        let obj = UILabel()
        obj.font = FontConstants.regularFont_14
        return obj
    }()
    
    private let lastVisitSiteUrlLabel: UILabel = {
        let obj = UILabel()
        obj.textColor = .gray
        obj.font = FontConstants.regularFont_12
        return obj
    }()
    
    private let lastVisitDateLabel: UILabel = {
        let obj = UILabel()
        obj.font = FontConstants.regularFont_12
        obj.textColor = .gray
        return obj
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup() {
        contentView.addSubview(searchImageLogo)
        contentView.addSubview(lastVisitSiteLabel)
        contentView.addSubview(lastVisitSiteUrlLabel)
        contentView.addSubview(lastVisitDateLabel)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
        searchImageLogo.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(8.sizeW)
            make.top.equalToSuperview().inset(8.sizeH)
            make.height.equalTo(18.sizeW)
            make.width.equalTo(18.sizeW)
        }
        
        lastVisitSiteLabel.snp.makeConstraints { make in
            make.leading.equalTo(searchImageLogo.snp.trailing).offset(8.sizeW)
            make.trailing.equalTo(lastVisitDateLabel.snp.leading).offset(-8.sizeW)
            make.top.equalToSuperview().inset(8.sizeH)
        }
        
        lastVisitDateLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(8.sizeW)
            make.top.equalToSuperview().inset(8.sizeH)
        }
        
        lastVisitSiteUrlLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(34.sizeW)
            make.top.equalTo(lastVisitSiteLabel.snp.bottom).offset(4.sizeH)
            make.bottom.equalToSuperview().inset(8.sizeH)
            make.trailing.equalToSuperview().inset(61.sizeW)
        }
    }
}

extension HistoryTableViewCell {
    private func handleUI() {
        let dateForLabel = DateManager.shared.getTimeFromDB(model?.lastVisit)
        
        lastVisitDateLabel.text = dateForLabel
        lastVisitSiteLabel.text = model?.siteTitle
        lastVisitSiteUrlLabel.text = model?.siteUrl
    }
}