HistoryTableViewCell.swift 3.05 KB
//
//  HistoryTableViewCell.swift
//  browser
//
//  Created by Artem Talko on 29.09.2023.
//

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.top.equalToSuperview().inset(8.sizeH)
            make.leading.equalToSuperview().inset(8.sizeW)
            make.height.equalTo(18.sizeW)
            make.width.equalTo(18.sizeW)
        }
        
        lastVisitSiteLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(8.sizeH)
            make.leading.equalTo(searchImageLogo.snp.trailing).offset(8.sizeW)
            make.trailing.equalTo(lastVisitDateLabel.snp.leading).offset(-8.sizeW)
        }
        
        lastVisitDateLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(8.sizeH)
            make.trailing.equalToSuperview().inset(8.sizeW)
        }
        
        lastVisitSiteUrlLabel.snp.makeConstraints { make in
            make.top.equalTo(lastVisitSiteLabel.snp.bottom).offset(4.sizeH)
            make.leading.equalToSuperview().offset(34.sizeW)
            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
    }
}